mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #4081
4081: Work around crlf in syntax tree r=matklad a=Veetaha Workarounds fixes #4067 Co-authored-by: veetaha <veetaha2@gmail.com>
This commit is contained in:
commit
e7bb260fbb
1 changed files with 35 additions and 2 deletions
|
@ -198,7 +198,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D
|
||||||
return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange);
|
return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange);
|
||||||
}
|
}
|
||||||
|
|
||||||
private findAstNodeRange(astLine: vscode.TextLine) {
|
private findAstNodeRange(astLine: vscode.TextLine): vscode.Range {
|
||||||
const lineOffset = astLine.range.start;
|
const lineOffset = astLine.range.start;
|
||||||
const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex);
|
const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex);
|
||||||
const end = lineOffset.translate(undefined, astLine.text.trimEnd().length);
|
const end = lineOffset.translate(undefined, astLine.text.trimEnd().length);
|
||||||
|
@ -209,10 +209,43 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D
|
||||||
const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine);
|
const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine);
|
||||||
if (!parsedRange) return;
|
if (!parsedRange) return;
|
||||||
|
|
||||||
const [begin, end] = parsedRange.slice(1).map(off => doc.positionAt(+off));
|
const [begin, end] = parsedRange
|
||||||
|
.slice(1)
|
||||||
|
.map(off => this.positionAt(doc, +off));
|
||||||
|
|
||||||
return new vscode.Range(begin, end);
|
return new vscode.Range(begin, end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Memoize the last value, otherwise the CPU is at 100% single core
|
||||||
|
// with quadratic lookups when we build rust2Ast cache
|
||||||
|
cache?: { doc: vscode.TextDocument; offset: number; line: number };
|
||||||
|
|
||||||
|
positionAt(doc: vscode.TextDocument, targetOffset: number): vscode.Position {
|
||||||
|
if (doc.eol === vscode.EndOfLine.LF) {
|
||||||
|
return doc.positionAt(targetOffset);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shitty workaround for crlf line endings
|
||||||
|
// We are still in this prehistoric era of carriage returns here...
|
||||||
|
|
||||||
|
let line = 0;
|
||||||
|
let offset = 0;
|
||||||
|
|
||||||
|
const cache = this.cache;
|
||||||
|
if (cache?.doc === doc && cache.offset <= targetOffset) {
|
||||||
|
({ line, offset } = cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
const lineLenWithLf = doc.lineAt(line).text.length + 1;
|
||||||
|
if (offset + lineLenWithLf > targetOffset) {
|
||||||
|
this.cache = { doc, offset, line };
|
||||||
|
return doc.positionAt(targetOffset + line);
|
||||||
|
}
|
||||||
|
offset += lineLenWithLf;
|
||||||
|
line += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Lazy<T> {
|
class Lazy<T> {
|
||||||
|
|
Loading…
Reference in a new issue