Skip to content

Commit

Permalink
file labels: show root name separated by •. Also absolute paths only …
Browse files Browse the repository at this point in the history
…for file schema

#51446
  • Loading branch information
isidorn committed Jun 13, 2018
1 parent d901821 commit a9a9195
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
34 changes: 20 additions & 14 deletions src/vs/base/common/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
'use strict';

import URI from 'vs/base/common/uri';
import { nativeSep, normalize, basename as pathsBasename, join, sep } from 'vs/base/common/paths';
import { endsWith, ltrim, equalsIgnoreCase, startsWithIgnoreCase, rtrim, startsWith } from 'vs/base/common/strings';
import { nativeSep, normalize, basename as pathsBasename, sep } from 'vs/base/common/paths';
import { endsWith, ltrim, startsWithIgnoreCase, rtrim, startsWith } from 'vs/base/common/strings';
import { Schemas } from 'vs/base/common/network';
import { isLinux, isWindows, isMacintosh } from 'vs/base/common/platform';
import { isEqual } from 'vs/base/common/resources';

export interface IWorkspaceFolderProvider {
getWorkspaceFolder(resource: URI): { uri: URI };
getWorkspaceFolder(resource: URI): { uri: URI, name?: string };
getWorkspace(): {
folders: { uri: URI }[];
folders: { uri: URI, name?: string }[];
};
}

export interface IUserHomeProvider {
userHome: string;
}

/**
* @param resource for which to compute the path label
* @param userHomeProvider if a resource has a file schema userHomeProvider is used for tildifiying the label
* @param rootProvider only passed in if the label should be relative to the workspace root
*/
export function getPathLabel(resource: URI | string, userHomeProvider: IUserHomeProvider, rootProvider?: IWorkspaceFolderProvider): string {
if (!resource) {
return null;
Expand All @@ -30,31 +36,31 @@ export function getPathLabel(resource: URI | string, userHomeProvider: IUserHome
resource = URI.file(resource);
}

// return early if the resource is neither file:// nor untitled://
if (resource.scheme !== Schemas.file && resource.scheme !== Schemas.untitled) {
return resource.with({ query: null, fragment: null }).toString(true);
}

// return early if we can resolve a relative path label from the root
const baseResource = rootProvider ? rootProvider.getWorkspaceFolder(resource) : null;
if (baseResource) {
const hasMultipleRoots = rootProvider.getWorkspace().folders.length > 1;

let pathLabel: string;
if (isLinux ? baseResource.uri.fsPath === resource.fsPath : equalsIgnoreCase(baseResource.uri.fsPath, resource.fsPath)) {
pathLabel = ''; // no label if pathes are identical
if (isEqual(baseResource.uri, resource, !isLinux)) {
pathLabel = ''; // no label if paths are identical
} else {
pathLabel = normalize(ltrim(resource.fsPath.substr(baseResource.uri.fsPath.length), nativeSep), true);
pathLabel = normalize(ltrim(resource.toString().substr(baseResource.uri.toString().length), nativeSep), true);
}

if (hasMultipleRoots) {
const rootName = pathsBasename(baseResource.uri.fsPath);
pathLabel = pathLabel ? join(rootName, pathLabel) : rootName; // always show root basename if there are multiple
const rootName = (baseResource && baseResource.name) ? baseResource.name : pathsBasename(baseResource.uri.fsPath);
pathLabel = pathLabel ? (rootName + ' • ' + pathLabel) : rootName; // always show root basename if there are multiple
}

return pathLabel;
}

// return if the resource is neither file:// nor untitled:// and no baseResource was provided
if (resource.scheme !== Schemas.file && resource.scheme !== Schemas.untitled) {
return resource.with({ query: null, fragment: null }).toString(true);
}

// convert c:\something => C:\something
if (hasDriveLetter(resource.fsPath)) {
return normalize(normalizeDriveLetter(resource.fsPath), true);
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/browser/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ export class ResourceLabel extends IconLabel {
iconLabelOptions.title = this.options.title;
} else if (resource && resource.scheme !== Schemas.data /* do not accidentally inline Data URIs */) {
if (!this.computedPathLabel) {
this.computedPathLabel = getPathLabel(resource, this.environmentService);
const rootProvider = resource.scheme !== Schemas.file ? this.contextService : undefined;
this.computedPathLabel = getPathLabel(resource, this.environmentService, rootProvider);
}

iconLabelOptions.title = this.computedPathLabel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment'
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { IHashService } from 'vs/workbench/services/hash/common/hashService';
import { FILE_EDITOR_INPUT_ID, TEXT_FILE_EDITOR_ID, BINARY_FILE_EDITOR_ID } from 'vs/workbench/parts/files/common/files';
import { Schemas } from 'vs/base/common/network';

/**
* A file editor input is the input type for the file editor of file system resources.
Expand Down Expand Up @@ -148,7 +149,8 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {

@memoize
private get longDescription(): string {
return labels.getPathLabel(resources.dirname(this.resource), this.environmentService);
const rootProvider = this.resource.scheme !== Schemas.file ? this.contextService : undefined;
return labels.getPathLabel(resources.dirname(this.resource), this.environmentService, rootProvider);
}

public getDescription(verbosity: Verbosity = Verbosity.MEDIUM): string {
Expand Down Expand Up @@ -181,7 +183,8 @@ export class FileEditorInput extends EditorInput implements IFileEditorInput {

@memoize
private get longTitle(): string {
return labels.getPathLabel(this.resource, this.environmentService);
const rootProvider = this.resource.scheme !== Schemas.file ? this.contextService : undefined;
return labels.getPathLabel(this.resource, this.environmentService, rootProvider);
}

public getTitle(verbosity: Verbosity): string {
Expand Down

0 comments on commit a9a9195

Please sign in to comment.