Skip to content

Commit 484b804

Browse files
author
Benjamin Pasero
committed
Untitled: show original resource name as description (fix microsoft#89423)
1 parent 103d079 commit 484b804

4 files changed

Lines changed: 66 additions & 44 deletions

File tree

src/vs/workbench/browser/labels.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,29 @@ class ResourceLabelWidget extends IconLabel {
338338
}
339339

340340
setResource(label: IResourceLabelProps, options?: IResourceLabelOptions): void {
341+
if (label.resource?.scheme === Schemas.untitled) {
342+
// Untitled labels are very dynamic because they may change
343+
// whenever the content changes (unless a path is associated).
344+
// As such we always ask the actual editor for it's name and
345+
// description to get latest in case name/description are
346+
// provided. If they are not provided from the label we got
347+
// we assume that the client does not want to display them
348+
// and as such do not override.
349+
const untitledEditor = this.textFileService.untitled.get(label.resource);
350+
if (untitledEditor && !untitledEditor.hasAssociatedFilePath) {
351+
if (typeof label.name === 'string') {
352+
label.name = untitledEditor.getName();
353+
}
354+
355+
if (typeof label.description === 'string') {
356+
const untitledDescription = untitledEditor.getDescription(options?.descriptionVerbosity);
357+
if (label.name !== untitledDescription) {
358+
label.description = untitledDescription;
359+
}
360+
}
361+
}
362+
}
363+
341364
const hasPathLabelChanged = this.hasPathLabelChanged(label, options);
342365
const clearIconCache = this.clearIconCache(label, options);
343366

@@ -404,8 +427,7 @@ class ResourceLabelWidget extends IconLabel {
404427
}
405428

406429
let description: string | undefined;
407-
const hidePath = (options && options.hidePath) || (resource.scheme === Schemas.untitled && !this.textFileService.untitled.hasAssociatedFilePath(resource));
408-
if (!hidePath) {
430+
if (!options?.hidePath) {
409431
description = this.labelService.getUriLabel(resources.dirname(resource), { relative: true });
410432
}
411433

@@ -463,6 +485,7 @@ class ResourceLabelWidget extends IconLabel {
463485
};
464486

465487
const resource = this.label.resource;
488+
const label = this.label.name;
466489

467490
if (this.options && typeof this.options.title === 'string') {
468491
iconLabelOptions.title = this.options.title;
@@ -509,18 +532,7 @@ class ResourceLabelWidget extends IconLabel {
509532
}
510533
}
511534

512-
let label = this.label.name || '';
513-
if (resource?.scheme === Schemas.untitled) {
514-
// Untitled labels are very dynamic because they may change
515-
// whenever the content changes. As such we always ask the
516-
// text file service for the name of the untitled editor
517-
const untitledName = this.textFileService.untitled.get(resource)?.getName();
518-
if (untitledName) {
519-
label = untitledName;
520-
}
521-
}
522-
523-
this.setLabel(label, this.label.description, iconLabelOptions);
535+
this.setLabel(label || '', this.label.description, iconLabelOptions);
524536

525537
this._onDidRender.fire();
526538
}

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

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,32 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin
7575
return this.hasAssociatedFilePath ? basenameOrAuthority(this.resource) : this.resource.path;
7676
}
7777

78+
getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string | undefined {
79+
80+
// Without associated path: only use if name and description differ
81+
if (!this.hasAssociatedFilePath) {
82+
const descriptionCandidate = this.resource.path;
83+
if (descriptionCandidate !== this.getName()) {
84+
return descriptionCandidate;
85+
}
86+
}
87+
88+
// With associated path: use label provider
89+
else {
90+
switch (verbosity) {
91+
case Verbosity.SHORT:
92+
return this.shortDescription;
93+
case Verbosity.LONG:
94+
return this.longDescription;
95+
case Verbosity.MEDIUM:
96+
default:
97+
return this.mediumDescription;
98+
}
99+
}
100+
101+
return undefined;
102+
}
103+
78104
@UntitledTextEditorInput.MEMOIZER
79105
private get shortDescription(): string {
80106
return this.labelService.getUriBasenameLabel(dirname(this.resource));
@@ -90,22 +116,6 @@ export class UntitledTextEditorInput extends TextEditorInput implements IEncodin
90116
return this.labelService.getUriLabel(dirname(this.resource));
91117
}
92118

93-
getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string | undefined {
94-
if (!this.hasAssociatedFilePath) {
95-
return undefined;
96-
}
97-
98-
switch (verbosity) {
99-
case Verbosity.SHORT:
100-
return this.shortDescription;
101-
case Verbosity.LONG:
102-
return this.longDescription;
103-
case Verbosity.MEDIUM:
104-
default:
105-
return this.mediumDescription;
106-
}
107-
}
108-
109119
@UntitledTextEditorInput.MEMOIZER
110120
private get shortTitle(): string {
111121
return this.getName();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface IUntitledTextEditorModel extends ITextEditorModel, IModeSupport
2525

2626
export class UntitledTextEditorModel extends BaseTextEditorModel implements IUntitledTextEditorModel {
2727

28-
private static readonly FIRST_LINE_NAME_MAX_LENGTH = 50;
28+
private static readonly FIRST_LINE_NAME_MAX_LENGTH = 40;
2929

3030
private readonly _onDidChangeContent = this._register(new Emitter<void>());
3131
readonly onDidChangeContent = this._onDidChangeContent.event;
@@ -226,7 +226,7 @@ export class UntitledTextEditorModel extends BaseTextEditorModel implements IUnt
226226
this.setDirty(true);
227227
}
228228

229-
// Check for name change if first line changed in the range of 0-50 columns
229+
// Check for name change if first line changed in the range of 0-FIRST_LINE_NAME_MAX_LENGTH columns
230230
if (e.changes.some(change => (change.range.startLineNumber === 1 || change.range.endLineNumber === 1) && change.range.startColumn <= UntitledTextEditorModel.FIRST_LINE_NAME_MAX_LENGTH)) {
231231
this.updateNameFromFirstLine();
232232
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,18 @@ export class FileEditorInput extends TextEditorInput implements IFileEditorInput
156156
return this.decorateLabel(this.labelService.getUriBasenameLabel(this.resource));
157157
}
158158

159+
getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string {
160+
switch (verbosity) {
161+
case Verbosity.SHORT:
162+
return this.shortDescription;
163+
case Verbosity.LONG:
164+
return this.longDescription;
165+
case Verbosity.MEDIUM:
166+
default:
167+
return this.mediumDescription;
168+
}
169+
}
170+
159171
@FileEditorInput.MEMOIZER
160172
private get shortDescription(): string {
161173
return this.labelService.getUriBasenameLabel(dirname(this.resource));
@@ -171,18 +183,6 @@ export class FileEditorInput extends TextEditorInput implements IFileEditorInput
171183
return this.labelService.getUriLabel(dirname(this.resource));
172184
}
173185

174-
getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string {
175-
switch (verbosity) {
176-
case Verbosity.SHORT:
177-
return this.shortDescription;
178-
case Verbosity.LONG:
179-
return this.longDescription;
180-
case Verbosity.MEDIUM:
181-
default:
182-
return this.mediumDescription;
183-
}
184-
}
185-
186186
@FileEditorInput.MEMOIZER
187187
private get shortTitle(): string {
188188
return this.getName();

0 commit comments

Comments
 (0)