diff --git a/src/vs/workbench/services/editor/common/customEditorLabelService.ts b/src/vs/workbench/services/editor/common/customEditorLabelService.ts index 2cec14d3fef..ceb97268c12 100644 --- a/src/vs/workbench/services/editor/common/customEditorLabelService.ts +++ b/src/vs/workbench/services/editor/common/customEditorLabelService.ts @@ -7,7 +7,7 @@ import { Emitter, Event } from 'vs/base/common/event'; import { ParsedPattern, parse as parseGlob } from 'vs/base/common/glob'; import { Disposable } from 'vs/base/common/lifecycle'; import { isAbsolute, parse as parsePath, ParsedPath } from 'vs/base/common/path'; -import { relativePath as getRelativePath } from 'vs/base/common/resources'; +import { dirname, relativePath as getRelativePath } from 'vs/base/common/resources'; import { URI } from 'vs/base/common/uri'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions'; @@ -139,7 +139,7 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito for (const pattern of this.patterns) { let relevantPath: string; if (root && !pattern.isAbsolutePath) { - relevantPath = relativePath = relativePath ?? getRelativePath(root.uri, resource) ?? resource.path; + relevantPath = relativePath ?? getRelativePath(dirname(root.uri), resource) ?? resource.path; } else { relevantPath = resource.path; } @@ -164,7 +164,7 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito return parsedPath.ext.slice(1); default: { // dirname and dirname(arg) const n = variable === 'dirname' ? 0 : parseInt(arg); - const nthDir = this.getNthDirname(relevantPath, n); + const nthDir = this.getNthDirname(relevantPath, parsedPath.name, n); if (nthDir) { return nthDir; } @@ -175,21 +175,20 @@ export class CustomEditorLabelService extends Disposable implements ICustomEdito }); } - private getNthDirname(path: string, n: number): string | undefined { + private getNthDirname(path: string, filename: string, n: number): string | undefined { // grand-parent/parent/filename.ext1.ext2 -> [grand-parent, parent] const pathFragments = path.split('/'); const length = pathFragments.length; - let nthDir; + let nth; if (n < 0) { - const nth = Math.abs(n) - 1; - nthDir = pathFragments[nth]; + nth = Math.abs(n) - 1; } else { - const nth = length - 1 - n - 1; // -1 for the filename, -1 for 0-based index - nthDir = pathFragments[nth]; + nth = length - 1 - n - 1; // -1 for the filename, -1 for 0-based index } + const nthDir = nth === pathFragments.length - 1 ? filename : pathFragments[nth]; if (nthDir === undefined || nthDir === '') { return undefined; }