Skip to content

Commit f358b1a

Browse files
committed
sourcegraph#86 - Adds enclosing markers
1 parent 0ca44f5 commit f358b1a

File tree

1 file changed

+82
-0
lines changed
  • packages/pyright-scip/src

1 file changed

+82
-0
lines changed

packages/pyright-scip/src/lib.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,25 @@ export function formatSnapshot(
7171
externalSymbolTable.set(externalSymbol.symbol, externalSymbol);
7272
}
7373

74+
const enclosingRanges: { range: Range; symbol: string }[] = [];
7475
const symbolsWithDefinitions: Set<string> = new Set();
76+
7577
for (let occurrence of doc.occurrences) {
7678
const isDefinition = (occurrence.symbol_roles & scip.SymbolRole.Definition) > 0;
7779
if (isDefinition) {
7880
symbolsWithDefinitions.add(occurrence.symbol);
7981
}
82+
83+
if (occurrence.enclosing_range.length > 0) {
84+
enclosingRanges.push({
85+
range: Range.fromLsif(occurrence.enclosing_range),
86+
symbol: occurrence.symbol,
87+
});
88+
}
8089
}
8190

91+
enclosingRanges.sort(enclosingRangesByLine);
92+
8293
const emittedDocstrings: Set<string> = new Set();
8394
const pushDoc = (range: Range, symbol: string, isDefinition: boolean, isStartOfLine: boolean) => {
8495
// Only emit docstrings once
@@ -157,8 +168,33 @@ export function formatSnapshot(
157168
out.push('\n');
158169
};
159170

171+
const pushEnclosingRange = (
172+
enclosingRange: {
173+
range: Range;
174+
symbol: string;
175+
},
176+
end: boolean = false
177+
) => {
178+
if (!formatOptions.showRanges) {
179+
return;
180+
}
181+
182+
out.push(commentSyntax);
183+
out.push(' '.repeat(Math.max(1, enclosingRange.range.start.character - 1)));
184+
if (end) {
185+
out.push('⌃ end ');
186+
} else {
187+
out.push('⌄ start ');
188+
}
189+
out.push('enclosing_range ');
190+
out.push(enclosingRange.symbol);
191+
out.push('\n');
192+
};
193+
160194
doc.occurrences.sort(occurrencesByLine);
161195
let occurrenceIndex = 0;
196+
const openEnclosingRanges = [];
197+
162198
for (const [lineNumber, line] of input.lines.entries()) {
163199
// Write 0,0 items ABOVE the first line.
164200
// This is the only case where we would need to do this.
@@ -182,6 +218,26 @@ export function formatSnapshot(
182218
}
183219
}
184220

221+
// Check if any enclosing ranges start on this line
222+
for (let rangeIndex = 0; rangeIndex < enclosingRanges.length; rangeIndex++) {
223+
const enclosingRange = enclosingRanges[rangeIndex];
224+
225+
if (enclosingRange.range.start.line == lineNumber) {
226+
// Switch the range to the open list
227+
enclosingRanges.splice(rangeIndex, 1);
228+
openEnclosingRanges.push(enclosingRange);
229+
230+
// Decrement the counter as an item was removed
231+
rangeIndex -= 1;
232+
233+
pushEnclosingRange(enclosingRange);
234+
235+
continue;
236+
}
237+
238+
break;
239+
}
240+
185241
out.push('');
186242
out.push(line);
187243
out.push('\n');
@@ -226,6 +282,21 @@ export function formatSnapshot(
226282

227283
pushDoc(range, occurrence.symbol, isDefinition, isStartOfLine);
228284
}
285+
286+
for (let openRangeIndex = openEnclosingRanges.length - 1; openRangeIndex >= 0; openRangeIndex--) {
287+
const enclosingRange = openEnclosingRanges[openRangeIndex];
288+
289+
if (enclosingRange.range.end.line == lineNumber) {
290+
// Switch the range to the open list
291+
openEnclosingRanges.splice(openRangeIndex, 1);
292+
293+
pushEnclosingRange(enclosingRange, true);
294+
295+
continue;
296+
}
297+
298+
break;
299+
}
229300
}
230301
return out.join('');
231302
}
@@ -261,3 +332,14 @@ export function diffSnapshot(outputPath: string, obtained: string): void {
261332
function occurrencesByLine(a: scip.Occurrence, b: scip.Occurrence): number {
262333
return Range.fromLsif(a.range).compare(Range.fromLsif(b.range));
263334
}
335+
336+
function enclosingRangesByLine(a: { range: Range; symbol: string }, b: { range: Range; symbol: string }): number {
337+
// Return the range that starts first, and if they start at the same line, the one that ends last (enclosing).
338+
const rangeCompare = a.range.compare(b.range);
339+
340+
if (rangeCompare !== 0) {
341+
return rangeCompare;
342+
}
343+
344+
return b.range.end.line - a.range.end.line;
345+
}

0 commit comments

Comments
 (0)