Skip to content

Commit b9ccfc4

Browse files
committed
labelService: getUriBasenameLabel
1 parent 00b43d3 commit b9ccfc4

6 files changed

Lines changed: 42 additions & 5 deletions

File tree

src/vs/editor/standalone/browser/simpleServices.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platf
4545
import { ILayoutService, IDimension } from 'vs/platform/layout/browser/layoutService';
4646
import { SimpleServicesNLS } from 'vs/editor/common/standaloneStrings';
4747
import { ClassifiedEvent, StrictPropertyCheck, GDPRClassification } from 'vs/platform/telemetry/common/gdprTypings';
48+
import { basename } from 'vs/base/common/path';
4849

4950
export class SimpleModel implements IResolvedTextEditorModel {
5051

@@ -656,6 +657,7 @@ export class SimpleBulkEditService implements IBulkEditService {
656657
}
657658

658659
export class SimpleUriLabelService implements ILabelService {
660+
659661
_serviceBrand: any;
660662

661663
private readonly _onDidRegisterFormatter = new Emitter<void>();
@@ -668,6 +670,10 @@ export class SimpleUriLabelService implements ILabelService {
668670
return resource.path;
669671
}
670672

673+
getUriBasenameLabel(resource: URI): string {
674+
return basename(resource.path);
675+
}
676+
671677
public getWorkspaceLabel(workspace: IWorkspaceIdentifier | URI | IWorkspace, options?: { verbose: boolean; }): string {
672678
return '';
673679
}

src/vs/platform/label/common/label.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface ILabelService {
2121
* If noPrefix is passed does not tildify the label and also does not prepand the root name for relative labels in a multi root scenario.
2222
*/
2323
getUriLabel(resource: URI, options?: { relative?: boolean, noPrefix?: boolean, endWithSeparator?: boolean }): string;
24+
getUriBasenameLabel(resource: URI): string;
2425
getWorkspaceLabel(workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | IWorkspace), options?: { verbose: boolean }): string;
2526
getHostLabel(scheme: string, authority?: string): string;
2627
getSeparator(scheme: string, authority?: string): '/' | '\\';

src/vs/workbench/common/editor/untitledEditorInput.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { URI } from 'vs/base/common/uri';
77
import { suggestFilename } from 'vs/base/common/mime';
88
import { memoize } from 'vs/base/common/decorators';
99
import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry';
10-
import { basename } from 'vs/base/common/path';
1110
import { basenameOrAuthority, dirname } from 'vs/base/common/resources';
1211
import { EditorInput, IEncodingSupport, EncodingMode, ConfirmResult, Verbosity, IModeSupport } from 'vs/workbench/common/editor';
1312
import { UntitledEditorModel } from 'vs/workbench/common/editor/untitledEditorModel';
@@ -64,7 +63,7 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport
6463

6564
@memoize
6665
private get shortDescription(): string {
67-
return basename(this.labelService.getUriLabel(dirname(this.resource)));
66+
return this.labelService.getUriBasenameLabel(dirname(this.resource));
6867
}
6968

7069
@memoize

src/vs/workbench/contrib/files/common/editors/fileEditorInput.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import { localize } from 'vs/nls';
77
import { memoize } from 'vs/base/common/decorators';
8-
import { basename } from 'vs/base/common/path';
98
import { dirname } from 'vs/base/common/resources';
109
import { URI } from 'vs/base/common/uri';
1110
import { EncodingMode, ConfirmResult, EditorInput, IFileEditorInput, ITextEditorModel, Verbosity, IRevertOptions } from 'vs/workbench/common/editor';
@@ -147,14 +146,14 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {
147146

148147
getName(): string {
149148
if (!this.name) {
150-
this.name = basename(this.labelService.getUriLabel(this.resource));
149+
this.name = this.labelService.getUriBasenameLabel(this.resource);
151150
}
152151

153152
return this.decorateLabel(this.name);
154153
}
155154

156155
private get shortDescription(): string {
157-
return basename(this.labelService.getUriLabel(dirname(this.resource)));
156+
return this.labelService.getUriBasenameLabel(dirname(this.resource));
158157
}
159158

160159
private get mediumDescription(): string {

src/vs/workbench/services/label/common/labelService.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import { localize } from 'vs/nls';
77
import { URI } from 'vs/base/common/uri';
88
import { IDisposable } from 'vs/base/common/lifecycle';
9+
import * as paths from 'vs/base/common/path';
910
import { Event, Emitter } from 'vs/base/common/event';
1011
import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry, IWorkbenchContribution } from 'vs/workbench/common/contributions';
1112
import { Registry } from 'vs/platform/registry/common/platform';
@@ -159,6 +160,16 @@ export class LabelService implements ILabelService {
159160
return options.endWithSeparator ? this.appendSeparatorIfMissing(label, formatting) : label;
160161
}
161162

163+
getUriBasenameLabel(resource: URI): string {
164+
const label = this.getUriLabel(resource);
165+
const formatting = this.findFormatting(resource);
166+
if (formatting && formatting.separator === '\\') {
167+
return paths.win32.basename(label);
168+
}
169+
170+
return paths.posix.basename(label);
171+
}
172+
162173
getWorkspaceLabel(workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | IWorkspace), options?: { verbose: boolean }): string {
163174
if (IWorkspace.isIWorkspace(workspace)) {
164175
const identifier = toWorkspaceIdentifier(workspace);

src/vs/workbench/services/label/test/label.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ suite('URI Label', () => {
3333
const uri1 = TestWorkspace.folders[0].uri.with({ path: TestWorkspace.folders[0].uri.path.concat('/a/b/c/d') });
3434
assert.equal(labelService.getUriLabel(uri1, { relative: true }), isWindows ? 'a\\b\\c\\d' : 'a/b/c/d');
3535
assert.equal(labelService.getUriLabel(uri1, { relative: false }), isWindows ? 'C:\\testWorkspace\\a\\b\\c\\d' : '/testWorkspace/a/b/c/d');
36+
assert.equal(labelService.getUriBasenameLabel(uri1), 'd');
3637

3738
const uri2 = URI.file('c:\\1/2/3');
3839
assert.equal(labelService.getUriLabel(uri2, { relative: false }), isWindows ? 'C:\\1\\2\\3' : '/c:\\1/2/3');
40+
assert.equal(labelService.getUriBasenameLabel(uri2), '3');
3941
});
4042

4143
test('custom scheme', function () {
@@ -51,6 +53,23 @@ suite('URI Label', () => {
5153

5254
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
5355
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'LABEL//1/2/3/4/5/microsoft.com/END');
56+
assert.equal(labelService.getUriBasenameLabel(uri1), 'END');
57+
});
58+
59+
test.only('separator', function () {
60+
labelService.registerFormatter({
61+
scheme: 'vscode',
62+
formatting: {
63+
label: 'LABEL\\${path}\\${authority}\\END',
64+
separator: '\\',
65+
tildify: true,
66+
normalizeDriveLetter: true
67+
}
68+
});
69+
70+
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
71+
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'LABEL\\\\1\\2\\3\\4\\5\\microsoft.com\\END');
72+
assert.equal(labelService.getUriBasenameLabel(uri1), 'END');
5473
});
5574

5675
test('custom authority', function () {
@@ -65,6 +84,7 @@ suite('URI Label', () => {
6584

6685
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
6786
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'LABEL//1/2/3/4/5/microsoft.com/END');
87+
assert.equal(labelService.getUriBasenameLabel(uri1), 'END');
6888
});
6989

7090
test('mulitple authority', function () {
@@ -96,6 +116,7 @@ suite('URI Label', () => {
96116
// Make sure the most specific authority is picked
97117
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
98118
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'second');
119+
assert.equal(labelService.getUriBasenameLabel(uri1), 'second');
99120
});
100121

101122
test('custom query', function () {

0 commit comments

Comments
 (0)