Skip to content

Commit

Permalink
feat: optional rg menu actions
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmu committed Mar 30, 2023
1 parent a0bb19d commit 37d1829
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This extension contributes the following settings:
* `rgOptions`: Additional options to pass to the 'rg' command, you can view all options in your terminal via 'rg --help'.
* `rgGlobExcludes`: Additional glob paths to exclude from the 'rg' search, eg: '__/dist/__'.
* `addSrcPaths`: Additional source paths to include in the rg search. You may want to add this as a workspace specific setting.
* `rgMenuActions`: Create menu items which can be selected prior to any query, these items will be added to the ripgrep command to generate the results. Eg: Add `-t js` as a menu option to only show javascript files in the results.
* `startFolderDisplayDepth`: The folder depth to display in the results before '...'.
* `endFolderDisplayDepth`: The folder depth to display in the results after '...'.
* `enableGotoNativeSearch`: If true, then swap to native vscode search if the custom suffix is entered using the current query.
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
},
"description": "Additional source paths to include in the rg search. You may want to add this as a workspace specific setting."
},
"periscope.rgMenuActions": {
"type": "array",
"default": [],
"items": {
"type": "string"
},
"description": "Create menu items which can be selected prior to any query, these items will be added to the ripgrep command to generate the results. Eg: Add `-t js` as a menu option to only show javascript files in the results."
},
"periscope.startFolderDisplayDepth": {
"type": "number",
"default": 1,
Expand Down
104 changes: 61 additions & 43 deletions src/periscope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const periscope = () => {

let spawnProcess: ChildProcessWithoutNullStreams | undefined;
let config = getConfig();
let rgMenuActionsSelected: string[] = [];

function register() {
setActiveContext(true);
Expand All @@ -35,13 +36,37 @@ export const periscope = () => {
quickPick = vscode.window.createQuickPick();

quickPick.placeholder = '🫧';

// if ripgrep actions are available then open preliminary quickpick
config.rgMenuActions.length ? setupRgMenuActions(quickPick) : setupQuickPickForQuery(quickPick);

quickPick.onDidHide(onDidHide);
quickPick.show();
}

// when ripgrep actions are available show preliminary quickpick for those options to add to the query
function setupRgMenuActions(quickPick: vscode.QuickPick<vscode.QuickPickItem | QuickPickItemCustom>) {
quickPick.canSelectMany = true;

// add items from the config
quickPick.items = config.rgMenuActions.map(item => ({
label: item,
}));

quickPick.onDidAccept(() => {
rgMenuActionsSelected = quickPick.selectedItems.map(item => item.label);
setupQuickPickForQuery(quickPick);
});
}

// update quickpick event listeners for the query
function setupQuickPickForQuery(quickPick: vscode.QuickPick<vscode.QuickPickItem | QuickPickItemCustom>) {
quickPick.items = [];
quickPick.canSelectMany = false;
quickPick.value = getSelectedText();
onDidChangeValue();
onDidChangeActive();
onDidAccept();
onDidHide();
quickPick.show();
quickPick.onDidChangeValue(onDidChangeValue);
quickPick.onDidChangeActive(onDidChangeActive);
quickPick.onDidAccept(onDidAccept);
}

// create vscode context for the extension for targeted keybindings
Expand All @@ -51,58 +76,50 @@ export const periscope = () => {
}

// when input query 'CHANGES'
function onDidChangeValue() {
quickPick.onDidChangeValue(value => {
checkKillProcess();

if (value) {
query = value;

// Jump to native vscode search option
if (
config.enableGotoNativeSearch &&
config.gotoNativeSearchSuffix &&
value.endsWith(config.gotoNativeSearchSuffix)
) {
openNativeVscodeSearch();
return;
}

search(value);
} else {
quickPick.items = [];
function onDidChangeValue(value: string) {
checkKillProcess();

if (value) {
query = value;

// Jump to native vscode search option
if (
config.enableGotoNativeSearch &&
config.gotoNativeSearchSuffix &&
value.endsWith(config.gotoNativeSearchSuffix)
) {
openNativeVscodeSearch();
return;
}
});

search(value);
} else {
quickPick.items = [];
}
}

// when item is 'FOCUSSED'
function onDidChangeActive() {
quickPick.onDidChangeActive(items => {
peekItem(items as readonly QuickPickItemCustom[]);
});
function onDidChangeActive(items: readonly (vscode.QuickPickItem | QuickPickItemCustom)[]) {
peekItem(items as readonly QuickPickItemCustom[]);
}

// when item is 'SELECTED'
function onDidAccept() {
quickPick.onDidAccept(() => {
accept();
});
accept();
}

// when prompt is 'CANCELLED'
function onDidHide() {
quickPick.onDidHide(() => {
if (!quickPick.selectedItems[0]) {
if (activeEditor) {
vscode.window.showTextDocument(
activeEditor.document,
activeEditor.viewColumn
);
}
if (!quickPick.selectedItems[0]) {
if (activeEditor) {
vscode.window.showTextDocument(
activeEditor.document,
activeEditor.viewColumn
);
}
}

finished();
});
finished();
}

function search(value: string) {
Expand Down Expand Up @@ -189,6 +206,7 @@ export const periscope = () => {
const rgFlags = [
...rgRequiredFlags,
...config.rgOptions,
...rgMenuActionsSelected,
...rootPaths,
...config.addSrcPaths,
...excludes,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/getConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type ConfigItems =
| 'rgOptions'
| 'addSrcPaths'
| 'rgGlobExcludes'
| 'rgMenuActions'
| 'startFolderDisplayDepth'
| 'endFolderDisplayDepth'
| 'enableGotoNativeSearch'
Expand All @@ -23,6 +24,7 @@ export function getConfig() {
]),
addSrcPaths: vsConfig.get<string[]>('addSrcPaths', []),
rgGlobExcludes: vsConfig.get<string[]>('rgGlobExcludes', []),
rgMenuActions: vsConfig.get<string[]>('rgMenuActions', []),
startFolderDisplayDepth: vsConfig.get<number>('startFolderDisplayDepth', 1),
endFolderDisplayDepth: vsConfig.get<number>('endFolderDisplayDepth', 4),
enableGotoNativeSearch: vsConfig.get<boolean>(
Expand Down

0 comments on commit 37d1829

Please sign in to comment.