diff options
author | veetaha <[email protected]> | 2020-04-22 00:28:44 +0100 |
---|---|---|
committer | veetaha <[email protected]> | 2020-04-22 10:35:03 +0100 |
commit | c12d0e0214ea3d7262eff4b9e21a033ee423ef99 (patch) | |
tree | ece08a471d69fa678ac1cd588c2def1149fddec8 /editors | |
parent | 07bd4bedcb9a0c560578399e840b413b500d4a46 (diff) |
Refactor the workaround a bit
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/src/commands/syntax_tree.ts | 35 |
1 files changed, 16 insertions, 19 deletions
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 | |||
216 | return new vscode.Range(begin, end); | 216 | return new vscode.Range(begin, end); |
217 | } | 217 | } |
218 | 218 | ||
219 | // Shitty memoize the last value, otherwise the CPU is at 100% single core | 219 | // Memoize the last value, otherwise the CPU is at 100% single core |
220 | // with quadratic lookups when we build rust2Ast cache | 220 | // with quadratic lookups when we build rust2Ast cache |
221 | memo?: [vscode.TextDocument, number, number]; | 221 | cache?: { doc: vscode.TextDocument; offset: number; line: number }; |
222 | 222 | ||
223 | positionAt(doc: vscode.TextDocument, offset: number): vscode.Position { | 223 | positionAt(doc: vscode.TextDocument, targetOffset: number): vscode.Position { |
224 | if (doc.eol === vscode.EndOfLine.LF) { | 224 | if (doc.eol === vscode.EndOfLine.LF) { |
225 | return doc.positionAt(offset); | 225 | return doc.positionAt(targetOffset); |
226 | } | 226 | } |
227 | 227 | ||
228 | // God damn shitty workaround for crlf line endings | 228 | // Shitty workaround for crlf line endings |
229 | // We are still in this prehistoric era of carriage returns here... | 229 | // We are still in this prehistoric era of carriage returns here... |
230 | 230 | ||
231 | let i = 0; | 231 | let line = 0; |
232 | let curOffset = 0; | 232 | let offset = 0; |
233 | 233 | ||
234 | if (this.memo) { | 234 | const cache = this.cache; |
235 | const [memDoc, memOffset, memI] = this.memo; | 235 | if (cache?.doc === doc && cache.offset <= targetOffset) { |
236 | if (memDoc === doc && memOffset <= offset) { | 236 | ({ line, offset } = cache); |
237 | curOffset = memOffset; | ||
238 | i = memI; | ||
239 | } | ||
240 | } | 237 | } |
241 | 238 | ||
242 | while (true) { | 239 | while (true) { |
243 | const lineLenWithLf = doc.lineAt(i).text.length + 1; | 240 | const lineLenWithLf = doc.lineAt(line).text.length + 1; |
244 | curOffset += lineLenWithLf; | 241 | if (offset + lineLenWithLf > targetOffset) { |
245 | if (curOffset > offset) { | 242 | this.cache = { doc, offset, line }; |
246 | this.memo = [doc, curOffset - lineLenWithLf, i]; | 243 | return doc.positionAt(targetOffset + line); |
247 | return doc.positionAt(offset + i); | ||
248 | } | 244 | } |
249 | i += 1; | 245 | offset += lineLenWithLf; |
246 | line += 1; | ||
250 | } | 247 | } |
251 | } | 248 | } |
252 | } | 249 | } |