Skip to content

Commit

Permalink
Start with last searched value or current editor selection
Browse files Browse the repository at this point in the history
  • Loading branch information
wsbak committed Mar 12, 2024
1 parent 0b4bd3f commit 8ea18f0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@
- feat: can select multiple cases.
- feat: add Capital Case.
- feat: add path/case.

## 0.0.6

- feat: start with last searched value or current editor selection
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "case-search",
"displayName": "Case Search",
"description": "A VS Code search extension based on native search component.",
"version": "0.0.5",
"version": "0.0.6",
"engines": {
"vscode": "^1.40.0"
},
Expand Down
34 changes: 34 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ function saveSelectedItems(context: ExtensionContext, selectedItems: readonly Qu
}
}

function getSelectedText() {
const editor = window.activeTextEditor;
const selection = editor?.selection;
const selectedText = editor?.document.getText(selection);
return selectedText;
}

// Get the initial value to search
function getInitialValue(context: ExtensionContext) {
const lastSearchedValue = context.workspaceState.get<string>("lastSearchedValue", "");
const selectedText = getSelectedText();

if (selectedText === undefined) {
// No selected text
// So start with last searched value or ""
return lastSearchedValue;
}

const lastSelectedText = context.workspaceState.get<string>("lastSelectedText", "");
if (selectedText !== lastSelectedText) {
// Selected text modified (by user) since last search
// So start with it
return selectedText;
}

// Selected text NOT modified since last search
// So start with last searched value or ""
return lastSearchedValue;
}

export function activate(context: ExtensionContext) {
context.subscriptions.push(commands.registerCommand('case-search.showSearchBox', async () => {

Expand All @@ -83,11 +113,15 @@ export function activate(context: ExtensionContext) {
quickPick.canSelectMany = true;
quickPick.selectedItems = readSelectedItems(context);
quickPick.placeholder = "please input query text, default scope is all cases";
quickPick.value = getInitialValue(context);
quickPick.onDidAccept(() => {
// console.log("onDidAccept", "value", quickPick.value, "selectedItems", quickPick.selectedItems);
saveSelectedItems(context, quickPick.selectedItems);

if (quickPick.value) {
context.workspaceState.update("lastSearchedValue", quickPick.value);
context.workspaceState.update("lastSelectedText", getSelectedText());

// If no selectedItems, we take all quickPickItems
let items = quickPick.selectedItems.length <= 0 ? quickPickItems : quickPick.selectedItems;
let query = buildRegexQuery(quickPick.value, items);
Expand Down

0 comments on commit 8ea18f0

Please sign in to comment.