mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-06-13 07:10:26 +08:00
smarter sorting of references, better merging of neighour references, fixes
This commit is contained in:
@ -18,7 +18,7 @@ export default class ReferencesDocument {
|
||||
constructor(emitter: vscode.EventEmitter<vscode.Uri>, uri: vscode.Uri, locations: vscode.Location[]) {
|
||||
this._emitter = emitter;
|
||||
this._uri = uri;
|
||||
this._locations = locations.sort(ReferencesDocument._compareLocations);
|
||||
this._locations = locations;
|
||||
|
||||
// print header
|
||||
this._lines = [`Found ${this._locations.length} references`];
|
||||
@ -66,8 +66,8 @@ export default class ReferencesDocument {
|
||||
resolve(this);
|
||||
return;
|
||||
}
|
||||
let [uri, ranges] = entry.value;
|
||||
|
||||
let [uri, ranges] = entry.value;
|
||||
this._fetchAndFormatLocation(vscode.Uri.parse(uri), ranges).then(lines => {
|
||||
this._emitter.fire(this._uri);
|
||||
next();
|
||||
@ -82,21 +82,12 @@ export default class ReferencesDocument {
|
||||
return vscode.workspace.openTextDocument(uri).then(doc => {
|
||||
|
||||
this._lines.push('', uri.toString());
|
||||
|
||||
for (let i = 0; i < ranges.length; i++) {
|
||||
const range = ranges[i];
|
||||
const {start: {line}} = range;
|
||||
|
||||
let prev = ranges[i - 1];
|
||||
this._appendContext(doc, line, prev ? Math.min(2, line - prev.start.line) : 2, false);
|
||||
|
||||
this._appendMatch(doc, range);
|
||||
|
||||
let next = ranges[i + 1];
|
||||
this._appendContext(doc, line + 1, next ? Math.min(2, next.start.line - line) : 2, true);
|
||||
|
||||
if (next) {
|
||||
this._lines.push(' ...');
|
||||
}
|
||||
const {start: {line}} = ranges[i];
|
||||
this._appendLeading(doc, line, ranges[i - 1]);
|
||||
this._appendMatch(doc, line, ranges[i]);
|
||||
this._appendTrailing(doc, line, ranges[i + 1]);
|
||||
}
|
||||
|
||||
}, err => {
|
||||
@ -104,34 +95,36 @@ export default class ReferencesDocument {
|
||||
});
|
||||
}
|
||||
|
||||
private _appendContext(doc: vscode.TextDocument, line: number, offset: number, down: boolean) {
|
||||
let from = down ? line : line - offset;
|
||||
let to = down ? line + offset : line;
|
||||
while (from < to) {
|
||||
if (from >= 0 && from < doc.lineCount) {
|
||||
const text = doc.lineAt(from).text;
|
||||
this._lines.push(` ${from + 1}` + (text && ` ${text}`));
|
||||
}
|
||||
from++;
|
||||
private _appendLeading(doc: vscode.TextDocument, line: number, previous: vscode.Range): void {
|
||||
let from = Math.max(0, line - 3, previous && previous.end.line || 0);
|
||||
while (++from < line) {
|
||||
const text = doc.lineAt(from).text;
|
||||
this._lines.push(` ${from + 1}` + (text && ` ${text}`));
|
||||
}
|
||||
}
|
||||
|
||||
private _appendMatch(doc: vscode.TextDocument, range: vscode.Range) {
|
||||
let line = range.start.line;
|
||||
private _appendMatch(doc: vscode.TextDocument, line:number, match: vscode.Range) {
|
||||
let text = doc.lineAt(line).text;
|
||||
let preamble = ` ${line + 1}: `;
|
||||
|
||||
this._ranges.push(new vscode.Range(this._lines.length, preamble.length + range.start.character, this._lines.length, preamble.length + range.end.character));
|
||||
this._ranges.push(new vscode.Range(
|
||||
this._lines.length, preamble.length + match.start.character,
|
||||
this._lines.length, preamble.length + match.end.character)
|
||||
);
|
||||
this._lines.push(preamble + text);
|
||||
}
|
||||
|
||||
private static _compareLocations(a: vscode.Location, b: vscode.Location): number {
|
||||
if (a.uri.toString() < b.uri.toString()) {
|
||||
return -1;
|
||||
} else if (a.uri.toString() > b.uri.toString()) {
|
||||
return 1;
|
||||
} else {
|
||||
return a.range.start.compareTo(b.range.start)
|
||||
private _appendTrailing(doc: vscode.TextDocument, line: number, next: vscode.Range): void {
|
||||
let to = Math.min(doc.lineCount, line + 3);
|
||||
if (next && next.start.line - to <= 2) {
|
||||
// next is too close
|
||||
return;
|
||||
}
|
||||
while (++line < to) {
|
||||
const text = doc.lineAt(line).text;
|
||||
this._lines.push(` ${line + 1}` + (text && ` ${text}`));
|
||||
}
|
||||
if (next) {
|
||||
this._lines.push(` ...`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user