@@ -135,15 +135,15 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito
135
135
}
136
136
137
137
if ( pattern . parsedPattern ( relevantPath ) ) {
138
- return this . applyTempate ( pattern . template , resource ) ;
138
+ return this . applyTempate ( pattern . template , resource , relevantPath ) ;
139
139
}
140
140
}
141
141
142
142
return undefined ;
143
143
}
144
144
145
- private readonly _parsedTemplateExpression = / \$ \{ ( d i r n a m e | f i l e n a m e | e x t n a m e | d i r n a m e \( ( \d + ) \) ) \} / g;
146
- private applyTempate ( template : string , resource : URI ) : string {
145
+ private readonly _parsedTemplateExpression = / \$ \{ ( d i r n a m e | f i l e n a m e | e x t n a m e | d i r n a m e \( ( [ - + ] ? \d + ) \) ) \} / g;
146
+ private applyTempate ( template : string , resource : URI , relevantPath : string ) : string {
147
147
let parsedPath : undefined | ParsedPath ;
148
148
return template . replace ( this . _parsedTemplateExpression , ( match : string , variable : string , arg : string ) => {
149
149
parsedPath = parsedPath ?? parsePath ( resource . path ) ;
@@ -154,7 +154,7 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito
154
154
return parsedPath . ext . slice ( 1 ) ;
155
155
default : { // dirname and dirname(arg)
156
156
const n = variable === 'dirname' ? 0 : parseInt ( arg ) ;
157
- const nthDir = this . getNthDirname ( parsedPath , n ) ;
157
+ const nthDir = this . getNthDirname ( relevantPath , n ) ;
158
158
if ( nthDir ) {
159
159
return nthDir ;
160
160
}
@@ -165,16 +165,21 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito
165
165
} ) ;
166
166
}
167
167
168
- private getNthDirname ( path : ParsedPath , n : number ) : string | undefined {
168
+ private getNthDirname ( path : string , n : number ) : string | undefined {
169
169
// grand-parent/parent/filename.ext1.ext2 -> [grand-parent, parent]
170
- const pathFragments = path . dir . split ( '/' ) ;
170
+ const pathFragments = path . split ( '/' ) ;
171
171
172
172
const length = pathFragments . length ;
173
- const nth = length - 1 - n ;
174
- if ( nth < 0 ) {
175
- return undefined ;
173
+
174
+ let nthDir ;
175
+ if ( n < 0 ) {
176
+ const nth = Math . abs ( n ) - 1 ;
177
+ nthDir = pathFragments [ nth ] ;
178
+ } else {
179
+ const nth = length - 1 - n - 1 ; // -1 for the filename, -1 for 0-based index
180
+ nthDir = pathFragments [ nth ] ;
176
181
}
177
- const nthDir = pathFragments [ nth ] ;
182
+
178
183
if ( nthDir === undefined || nthDir === '' ) {
179
184
return undefined ;
180
185
}
0 commit comments