-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathcodeSearchStrategy.ts
More file actions
134 lines (120 loc) · 4.26 KB
/
codeSearchStrategy.ts
File metadata and controls
134 lines (120 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import { injectable } from 'inversify';
import type * as vscodeType from 'vscode';
import { PathMapping } from '../../configuration';
import { FileGlobList } from '../fileGlobList';
import { ILogger, LogTag } from '../logging';
import { forceForwardSlashes } from '../pathUtils';
import { NodeSearchStrategy } from './nodeSearchStrategy';
import { ISourceMapMetadata } from './sourceMap';
import { createMetadataForFile, ISearchStrategy } from './sourceMapRepository';
/**
* A source map repository that uses VS Code's proposed search API to
* look for candidate files.
*/
@injectable()
export class CodeSearchStrategy implements ISearchStrategy {
private readonly nodeStrategy = new NodeSearchStrategy(this.logger);
constructor(private readonly vscode: typeof vscodeType, private readonly logger: ILogger) {}
public static createOrFallback(logger: ILogger) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const code: typeof import('vscode') = require('vscode');
if (code.workspace.findTextInFiles !== undefined) {
return new CodeSearchStrategy(code, logger);
}
} catch {
// ignored -- VS won't have vscode as a viable import, fall back to the memory/node.js version
}
return new NodeSearchStrategy(logger);
}
/**
* @inheritdoc
*/
public async streamAllChildren<T>(
files: FileGlobList,
onChild: (child: string) => T | Promise<T>,
): Promise<T[]> {
// see https://github.com/microsoft/vscode/issues/101889
return this.nodeStrategy.streamAllChildren(files, onChild);
}
/**
* @inheritdoc
*/
public async streamChildrenWithPathMaps<T>(
pathMapping: PathMapping,
onChild: (child: Required<ISourceMapMetadata>) => Promise<T>,
pattern = '**',
): Promise<T[]> {
const todo: Promise<T>[] = [];
// process pathMapping config
const mappedPaths = Object.keys(pathMapping);
for (const path of mappedPaths) {
const files = await this.vscode.workspace.findFiles(
new this.vscode.RelativePattern(path, pattern),
);
for (const file of files) {
const sourceMapUrl = file.path.replace(path, pathMapping[path]);
todo.push(
onChild({
compiledPath: file.path,
mtime: -1,
sourceMapUrl,
}),
);
}
}
return (await Promise.all(todo)).filter((t): t is T => t !== undefined);
}
/**
* @inheritdoc
*/
public async streamChildrenWithSourcemaps<T>(
outFiles: FileGlobList,
onChild: (child: Required<ISourceMapMetadata>) => T | Promise<T>,
): Promise<T[]> {
const todo: Promise<T | void>[] = [];
await this.vscode.workspace.findTextInFiles(
{ pattern: 'sourceMappingURL', isCaseSensitive: true },
{
...this.getTextSearchOptions(outFiles),
previewOptions: { charsPerLine: Number.MAX_SAFE_INTEGER, matchLines: 1 },
},
result => {
const text = 'text' in result ? result.text : result.preview.text;
todo.push(
createMetadataForFile(result.uri.fsPath, text)
.then(parsed => parsed && onChild(parsed))
.catch(error =>
this.logger.warn(LogTag.SourceMapParsing, 'Error parsing source map', {
error,
file: result.uri.fsPath,
}),
),
);
},
);
this.logger.info(LogTag.SourceMapParsing, `findTextInFiles search found ${todo.length} files`);
return (await Promise.all(todo)).filter((t): t is T => t !== undefined);
}
private getTextSearchOptions(files: FileGlobList): vscodeType.FindTextInFilesOptions {
return {
include: new this.vscode.RelativePattern(
files.rootPath,
forceForwardSlashes(files.patterns.filter(p => !p.startsWith('!')).join(', ')),
),
exclude: files.patterns
.filter(p => p.startsWith('!'))
.map(p => forceForwardSlashes(p.slice(1)))
.join(','),
useDefaultExcludes: false,
useIgnoreFiles: false,
useGlobalIgnoreFiles: false,
followSymlinks: true,
beforeContext: 0,
afterContext: 0,
};
}
}