@theia/file-search 1.45.0 → 1.46.0-next.72
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/README.md +30 -30
- package/lib/browser/file-search-frontend-module.d.ts +3 -3
- package/lib/browser/file-search-frontend-module.js +33 -33
- package/lib/browser/quick-file-open-contribution.d.ts +10 -10
- package/lib/browser/quick-file-open-contribution.js +75 -75
- package/lib/browser/quick-file-open.d.ts +79 -79
- package/lib/browser/quick-file-open.js +380 -380
- package/lib/common/file-search-service.d.ts +30 -30
- package/lib/common/file-search-service.js +21 -21
- package/lib/node/file-search-backend-module.d.ts +3 -3
- package/lib/node/file-search-backend-module.js +25 -25
- package/lib/node/file-search-service-impl.d.ts +15 -15
- package/lib/node/file-search-service-impl.js +196 -196
- package/lib/node/file-search-service-impl.js.map +1 -1
- package/lib/node/file-search-service-impl.spec.d.ts +1 -1
- package/lib/node/file-search-service-impl.spec.js +195 -195
- package/package.json +8 -8
- package/src/browser/file-search-frontend-module.ts +37 -37
- package/src/browser/quick-file-open-contribution.ts +66 -66
- package/src/browser/quick-file-open.ts +387 -387
- package/src/common/file-search-service.ts +52 -52
- package/src/node/file-search-backend-module.ts +30 -30
- package/src/node/file-search-service-impl.spec.ts +230 -230
- package/src/node/file-search-service-impl.ts +187 -187
|
@@ -1,187 +1,187 @@
|
|
|
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 * as cp from 'child_process';
|
|
18
|
-
import * as fuzzy from '@theia/core/shared/fuzzy';
|
|
19
|
-
import * as readline from 'readline';
|
|
20
|
-
import { rgPath } from '@vscode/ripgrep';
|
|
21
|
-
import { injectable, inject } from '@theia/core/shared/inversify';
|
|
22
|
-
import URI from '@theia/core/lib/common/uri';
|
|
23
|
-
import { FileUri } from '@theia/core/lib/
|
|
24
|
-
import { CancellationTokenSource, CancellationToken, ILogger, isWindows } from '@theia/core';
|
|
25
|
-
import { RawProcessFactory } from '@theia/process/lib/node';
|
|
26
|
-
import { FileSearchService, WHITESPACE_QUERY_SEPARATOR } from '../common/file-search-service';
|
|
27
|
-
import * as path from 'path';
|
|
28
|
-
|
|
29
|
-
@injectable()
|
|
30
|
-
export class FileSearchServiceImpl implements FileSearchService {
|
|
31
|
-
|
|
32
|
-
constructor(
|
|
33
|
-
@inject(ILogger) protected readonly logger: ILogger,
|
|
34
|
-
/** @deprecated since 1.7.0 */
|
|
35
|
-
@inject(RawProcessFactory) protected readonly rawProcessFactory: RawProcessFactory,
|
|
36
|
-
) { }
|
|
37
|
-
|
|
38
|
-
async find(searchPattern: string, options: FileSearchService.Options, clientToken?: CancellationToken): Promise<string[]> {
|
|
39
|
-
const cancellationSource = new CancellationTokenSource();
|
|
40
|
-
if (clientToken) {
|
|
41
|
-
clientToken.onCancellationRequested(() => cancellationSource.cancel());
|
|
42
|
-
}
|
|
43
|
-
const token = cancellationSource.token;
|
|
44
|
-
const opts = {
|
|
45
|
-
fuzzyMatch: true,
|
|
46
|
-
limit: Number.MAX_SAFE_INTEGER,
|
|
47
|
-
useGitIgnore: true,
|
|
48
|
-
...options
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
const roots: FileSearchService.RootOptions = options.rootOptions || {};
|
|
52
|
-
if (options.rootUris) {
|
|
53
|
-
for (const rootUri of options.rootUris) {
|
|
54
|
-
if (!roots[rootUri]) {
|
|
55
|
-
roots[rootUri] = {};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
// eslint-disable-next-line guard-for-in
|
|
60
|
-
for (const rootUri in roots) {
|
|
61
|
-
const rootOptions = roots[rootUri];
|
|
62
|
-
if (opts.includePatterns) {
|
|
63
|
-
const includePatterns = rootOptions.includePatterns || [];
|
|
64
|
-
rootOptions.includePatterns = [...includePatterns, ...opts.includePatterns];
|
|
65
|
-
}
|
|
66
|
-
if (opts.excludePatterns) {
|
|
67
|
-
const excludePatterns = rootOptions.excludePatterns || [];
|
|
68
|
-
rootOptions.excludePatterns = [...excludePatterns, ...opts.excludePatterns];
|
|
69
|
-
}
|
|
70
|
-
if (rootOptions.useGitIgnore === undefined) {
|
|
71
|
-
rootOptions.useGitIgnore = opts.useGitIgnore;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const exactMatches = new Set<string>();
|
|
76
|
-
const fuzzyMatches = new Set<string>();
|
|
77
|
-
|
|
78
|
-
if (isWindows) {
|
|
79
|
-
// Allow users on Windows to search for paths using either forwards or backwards slash
|
|
80
|
-
searchPattern = searchPattern.replace(/\//g, '\\');
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const patterns = searchPattern.toLocaleLowerCase().trim().split(WHITESPACE_QUERY_SEPARATOR);
|
|
84
|
-
|
|
85
|
-
await Promise.all(Object.keys(roots).map(async root => {
|
|
86
|
-
try {
|
|
87
|
-
const rootUri = new URI(root);
|
|
88
|
-
const rootPath = FileUri.fsPath(rootUri);
|
|
89
|
-
const rootOptions = roots[root];
|
|
90
|
-
|
|
91
|
-
await this.doFind(rootUri, rootOptions, candidate => {
|
|
92
|
-
|
|
93
|
-
// Convert OS-native candidate path to a file URI string
|
|
94
|
-
const fileUri = FileUri.create(path.resolve(rootPath, candidate)).toString();
|
|
95
|
-
|
|
96
|
-
// Skip results that have already been matched.
|
|
97
|
-
if (exactMatches.has(fileUri) || fuzzyMatches.has(fileUri)) {
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// Determine if the candidate matches any of the patterns exactly or fuzzy
|
|
102
|
-
const candidatePattern = candidate.toLocaleLowerCase();
|
|
103
|
-
const patternExists = patterns.every(pattern => candidatePattern.indexOf(pattern) !== -1);
|
|
104
|
-
if (patternExists) {
|
|
105
|
-
exactMatches.add(fileUri);
|
|
106
|
-
} else if (!searchPattern || searchPattern === '*') {
|
|
107
|
-
exactMatches.add(fileUri);
|
|
108
|
-
} else {
|
|
109
|
-
const fuzzyPatternExists = patterns.every(pattern => fuzzy.test(pattern, candidate));
|
|
110
|
-
if (opts.fuzzyMatch && fuzzyPatternExists) {
|
|
111
|
-
fuzzyMatches.add(fileUri);
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// Preemptively terminate the search when the list of exact matches reaches the limit.
|
|
116
|
-
if (exactMatches.size === opts.limit) {
|
|
117
|
-
cancellationSource.cancel();
|
|
118
|
-
}
|
|
119
|
-
}, token);
|
|
120
|
-
} catch (e) {
|
|
121
|
-
console.error('Failed to search:', root, e);
|
|
122
|
-
}
|
|
123
|
-
}));
|
|
124
|
-
|
|
125
|
-
if (clientToken && clientToken.isCancellationRequested) {
|
|
126
|
-
return [];
|
|
127
|
-
}
|
|
128
|
-
// Return the list of results limited by the search limit.
|
|
129
|
-
return [...exactMatches, ...fuzzyMatches].slice(0, opts.limit);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
protected doFind(rootUri: URI, options: FileSearchService.BaseOptions, accept: (fileUri: string) => void, token: CancellationToken): Promise<void> {
|
|
133
|
-
return new Promise((resolve, reject) => {
|
|
134
|
-
const cwd = FileUri.fsPath(rootUri);
|
|
135
|
-
const args = this.getSearchArgs(options);
|
|
136
|
-
const ripgrep = cp.spawn(rgPath, args, { cwd });
|
|
137
|
-
ripgrep.on('error', reject);
|
|
138
|
-
ripgrep.on('exit', (code, signal) => {
|
|
139
|
-
if (typeof code === 'number' && code !== 0) {
|
|
140
|
-
reject(new Error(`"${rgPath}" exited with code: ${code}`));
|
|
141
|
-
} else if (typeof signal === 'string') {
|
|
142
|
-
reject(new Error(`"${rgPath}" was terminated by signal: ${signal}`));
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
token.onCancellationRequested(() => {
|
|
146
|
-
ripgrep.kill(); // most likely sends a signal.
|
|
147
|
-
resolve(); // avoid rejecting for no good reason.
|
|
148
|
-
});
|
|
149
|
-
const lineReader = readline.createInterface({
|
|
150
|
-
input: ripgrep.stdout,
|
|
151
|
-
crlfDelay: Infinity,
|
|
152
|
-
});
|
|
153
|
-
lineReader.on('line', line => {
|
|
154
|
-
if (!token.isCancellationRequested) {
|
|
155
|
-
accept(line);
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
lineReader.on('close', () => resolve());
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
protected getSearchArgs(options: FileSearchService.BaseOptions): string[] {
|
|
163
|
-
const args = ['--files', '--hidden', '--case-sensitive', '--no-require-git', '--no-config'];
|
|
164
|
-
if (options.includePatterns) {
|
|
165
|
-
for (const includePattern of options.includePatterns) {
|
|
166
|
-
if (includePattern) {
|
|
167
|
-
args.push('--glob', includePattern);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
if (options.excludePatterns) {
|
|
172
|
-
for (const excludePattern of options.excludePatterns) {
|
|
173
|
-
if (excludePattern) {
|
|
174
|
-
args.push('--glob', `!${excludePattern}`);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (options.useGitIgnore) {
|
|
179
|
-
// ripgrep follows `.gitignore` by default, but it doesn't exclude `.git`:
|
|
180
|
-
args.push('--glob', '!.git');
|
|
181
|
-
} else {
|
|
182
|
-
args.push('--no-ignore');
|
|
183
|
-
}
|
|
184
|
-
return args;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
}
|
|
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 * as cp from 'child_process';
|
|
18
|
+
import * as fuzzy from '@theia/core/shared/fuzzy';
|
|
19
|
+
import * as readline from 'readline';
|
|
20
|
+
import { rgPath } from '@vscode/ripgrep';
|
|
21
|
+
import { injectable, inject } from '@theia/core/shared/inversify';
|
|
22
|
+
import URI from '@theia/core/lib/common/uri';
|
|
23
|
+
import { FileUri } from '@theia/core/lib/common/file-uri';
|
|
24
|
+
import { CancellationTokenSource, CancellationToken, ILogger, isWindows } from '@theia/core';
|
|
25
|
+
import { RawProcessFactory } from '@theia/process/lib/node';
|
|
26
|
+
import { FileSearchService, WHITESPACE_QUERY_SEPARATOR } from '../common/file-search-service';
|
|
27
|
+
import * as path from 'path';
|
|
28
|
+
|
|
29
|
+
@injectable()
|
|
30
|
+
export class FileSearchServiceImpl implements FileSearchService {
|
|
31
|
+
|
|
32
|
+
constructor(
|
|
33
|
+
@inject(ILogger) protected readonly logger: ILogger,
|
|
34
|
+
/** @deprecated since 1.7.0 */
|
|
35
|
+
@inject(RawProcessFactory) protected readonly rawProcessFactory: RawProcessFactory,
|
|
36
|
+
) { }
|
|
37
|
+
|
|
38
|
+
async find(searchPattern: string, options: FileSearchService.Options, clientToken?: CancellationToken): Promise<string[]> {
|
|
39
|
+
const cancellationSource = new CancellationTokenSource();
|
|
40
|
+
if (clientToken) {
|
|
41
|
+
clientToken.onCancellationRequested(() => cancellationSource.cancel());
|
|
42
|
+
}
|
|
43
|
+
const token = cancellationSource.token;
|
|
44
|
+
const opts = {
|
|
45
|
+
fuzzyMatch: true,
|
|
46
|
+
limit: Number.MAX_SAFE_INTEGER,
|
|
47
|
+
useGitIgnore: true,
|
|
48
|
+
...options
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const roots: FileSearchService.RootOptions = options.rootOptions || {};
|
|
52
|
+
if (options.rootUris) {
|
|
53
|
+
for (const rootUri of options.rootUris) {
|
|
54
|
+
if (!roots[rootUri]) {
|
|
55
|
+
roots[rootUri] = {};
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// eslint-disable-next-line guard-for-in
|
|
60
|
+
for (const rootUri in roots) {
|
|
61
|
+
const rootOptions = roots[rootUri];
|
|
62
|
+
if (opts.includePatterns) {
|
|
63
|
+
const includePatterns = rootOptions.includePatterns || [];
|
|
64
|
+
rootOptions.includePatterns = [...includePatterns, ...opts.includePatterns];
|
|
65
|
+
}
|
|
66
|
+
if (opts.excludePatterns) {
|
|
67
|
+
const excludePatterns = rootOptions.excludePatterns || [];
|
|
68
|
+
rootOptions.excludePatterns = [...excludePatterns, ...opts.excludePatterns];
|
|
69
|
+
}
|
|
70
|
+
if (rootOptions.useGitIgnore === undefined) {
|
|
71
|
+
rootOptions.useGitIgnore = opts.useGitIgnore;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const exactMatches = new Set<string>();
|
|
76
|
+
const fuzzyMatches = new Set<string>();
|
|
77
|
+
|
|
78
|
+
if (isWindows) {
|
|
79
|
+
// Allow users on Windows to search for paths using either forwards or backwards slash
|
|
80
|
+
searchPattern = searchPattern.replace(/\//g, '\\');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const patterns = searchPattern.toLocaleLowerCase().trim().split(WHITESPACE_QUERY_SEPARATOR);
|
|
84
|
+
|
|
85
|
+
await Promise.all(Object.keys(roots).map(async root => {
|
|
86
|
+
try {
|
|
87
|
+
const rootUri = new URI(root);
|
|
88
|
+
const rootPath = FileUri.fsPath(rootUri);
|
|
89
|
+
const rootOptions = roots[root];
|
|
90
|
+
|
|
91
|
+
await this.doFind(rootUri, rootOptions, candidate => {
|
|
92
|
+
|
|
93
|
+
// Convert OS-native candidate path to a file URI string
|
|
94
|
+
const fileUri = FileUri.create(path.resolve(rootPath, candidate)).toString();
|
|
95
|
+
|
|
96
|
+
// Skip results that have already been matched.
|
|
97
|
+
if (exactMatches.has(fileUri) || fuzzyMatches.has(fileUri)) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Determine if the candidate matches any of the patterns exactly or fuzzy
|
|
102
|
+
const candidatePattern = candidate.toLocaleLowerCase();
|
|
103
|
+
const patternExists = patterns.every(pattern => candidatePattern.indexOf(pattern) !== -1);
|
|
104
|
+
if (patternExists) {
|
|
105
|
+
exactMatches.add(fileUri);
|
|
106
|
+
} else if (!searchPattern || searchPattern === '*') {
|
|
107
|
+
exactMatches.add(fileUri);
|
|
108
|
+
} else {
|
|
109
|
+
const fuzzyPatternExists = patterns.every(pattern => fuzzy.test(pattern, candidate));
|
|
110
|
+
if (opts.fuzzyMatch && fuzzyPatternExists) {
|
|
111
|
+
fuzzyMatches.add(fileUri);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Preemptively terminate the search when the list of exact matches reaches the limit.
|
|
116
|
+
if (exactMatches.size === opts.limit) {
|
|
117
|
+
cancellationSource.cancel();
|
|
118
|
+
}
|
|
119
|
+
}, token);
|
|
120
|
+
} catch (e) {
|
|
121
|
+
console.error('Failed to search:', root, e);
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
|
|
125
|
+
if (clientToken && clientToken.isCancellationRequested) {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
// Return the list of results limited by the search limit.
|
|
129
|
+
return [...exactMatches, ...fuzzyMatches].slice(0, opts.limit);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
protected doFind(rootUri: URI, options: FileSearchService.BaseOptions, accept: (fileUri: string) => void, token: CancellationToken): Promise<void> {
|
|
133
|
+
return new Promise((resolve, reject) => {
|
|
134
|
+
const cwd = FileUri.fsPath(rootUri);
|
|
135
|
+
const args = this.getSearchArgs(options);
|
|
136
|
+
const ripgrep = cp.spawn(rgPath, args, { cwd });
|
|
137
|
+
ripgrep.on('error', reject);
|
|
138
|
+
ripgrep.on('exit', (code, signal) => {
|
|
139
|
+
if (typeof code === 'number' && code !== 0) {
|
|
140
|
+
reject(new Error(`"${rgPath}" exited with code: ${code}`));
|
|
141
|
+
} else if (typeof signal === 'string') {
|
|
142
|
+
reject(new Error(`"${rgPath}" was terminated by signal: ${signal}`));
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
token.onCancellationRequested(() => {
|
|
146
|
+
ripgrep.kill(); // most likely sends a signal.
|
|
147
|
+
resolve(); // avoid rejecting for no good reason.
|
|
148
|
+
});
|
|
149
|
+
const lineReader = readline.createInterface({
|
|
150
|
+
input: ripgrep.stdout,
|
|
151
|
+
crlfDelay: Infinity,
|
|
152
|
+
});
|
|
153
|
+
lineReader.on('line', line => {
|
|
154
|
+
if (!token.isCancellationRequested) {
|
|
155
|
+
accept(line);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
lineReader.on('close', () => resolve());
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
protected getSearchArgs(options: FileSearchService.BaseOptions): string[] {
|
|
163
|
+
const args = ['--files', '--hidden', '--case-sensitive', '--no-require-git', '--no-config'];
|
|
164
|
+
if (options.includePatterns) {
|
|
165
|
+
for (const includePattern of options.includePatterns) {
|
|
166
|
+
if (includePattern) {
|
|
167
|
+
args.push('--glob', includePattern);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (options.excludePatterns) {
|
|
172
|
+
for (const excludePattern of options.excludePatterns) {
|
|
173
|
+
if (excludePattern) {
|
|
174
|
+
args.push('--glob', `!${excludePattern}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (options.useGitIgnore) {
|
|
179
|
+
// ripgrep follows `.gitignore` by default, but it doesn't exclude `.git`:
|
|
180
|
+
args.push('--glob', '!.git');
|
|
181
|
+
} else {
|
|
182
|
+
args.push('--no-ignore');
|
|
183
|
+
}
|
|
184
|
+
return args;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
}
|