-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathlib.ts
More file actions
345 lines (288 loc) · 11.2 KB
/
lib.ts
File metadata and controls
345 lines (288 loc) · 11.2 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { createTwoFilesPatch } from 'diff';
import * as fs from 'fs';
import * as path from 'path';
import { exit } from 'process';
import { scip } from './scip';
import { Input } from './lsif-typescript/Input';
import { Range } from './lsif-typescript/Range';
import { IndexOptions } from './MainCommand';
export interface ScipConfig extends IndexOptions {
/**
* The directory where to generate the index.scip file.
*
* All `Document.relative_path` fields will be relative paths to this directory.
*/
projectRoot: string;
infer: { projectVersionFromCommit: boolean };
writeIndex: (index: scip.Index) => void;
}
function getSymbolTable(doc: scip.Document): Map<string, scip.SymbolInformation> {
let symbolTable = new Map();
for (const symbol of doc.symbols) {
symbolTable.set(symbol.symbol, symbol);
}
return symbolTable;
}
const packageName = 'scip-python python';
const commentSyntax = '#';
const formatOptionsPrefix = '# format-options:';
function parseOptions(lines: string[]): {
showDocs: boolean;
showRanges: boolean;
} {
const formatOptions = {
showDocs: false,
showRanges: false,
};
for (let line of lines) {
if (!line.startsWith(formatOptionsPrefix)) {
continue;
}
const options = line.slice(formatOptionsPrefix.length).trim().split(',');
for (let option of options) {
const optionName = option.trim();
if (!(optionName in formatOptions)) {
throw new Error(`Invalid format option: ${optionName}`);
}
formatOptions[optionName as keyof typeof formatOptions] = true;
}
break;
}
return formatOptions;
}
export function formatSnapshot(
input: Input,
doc: scip.Document,
externalSymbols: scip.SymbolInformation[] = []
): string {
const out: string[] = [];
const symbolTable = getSymbolTable(doc);
const externalSymbolTable: Map<string, scip.SymbolInformation> = new Map();
for (let externalSymbol of externalSymbols) {
externalSymbolTable.set(externalSymbol.symbol, externalSymbol);
}
const enclosingRanges: { range: Range; symbol: string }[] = [];
const symbolsWithDefinitions: Set<string> = new Set();
const formatOptions = parseOptions(input.lines);
for (let occurrence of doc.occurrences) {
const isDefinition = (occurrence.symbol_roles & scip.SymbolRole.Definition) > 0;
if (isDefinition) {
symbolsWithDefinitions.add(occurrence.symbol);
}
if (formatOptions.showRanges && occurrence.enclosing_range.length > 0) {
enclosingRanges.push({
range: Range.fromLsif(occurrence.enclosing_range),
symbol: occurrence.symbol,
});
}
}
enclosingRanges.sort(enclosingRangesByLine);
const enclosingRangeStarts: (typeof enclosingRanges)[number][][] = Array.from(Array(input.lines.length), () => []);
const enclosingRangeEnds: (typeof enclosingRanges)[number][][] = Array.from(Array(input.lines.length), () => []);
for (const enclosingRange of enclosingRanges) {
enclosingRangeStarts[enclosingRange.range.start.line].push(enclosingRange);
enclosingRangeEnds[enclosingRange.range.end.line].unshift(enclosingRange);
}
const emittedDocstrings: Set<string> = new Set();
const pushDoc = (range: Range, symbol: string, isDefinition: boolean, isStartOfLine: boolean) => {
// Only emit docstrings once
if (emittedDocstrings.has(symbol)) {
out.push('\n');
return;
}
// Only definitions OR symbols without a definition should be emitted
if (!isDefinition && symbolsWithDefinitions.has(symbol)) {
out.push('\n');
return;
}
emittedDocstrings.add(symbol);
let prefix = '\n' + commentSyntax;
if (!isStartOfLine) {
prefix += ' '.repeat(range.start.character - 1);
}
const pushOneDoc = (docs: string[], external: boolean) => {
if (!formatOptions.showDocs) {
return;
}
for (const documentation of docs) {
for (const [idx, line] of documentation.split('\n').entries()) {
out.push(prefix);
if (idx == 0) {
if (external) {
out.push('external ');
}
out.push('documentation ');
} else {
out.push(' > ');
}
out.push(line.slice(0, 40));
if (line.length > 40) {
out.push('...');
}
}
}
};
const pushOneRelationship = (relationships: scip.Relationship[]) => {
relationships.sort((a, b) => a.symbol.localeCompare(b.symbol));
for (const relationship of relationships) {
out.push(prefix);
out.push('relationship');
if (relationship.is_implementation) {
out.push(' implementation');
}
if (relationship.is_reference) {
out.push(' reference');
}
if (relationship.is_type_definition) {
out.push(' type_definition');
}
out.push(' ' + relationship.symbol);
}
};
const externalSymbol = externalSymbolTable.get(symbol);
if (externalSymbol) {
pushOneDoc(externalSymbol.documentation, true);
pushOneRelationship(externalSymbol.relationships);
} else {
const info = symbolTable.get(symbol);
if (info) {
pushOneDoc(info.documentation, false);
pushOneRelationship(info.relationships);
}
}
out.push('\n');
};
const pushEnclosingRange = (
enclosingRange: {
range: Range;
symbol: string;
},
end: boolean = false
) => {
if (!formatOptions.showRanges) {
return;
}
out.push(commentSyntax);
out.push(' '.repeat(Math.max(1, enclosingRange.range.start.character - 1)));
if (enclosingRange.range.start.character < 2) {
out.push('<');
} else if (end) {
out.push('^');
} else {
out.push('⌄');
}
if (end) {
out.push(' end ');
} else {
out.push(' start ');
}
out.push('enclosing_range ');
out.push(enclosingRange.symbol);
out.push('\n');
};
doc.occurrences.sort(occurrencesByLine);
let occurrenceIndex = 0;
for (const [lineNumber, line] of input.lines.entries()) {
// Write 0,0 items ABOVE the first line.
// This is the only case where we would need to do this.
if (occurrenceIndex == 0) {
const occurrence = doc.occurrences[occurrenceIndex];
const range = Range.fromLsif(occurrence.range);
// This is essentially a "file-based" item.
// This guarantees that this sits above everything else in the file.
if (range.start.character == 0 && range.end.character == 0) {
const isDefinition = (occurrence.symbol_roles & scip.SymbolRole.Definition) > 0;
out.push(commentSyntax);
out.push(' < ');
out.push(isDefinition ? 'definition' : 'reference');
out.push(' ');
out.push(occurrence.symbol);
pushDoc(range, occurrence.symbol, isDefinition, true);
out.push('\n');
occurrenceIndex++;
}
}
// Check if any enclosing ranges start on this line
for (const enclosingRange of enclosingRangeStarts[lineNumber]) {
pushEnclosingRange(enclosingRange);
}
out.push('');
out.push(line);
out.push('\n');
while (occurrenceIndex < doc.occurrences.length && doc.occurrences[occurrenceIndex].range[0] === lineNumber) {
const occurrence = doc.occurrences[occurrenceIndex];
occurrenceIndex++;
if (occurrence.symbol === undefined) {
continue;
}
if (occurrence.range.length > 3) {
throw 'not yet implemented, multi-line ranges';
}
const range = Range.fromLsif(occurrence.range);
out.push(commentSyntax);
const isStartOfLine = range.start.character == 0;
if (!isStartOfLine) {
out.push(' '.repeat(range.start.character - 1));
}
let modifier = 0;
if (isStartOfLine) {
modifier = 1;
}
const caretLength = range.end.character - range.start.character - modifier;
if (caretLength < 0) {
throw new Error(input.format(range, 'negative length occurrence!'));
}
out.push('^'.repeat(caretLength));
out.push(' ');
const isDefinition = (occurrence.symbol_roles & scip.SymbolRole.Definition) > 0;
out.push(isDefinition ? 'definition' : 'reference');
out.push(' ');
const symbol = occurrence.symbol.startsWith(packageName)
? occurrence.symbol.slice(packageName.length)
: occurrence.symbol;
out.push(symbol.replace('\n', '|'));
pushDoc(range, occurrence.symbol, isDefinition, isStartOfLine);
}
// Check if any enclosing ranges end on this line
for (const enclosingRange of enclosingRangeEnds[lineNumber]) {
pushEnclosingRange(enclosingRange, true);
}
}
return out.join('');
}
export function writeSnapshot(outputPath: string, obtained: string): void {
// eslint-disable-next-line no-sync
fs.mkdirSync(path.dirname(outputPath), {
recursive: true,
});
// eslint-disable-next-line no-sync
fs.writeFileSync(outputPath, obtained, { flag: 'w' });
}
export function diffSnapshot(outputPath: string, obtained: string): 'equal' | 'different' {
let existing = fs.readFileSync(outputPath, { encoding: 'utf8' });
if (obtained === existing) {
return 'equal';
}
console.error(
createTwoFilesPatch(
outputPath,
outputPath,
existing,
obtained,
'(what the snapshot tests expect)',
'(what the current code produces). Run the command "npm run update-snapshots" to accept the new behavior.'
)
);
return 'different';
}
function occurrencesByLine(a: scip.Occurrence, b: scip.Occurrence): number {
return Range.fromLsif(a.range).compare(Range.fromLsif(b.range));
}
function enclosingRangesByLine(a: { range: Range; symbol: string }, b: { range: Range; symbol: string }): number {
// Return the range that starts first, and if they start at the same line, the one that ends last (enclosing).
const rangeCompare = a.range.compare(b.range);
if (rangeCompare !== 0) {
return rangeCompare;
}
return b.range.end.line - a.range.end.line;
}