1515-- of the license. --
1616----------------------------------------------------------------------------*/
1717import * as vscode from 'vscode' ;
18- import * as path from 'path'
18+ import * as path from 'path' ;
1919
2020/**
2121 * Substitue any variable reference present in the given string. VS Code
@@ -26,12 +26,11 @@ import * as path from 'path'
2626 * @returns
2727 */
2828export 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 ?? '' ;
3534
3635 if ( workspace != null ) {
3736 str = str . replace ( / \$ { workspaceFolder} / g, workspace ?. uri . fsPath ) ;
@@ -41,31 +40,44 @@ export function substituteVariables(str: string, recursive = false) {
4140 str = str . replace ( / \$ { file} / g, absoluteFilePath ) ;
4241 let activeWorkspace = workspace ;
4342 let relativeFilePath = absoluteFilePath ;
44- for ( let workspace of workspaces ) {
43+ for ( const workspace of workspaces ) {
4544 if ( absoluteFilePath . replace ( workspace . uri . fsPath , '' ) !== absoluteFilePath ) {
4645 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 ) ;
4849 break ;
4950 }
5051 }
51- let parsedPath = path . parse ( absoluteFilePath ) ;
52+ const parsedPath = path . parse ( absoluteFilePath ) ;
5253
5354 if ( activeWorkspace != null ) {
5455 str = str . replace ( / \$ { fileWorkspaceFolder} / g, activeWorkspace ?. uri . fsPath ) ;
5556 }
5657
5758 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+ ) ;
5963 str = str . replace ( / \$ { fileBasename} / g, parsedPath . base ) ;
6064 str = str . replace ( / \$ { fileBasenameNoExtension} / g, parsedPath . name ) ;
6165 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+ ) ;
6370 str = str . replace ( / \$ { cwd} / g, parsedPath . dir ) ;
6471 str = str . replace ( / \$ { pathSeparator} / g, path . sep ) ;
6572
6673 if ( activeEditor != null ) {
6774 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+ ) ;
6981 }
7082
7183 str = str . replace ( / \$ { env:( .* ?) } / g, function ( variable ) {
@@ -76,8 +88,13 @@ export function substituteVariables(str: string, recursive = false) {
7688 return vscode . workspace . getConfiguration ( ) . get ( variable . match ( / \$ { config:( .* ?) } / ) ! [ 1 ] , '' ) ;
7789 } ) ;
7890
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+ ) {
8097 str = substituteVariables ( str , recursive ) ;
8198 }
8299 return str ;
83- }
100+ }
0 commit comments