1- import * as vscode from ' vscode' ;
1+ import * as vscode from " vscode" ;
22
3- import { Ctx , Disposable } from ' ./ctx' ;
4- import { RustEditor , isRustEditor } from ' ./util' ;
3+ import { Ctx , Disposable } from " ./ctx" ;
4+ import { RustEditor , isRustEditor } from " ./util" ;
55
66// FIXME: consider implementing this via the Tree View API?
77// https://code.visualstudio.com/api/extension-guides/tree-view
88export class AstInspector implements vscode . HoverProvider , vscode . DefinitionProvider , Disposable {
99 private readonly astDecorationType = vscode . window . createTextEditorDecorationType ( {
10- borderColor : new vscode . ThemeColor ( ' rust_analyzer.syntaxTreeBorder' ) ,
10+ borderColor : new vscode . ThemeColor ( " rust_analyzer.syntaxTreeBorder" ) ,
1111 borderStyle : "solid" ,
1212 borderWidth : "2px" ,
1313 } ) ;
@@ -35,11 +35,23 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
3535 } ) ;
3636
3737 constructor ( ctx : Ctx ) {
38- ctx . pushCleanup ( vscode . languages . registerHoverProvider ( { scheme : ' rust-analyzer' } , this ) ) ;
38+ ctx . pushCleanup ( vscode . languages . registerHoverProvider ( { scheme : " rust-analyzer" } , this ) ) ;
3939 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+ ) ;
4355
4456 ctx . pushCleanup ( this ) ;
4557 }
@@ -48,7 +60,10 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
4860 }
4961
5062 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+ ) {
5267 this . rust2Ast . reset ( ) ;
5368 }
5469 }
@@ -68,7 +83,9 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
6883 }
6984
7085 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+ ) ;
7289 }
7390
7491 private setRustEditor ( newRustEditor : undefined | RustEditor ) {
@@ -80,30 +97,41 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
8097 }
8198
8299 // 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 ;
85106
86107 const astEditor = this . findAstTextEditor ( ) ;
87108 if ( ! astEditor ) return ;
88109
89- const rust2AstRanges = this . rust2Ast . get ( ) ?. find ( ( [ rustRange , _ ] ) => rustRange . contains ( pos ) ) ;
110+ const rust2AstRanges = this . rust2Ast
111+ . get ( )
112+ ?. find ( ( [ rustRange , _ ] ) => rustRange . contains ( pos ) ) ;
90113 if ( ! rust2AstRanges ) return ;
91114
92115 const [ rustFileRange , astFileRange ] = rust2AstRanges ;
93116
94117 astEditor . revealRange ( astFileRange ) ;
95118 astEditor . selection = new vscode . Selection ( astFileRange . start , astFileRange . end ) ;
96119
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+ ] ;
103128 }
104129
105130 // 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 > {
107135 if ( ! this . rustEditor ) return ;
108136
109137 const astFileLine = doc . lineAt ( hoverPosition . line ) ;
@@ -127,13 +155,14 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
127155 return new vscode . Range ( begin , end ) ;
128156 }
129157
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 {
131162 const parsedRange = / ( \d + ) \. \. ( \d + ) / . exec ( astLine ) ;
132163 if ( ! parsedRange ) return ;
133164
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 ) ) ;
137166
138167 return new vscode . Range ( begin , end ) ;
139168 }
@@ -173,7 +202,7 @@ export class AstInspector implements vscode.HoverProvider, vscode.DefinitionProv
173202class Lazy < T > {
174203 val : undefined | T ;
175204
176- constructor ( private readonly compute : ( ) => undefined | T ) { }
205+ constructor ( private readonly compute : ( ) => undefined | T ) { }
177206
178207 get ( ) {
179208 return this . val ?? ( this . val = this . compute ( ) ) ;
0 commit comments