Skip to content

Commit 4d7442e

Browse files
committed
Add keywords match mode selection to the file search command
- when selected 'Keywords', show another quick pick: 'All', 'Some'. When 'some' is selected - use logical OR. When 'all' is selected - use logical AND. - remove redundant comments - use '==' instead of '===' when comparing strings numbers - [Pasted text]
1 parent fca1f4d commit 4d7442e

11 files changed

Lines changed: 433 additions & 199 deletions

‎apps/editor/src/commands/search-files-command/search-files-commands.ts‎

Lines changed: 231 additions & 184 deletions
Large diffs are not rendered by default.

‎apps/editor/src/commands/search-files-command/utils/prompt-for-config.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export const prompt_for_api_configuration = async (params: {
3838
if (default_api_configuration) {
3939
selected_api_configuration = default_api_configuration
4040
skipped = true
41-
} else if (params.api_configurations.length === 1) {
41+
} else if (params.api_configurations.length == 1) {
4242
selected_api_configuration = params.api_configurations[0]
4343
skipped = true
4444
}
@@ -62,7 +62,7 @@ export const prompt_for_api_configuration = async (params: {
6262
show_back_button: true
6363
})
6464

65-
if (result === 'back') return 'back'
65+
if (result == 'back') return 'back'
6666
if (!result) return 'cancel'
6767
selected_api_configuration = result.item
6868
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as vscode from 'vscode'
2+
import { t } from '@/i18n'
3+
4+
export const prompt_for_keywords_match_mode = async (
5+
last_mode: 'all' | 'some'
6+
): Promise<'all' | 'some' | 'back' | undefined> => {
7+
const items: (vscode.QuickPickItem & { mode: 'all' | 'some' })[] = [
8+
{
9+
label: t('command.search.keywords.match-mode.all'),
10+
description: t('command.search.keywords.match-mode.all-description'),
11+
mode: 'all'
12+
},
13+
{
14+
label: t('command.search.keywords.match-mode.some'),
15+
description: t('command.search.keywords.match-mode.some-description'),
16+
mode: 'some'
17+
}
18+
]
19+
20+
const active_item = items.find((i) => i.mode == last_mode) || items[0]
21+
22+
const close_button = {
23+
iconPath: new vscode.ThemeIcon('close'),
24+
tooltip: t('common.close')
25+
}
26+
27+
const quick_pick = vscode.window.createQuickPick<
28+
vscode.QuickPickItem & { mode: 'all' | 'some' }
29+
>()
30+
quick_pick.items = items
31+
quick_pick.activeItems = [active_item]
32+
quick_pick.title = t('command.search.keywords.match-mode.title')
33+
quick_pick.placeholder = t('command.search.keywords.match-mode.placeholder')
34+
quick_pick.ignoreFocusOut = false
35+
quick_pick.buttons = [vscode.QuickInputButtons.Back, close_button]
36+
37+
return new Promise((resolve) => {
38+
let is_resolved = false
39+
40+
quick_pick.onDidTriggerButton((button) => {
41+
if (button === vscode.QuickInputButtons.Back) {
42+
resolve('back')
43+
quick_pick.hide()
44+
} else if (button === close_button) {
45+
resolve(undefined)
46+
quick_pick.hide()
47+
}
48+
})
49+
50+
quick_pick.onDidAccept(() => {
51+
const selected = quick_pick.selectedItems[0]
52+
if (selected) {
53+
is_resolved = true
54+
resolve(selected.mode)
55+
quick_pick.hide()
56+
}
57+
})
58+
59+
quick_pick.onDidHide(() => {
60+
if (!is_resolved) {
61+
resolve(undefined)
62+
}
63+
quick_pick.dispose()
64+
})
65+
66+
quick_pick.show()
67+
})
68+
}

‎apps/editor/src/commands/search-files-command/utils/prompt-for-search-mode.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const prompt_for_search_mode = async (
2424
}
2525
]
2626

27-
const active_item = items.find((i) => i.mode === last_mode) || items[0]
27+
const active_item = items.find((i) => i.mode == last_mode) || items[0]
2828

2929
const close_button = {
3030
iconPath: new vscode.ThemeIcon('close'),

‎apps/editor/src/commands/search-files-command/utils/prompt-for-search-results.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const prompt_for_search_results = async (params: {
113113
const text = doc.getText()
114114

115115
let regexes: RegExp[] = []
116-
if (params.search_mode === 'keywords') {
116+
if (params.search_mode == 'keywords') {
117117
regexes = params.search_term
118118
.split(',')
119119
.map((k) => k.trim())
@@ -140,7 +140,7 @@ export const prompt_for_search_results = async (params: {
140140
})
141141
} catch (error) {
142142
vscode.window.showErrorMessage(
143-
t('command.context.check-references.error-opening', {
143+
t('command.search.error.opening-file', {
144144
error: String(error)
145145
})
146146
)

‎apps/editor/src/commands/search-files-command/utils/prompt-for-search-term.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ export const prompt_for_search_term = async (
1313
const input_box = vscode.window.createInputBox()
1414
input_box.title = t('command.search.title')
1515
input_box.prompt =
16-
mode === 'keywords'
16+
mode == 'keywords'
1717
? t('command.search.prompt.keywords')
18-
: mode === 'intelligent'
18+
: mode == 'intelligent'
1919
? t('command.search.prompt.intelligent')
2020
: t('command.search.prompt')
2121
input_box.placeholder = t('command.search.placeholder')

‎apps/editor/src/commands/search-files-command/utils/prompt-for-shrink-mode.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const prompt_for_shrink_mode = async (params: {
3939
shrink_quick_pick.onDidAccept(() => {
4040
is_resolved = true
4141
resolve(
42-
shrink_quick_pick.selectedItems[0].label ===
42+
shrink_quick_pick.selectedItems[0].label ==
4343
t('command.search.shrink.strip')
4444
)
4545
shrink_quick_pick.hide()

‎apps/editor/src/commands/search-files-command/utils/search-files-by-term.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ export const search_files_by_term = async (params: {
88
files: string[]
99
search_term: string
1010
search_mode: 'phrase' | 'keywords' | 'intelligent'
11+
keywords_match_mode?: 'all' | 'some'
1112
}): Promise<string[]> => {
1213
const matched_files: string[] = []
1314

1415
let regexes: RegExp[] = []
15-
if (params.search_mode === 'keywords') {
16+
if (params.search_mode == 'keywords') {
1617
const keywords = params.search_term
1718
.split(',')
1819
.map((k) => k.trim())
@@ -22,9 +23,17 @@ export const search_files_by_term = async (params: {
2223
regexes = [create_search_regex(params.search_term)]
2324
}
2425

25-
if (regexes.length === 0) return []
26+
if (regexes.length == 0) return []
2627

27-
const matches_all = (text: string) => regexes.every((r) => r.test(text))
28+
const matches_condition = (text: string) => {
29+
if (
30+
params.search_mode == 'keywords' &&
31+
params.keywords_match_mode == 'some'
32+
) {
33+
return regexes.some((r) => r.test(text))
34+
}
35+
return regexes.every((r) => r.test(text))
36+
}
2837

2938
for (const file_path of params.files) {
3039
try {
@@ -34,7 +43,7 @@ export const search_files_by_term = async (params: {
3443
continue
3544
}
3645

37-
if (matches_all(file_name)) {
46+
if (matches_condition(file_name)) {
3847
matched_files.push(file_path)
3948
continue
4049
}
@@ -46,7 +55,7 @@ export const search_files_by_term = async (params: {
4655

4756
const content = await fs.promises.readFile(file_path, 'utf-8')
4857

49-
if (matches_all(content)) {
58+
if (matches_condition(content)) {
5059
matched_files.push(file_path)
5160
}
5261
} catch (error) {

‎apps/editor/src/commands/search-files-command/utils/show-results-and-apply.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const show_results_and_apply = async (params: {
134134
quick_pick.show()
135135
})
136136

137-
if (list_selection === 'back' || list_selection === 'cancel') {
137+
if (list_selection == 'back' || list_selection == 'cancel') {
138138
return list_selection
139139
}
140140

@@ -224,7 +224,7 @@ export const show_results_and_apply = async (params: {
224224
quick_pick_merge.show()
225225
})
226226

227-
if (choice === 'back') {
227+
if (choice == 'back') {
228228
should_continue_loop = true
229229
} else if (!choice) {
230230
return 'cancel'

‎apps/editor/src/constants/state-keys.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ export const LAST_SEARCH_FILES_PHRASE_QUERY_STATE_KEY =
8585
'last-search-files-phrase-query'
8686
export const LAST_SEARCH_FILES_KEYWORDS_QUERY_STATE_KEY =
8787
'last-search-files-keywords-query'
88+
export const LAST_SEARCH_FILES_KEYWORDS_MATCH_MODE_STATE_KEY =
89+
'last-search-files-keywords-match-mode'
8890
export const LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY =
8991
'last-search-files-intelligent-query'
9092

0 commit comments

Comments
 (0)