Skip to content

Commit

Permalink
fix: rg menu actions resolved via app state machine, additional refac…
Browse files Browse the repository at this point in the history
…tor also
  • Loading branch information
joshmu committed Apr 27, 2024
1 parent 57ec74c commit c34ae78
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 35 deletions.
13 changes: 12 additions & 1 deletion src/lib/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { initHighlightLineInstance } from '../utils/highlightLineDecorationType'
// simple context for each invoke of periscope search
let qp = vscode.window.createQuickPick<AllQPItemVariants>(); // @see https://code.visualstudio.com/api/references/vscode-api#QuickPick
let workspaceFolders = vscode.workspace.workspaceFolders;
let previousActiveEditor = vscode.window.activeTextEditor;
let query = '';
let spawnRegistry: ChildProcessWithoutNullStreams[] = [];
let config = getConfig();
Expand All @@ -17,23 +18,27 @@ let disposables: DisposablesMap = {
rgMenuActions: [],
query: [],
};
let appState = updateAppState('IDLE');

export const context = {
resetContext,
qp,
workspaceFolders,
previousActiveEditor,
query,
spawnRegistry,
config,
rgMenuActionsSelected,
highlightDecoration,
disposables
disposables,
appState
};

// reset the context
function resetContext() {
context.qp = vscode.window.createQuickPick<AllQPItemVariants>();
context.workspaceFolders = vscode.workspace.workspaceFolders;
context.previousActiveEditor = vscode.window.activeTextEditor;
context.query = '';
context.spawnRegistry = [];
context.config = getConfig();
Expand All @@ -44,4 +49,10 @@ function resetContext() {
rgMenuActions: [],
query: [],
};
}

type AppState = 'IDLE' | 'SEARCHING' | 'FINISHED';
export function updateAppState(state: AppState) {
if (context?.appState) {context.appState = state;}
return state;
}
9 changes: 4 additions & 5 deletions src/lib/editorActions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { previousActiveEditor, updatePreviousActiveEditor } from './editorContext';
import { AllQPItemVariants, QPItemQuery } from '../types';
import { context as cx } from './context';

