@theia/file-search 1.58.2 → 1.59.0-next.62
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser/file-search-frontend-module.d.ts.map +1 -1
- package/lib/browser/file-search-frontend-module.js +2 -0
- package/lib/browser/file-search-frontend-module.js.map +1 -1
- package/lib/browser/quick-file-open.d.ts +7 -20
- package/lib/browser/quick-file-open.d.ts.map +1 -1
- package/lib/browser/quick-file-open.js +16 -178
- package/lib/browser/quick-file-open.js.map +1 -1
- package/lib/browser/quick-file-select-service.d.ts +57 -0
- package/lib/browser/quick-file-select-service.d.ts.map +1 -0
- package/lib/browser/quick-file-select-service.js +276 -0
- package/lib/browser/quick-file-select-service.js.map +1 -0
- package/package.json +8 -8
- package/src/browser/file-search-frontend-module.ts +2 -0
- package/src/browser/quick-file-open.ts +14 -196
- package/src/browser/quick-file-select-service.ts +298 -0
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
// *****************************************************************************
|
|
2
|
+
// Copyright (C) 2017 TypeFox and others.
|
|
3
|
+
//
|
|
4
|
+
// This program and the accompanying materials are made available under the
|
|
5
|
+
// terms of the Eclipse Public License v. 2.0 which is available at
|
|
6
|
+
// http://www.eclipse.org/legal/epl-2.0.
|
|
7
|
+
//
|
|
8
|
+
// This Source Code may also be made available under the following Secondary
|
|
9
|
+
// Licenses when the conditions for such availability set forth in the Eclipse
|
|
10
|
+
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
|
|
11
|
+
// with the GNU Classpath Exception which is available at
|
|
12
|
+
// https://www.gnu.org/software/classpath/license.html.
|
|
13
|
+
//
|
|
14
|
+
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
|
|
15
|
+
// *****************************************************************************
|
|
16
|
+
|
|
17
|
+
import { KeybindingRegistry, OpenerService, PreferenceService, QuickAccessRegistry } from '@theia/core/lib/browser';
|
|
18
|
+
import { LabelProvider } from '@theia/core/lib/browser/label-provider';
|
|
19
|
+
import { findMatches, QuickInputService, QuickPickItem, QuickPicks } from '@theia/core/lib/browser/quick-input/quick-input-service';
|
|
20
|
+
import { CancellationToken, nls, QuickPickSeparator } from '@theia/core/lib/common';
|
|
21
|
+
import { MessageService } from '@theia/core/lib/common/message-service';
|
|
22
|
+
import URI from '@theia/core/lib/common/uri';
|
|
23
|
+
import * as fuzzy from '@theia/core/shared/fuzzy';
|
|
24
|
+
import { inject, injectable, optional } from '@theia/core/shared/inversify';
|
|
25
|
+
import { Position, Range } from '@theia/editor/lib/browser';
|
|
26
|
+
import { NavigationLocationService } from '@theia/editor/lib/browser/navigation/navigation-location-service';
|
|
27
|
+
import { FileSystemPreferences } from '@theia/filesystem/lib/browser';
|
|
28
|
+
import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service';
|
|
29
|
+
import { FileSearchService, WHITESPACE_QUERY_SEPARATOR } from '../common/file-search-service';
|
|
30
|
+
|
|
31
|
+
export interface FilterAndRange {
|
|
32
|
+
filter: string;
|
|
33
|
+
range?: Range;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface QuickFileSelectOptions {
|
|
37
|
+
/** Whether to hide .gitignored (and other ignored) files. */
|
|
38
|
+
hideIgnoredFiles?: boolean;
|
|
39
|
+
/** Executed when the item is selected. */
|
|
40
|
+
onSelect?: (item: FileQuickPickItem) => void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Supports patterns of <path><#|:><line><#|:|,><col?>
|
|
44
|
+
const LINE_COLON_PATTERN = /\s?[#:\(](?:line )?(\d*)(?:[#:,](\d*))?\)?\s*$/;
|
|
45
|
+
export type FileQuickPickItem = QuickPickItem & { uri: URI };
|
|
46
|
+
|
|
47
|
+
export namespace FileQuickPickItem {
|
|
48
|
+
export function is(obj: QuickPickItem | QuickPickSeparator): obj is FileQuickPickItem {
|
|
49
|
+
return obj && 'uri' in obj;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@injectable()
|
|
54
|
+
export class QuickFileSelectService {
|
|
55
|
+
|
|
56
|
+
@inject(KeybindingRegistry)
|
|
57
|
+
protected readonly keybindingRegistry: KeybindingRegistry;
|
|
58
|
+
@inject(WorkspaceService)
|
|
59
|
+
protected readonly workspaceService: WorkspaceService;
|
|
60
|
+
@inject(OpenerService)
|
|
61
|
+
protected readonly openerService: OpenerService;
|
|
62
|
+
@inject(QuickInputService) @optional()
|
|
63
|
+
protected readonly quickInputService: QuickInputService;
|
|
64
|
+
@inject(QuickAccessRegistry)
|
|
65
|
+
protected readonly quickAccessRegistry: QuickAccessRegistry;
|
|
66
|
+
@inject(FileSearchService)
|
|
67
|
+
protected readonly fileSearchService: FileSearchService;
|
|
68
|
+
@inject(LabelProvider)
|
|
69
|
+
protected readonly labelProvider: LabelProvider;
|
|
70
|
+
@inject(NavigationLocationService)
|
|
71
|
+
protected readonly navigationLocationService: NavigationLocationService;
|
|
72
|
+
@inject(MessageService)
|
|
73
|
+
protected readonly messageService: MessageService;
|
|
74
|
+
@inject(FileSystemPreferences)
|
|
75
|
+
protected readonly fsPreferences: FileSystemPreferences;
|
|
76
|
+
@inject(PreferenceService)
|
|
77
|
+
protected readonly preferences: PreferenceService;
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* The score constants when comparing file search results.
|
|
81
|
+
*/
|
|
82
|
+
private static readonly Scores = {
|
|
83
|
+
max: 1000, // represents the maximum score from fuzzy matching (Infinity).
|
|
84
|
+
exact: 500, // represents the score assigned to exact matching.
|
|
85
|
+
partial: 250 // represents the score assigned to partial matching.
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
async getPicks(
|
|
89
|
+
fileFilter: string = '',
|
|
90
|
+
token: CancellationToken = CancellationToken.None,
|
|
91
|
+
options: QuickFileSelectOptions = {
|
|
92
|
+
hideIgnoredFiles: true
|
|
93
|
+
}
|
|
94
|
+
): Promise<QuickPicks> {
|
|
95
|
+
const roots = this.workspaceService.tryGetRoots();
|
|
96
|
+
|
|
97
|
+
const alreadyCollected = new Set<string>();
|
|
98
|
+
const recentlyUsedItems: QuickPicks = [];
|
|
99
|
+
|
|
100
|
+
if (this.preferences.get('search.quickOpen.includeHistory')) {
|
|
101
|
+
const locations = [...this.navigationLocationService.locations()].reverse();
|
|
102
|
+
for (const location of locations) {
|
|
103
|
+
const uriString = location.uri.toString();
|
|
104
|
+
|
|
105
|
+
if (location.uri.scheme === 'file' && !alreadyCollected.has(uriString) && fuzzy.test(fileFilter, uriString)) {
|
|
106
|
+
if (recentlyUsedItems.length === 0) {
|
|
107
|
+
recentlyUsedItems.push({
|
|
108
|
+
type: 'separator',
|
|
109
|
+
label: nls.localizeByDefault('recently opened')
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
const item = this.toItem(fileFilter, location.uri);
|
|
113
|
+
recentlyUsedItems.push(item);
|
|
114
|
+
alreadyCollected.add(uriString);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
if (fileFilter.length > 0) {
|
|
120
|
+
const handler = async (results: string[]) => {
|
|
121
|
+
if (token.isCancellationRequested || results.length <= 0) {
|
|
122
|
+
return [];
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
const result = [...recentlyUsedItems];
|
|
126
|
+
const fileSearchResultItems: FileQuickPickItem[] = [];
|
|
127
|
+
|
|
128
|
+
for (const fileUri of results) {
|
|
129
|
+
if (!alreadyCollected.has(fileUri)) {
|
|
130
|
+
const item = this.toItem(fileFilter, fileUri, options.onSelect);
|
|
131
|
+
fileSearchResultItems.push(item);
|
|
132
|
+
alreadyCollected.add(fileUri);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Create a copy of the file search results and sort.
|
|
137
|
+
const sortedResults = fileSearchResultItems.slice();
|
|
138
|
+
sortedResults.sort((a, b) => this.compareItems(a, b, fileFilter));
|
|
139
|
+
|
|
140
|
+
if (sortedResults.length > 0) {
|
|
141
|
+
result.push({
|
|
142
|
+
type: 'separator',
|
|
143
|
+
label: nls.localizeByDefault('file results')
|
|
144
|
+
});
|
|
145
|
+
result.push(...sortedResults);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Return the recently used items, followed by the search results.
|
|
149
|
+
return result;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
return this.fileSearchService.find(fileFilter, {
|
|
153
|
+
rootUris: roots.map(r => r.resource.toString()),
|
|
154
|
+
fuzzyMatch: true,
|
|
155
|
+
limit: 200,
|
|
156
|
+
useGitIgnore: options.hideIgnoredFiles,
|
|
157
|
+
excludePatterns: options.hideIgnoredFiles
|
|
158
|
+
? Object.keys(this.fsPreferences['files.exclude'])
|
|
159
|
+
: undefined,
|
|
160
|
+
}, token).then(handler);
|
|
161
|
+
} else {
|
|
162
|
+
return roots.length !== 0 ? recentlyUsedItems : [];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
protected compareItems(
|
|
167
|
+
left: FileQuickPickItem,
|
|
168
|
+
right: FileQuickPickItem,
|
|
169
|
+
fileFilter: string
|
|
170
|
+
): number {
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Score a given string.
|
|
174
|
+
*
|
|
175
|
+
* @param str the string to score on.
|
|
176
|
+
* @returns the score.
|
|
177
|
+
*/
|
|
178
|
+
function score(str: string | undefined): number {
|
|
179
|
+
if (!str) {
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
let exactMatch = true;
|
|
184
|
+
const partialMatches = querySplit.reduce((matched, part) => {
|
|
185
|
+
const partMatches = str.includes(part);
|
|
186
|
+
exactMatch = exactMatch && partMatches;
|
|
187
|
+
return partMatches ? matched + QuickFileSelectService.Scores.partial : matched;
|
|
188
|
+
}, 0);
|
|
189
|
+
|
|
190
|
+
// Check fuzzy matches.
|
|
191
|
+
const fuzzyMatch = fuzzy.match(queryJoin, str) ?? { score: 0 };
|
|
192
|
+
if (fuzzyMatch.score === Infinity && exactMatch) {
|
|
193
|
+
return Number.MAX_SAFE_INTEGER;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
return fuzzyMatch.score + partialMatches + (exactMatch ? QuickFileSelectService.Scores.exact : 0);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const query: string = normalize(fileFilter);
|
|
200
|
+
// Adjust for whitespaces in the query.
|
|
201
|
+
const querySplit = query.split(WHITESPACE_QUERY_SEPARATOR);
|
|
202
|
+
const queryJoin = querySplit.join('');
|
|
203
|
+
|
|
204
|
+
const compareByLabelScore = (l: FileQuickPickItem, r: FileQuickPickItem) => score(r.label) - score(l.label);
|
|
205
|
+
const compareByLabelIndex = (l: FileQuickPickItem, r: FileQuickPickItem) => r.label.indexOf(query) - l.label.indexOf(query);
|
|
206
|
+
const compareByLabel = (l: FileQuickPickItem, r: FileQuickPickItem) => l.label.localeCompare(r.label);
|
|
207
|
+
|
|
208
|
+
const compareByPathScore = (l: FileQuickPickItem, r: FileQuickPickItem) => score(r.uri.path.toString()) - score(l.uri.path.toString());
|
|
209
|
+
const compareByPathIndex = (l: FileQuickPickItem, r: FileQuickPickItem) => r.uri.path.toString().indexOf(query) - l.uri.path.toString().indexOf(query);
|
|
210
|
+
const compareByPathLabel = (l: FileQuickPickItem, r: FileQuickPickItem) => l.uri.path.toString().localeCompare(r.uri.path.toString());
|
|
211
|
+
|
|
212
|
+
return compareWithDiscriminators(left, right, compareByLabelScore, compareByLabelIndex, compareByLabel, compareByPathScore, compareByPathIndex, compareByPathLabel);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
private toItem(lookFor: string, uriOrString: URI | string, onSelect?: ((item: FileQuickPickItem) => void) | undefined): FileQuickPickItem {
|
|
216
|
+
const uri = uriOrString instanceof URI ? uriOrString : new URI(uriOrString);
|
|
217
|
+
const label = this.labelProvider.getName(uri);
|
|
218
|
+
const description = this.getItemDescription(uri);
|
|
219
|
+
const iconClasses = this.getItemIconClasses(uri);
|
|
220
|
+
|
|
221
|
+
const item = <FileQuickPickItem>{
|
|
222
|
+
label,
|
|
223
|
+
description,
|
|
224
|
+
highlights: {
|
|
225
|
+
label: findMatches(label, lookFor),
|
|
226
|
+
description: findMatches(description, lookFor)
|
|
227
|
+
},
|
|
228
|
+
iconClasses,
|
|
229
|
+
uri
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
...item,
|
|
233
|
+
execute: () => onSelect ? onSelect(item) : undefined
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
private getItemIconClasses(uri: URI): string[] | undefined {
|
|
238
|
+
const icon = this.labelProvider.getIcon(uri).split(' ').filter(v => v.length > 0);
|
|
239
|
+
if (icon.length > 0) {
|
|
240
|
+
icon.push('file-icon');
|
|
241
|
+
}
|
|
242
|
+
return icon;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
private getItemDescription(uri: URI): string {
|
|
246
|
+
return this.labelProvider.getDetails(uri);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Splits the given expression into a structure of search-file-filter and
|
|
251
|
+
* location-range.
|
|
252
|
+
*
|
|
253
|
+
* @param expression patterns of <path><#|:><line><#|:|,><col?>
|
|
254
|
+
*/
|
|
255
|
+
protected splitFilterAndRange(expression: string): FilterAndRange {
|
|
256
|
+
let filter = expression;
|
|
257
|
+
let range = undefined;
|
|
258
|
+
|
|
259
|
+
// Find line and column number from the expression using RegExp.
|
|
260
|
+
const patternMatch = LINE_COLON_PATTERN.exec(expression);
|
|
261
|
+
|
|
262
|
+
if (patternMatch) {
|
|
263
|
+
const line = parseInt(patternMatch[1] ?? '', 10);
|
|
264
|
+
if (Number.isFinite(line)) {
|
|
265
|
+
const lineNumber = line > 0 ? line - 1 : 0;
|
|
266
|
+
|
|
267
|
+
const column = parseInt(patternMatch[2] ?? '', 10);
|
|
268
|
+
const startColumn = Number.isFinite(column) && column > 0 ? column - 1 : 0;
|
|
269
|
+
const position = Position.create(lineNumber, startColumn);
|
|
270
|
+
|
|
271
|
+
filter = expression.substring(0, patternMatch.index);
|
|
272
|
+
range = Range.create(position, position);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return { filter, range };
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Normalize a given string.
|
|
281
|
+
*
|
|
282
|
+
* @param str the raw string value.
|
|
283
|
+
* @returns the normalized string value.
|
|
284
|
+
*/
|
|
285
|
+
function normalize(str: string): string {
|
|
286
|
+
return str.trim().toLowerCase();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function compareWithDiscriminators<T>(left: T, right: T, ...discriminators: ((left: T, right: T) => number)[]): number {
|
|
290
|
+
let comparisonValue = 0;
|
|
291
|
+
let i = 0;
|
|
292
|
+
|
|
293
|
+
while (comparisonValue === 0 && i < discriminators.length) {
|
|
294
|
+
comparisonValue = discriminators[i](left, right);
|
|
295
|
+
i++;
|
|
296
|
+
}
|
|
297
|
+
return comparisonValue;
|
|
298
|
+
}
|