1
- import * as vscode from ' vscode' ;
1
+ import * as vscode from " vscode" ;
2
2
3
- import { Ctx , Disposable } from ' ./ctx' ;
4
- import { RustEditor , isRustEditor } from ' ./util' ;
3
+ import { Ctx , Disposable } from " ./ctx" ;
4
+ import { RustEditor , isRustEditor } from " ./util" ;
5
5
6
6
// FIXME: consider implementing this via the Tree View API?
7
7
// https://code.visualstudio.com/api/extension-guides/tree-view
8
8
export class AstInspector implements vscode . HoverProvider , vscode . DefinitionProvider , Disposable {
9
9
private readonly astDecorationType = vscode . window . createTextEditorDecorationType ( {
10
- borderColor : new vscode . ThemeColor ( ' rust_analyzer.syntaxTreeBorder' ) ,
10
+ borderColor : new vscode . ThemeColor ( " rust_analyzer.syntaxTreeBorder" ) ,
11
11
borderStyle : "solid" ,
12
12
borderWidth : "2px" ,
13
13
} ) ;
@@ -35,11 +35,23 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
35
35
} ) ;
36
36
37
37
constructor ( ctx : Ctx ) {
38
- ctx . pushCleanup ( vscode . languages . registerHoverProvider ( { scheme : ' rust-analyzer' } , this ) ) ;
38
+ ctx . pushCleanup ( vscode . languages . registerHoverProvider ( { scheme : " rust-analyzer" } , this ) ) ;
39
39
ctx . pushCleanup ( vscode . languages . registerDefinitionProvider ( { language : "rust" } , this ) ) ;
40
- vscode . workspace . onDidCloseTextDocument ( this . onDidCloseTextDocument , this , ctx . subscriptions ) ;
41
- vscode . workspace . onDidChangeTextDocument ( this . onDidChangeTextDocument , this , ctx . subscriptions ) ;
42
- vscode . window . onDidChangeVisibleTextEditors ( this . onDidChangeVisibleTextEditors , this , ctx . subscriptions ) ;
40
+ vscode . workspace . onDidCloseTextDocument (
41
+ this . onDidCloseTextDocument ,
42
+ this ,
43
+ ctx . subscriptions
44
+ ) ;
45
+ vscode . workspace . onDidChangeTextDocument (
46
+ this . onDidChangeTextDocument ,
47
+ this ,
48
+ ctx . subscriptions
49
+ ) ;
50
+ vscode . window . onDidChangeVisibleTextEditors (
51
+ this . onDidChangeVisibleTextEditors ,
52
+ this ,
53
+ ctx . subscriptions
54
+ ) ;
43
55
44
56
ctx . pushCleanup ( this ) ;
45
57
}
@@ -48,7 +60,10 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
48
60
}
49
61
50
62
private onDidChangeTextDocument ( event : vscode . TextDocumentChangeEvent ) {
51
- if ( this . rustEditor && event . document . uri . toString ( ) === this . rustEditor . document . uri . toString ( ) ) {
63
+ if (
64
+ this . rustEditor &&
65
+ event . document . uri . toString ( ) === this . rustEditor . document . uri . toString ( )
66
+ ) {
52
67
this . rust2Ast . reset ( ) ;
53
68
}
54
69
}
@@ -68,7 +83,9 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
68
83
}
69
84
70
85
private findAstTextEditor ( ) : undefined | vscode . TextEditor {
71
- return vscode . window . visibleTextEditors . find ( it => it . document . uri . scheme === 'rust-analyzer' ) ;
86
+ return vscode . window . visibleTextEditors . find (
87
+ ( it ) => it . document . uri . scheme === "rust-analyzer"
88
+ ) ;
72
89
}
73
90
74
91
private setRustEditor ( newRustEditor : undefined | RustEditor ) {
@@ -80,30 +97,41 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
80
97
}
81
98
82
99
// additional positional params are omitted
83
- provideDefinition ( doc : vscode . TextDocument , pos : vscode . Position ) : vscode . ProviderResult < vscode . DefinitionLink [ ] > {
84
- if ( ! this . rustEditor || doc . uri . toString ( ) !== this . rustEditor . document . uri . toString ( ) ) return ;
100
+ provideDefinition (
101
+ doc : vscode . TextDocument ,
102
+ pos : vscode . Position
103
+ ) : vscode . ProviderResult < vscode . DefinitionLink [ ] > {
104
+ if ( ! this . rustEditor || doc . uri . toString ( ) !== this . rustEditor . document . uri . toString ( ) )
105
+ return ;
85
106
86
107
const astEditor = this . findAstTextEditor ( ) ;
87
108
if ( ! astEditor ) return ;
88
109
89
- const rust2AstRanges = this . rust2Ast . get ( ) ?. find ( ( [ rustRange , _ ] ) => rustRange . contains ( pos ) ) ;
110
+ const rust2AstRanges = this . rust2Ast
111
+ . get ( )
112
+ ?. find ( ( [ rustRange , _ ] ) => rustRange . contains ( pos ) ) ;
90
113
if ( ! rust2AstRanges ) return ;
91
114
92
115
const [ rustFileRange , astFileRange ] = rust2AstRanges ;
93
116
94
117
astEditor . revealRange ( astFileRange ) ;
95
118
astEditor . selection = new vscode . Selection ( astFileRange . start , astFileRange . end ) ;
96
119
97
- return [ {
98
- targetRange : astFileRange ,
99
- targetUri : astEditor . document . uri ,
100
- originSelectionRange : rustFileRange ,
101
- targetSelectionRange : astFileRange ,
102
- } ] ;
120
+ return [
121
+ {
122
+ targetRange : astFileRange ,
123
+ targetUri : astEditor . document . uri ,
124
+ originSelectionRange : rustFileRange ,
125
+ targetSelectionRange : astFileRange ,
126
+ } ,
127
+ ] ;
103
128
}
104
129
105
130
// additional positional params are omitted
106
- provideHover ( doc : vscode . TextDocument , hoverPosition : vscode . Position ) : vscode . ProviderResult < vscode . Hover > {
131
+ provideHover (
132
+ doc : vscode . TextDocument ,
133
+ hoverPosition : vscode . Position
134
+ ) : vscode . ProviderResult < vscode . Hover > {
107
135
if ( ! this . rustEditor ) return ;
108
136
109
137
const astFileLine = doc . lineAt ( hoverPosition . line ) ;
@@ -127,13 +155,14 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
127
155
return new vscode . Range ( begin , end ) ;
128
156
}
129
157
130
- private parseRustTextRange ( doc : vscode . TextDocument , astLine : string ) : undefined | vscode . Range {
158
+ private parseRustTextRange (
159
+ doc : vscode . TextDocument ,
160
+ astLine : string
161
+ ) : undefined | vscode . Range {
131
162
const parsedRange = / ( \d + ) \. \. ( \d + ) / . exec ( astLine ) ;
132
163
if ( ! parsedRange ) return ;
133
164
134
- const [ begin , end ] = parsedRange
135
- . slice ( 1 )
136
- . map ( off => this . positionAt ( doc , + off ) ) ;
165
+ const [ begin , end ] = parsedRange . slice ( 1 ) . map ( ( off ) => this . positionAt ( doc , + off ) ) ;
137
166
138
167
return new vscode . Range ( begin , end ) ;
139
168
}
@@ -173,7 +202,7 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
173
202
class Lazy < T > {
174
203
val : undefined | T ;
175
204
176
- constructor ( private readonly compute : ( ) => undefined | T ) { }
205
+ constructor ( private readonly compute : ( ) => undefined | T ) { }
177
206
178
207
get ( ) {
179
208
return this . val ?? ( this . val = this . compute ( ) ) ;
0 commit comments