aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorveetaha <[email protected]>2020-04-22 00:04:28 +0100
committerveetaha <[email protected]>2020-04-22 10:33:11 +0100
commit07bd4bedcb9a0c560578399e840b413b500d4a46 (patch)
tree6bbbb943f1d9b3f17508d4f0ada0ed1d1bfbfa5f /editors/code/src
parent546f9ee7a7eb1d208fe279ec469b5981d47934fc (diff)
Work around crlf in syntax tree
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/commands/syntax_tree.ts40
1 files changed, 38 insertions, 2 deletions
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
198 return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange); 198 return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange);
199 } 199 }
200 200
201 private findAstNodeRange(astLine: vscode.TextLine) { 201 private findAstNodeRange(astLine: vscode.TextLine): vscode.Range {
202 const lineOffset = astLine.range.start; 202 const lineOffset = astLine.range.start;
203 const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex); 203 const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex);
204 const end = lineOffset.translate(undefined, astLine.text.trimEnd().length); 204 const end = lineOffset.translate(undefined, astLine.text.trimEnd().length);
@@ -209,10 +209,46 @@ class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, D
209 const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine); 209 const parsedRange = /\[(\d+); (\d+)\)/.exec(astLine);
210 if (!parsedRange) return; 210 if (!parsedRange) return;
211 211
212 const [begin, end] = parsedRange.slice(1).map(off => doc.positionAt(+off)); 212 const [begin, end] = parsedRange
213 .slice(1)
214 .map(off => this.positionAt(doc, +off));
213 215
214 return new vscode.Range(begin, end); 216 return new vscode.Range(begin, end);
215 } 217 }
218
219 // Shitty memoize the last value, otherwise the CPU is at 100% single core
220 // with quadratic lookups when we build rust2Ast cache
221 memo?: [vscode.TextDocument, number, number];
222
223 positionAt(doc: vscode.TextDocument, offset: number): vscode.Position {
224 if (doc.eol === vscode.EndOfLine.LF) {
225 return doc.positionAt(offset);
226 }
227
228 // God damn shitty workaround for crlf line endings
229 // We are still in this prehistoric era of carriage returns here...
230
231 let i = 0;
232 let curOffset = 0;
233
234 if (this.memo) {
235 const [memDoc, memOffset, memI] = this.memo;
236 if (memDoc === doc && memOffset <= offset) {
237 curOffset = memOffset;
238 i = memI;
239 }
240 }
241
242 while (true) {
243 const lineLenWithLf = doc.lineAt(i).text.length + 1;
244 curOffset += lineLenWithLf;
245 if (curOffset > offset) {
246 this.memo = [doc, curOffset - lineLenWithLf, i];
247 return doc.positionAt(offset + i);
248 }
249 i += 1;
250 }
251 }
216} 252}
217 253
218class Lazy<T> { 254class Lazy<T> {