@@ -7,148 +7,148 @@ import { activeToolchain, Cargo, Crate, getRustcVersion } from './toolchain';
7
7
const debugOutput = vscode . window . createOutputChannel ( "Debug" ) ;
8
8
9
9
export class RustDependenciesProvider implements vscode . TreeDataProvider < Dependency | DependencyFile > {
10
- cargo : Cargo ;
11
- dependenciesMap : { [ id : string ] : Dependency | DependencyFile } ;
12
-
13
- constructor (
14
- private readonly workspaceRoot : string ,
15
- ) {
16
- this . cargo = new Cargo ( this . workspaceRoot || '.' , debugOutput ) ;
17
- this . dependenciesMap = { } ;
18
- }
19
-
20
- private _onDidChangeTreeData : vscode . EventEmitter < Dependency | DependencyFile | undefined | null | void > = new vscode . EventEmitter < Dependency | undefined | null | void > ( ) ;
21
-
22
- readonly onDidChangeTreeData : vscode . Event < Dependency | DependencyFile | undefined | null | void > = this . _onDidChangeTreeData . event ;
23
-
24
-
25
- getDependency ( filePath : string ) : Dependency | DependencyFile | undefined {
26
- return this . dependenciesMap [ filePath . toLowerCase ( ) ] ;
27
- }
28
-
29
- contains ( filePath : string ) : boolean {
30
- return filePath . toLowerCase ( ) in this . dependenciesMap ;
31
- }
32
-
33
- refresh ( ) : void {
34
- this . _onDidChangeTreeData . fire ( ) ;
35
- }
36
-
37
- getParent ?( element : Dependency | DependencyFile ) : vscode . ProviderResult < Dependency | DependencyFile > {
38
- if ( element instanceof Dependency ) return undefined ;
39
- return element . parent ;
40
- }
41
-
42
- getTreeItem ( element : Dependency | DependencyFile ) : vscode . TreeItem | Thenable < vscode . TreeItem > {
43
- if ( element . id ! in this . dependenciesMap ) return this . dependenciesMap [ element . id ! ] ;
44
- return element ;
45
- }
46
-
47
- getChildren ( element ?: Dependency | DependencyFile ) : vscode . ProviderResult < Dependency [ ] | DependencyFile [ ] > {
48
- return new Promise ( ( resolve , _reject ) => {
49
- if ( ! this . workspaceRoot ) {
50
- void vscode . window . showInformationMessage ( 'No dependency in empty workspace' ) ;
51
- return Promise . resolve ( [ ] ) ;
52
- }
53
-
54
- if ( element ) {
55
- const files = fs . readdirSync ( element . dependencyPath ) . map ( fileName => {
56
- const filePath = fspath . join ( element . dependencyPath , fileName ) ;
57
- const collapsibleState = fs . lstatSync ( filePath ) . isDirectory ( ) ?
58
- vscode . TreeItemCollapsibleState . Collapsed :
59
- vscode . TreeItemCollapsibleState . None ;
60
- const dep = new DependencyFile (
61
- fileName ,
62
- filePath ,
63
- element ,
64
- collapsibleState
65
- ) ;
66
- this . dependenciesMap [ dep . dependencyPath . toLowerCase ( ) ] = dep ;
67
- return dep ;
10
+ cargo : Cargo ;
11
+ dependenciesMap : { [ id : string ] : Dependency | DependencyFile } ;
12
+
13
+ constructor (
14
+ private readonly workspaceRoot : string ,
15
+ ) {
16
+ this . cargo = new Cargo ( this . workspaceRoot || '.' , debugOutput ) ;
17
+ this . dependenciesMap = { } ;
18
+ }
19
+
20
+ private _onDidChangeTreeData : vscode . EventEmitter < Dependency | DependencyFile | undefined | null | void > = new vscode . EventEmitter < Dependency | undefined | null | void > ( ) ;
21
+
22
+ readonly onDidChangeTreeData : vscode . Event < Dependency | DependencyFile | undefined | null | void > = this . _onDidChangeTreeData . event ;
23
+
24
+
25
+ getDependency ( filePath : string ) : Dependency | DependencyFile | undefined {
26
+ return this . dependenciesMap [ filePath . toLowerCase ( ) ] ;
27
+ }
28
+
29
+ contains ( filePath : string ) : boolean {
30
+ return filePath . toLowerCase ( ) in this . dependenciesMap ;
31
+ }
32
+
33
+ refresh ( ) : void {
34
+ this . _onDidChangeTreeData . fire ( ) ;
35
+ }
36
+
37
+ getParent ?( element : Dependency | DependencyFile ) : vscode . ProviderResult < Dependency | DependencyFile > {
38
+ if ( element instanceof Dependency ) return undefined ;
39
+ return element . parent ;
40
+ }
41
+
42
+ getTreeItem ( element : Dependency | DependencyFile ) : vscode . TreeItem | Thenable < vscode . TreeItem > {
43
+ if ( element . id ! in this . dependenciesMap ) return this . dependenciesMap [ element . id ! ] ;
44
+ return element ;
45
+ }
46
+
47
+ getChildren ( element ?: Dependency | DependencyFile ) : vscode . ProviderResult < Dependency [ ] | DependencyFile [ ] > {
48
+ return new Promise ( ( resolve , _reject ) => {
49
+ if ( ! this . workspaceRoot ) {
50
+ void vscode . window . showInformationMessage ( 'No dependency in empty workspace' ) ;
51
+ return Promise . resolve ( [ ] ) ;
52
+ }
53
+
54
+ if ( element ) {
55
+ const files = fs . readdirSync ( element . dependencyPath ) . map ( fileName => {
56
+ const filePath = fspath . join ( element . dependencyPath , fileName ) ;
57
+ const collapsibleState = fs . lstatSync ( filePath ) . isDirectory ( ) ?
58
+ vscode . TreeItemCollapsibleState . Collapsed :
59
+ vscode . TreeItemCollapsibleState . None ;
60
+ const dep = new DependencyFile (
61
+ fileName ,
62
+ filePath ,
63
+ element ,
64
+ collapsibleState
65
+ ) ;
66
+ this . dependenciesMap [ dep . dependencyPath . toLowerCase ( ) ] = dep ;
67
+ return dep ;
68
+ } ) ;
69
+ return resolve (
70
+ files
71
+ ) ;
72
+ } else {
73
+ return resolve ( this . getRootDependencies ( ) ) ;
74
+ }
68
75
} ) ;
69
- return resolve (
70
- files
76
+ }
77
+
78
+ private async getRootDependencies ( ) : Promise < Dependency [ ] > {
79
+ const registryDir = fspath . join ( os . homedir ( ) , '.cargo' , 'registry' , 'src' ) ;
80
+ const basePath = fspath . join ( registryDir , fs . readdirSync ( registryDir ) [ 0 ] ) ;
81
+ const deps = await this . getDepsInCartoTree ( basePath ) ;
82
+ const stdlib = await this . getStdLib ( ) ;
83
+ this . dependenciesMap [ stdlib . dependencyPath . toLowerCase ( ) ] = stdlib ;
84
+ return [ stdlib ] . concat ( deps ) ;
85
+ }
86
+
87
+ private async getStdLib ( ) : Promise < Dependency > {
88
+ const toolchain = await activeToolchain ( ) ;
89
+ const rustVersion = await getRustcVersion ( os . homedir ( ) ) ;
90
+ const stdlibPath = fspath . join ( os . homedir ( ) , '.rustup' , 'toolchains' , toolchain , 'lib' , 'rustlib' , 'src' , 'rust' , 'library' ) ;
91
+ const stdlib = new Dependency (
92
+ "stdlib" ,
93
+ rustVersion ,
94
+ stdlibPath ,
95
+ vscode . TreeItemCollapsibleState . Collapsed
71
96
) ;
72
- } else {
73
- return resolve ( this . getRootDependencies ( ) ) ;
74
- }
75
- } ) ;
76
- }
77
-
78
- private async getRootDependencies ( ) : Promise < Dependency [ ] > {
79
- const registryDir = fspath . join ( os . homedir ( ) , '.cargo' , 'registry' , 'src' ) ;
80
- const basePath = fspath . join ( registryDir , fs . readdirSync ( registryDir ) [ 0 ] ) ;
81
- const deps = await this . getDepsInCartoTree ( basePath ) ;
82
- const stdlib = await this . getStdLib ( ) ;
83
- this . dependenciesMap [ stdlib . dependencyPath . toLowerCase ( ) ] = stdlib ;
84
- return [ stdlib ] . concat ( deps ) ;
85
- }
86
-
87
- private async getStdLib ( ) : Promise < Dependency > {
88
- const toolchain = await activeToolchain ( ) ;
89
- const rustVersion = await getRustcVersion ( os . homedir ( ) ) ;
90
- const stdlibPath = fspath . join ( os . homedir ( ) , '.rustup' , 'toolchains' , toolchain , 'lib' , 'rustlib' , 'src' , 'rust' , 'library' ) ;
91
- const stdlib = new Dependency (
92
- "stdlib" ,
93
- rustVersion ,
94
- stdlibPath ,
95
- vscode . TreeItemCollapsibleState . Collapsed
96
- ) ;
97
-
98
- return stdlib ;
99
- }
100
-
101
- private async getDepsInCartoTree ( basePath : string ) : Promise < Dependency [ ] > {
102
- const crates : Crate [ ] = await this . cargo . crates ( ) ;
103
- const toDep = ( moduleName : string , version : string ) : Dependency => {
104
- const cratePath = fspath . join ( basePath , `${ moduleName } -${ version } ` ) ;
105
- return new Dependency (
106
- moduleName ,
107
- version ,
108
- cratePath ,
109
- vscode . TreeItemCollapsibleState . Collapsed
110
- ) ;
111
- } ;
112
-
113
- const deps = crates . map ( crate => {
114
- const dep = toDep ( crate . name , crate . version ) ;
115
- this . dependenciesMap [ dep . dependencyPath . toLowerCase ( ) ] = dep ;
116
- return dep ;
117
- } ) ;
118
- return deps ;
119
- }
97
+
98
+ return stdlib ;
99
+ }
100
+
101
+ private async getDepsInCartoTree ( basePath : string ) : Promise < Dependency [ ] > {
102
+ const crates : Crate [ ] = await this . cargo . crates ( ) ;
103
+ const toDep = ( moduleName : string , version : string ) : Dependency => {
104
+ const cratePath = fspath . join ( basePath , `${ moduleName } -${ version } ` ) ;
105
+ return new Dependency (
106
+ moduleName ,
107
+ version ,
108
+ cratePath ,
109
+ vscode . TreeItemCollapsibleState . Collapsed
110
+ ) ;
111
+ } ;
112
+
113
+ const deps = crates . map ( crate => {
114
+ const dep = toDep ( crate . name , crate . version ) ;
115
+ this . dependenciesMap [ dep . dependencyPath . toLowerCase ( ) ] = dep ;
116
+ return dep ;
117
+ } ) ;
118
+ return deps ;
119
+ }
120
120
}
121
121
122
122
123
123
export class Dependency extends vscode . TreeItem {
124
- constructor (
125
- public readonly label : string ,
126
- private version : string ,
127
- readonly dependencyPath : string ,
128
- public readonly collapsibleState : vscode . TreeItemCollapsibleState
129
- ) {
130
- super ( label , collapsibleState ) ;
131
- this . tooltip = `${ this . label } -${ this . version } ` ;
132
- this . description = this . version ;
133
- this . resourceUri = vscode . Uri . file ( dependencyPath ) ;
134
- }
124
+ constructor (
125
+ public readonly label : string ,
126
+ private version : string ,
127
+ readonly dependencyPath : string ,
128
+ public readonly collapsibleState : vscode . TreeItemCollapsibleState
129
+ ) {
130
+ super ( label , collapsibleState ) ;
131
+ this . tooltip = `${ this . label } -${ this . version } ` ;
132
+ this . description = this . version ;
133
+ this . resourceUri = vscode . Uri . file ( dependencyPath ) ;
134
+ }
135
135
}
136
136
137
137
export class DependencyFile extends vscode . TreeItem {
138
138
139
- constructor (
140
- readonly label : string ,
141
- readonly dependencyPath : string ,
142
- readonly parent : Dependency | DependencyFile ,
143
- public readonly collapsibleState : vscode . TreeItemCollapsibleState
144
- ) {
145
- super ( vscode . Uri . file ( dependencyPath ) , collapsibleState ) ;
146
- const isDir = fs . lstatSync ( this . dependencyPath ) . isDirectory ( ) ;
147
- this . id = this . dependencyPath . toLowerCase ( ) ;
148
- if ( ! isDir ) {
149
- this . command = { command : 'rust-analyzer.openFile' , title : "Open File" , arguments : [ vscode . Uri . file ( this . dependencyPath ) ] , } ;
139
+ constructor (
140
+ readonly label : string ,
141
+ readonly dependencyPath : string ,
142
+ readonly parent : Dependency | DependencyFile ,
143
+ public readonly collapsibleState : vscode . TreeItemCollapsibleState
144
+ ) {
145
+ super ( vscode . Uri . file ( dependencyPath ) , collapsibleState ) ;
146
+ const isDir = fs . lstatSync ( this . dependencyPath ) . isDirectory ( ) ;
147
+ this . id = this . dependencyPath . toLowerCase ( ) ;
148
+ if ( ! isDir ) {
149
+ this . command = { command : 'rust-analyzer.openFile' , title : "Open File" , arguments : [ vscode . Uri . file ( this . dependencyPath ) ] , } ;
150
+ }
150
151
}
151
- }
152
152
}
153
153
154
154
export type DependencyId = { id : string } ;
0 commit comments