export function closePreviewEditor() {
if(previousActiveEditor) {
if(cx.previousActiveEditor) {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
updatePreviousActiveEditor(undefined); // prevent focus onDidHide
cx.previousActiveEditor = undefined; // prevent focus onDidHide
}
}

Expand Down Expand Up @@ -92,8 +91,8 @@ export function handleNoResultsFound() {
}

export function showPreviewOfOriginDocument() {
if (!previousActiveEditor) {return;}
vscode.window.showTextDocument(previousActiveEditor.document, {
if (!cx.previousActiveEditor) {return;}
vscode.window.showTextDocument(cx.previousActiveEditor.document, {
preserveFocus: true,
preview: true
});
Expand Down
8 changes: 0 additions & 8 deletions src/lib/editorContext.ts

This file was deleted.

26 changes: 21 additions & 5 deletions src/lib/globalActions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import * as vscode from 'vscode';
import * as path from 'path';
import { log } from "../utils/log";
import { updatePreviousActiveEditor } from "./editorContext";
import { checkKillProcess } from "./ripgrep";
import { context as cx } from './context';
import { context as cx, updateAppState } from './context';
import { QPItemQuery } from '../types';
import { closePreviewEditor, setCursorPosition } from './editorActions';

Expand Down Expand Up @@ -44,17 +43,34 @@ export function confirm(payload: ConfirmPayload = {context: 'unknown'}) {
});
}

// start periscope extension/search
export function start() {
log('start');
cx.resetContext();
setExtensionActiveContext(true);
}

// end periscope extension
export function finished() {
setExtensionActiveContext(false);
updateAppState('FINISHED');
checkKillProcess();
cx.highlightDecoration.remove();
setActiveContext(false);
disposeAll();
updatePreviousActiveEditor(undefined);
cx.previousActiveEditor = undefined;
log('finished');
}

// create vscode context for the extension for targeted keybindings
export function setActiveContext(flag: boolean) {
/**
* eg:
* {
"key": "ctrl+\\",
"command": "periscope.openInHorizontalSplit",
"when": "periscopeActive" <<< this is the context
}
*/
export function setExtensionActiveContext(flag: boolean) {
log(`setContext ${flag}`);
vscode.commands.executeCommand('setContext', 'periscopeActive', flag);
}
Expand Down
13 changes: 4 additions & 9 deletions src/lib/periscope.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import * as vscode from 'vscode';
import { updatePreviousActiveEditor } from './editorContext';
import { log } from '../utils/log';
import { context as cx } from './context';
import { onDidHide, setupQuickPickForQuery, setupRgMenuActions } from './quickpickActions';
import { setActiveContext } from './globalActions';
import { start } from './globalActions';
import { openInHorizontalSplit } from './editorActions';

function search() {
cx.resetContext();
log('start');

setActiveContext(true);
updatePreviousActiveEditor(vscode.window.activeTextEditor);
start();

// if ripgrep actions are available then open preliminary quickpick
const openRgMenuActions = cx.config.alwaysShowRgMenuActions && cx.config.rgMenuActions.length > 0;
Expand All @@ -20,6 +13,8 @@ function search() {
cx.disposables.general.push(
cx.qp.onDidHide(onDidHide)
);

// search logic is triggered from the QuickPick event handlers...
cx.qp.show();
}

Expand Down
10 changes: 5 additions & 5 deletions src/lib/quickpickActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import * as vscode from 'vscode';
import { AllQPItemVariants, QPItemQuery, QPItemRgMenuAction } from "../types";
import { openNativeVscodeSearch, peekItem } from "./editorActions";
import { checkKillProcess, checkAndExtractRgFlagsFromQuery, rgSearch } from "./ripgrep";
import { context as cx } from './context';
import { context as cx, updateAppState } from './context';
import { getSelectedText } from "../utils/getSelectedText";
import { log } from '../utils/log';
import { previousActiveEditor } from './editorContext';
import { confirm, finished } from './globalActions';

// update quickpick event listeners for the query
Expand Down Expand Up @@ -99,10 +98,10 @@ function onDidTriggerItemButton(e: vscode.QuickPickItemButtonEvent<AllQPItemVari
// when prompt is 'CANCELLED'
export function onDidHide() {
if (!cx.qp.selectedItems[0]) {
if (previousActiveEditor) {
if (cx.previousActiveEditor) {
vscode.window.showTextDocument(
previousActiveEditor.document,
previousActiveEditor.viewColumn
cx.previousActiveEditor.document,
cx.previousActiveEditor.viewColumn
);
}
}
Expand All @@ -114,6 +113,7 @@ export function onDidHide() {
// when ripgrep actions are available show preliminary quickpick for those options to add to the query
export function setupRgMenuActions() {
reset();
updateAppState('IDLE');
cx.qp.placeholder = '🫧 Select actions or type custom rg options (Space key to check/uncheck)';
cx.qp.canSelectMany = true;

Expand Down
6 changes: 4 additions & 2 deletions src/lib/ripgrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { rgPath } from '@vscode/ripgrep';
import { spawn } from 'child_process';
import * as vscode from 'vscode';
import { getConfig } from "../utils/getConfig";
import { context as cx } from './context';
import { context as cx, updateAppState } from './context';
import { tryJsonParse } from '../utils/jsonUtils';
import { QPItemQuery, RgLine } from '../types';
import { log, notifyError } from '../utils/log';
Expand Down Expand Up @@ -55,6 +55,7 @@ function getRgCommand(value: string, extraFlags?: string[]) {
}

export function rgSearch(value: string, rgExtraFlags?: string[]) {
updateAppState('SEARCHING');
cx.qp.busy = true;
const rgCmd = getRgCommand(value, rgExtraFlags);
log('rgCmd:', rgCmd);
Expand Down Expand Up @@ -89,7 +90,8 @@ export function rgSearch(value: string, rgExtraFlags?: string[]) {
});

spawnProcess.on('exit', (code: number) => {
if (code === 0 && searchResults.length) {
// we need to additionally check 'SEARCHING' state as the user might have cancelled the search but the process may still be running (eg: go back to the rg menu actions view)
if (code === 0 && searchResults.length && cx.appState === 'SEARCHING') {
cx.qp.items = searchResults
.map(searchResult => {
// break the filename via regext ':line:col:'
Expand Down

0 comments on commit c34ae78

Please sign in to comment.