Skip to content

Commit d3a9a4c

Browse files
committed
Add filename search mode to the file search feature
- consitently rename 'tree' to 'ascii tree' (including translations) - below 'intelligent' implement mode: 'filename'. In this mode use logic used for 'keywords' (AND, OR) but search in file names only. prefer code duplication over reusing what we already have for cleaner reading. adjust descriptions properly. - add missing translations to [Fragment]
1 parent ac24451 commit d3a9a4c

8 files changed

Lines changed: 343 additions & 34 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ export const LAST_SEARCH_FILES_KEYWORDS_QUERY_STATE_KEY =
8787
'last-search-files-keywords-query'
8888
export const LAST_SEARCH_FILES_KEYWORDS_MATCH_MODE_STATE_KEY =
8989
'last-search-files-keywords-match-mode'
90+
export const LAST_SEARCH_FILES_FILENAME_QUERY_STATE_KEY =
91+
'last-search-files-filename-query'
92+
export const LAST_SEARCH_FILES_FILENAME_MATCH_MODE_STATE_KEY =
93+
'last-search-files-filename-match-mode'
9094
export const LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY =
9195
'last-search-files-intelligent-query'
9296

‎apps/editor/src/features/search-files/actions/search-files.ts‎

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import {
55
LAST_SEARCH_FILES_PHRASE_QUERY_STATE_KEY,
66
LAST_SEARCH_FILES_KEYWORDS_QUERY_STATE_KEY,
77
LAST_SEARCH_FILES_KEYWORDS_MATCH_MODE_STATE_KEY,
8+
LAST_SEARCH_FILES_FILENAME_QUERY_STATE_KEY,
9+
LAST_SEARCH_FILES_FILENAME_MATCH_MODE_STATE_KEY,
810
LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY,
911
LAST_SEARCH_FILES_FOR_CONTEXT_MODE_STATE_KEY,
1012
LAST_FIND_RELEVANT_FILES_SHRINK_STATE_KEY
1113
} from '@/constants/state-keys'
1214
import { prompt_for_search_mode } from '../utils/prompt-for-search-mode'
1315
import { prompt_for_keywords_match_mode } from '../utils/prompt-for-keywords-match-mode'
16+
import { prompt_for_filename_match_mode } from '../utils/prompt-for-filename-match-mode'
1417
import { prompt_for_search_term } from '../utils/prompt-for-search-term'
1518
import { analyze_files } from '../utils/analyze-files'
1619
import { prompt_for_shrink_mode } from '../utils/prompt-for-shrink-mode'
@@ -32,7 +35,7 @@ export const search_files = async (params: {
3235
> => {
3336
let initial_search_mode =
3437
params.extension_context.workspaceState.get<
35-
'phrase' | 'keywords' | 'intelligent'
38+
'phrase' | 'keywords' | 'filename' | 'intelligent'
3639
>(LAST_SEARCH_FILES_FOR_CONTEXT_MODE_STATE_KEY) || 'phrase'
3740

3841
while (true) {
@@ -79,6 +82,25 @@ export const search_files = async (params: {
7982
LAST_SEARCH_FILES_KEYWORDS_MATCH_MODE_STATE_KEY,
8083
keywords_match_mode
8184
)
85+
} else if (search_mode == 'filename') {
86+
const last_match_mode =
87+
params.extension_context.workspaceState.get<'all' | 'some'>(
88+
LAST_SEARCH_FILES_FILENAME_MATCH_MODE_STATE_KEY
89+
) || 'all'
90+
91+
const match_mode_result =
92+
await prompt_for_filename_match_mode(last_match_mode)
93+
if (match_mode_result == 'back') {
94+
go_back_to_mode = true
95+
break
96+
}
97+
if (!match_mode_result) return undefined
98+
99+
keywords_match_mode = match_mode_result
100+
await params.extension_context.workspaceState.update(
101+
LAST_SEARCH_FILES_FILENAME_MATCH_MODE_STATE_KEY,
102+
keywords_match_mode
103+
)
82104
}
83105

84106
let go_back_to_match_mode = false
@@ -90,7 +112,9 @@ export const search_files = async (params: {
90112
? LAST_SEARCH_FILES_PHRASE_QUERY_STATE_KEY
91113
: search_mode == 'keywords'
92114
? LAST_SEARCH_FILES_KEYWORDS_QUERY_STATE_KEY
93-
: LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY
115+
: search_mode == 'filename'
116+
? LAST_SEARCH_FILES_FILENAME_QUERY_STATE_KEY
117+
: LAST_SEARCH_FILES_INTELLIGENT_QUERY_STATE_KEY
94118

95119
const initial_search_term =
96120
params.extension_context.workspaceState.get<string>(state_key) || ''
@@ -100,7 +124,7 @@ export const search_files = async (params: {
100124
search_mode
101125
)
102126
if (result.back) {
103-
if (search_mode == 'keywords') {
127+
if (search_mode == 'keywords' || search_mode == 'filename') {
104128
go_back_to_match_mode = true
105129
} else {
106130
go_back_to_mode = true
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_filename_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.filename.match-mode.all'),
10+
description: t('command.search.filename.match-mode.all-description'),
11+
mode: 'all'
12+
},
13+
{
14+
label: t('command.search.filename.match-mode.some'),
15+
description: t('command.search.filename.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.filename.match-mode.title')
33+
quick_pick.placeholder = t('command.search.filename.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/features/search-files/utils/prompt-for-search-mode.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import * as vscode from 'vscode'
22
import { t } from '@/i18n'
33

44
export const prompt_for_search_mode = async (
5-
last_mode: 'phrase' | 'keywords' | 'intelligent',
5+
last_mode: 'phrase' | 'keywords' | 'filename' | 'intelligent',
66
show_back_button?: boolean
7-
): Promise<'phrase' | 'keywords' | 'intelligent' | undefined | 'back'> => {
7+
): Promise<
8+
'phrase' | 'keywords' | 'filename' | 'intelligent' | undefined | 'back'
9+
> => {
810
const items: (vscode.QuickPickItem & {
9-
mode: 'phrase' | 'keywords' | 'intelligent'
11+
mode: 'phrase' | 'keywords' | 'filename' | 'intelligent'
1012
})[] = [
1113
{
1214
label: t('command.search.mode.phrase'),
@@ -18,6 +20,11 @@ export const prompt_for_search_mode = async (
1820
description: t('command.search.mode.keywords-description'),
1921
mode: 'keywords'
2022
},
23+
{
24+
label: t('command.search.mode.filename'),
25+
description: t('command.search.mode.filename-description'),
26+
mode: 'filename'
27+
},
2128
{
2229
label: t('command.search.mode.intelligent'),
2330
description: t('command.search.mode.intelligent-description'),
@@ -33,7 +40,9 @@ export const prompt_for_search_mode = async (
3340
}
3441

3542
const quick_pick = vscode.window.createQuickPick<
36-
vscode.QuickPickItem & { mode: 'phrase' | 'keywords' | 'intelligent' }
43+
vscode.QuickPickItem & {
44+
mode: 'phrase' | 'keywords' | 'filename' | 'intelligent'
45+
}
3746
>()
3847
quick_pick.items = items
3948
quick_pick.activeItems = [active_item]

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { create_search_regex } from './create-search-regex'
88
export const prompt_for_search_results = async (params: {
99
matched_files: string[]
1010
search_term: string
11-
search_mode: 'phrase' | 'keywords' | 'intelligent'
11+
search_mode: 'phrase' | 'keywords' | 'filename' | 'intelligent'
1212
workspace_provider: WorkspaceProvider
1313
}): Promise<
1414
{ selected_paths: string[]; matched_paths: string[] } | undefined | 'back'
@@ -120,6 +120,8 @@ export const prompt_for_search_results = async (params: {
120120
.map((k) => k.trim())
121121
.filter((k) => k.length > 0)
122122
.map((k) => create_search_regex(k))
123+
} else if (params.search_mode == 'filename') {
124+
regexes = []
123125
} else {
124126
regexes = [create_search_regex(params.search_term)]
125127
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { t } from '@/i18n'
33

44
export const prompt_for_search_term = async (
55
initial_search_term: string,
6-
mode: 'phrase' | 'keywords' | 'intelligent'
6+
mode: 'phrase' | 'keywords' | 'filename' | 'intelligent'
77
): Promise<{ value: string | undefined; back?: boolean }> => {
88
const close_button = {
99
iconPath: new vscode.ThemeIcon('close'),
@@ -15,9 +15,11 @@ export const prompt_for_search_term = async (
1515
input_box.prompt =
1616
mode == 'keywords'
1717
? t('command.search.prompt.keywords')
18-
: mode == 'intelligent'
19-
? t('command.search.prompt.intelligent')
20-
: t('command.search.prompt')
18+
: mode == 'filename'
19+
? t('command.search.prompt.filename')
20+
: mode == 'intelligent'
21+
? t('command.search.prompt.intelligent')
22+
: t('command.search.prompt')
2123
input_box.placeholder = t('command.search.placeholder')
2224
input_box.value = initial_search_term
2325
input_box.ignoreFocusOut = true

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,50 @@ import { IGNORED_LOCK_FILES } from '@/constants/ignored-lock-files'
77
export const search_files_by_term = async (params: {
88
files: string[]
99
search_term: string
10-
search_mode: 'phrase' | 'keywords' | 'intelligent'
10+
search_mode: 'phrase' | 'keywords' | 'filename' | 'intelligent'
1111
keywords_match_mode?: 'all' | 'some'
1212
}): Promise<string[]> => {
1313
const matched_files: string[] = []
1414

15+
if (params.search_mode === 'filename') {
16+
const keywords = params.search_term
17+
.split(',')
18+
.map((k) => k.trim())
19+
.filter((k) => k.length > 0)
20+
const regexes = keywords.map((k) => create_search_regex(k))
21+
22+
if (regexes.length == 0) return []
23+
24+
const matches_condition = (text: string) => {
25+
if (params.keywords_match_mode == 'some') {
26+
return regexes.some((r) => r.test(text))
27+
}
28+
return regexes.every((r) => r.test(text))
29+
}
30+
31+
for (const file_path of params.files) {
32+
try {
33+
const file_name = path.basename(file_path)
34+
35+
if (IGNORED_LOCK_FILES.includes(file_name)) {
36+
continue
37+
}
38+
39+
if (matches_condition(file_name)) {
40+
matched_files.push(file_path)
41+
}
42+
} catch (error) {
43+
Logger.error({
44+
function_name: 'search_files_by_term',
45+
message: `Error reading file during search: ${file_path}`,
46+
data: error
47+
})
48+
}
49+
}
50+
51+
return matched_files
52+
}
53+
1554
let regexes: RegExp[] = []
1655
if (params.search_mode == 'keywords') {
1756
const keywords = params.search_term
@@ -43,11 +82,6 @@ export const search_files_by_term = async (params: {
4382
continue
4483
}
4584

46-
if (matches_condition(file_name)) {
47-
matched_files.push(file_path)
48-
continue
49-
}
50-
5185
const stats = await fs.promises.stat(file_path)
5286
if (stats.size > 1024 * 1024) {
5387
continue

0 commit comments

Comments
 (0)