4
4
*--------------------------------------------------------------------------------------------*/
5
5
6
6
import * as jsonc from 'jsonc-parser' ;
7
- import { basename , posix } from 'path' ;
7
+ import { posix } from 'path' ;
8
8
import * as vscode from 'vscode' ;
9
9
import { Utils } from 'vscode-uri' ;
10
10
import { coalesce } from '../utils/arrays' ;
@@ -42,19 +42,31 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
42
42
}
43
43
44
44
private getExtendsLink ( document : vscode . TextDocument , root : jsonc . Node ) : vscode . DocumentLink | undefined {
45
- const extendsNode = jsonc . findNodeAtLocation ( root , [ 'extends' ] ) ;
46
- if ( ! this . isPathValue ( extendsNode ) ) {
45
+ const node = jsonc . findNodeAtLocation ( root , [ 'extends' ] ) ;
46
+ return node && this . tryCreateTsConfigLink ( document , node ) ;
47
+ }
48
+
49
+ private getReferencesLinks ( document : vscode . TextDocument , root : jsonc . Node ) {
50
+ return mapChildren (
51
+ jsonc . findNodeAtLocation ( root , [ 'references' ] ) ,
52
+ child => {
53
+ const pathNode = jsonc . findNodeAtLocation ( child , [ 'path' ] ) ;
54
+ return pathNode && this . tryCreateTsConfigLink ( document , pathNode ) ;
55
+ } ) ;
56
+ }
57
+
58
+ private tryCreateTsConfigLink ( document : vscode . TextDocument , node : jsonc . Node ) : vscode . DocumentLink | undefined {
59
+ if ( ! this . isPathValue ( node ) ) {
47
60
return undefined ;
48
61
}
49
62
50
- const extendsValue : string = extendsNode . value ;
51
63
const args : OpenExtendsLinkCommandArgs = {
52
- resourceUri : { ...document . uri . toJSON ( ) , $mid : undefined } , // Prevent VS Code from trying to transform the uri
53
- extendsValue : extendsValue
64
+ resourceUri : { ...document . uri . toJSON ( ) , $mid : undefined } ,
65
+ extendsValue : node . value
54
66
} ;
55
67
56
68
const link = new vscode . DocumentLink (
57
- this . getRange ( document , extendsNode ) ,
69
+ this . getRange ( document , node ) ,
58
70
vscode . Uri . parse ( `command:${ openExtendsLinkCommandId } ?${ JSON . stringify ( args ) } ` ) ) ;
59
71
link . tooltip = vscode . l10n . t ( "Follow link" ) ;
60
72
return link ;
@@ -66,22 +78,6 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
66
78
child => this . pathNodeToLink ( document , child ) ) ;
67
79
}
68
80
69
- private getReferencesLinks ( document : vscode . TextDocument , root : jsonc . Node ) {
70
- return mapChildren (
71
- jsonc . findNodeAtLocation ( root , [ 'references' ] ) ,
72
- child => {
73
- const pathNode = jsonc . findNodeAtLocation ( child , [ 'path' ] ) ;
74
- if ( ! this . isPathValue ( pathNode ) ) {
75
- return undefined ;
76
- }
77
-
78
- return new vscode . DocumentLink ( this . getRange ( document , pathNode ) ,
79
- basename ( pathNode . value ) . endsWith ( '.json' )
80
- ? this . getFileTarget ( document , pathNode )
81
- : this . getFolderTarget ( document , pathNode ) ) ;
82
- } ) ;
83
- }
84
-
85
81
private pathNodeToLink (
86
82
document : vscode . TextDocument ,
87
83
node : jsonc . Node | undefined
@@ -91,21 +87,17 @@ class TsconfigLinkProvider implements vscode.DocumentLinkProvider {
91
87
: undefined ;
92
88
}
93
89
94
- private isPathValue ( extendsNode : jsonc . Node | undefined ) : extendsNode is jsonc . Node {
95
- return extendsNode
96
- && extendsNode . type === 'string'
97
- && extendsNode . value
98
- && ! ( extendsNode . value as string ) . includes ( '*' ) ; // don't treat globs as links.
90
+ private isPathValue ( node : jsonc . Node | undefined ) : node is jsonc . Node {
91
+ return node
92
+ && node . type === 'string'
93
+ && node . value
94
+ && ! ( node . value as string ) . includes ( '*' ) ; // don't treat globs as links.
99
95
}
100
96
101
97
private getFileTarget ( document : vscode . TextDocument , node : jsonc . Node ) : vscode . Uri {
102
98
return vscode . Uri . joinPath ( Utils . dirname ( document . uri ) , node . value ) ;
103
99
}
104
100
105
- private getFolderTarget ( document : vscode . TextDocument , node : jsonc . Node ) : vscode . Uri {
106
- return vscode . Uri . joinPath ( Utils . dirname ( document . uri ) , node . value , 'tsconfig.json' ) ;
107
- }
108
-
109
101
private getRange ( document : vscode . TextDocument , node : jsonc . Node ) {
110
102
const offset = node . offset ;
111
103
const start = document . positionAt ( offset + 1 ) ;
@@ -156,7 +148,7 @@ async function resolveNodeModulesPath(baseDirUri: vscode.Uri, pathCandidates: st
156
148
/**
157
149
* @returns Returns undefined in case of lack of result while trying to resolve from node_modules
158
150
*/
159
- async function getTsconfigPath ( baseDirUri : vscode . Uri , extendsValue : string ) : Promise < vscode . Uri | undefined > {
151
+ async function getTsconfigPath ( baseDirUri : vscode . Uri , pathValue : string ) : Promise < vscode . Uri | undefined > {
160
152
async function resolve ( absolutePath : vscode . Uri ) : Promise < vscode . Uri > {
161
153
if ( absolutePath . path . endsWith ( '.json' ) || await exists ( absolutePath ) ) {
162
154
return absolutePath ;
@@ -166,21 +158,21 @@ async function getTsconfigPath(baseDirUri: vscode.Uri, extendsValue: string): Pr
166
158
} ) ;
167
159
}
168
160
169
- const isRelativePath = [ './' , '../' ] . some ( str => extendsValue . startsWith ( str ) ) ;
161
+ const isRelativePath = [ './' , '../' ] . some ( str => pathValue . startsWith ( str ) ) ;
170
162
if ( isRelativePath ) {
171
- return resolve ( vscode . Uri . joinPath ( baseDirUri , extendsValue ) ) ;
163
+ return resolve ( vscode . Uri . joinPath ( baseDirUri , pathValue ) ) ;
172
164
}
173
165
174
- if ( extendsValue . startsWith ( '/' ) || looksLikeAbsoluteWindowsPath ( extendsValue ) ) {
175
- return resolve ( vscode . Uri . file ( extendsValue ) ) ;
166
+ if ( pathValue . startsWith ( '/' ) || looksLikeAbsoluteWindowsPath ( pathValue ) ) {
167
+ return resolve ( vscode . Uri . file ( pathValue ) ) ;
176
168
}
177
169
178
170
// Otherwise resolve like a module
179
171
return resolveNodeModulesPath ( baseDirUri , [
180
- extendsValue ,
181
- ...extendsValue . endsWith ( '.json' ) ? [ ] : [
182
- `${ extendsValue } .json` ,
183
- `${ extendsValue } /tsconfig.json` ,
172
+ pathValue ,
173
+ ...pathValue . endsWith ( '.json' ) ? [ ] : [
174
+ `${ pathValue } .json` ,
175
+ `${ pathValue } /tsconfig.json` ,
184
176
]
185
177
] ) ;
186
178
}
0 commit comments