mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
Refactor the workaround a bit
This commit is contained in:
parent
07bd4bedcb
commit
c12d0e0214
1 changed files with 16 additions and 19 deletions
|
@ -216,37 +216,34 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D
|
||||||
return new vscode.Range(begin, end);
|
return new vscode.Range(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Shitty memoize the last value, otherwise the CPU is at 100% single core
|
// Memoize the last value, otherwise the CPU is at 100% single core
|
||||||
// with quadratic lookups when we build rust2Ast cache
|
// with quadratic lookups when we build rust2Ast cache
|
||||||
memo?: [vscode.TextDocument, number, number];
|
cache?: { doc: vscode.TextDocument; offset: number; line: number };
|
||||||
|
|
||||||
positionAt(doc: vscode.TextDocument, offset: number): vscode.Position {
|
positionAt(doc: vscode.TextDocument, targetOffset: number): vscode.Position {
|
||||||
if (doc.eol === vscode.EndOfLine.LF) {
|
if (doc.eol === vscode.EndOfLine.LF) {
|
||||||
return doc.positionAt(offset);
|
return doc.positionAt(targetOffset);
|
||||||
}
|
}
|
||||||
|
|
||||||
// God damn shitty workaround for crlf line endings
|
// Shitty workaround for crlf line endings
|
||||||
// We are still in this prehistoric era of carriage returns here...
|
// We are still in this prehistoric era of carriage returns here...
|
||||||
|
|
||||||
let i = 0;
|
let line = 0;
|
||||||
let curOffset = 0;
|
let offset = 0;
|
||||||
|
|
||||||
if (this.memo) {
|
const cache = this.cache;
|
||||||
const [memDoc, memOffset, memI] = this.memo;
|
if (cache?.doc === doc && cache.offset <= targetOffset) {
|
||||||
if (memDoc === doc && memOffset <= offset) {
|
({ line, offset } = cache);
|
||||||
curOffset = memOffset;
|
|
||||||
i = memI;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const lineLenWithLf = doc.lineAt(i).text.length + 1;
|
const lineLenWithLf = doc.lineAt(line).text.length + 1;
|
||||||
curOffset += lineLenWithLf;
|
if (offset + lineLenWithLf > targetOffset) {
|
||||||
if (curOffset > offset) {
|
this.cache = { doc, offset, line };
|
||||||
this.memo = [doc, curOffset - lineLenWithLf, i];
|
return doc.positionAt(targetOffset + line);
|
||||||
return doc.positionAt(offset + i);
|
|
||||||
}
|
}
|
||||||
i += 1;
|
offset += lineLenWithLf;
|
||||||
|
line += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue