15
15
-- of the license. --
16
16
----------------------------------------------------------------------------*/
17
17
import * as vscode from 'vscode' ;
18
- import * as path from 'path'
18
+ import * as path from 'path' ;
19
19
20
20
/**
21
21
* Substitue any variable reference present in the given string. VS Code
@@ -26,12 +26,11 @@ import * as path from 'path'
26
26
* @returns
27
27
*/
28
28
export function substituteVariables ( str : string , recursive = false ) {
29
-
30
- let workspaces = vscode . workspace . workspaceFolders ?? [ ] ;
31
- let workspace = workspaces . length ? workspaces [ 0 ] : null ;
32
- let activeEditor = vscode . window . activeTextEditor
33
- let activeFile = activeEditor ?. document ;
34
- let absoluteFilePath = activeFile ?. uri . fsPath ?? ""
29
+ const workspaces = vscode . workspace . workspaceFolders ?? [ ] ;
30
+ const workspace = workspaces . length ? workspaces [ 0 ] : null ;
31
+ const activeEditor = vscode . window . activeTextEditor ;
32
+ const activeFile = activeEditor ?. document ;
33
+ const absoluteFilePath = activeFile ?. uri . fsPath ?? '' ;
35
34
36
35
if ( workspace != null ) {
37
36
str = str . replace ( / \$ { workspaceFolder} / g, workspace ?. uri . fsPath ) ;
@@ -41,31 +40,44 @@ export function substituteVariables(str: string, recursive = false) {
41
40
str = str . replace ( / \$ { file} / g, absoluteFilePath ) ;
42
41
let activeWorkspace = workspace ;
43
42
let relativeFilePath = absoluteFilePath ;
44
- for ( let workspace of workspaces ) {
43
+ for ( const workspace of workspaces ) {
45
44
if ( absoluteFilePath . replace ( workspace . uri . fsPath , '' ) !== absoluteFilePath ) {
46
45
activeWorkspace = workspace ;
47
- relativeFilePath = absoluteFilePath . replace ( workspace . uri . fsPath , '' ) . substr ( path . sep . length ) ;
46
+ relativeFilePath = absoluteFilePath
47
+ . replace ( workspace . uri . fsPath , '' )
48
+ . substr ( path . sep . length ) ;
48
49
break ;
49
50
}
50
51
}
51
- let parsedPath = path . parse ( absoluteFilePath ) ;
52
+ const parsedPath = path . parse ( absoluteFilePath ) ;
52
53
53
54
if ( activeWorkspace != null ) {
54
55
str = str . replace ( / \$ { fileWorkspaceFolder} / g, activeWorkspace ?. uri . fsPath ) ;
55
56
}
56
57
57
58
str = str . replace ( / \$ { relativeFile} / g, relativeFilePath ) ;
58
- str = str . replace ( / \$ { relativeFileDirname} / g, relativeFilePath . substr ( 0 , relativeFilePath . lastIndexOf ( path . sep ) ) ) ;
59
+ str = str . replace (
60
+ / \$ { relativeFileDirname} / g,
61
+ relativeFilePath . substr ( 0 , relativeFilePath . lastIndexOf ( path . sep ) )
62
+ ) ;
59
63
str = str . replace ( / \$ { fileBasename} / g, parsedPath . base ) ;
60
64
str = str . replace ( / \$ { fileBasenameNoExtension} / g, parsedPath . name ) ;
61
65
str = str . replace ( / \$ { fileExtname} / g, parsedPath . ext ) ;
62
- str = str . replace ( / \$ { fileDirname} / g, parsedPath . dir . substr ( parsedPath . dir . lastIndexOf ( path . sep ) + 1 ) ) ;
66
+ str = str . replace (
67
+ / \$ { fileDirname} / g,
68
+ parsedPath . dir . substr ( parsedPath . dir . lastIndexOf ( path . sep ) + 1 )
69
+ ) ;
63
70
str = str . replace ( / \$ { cwd} / g, parsedPath . dir ) ;
64
71
str = str . replace ( / \$ { pathSeparator} / g, path . sep ) ;
65
72
66
73
if ( activeEditor != null ) {
67
74
str = str . replace ( / \$ { lineNumber} / g, ( activeEditor . selection . start . line + 1 ) . toString ( ) ) ;
68
- str = str . replace ( / \$ { selectedText} / g, activeEditor . document . getText ( new vscode . Range ( activeEditor . selection . start , activeEditor . selection . end ) ) ) ;
75
+ str = str . replace (
76
+ / \$ { selectedText} / g,
77
+ activeEditor . document . getText (
78
+ new vscode . Range ( activeEditor . selection . start , activeEditor . selection . end )
79
+ )
80
+ ) ;
69
81
}
70
82
71
83
str = str . replace ( / \$ { env:( .* ?) } / g, function ( variable ) {
@@ -76,8 +88,13 @@ export function substituteVariables(str: string, recursive = false) {
76
88
return vscode . workspace . getConfiguration ( ) . get ( variable . match ( / \$ { config:( .* ?) } / ) ! [ 1 ] , '' ) ;
77
89
} ) ;
78
90
79
- if ( recursive && str . match ( / \$ { ( w o r k s p a c e F o l d e r | w o r k s p a c e F o l d e r B a s e n a m e | f i l e W o r k s p a c e F o l d e r | r e l a t i v e F i l e | f i l e B a s e n a m e | f i l e B a s e n a m e N o E x t e n s i o n | f i l e E x t n a m e | f i l e D i r n a m e | c w d | p a t h S e p a r a t o r | l i n e N u m b e r | s e l e c t e d T e x t | e n v : ( .* ?) | c o n f i g : ( .* ?) ) } / ) ) {
91
+ if (
92
+ recursive &&
93
+ str . match (
94
+ / \$ { ( w o r k s p a c e F o l d e r | w o r k s p a c e F o l d e r B a s e n a m e | f i l e W o r k s p a c e F o l d e r | r e l a t i v e F i l e | f i l e B a s e n a m e | f i l e B a s e n a m e N o E x t e n s i o n | f i l e E x t n a m e | f i l e D i r n a m e | c w d | p a t h S e p a r a t o r | l i n e N u m b e r | s e l e c t e d T e x t | e n v : ( .* ?) | c o n f i g : ( .* ?) ) } /
95
+ )
96
+ ) {
80
97
str = substituteVariables ( str , recursive ) ;
81
98
}
82
99
return str ;
83
- }
100
+ }
0 commit comments