Skip to content

Commit 910cdff

Browse files
authored
Allow negative indices (microsoft#209869)
1 parent 51bad3e commit 910cdff

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

src/vs/workbench/services/editor/common/customEditorLabelService.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito
135135
}
136136

137137
if (pattern.parsedPattern(relevantPath)) {
138-
return this.applyTempate(pattern.template, resource);
138+
return this.applyTempate(pattern.template, resource, relevantPath);
139139
}
140140
}
141141

142142
return undefined;
143143
}
144144

145-
private readonly _parsedTemplateExpression = /\$\{(dirname|filename|extname|dirname\((\d+)\))\}/g;
146-
private applyTempate(template: string, resource: URI): string {
145+
private readonly _parsedTemplateExpression = /\$\{(dirname|filename|extname|dirname\(([-+]?\d+)\))\}/g;
146+
private applyTempate(template: string, resource: URI, relevantPath: string): string {
147147
let parsedPath: undefined | ParsedPath;
148148
return template.replace(this._parsedTemplateExpression, (match: string, variable: string, arg: string) => {
149149
parsedPath = parsedPath ?? parsePath(resource.path);
@@ -154,7 +154,7 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito
154154
return parsedPath.ext.slice(1);
155155
default: { // dirname and dirname(arg)
156156
const n = variable === 'dirname' ? 0 : parseInt(arg);
157-
const nthDir = this.getNthDirname(parsedPath, n);
157+
const nthDir = this.getNthDirname(relevantPath, n);
158158
if (nthDir) {
159159
return nthDir;
160160
}
@@ -165,16 +165,21 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito
165165
});
166166
}
167167

168-
private getNthDirname(path: ParsedPath, n: number): string | undefined {
168+
private getNthDirname(path: string, n: number): string | undefined {
169169
// grand-parent/parent/filename.ext1.ext2 -> [grand-parent, parent]
170-
const pathFragments = path.dir.split('/');
170+
const pathFragments = path.split('/');
171171

172172
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];
176181
}
177-
const nthDir = pathFragments[nth];
182+
178183
if (nthDir === undefined || nthDir === '') {
179184
return undefined;
180185
}

0 commit comments

Comments
 (0)