From 55354382c6caa6c350bcd0f7f616d6ae0c7b079c Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Mon, 11 Sep 2017 15:22:20 +0200 Subject: [PATCH] implement getTitle/getDescription for untitled editors --- .../browser/parts/editor/tabsTitleControl.ts | 118 ++++++++++-------- .../common/editor/untitledEditorInput.ts | 52 +++++++- .../files/common/editors/fileEditorInput.ts | 21 ++-- 3 files changed, 123 insertions(+), 68 deletions(-) diff --git a/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts b/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts index fcf48f1b1ff..d2cbb63dd4c 100644 --- a/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts +++ b/src/vs/workbench/browser/parts/editor/tabsTitleControl.ts @@ -51,6 +51,8 @@ interface IEditorInputLabel { title?: string; } +type AugmentedLabel = IEditorInputLabel & { editor: IEditorInput }; + export class TabsTitleControl extends TitleControl { private titleContainer: HTMLElement; private tabsContainer: HTMLElement; @@ -342,7 +344,6 @@ export class TabsTitleControl extends TitleControl { const { verbosity, shortenDuplicates } = this.getSubtitleConfigFlags(tabSubtitleStyle); // Build labels and descriptions for each editor - type AugmentedLabel = IEditorInputLabel & { editor: IEditorInput }; const labels = editors.map(editor => ({ editor, name: editor.getName(), @@ -350,72 +351,81 @@ export class TabsTitleControl extends TitleControl { title: editor.getTitle(Verbosity.LONG) })); + // Shorten labels as needed if (shortenDuplicates) { - // gather duplicate titles, while filtering out invalid descriptions - const mapTitleToDuplicates = new Map(); - for (const label of labels) { - if (typeof label.description === 'string' && label.description) { - getOrSet(mapTitleToDuplicates, label.name, []).push(label); - } else { - label.description = ''; - } + this.shortenTabLabels(labels); + } + + return labels; + } + + private shortenTabLabels(labels: AugmentedLabel[]): void { + + // Gather duplicate titles, while filtering out invalid descriptions + const mapTitleToDuplicates = new Map(); + for (const label of labels) { + if (typeof label.description === 'string' && label.description) { + getOrSet(mapTitleToDuplicates, label.name, []).push(label); + } else { + label.description = ''; } + } - // identify duplicate titles and shorten descriptions - mapTitleToDuplicates.forEach(duplicateTitles => { - // remove description if the title isn't duplicated - if (duplicateTitles.length === 1) { - duplicateTitles[0].description = ''; - return; - } + // Identify duplicate titles and shorten descriptions + mapTitleToDuplicates.forEach(duplicateTitles => { - // identify duplicate descriptions - const mapDescriptionToDuplicates = new Map(); - for (const label of duplicateTitles) { - getOrSet(mapDescriptionToDuplicates, label.description, []).push(label); + // Remove description if the title isn't duplicated + if (duplicateTitles.length === 1) { + duplicateTitles[0].description = ''; + + return; + } + + // Identify duplicate descriptions + const mapDescriptionToDuplicates = new Map(); + for (const label of duplicateTitles) { + getOrSet(mapDescriptionToDuplicates, label.description, []).push(label); + } + + // For editors with duplicate descriptions, check whether any long descriptions differ + let useLongDescriptions = false; + mapDescriptionToDuplicates.forEach((duplicateDescriptions, name) => { + if (!useLongDescriptions && duplicateDescriptions.length > 1) { + const [first, ...rest] = duplicateDescriptions.map(({ editor }) => editor.getDescription(Verbosity.LONG)); + useLongDescriptions = rest.some(description => description !== first); } + }); - // for editors with duplicate descriptions, check whether any long descriptions differ - let useLongDescriptions = false; - mapDescriptionToDuplicates.forEach((duplicateDescriptions, name) => { - if (!useLongDescriptions && duplicateDescriptions.length > 1) { - const [first, ...rest] = duplicateDescriptions.map(({ editor }) => editor.getDescription(Verbosity.LONG)); - useLongDescriptions = rest.some(description => description !== first); - } + // If so, replace all descriptions with long descriptions + if (useLongDescriptions) { + mapDescriptionToDuplicates.clear(); + duplicateTitles.forEach(label => { + label.description = label.editor.getDescription(Verbosity.LONG); + getOrSet(mapDescriptionToDuplicates, label.description, []).push(label); }); + } - // if so, replace all descriptions with long descriptions - if (useLongDescriptions) { - mapDescriptionToDuplicates.clear(); - duplicateTitles.forEach(label => { - label.description = label.editor.getDescription(Verbosity.LONG); - getOrSet(mapDescriptionToDuplicates, label.description, []).push(label); - }); + // Obtain final set of descriptions + const descriptions: string[] = []; + mapDescriptionToDuplicates.forEach((_, description) => descriptions.push(description)); + + // Remove description if all descriptions are identical + if (descriptions.length === 1) { + for (const label of mapDescriptionToDuplicates.get(descriptions[0])) { + label.description = ''; } - // obtain final set of descriptions - const descriptions: string[] = []; - mapDescriptionToDuplicates.forEach((_, description) => descriptions.push(description)); + return; + } - // remove description if all descriptions are identical - if (descriptions.length === 1) { - for (const label of mapDescriptionToDuplicates.get(descriptions[0])) { - label.description = ''; - } - return; + // Shorten descriptions + const shortenedDescriptions = shorten(descriptions); + descriptions.forEach((description, i) => { + for (const label of mapDescriptionToDuplicates.get(description)) { + label.description = shortenedDescriptions[i]; } - - // shorten descriptions - const shortenedDescriptions = shorten(descriptions); - descriptions.forEach((description, i) => { - for (const label of mapDescriptionToDuplicates.get(description)) { - label.description = shortenedDescriptions[i]; - } - }); }); - } - - return labels; + }); } private getSubtitleConfigFlags(value: string) { diff --git a/src/vs/workbench/common/editor/untitledEditorInput.ts b/src/vs/workbench/common/editor/untitledEditorInput.ts index 081b9bd7df4..8ce430fb0f5 100644 --- a/src/vs/workbench/common/editor/untitledEditorInput.ts +++ b/src/vs/workbench/common/editor/untitledEditorInput.ts @@ -19,6 +19,7 @@ import Event, { Emitter } from 'vs/base/common/event'; import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; import { telemetryURIDescriptor } from 'vs/platform/telemetry/common/telemetryUtils'; import { IEnvironmentService } from 'vs/platform/environment/common/environment'; +import { Verbosity } from 'vs/platform/editor/common/editor'; /** * An editor input to be used for untitled text buffers. @@ -36,6 +37,14 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport private toUnbind: IDisposable[]; + private shortDescription: string; + private mediumDescription: string; + private longDescription: string; + + private shortTitle: string; + private mediumTitle: string; + private longTitle: string; + constructor( private resource: URI, hasAssociatedFilePath: boolean, @@ -88,8 +97,47 @@ export class UntitledEditorInput extends EditorInput implements IEncodingSupport return this.hasAssociatedFilePath ? paths.basename(this.resource.fsPath) : this.resource.fsPath; } - public getDescription(): string { - return this.hasAssociatedFilePath ? labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService, this.environmentService) : null; + public getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string { + if (!this.hasAssociatedFilePath) { + return null; + } + + let description: string; + switch (verbosity) { + case Verbosity.SHORT: + description = this.shortDescription ? this.shortDescription : (this.shortDescription = paths.basename(labels.getPathLabel(paths.dirname(this.resource.fsPath), void 0, this.environmentService))); + break; + case Verbosity.LONG: + description = this.longDescription ? this.longDescription : (this.longDescription = labels.getPathLabel(paths.dirname(this.resource.fsPath), void 0, this.environmentService)); + break; + case Verbosity.MEDIUM: + default: + description = this.mediumDescription ? this.mediumDescription : (this.mediumDescription = labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService, this.environmentService)); + break; + } + + return description; + } + + public getTitle(verbosity: Verbosity): string { + if (!this.hasAssociatedFilePath) { + return this.getName(); + } + + let title: string; + switch (verbosity) { + case Verbosity.SHORT: + title = this.shortTitle ? this.shortTitle : (this.shortTitle = this.getName()); + break; + case Verbosity.MEDIUM: + title = this.mediumTitle ? this.mediumTitle : (this.mediumTitle = labels.getPathLabel(this.resource, this.contextService, this.environmentService)); + break; + case Verbosity.LONG: + title = this.longTitle ? this.longTitle : (this.longTitle = labels.getPathLabel(this.resource, void 0, this.environmentService)); + break; + } + + return title; } public isDirty(): boolean { diff --git a/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts b/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts index 25a5097deb4..1393994840d 100644 --- a/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts +++ b/src/vs/workbench/parts/files/common/editors/fileEditorInput.ts @@ -131,24 +131,21 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput { } public getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string { + let description: string; switch (verbosity) { case Verbosity.SHORT: - if (!this.shortDescription) { - this.shortDescription = paths.basename(labels.getPathLabel(paths.dirname(this.resource.fsPath), void 0, this.environmentService)); - } - return this.shortDescription; + description = this.shortDescription ? this.shortDescription : (this.shortDescription = paths.basename(labels.getPathLabel(paths.dirname(this.resource.fsPath), void 0, this.environmentService))); + break; case Verbosity.LONG: - if (!this.longDescription) { - this.longDescription = labels.getPathLabel(paths.dirname(this.resource.fsPath), void 0, this.environmentService); - } - return this.longDescription; + description = this.longDescription ? this.longDescription : (this.longDescription = labels.getPathLabel(paths.dirname(this.resource.fsPath), void 0, this.environmentService)); + break; case Verbosity.MEDIUM: default: - if (!this.mediumDescription) { - this.mediumDescription = labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService, this.environmentService); - } - return this.mediumDescription; + description = this.mediumDescription ? this.mediumDescription : (this.mediumDescription = labels.getPathLabel(paths.dirname(this.resource.fsPath), this.contextService, this.environmentService)); + break; } + + return description; } public getTitle(verbosity: Verbosity): string { -- GitLab