From 07bd4bedcb9a0c560578399e840b413b500d4a46 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 22 Apr 2020 02:04:28 +0300 Subject: Work around crlf in syntax tree --- editors/code/src/commands/syntax_tree.ts | 40 ++++++++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) (limited to 'editors/code/src') diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index b7a397414..f00f46be2 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -198,7 +198,7 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D 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 begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex); const end = lineOffset.translate(undefined, astLine.text.trimEnd().length); @@ -209,10 +209,46 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine); 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); } + + // Shitty memoize the last value, otherwise the CPU is at 100% single core + // with quadratic lookups when we build rust2Ast cache + memo?: [vscode.TextDocument, number, number]; + + positionAt(doc: vscode.TextDocument, offset: number): vscode.Position { + if (doc.eol === vscode.EndOfLine.LF) { + return doc.positionAt(offset); + } + + // God damn shitty workaround for crlf line endings + // We are still in this prehistoric era of carriage returns here... + + let i = 0; + let curOffset = 0; + + if (this.memo) { + const [memDoc, memOffset, memI] = this.memo; + if (memDoc === doc && memOffset <= offset) { + curOffset = memOffset; + i = memI; + } + } + + while (true) { + const lineLenWithLf = doc.lineAt(i).text.length + 1; + curOffset += lineLenWithLf; + if (curOffset > offset) { + this.memo = [doc, curOffset - lineLenWithLf, i]; + return doc.positionAt(offset + i); + } + i += 1; + } + } } class Lazy { -- cgit v1.2.3 From c12d0e0214ea3d7262eff4b9e21a033ee423ef99 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 22 Apr 2020 02:28:44 +0300 Subject: Refactor the workaround a bit --- editors/code/src/commands/syntax_tree.ts | 35 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'editors/code/src') diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index f00f46be2..cfcf47b2f 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -216,37 +216,34 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D 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 - 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) { - 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... - let i = 0; - let curOffset = 0; + let line = 0; + let offset = 0; - if (this.memo) { - const [memDoc, memOffset, memI] = this.memo; - if (memDoc === doc && memOffset <= offset) { - curOffset = memOffset; - i = memI; - } + const cache = this.cache; + if (cache?.doc === doc && cache.offset <= targetOffset) { + ({ line, offset } = cache); } while (true) { - const lineLenWithLf = doc.lineAt(i).text.length + 1; - curOffset += lineLenWithLf; - if (curOffset > offset) { - this.memo = [doc, curOffset - lineLenWithLf, i]; - return doc.positionAt(offset + i); + const lineLenWithLf = doc.lineAt(line).text.length + 1; + if (offset + lineLenWithLf > targetOffset) { + this.cache = { doc, offset, line }; + return doc.positionAt(targetOffset + line); } - i += 1; + offset += lineLenWithLf; + line += 1; } } } -- cgit v1.2.3