aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/commands
diff options
context:
space:
mode:
authorveetaha <[email protected]>2020-04-02 01:35:58 +0100
committerveetaha <[email protected]>2020-04-02 09:13:38 +0100
commit036a8aee2af960428218cecd68ea2ea1813ab7f6 (patch)
tree4e9d5e68530fb5dc60415c85ce996f6e64194667 /editors/code/src/commands
parente4c69443e7fac2843262d6890557de1f9803e34a (diff)
vscode: postrefactor
Diffstat (limited to 'editors/code/src/commands')
-rw-r--r--editors/code/src/commands/syntax_tree.ts18
1 files changed, 6 insertions, 12 deletions
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts
index 7b58cf788..b7a397414 100644
--- a/editors/code/src/commands/syntax_tree.ts
+++ b/editors/code/src/commands/syntax_tree.ts
@@ -82,7 +82,7 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
82 82
83// FIXME: consider implementing this via the Tree View API? 83// FIXME: consider implementing this via the Tree View API?
84// https://code.visualstudio.com/api/extension-guides/tree-view 84// https://code.visualstudio.com/api/extension-guides/tree-view
85class AstInspector implements vscode.HoverProvider, Disposable { 85class AstInspector implements vscode.HoverProvider, vscode.DefinitionProvider, Disposable {
86 private readonly astDecorationType = vscode.window.createTextEditorDecorationType({ 86 private readonly astDecorationType = vscode.window.createTextEditorDecorationType({
87 borderColor: new vscode.ThemeColor('rust_analyzer.syntaxTreeBorder'), 87 borderColor: new vscode.ThemeColor('rust_analyzer.syntaxTreeBorder'),
88 borderStyle: "solid", 88 borderStyle: "solid",
@@ -96,8 +96,7 @@ class AstInspector implements vscode.HoverProvider, Disposable {
96 const astEditor = this.findAstTextEditor(); 96 const astEditor = this.findAstTextEditor();
97 if (!this.rustEditor || !astEditor) return undefined; 97 if (!this.rustEditor || !astEditor) return undefined;
98 98
99 console.time("Build goto def index"); 99 const buf: [vscode.Range, vscode.Range][] = [];
100 let buf: [vscode.Range, vscode.Range][] = [];
101 for (let i = 0; i < astEditor.document.lineCount; ++i) { 100 for (let i = 0; i < astEditor.document.lineCount; ++i) {
102 const astLine = astEditor.document.lineAt(i); 101 const astLine = astEditor.document.lineAt(i);
103 102
@@ -108,10 +107,8 @@ class AstInspector implements vscode.HoverProvider, Disposable {
108 const rustRange = this.parseRustTextRange(this.rustEditor.document, astLine.text); 107 const rustRange = this.parseRustTextRange(this.rustEditor.document, astLine.text);
109 if (!rustRange) continue; 108 if (!rustRange) continue;
110 109
111 buf.push([rustRange, this.findAstRange(astLine)]); 110 buf.push([rustRange, this.findAstNodeRange(astLine)]);
112 } 111 }
113
114 console.timeEnd("Build goto def index");
115 return buf; 112 return buf;
116 }); 113 });
117 114
@@ -167,9 +164,7 @@ class AstInspector implements vscode.HoverProvider, Disposable {
167 const astEditor = this.findAstTextEditor(); 164 const astEditor = this.findAstTextEditor();
168 if (!astEditor) return; 165 if (!astEditor) return;
169 166
170 console.time("Goto def");
171 const rust2AstRanges = this.rust2Ast.get()?.find(([rustRange, _]) => rustRange.contains(pos)); 167 const rust2AstRanges = this.rust2Ast.get()?.find(([rustRange, _]) => rustRange.contains(pos));
172 console.timeEnd("Goto def");
173 if (!rust2AstRanges) return; 168 if (!rust2AstRanges) return;
174 169
175 const [rustFileRange, astFileRange] = rust2AstRanges; 170 const [rustFileRange, astFileRange] = rust2AstRanges;
@@ -198,12 +193,12 @@ class AstInspector implements vscode.HoverProvider, Disposable {
198 this.rustEditor.revealRange(rustFileRange); 193 this.rustEditor.revealRange(rustFileRange);
199 194
200 const rustSourceCode = this.rustEditor.document.getText(rustFileRange); 195 const rustSourceCode = this.rustEditor.document.getText(rustFileRange);
201 const astFileRange = this.findAstRange(astFileLine); 196 const astFileRange = this.findAstNodeRange(astFileLine);
202 197
203 return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange); 198 return new vscode.Hover(["```rust\n" + rustSourceCode + "\n```"], astFileRange);
204 } 199 }
205 200
206 private findAstRange(astLine: vscode.TextLine) { 201 private findAstNodeRange(astLine: vscode.TextLine) {
207 const lineOffset = astLine.range.start; 202 const lineOffset = astLine.range.start;
208 const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex); 203 const begin = lineOffset.translate(undefined, astLine.firstNonWhitespaceCharacterIndex);
209 const end = lineOffset.translate(undefined, astLine.text.trimEnd().length); 204 const end = lineOffset.translate(undefined, astLine.text.trimEnd().length);
@@ -223,7 +218,7 @@ class AstInspector implements vscode.HoverProvider, Disposable {
223class Lazy<T> { 218class Lazy<T> {
224 val: undefined | T; 219 val: undefined | T;
225 220
226 constructor(private readonly compute: () => undefined | T) {} 221 constructor(private readonly compute: () => undefined | T) { }
227 222
228 get() { 223 get() {
229 return this.val ?? (this.val = this.compute()); 224 return this.val ?? (this.val = this.compute());
@@ -232,5 +227,4 @@ class Lazy<T> {
232 reset() { 227 reset() {
233 this.val = undefined; 228 this.val = undefined;
234 } 229 }
235
236} 230}