提交 9ba00291 编写于 作者: M Martin Aeschlimann

labelservice: match prefix, take longest match

上级 39bff6cf
......@@ -236,7 +236,7 @@ export class CodeApplication {
});
ipc.on('vscode:labelRegisterFormatter', (event: any, data: RegisterFormatterEvent) => {
this.labelService.registerFormatter(data.scheme, data.formatter);
this.labelService.registerFormatter(data.selector, data.formatter);
});
ipc.on('vscode:toggleDevTools', (event: Event) => {
......
......@@ -613,7 +613,7 @@ export class SimpleUriLabelService implements ILabelService {
return '';
}
public registerFormatter(schema: string, formatter: LabelRules): IDisposable {
public registerFormatter(selector: string, formatter: LabelRules): IDisposable {
throw new Error('Not implemented');
}
}
......@@ -12,7 +12,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
import { isEqual, basenameOrAuthority } from 'vs/base/common/resources';
import { isLinux, isWindows } from 'vs/base/common/platform';
import { tildify, getPathLabel } from 'vs/base/common/labels';
import { ltrim } from 'vs/base/common/strings';
import { ltrim, startsWith } from 'vs/base/common/strings';
import { IWorkspaceIdentifier, ISingleFolderWorkspaceIdentifier, isSingleFolderWorkspaceIdentifier, WORKSPACE_EXTENSION, toWorkspaceIdentifier, isWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
import { localize } from 'vs/nls';
import { isParent } from 'vs/platform/files/common/files';
......@@ -20,7 +20,7 @@ import { basename, dirname, join } from 'vs/base/common/paths';
import { Schemas } from 'vs/base/common/network';
export interface RegisterFormatterEvent {
scheme: string;
selector: string;
formatter: LabelRules;
}
......@@ -33,7 +33,7 @@ export interface ILabelService {
*/
getUriLabel(resource: URI, options?: { relative?: boolean, noPrefix?: boolean }): string;
getWorkspaceLabel(workspace: (IWorkspaceIdentifier | ISingleFolderWorkspaceIdentifier | IWorkspace), options?: { verbose: boolean }): string;
registerFormatter(schema: string, formatter: LabelRules): IDisposable;
registerFormatter(selector: string, formatter: LabelRules): IDisposable;
onDidRegisterFormatter: Event<RegisterFormatterEvent>;
}
......@@ -61,7 +61,7 @@ function hasDriveLetter(path: string): boolean {
export class LabelService implements ILabelService {
_serviceBrand: any;
private readonly formatters = new Map<string, LabelRules>();
private readonly formatters: { [prefix: string]: LabelRules } = Object.create(null);
private readonly _onDidRegisterFormatter = new Emitter<RegisterFormatterEvent>();
constructor(
......@@ -73,11 +73,25 @@ export class LabelService implements ILabelService {
return this._onDidRegisterFormatter.event;
}
findFormatter(resource: URI): LabelRules {
const path = `${resource.scheme}://${resource.authority}`;
let bestPrefix = '';
for (let prefix in this.formatters) {
if (startsWith(path, prefix) && prefix.length > bestPrefix.length) {
bestPrefix = prefix;
}
}
if (bestPrefix.length) {
return this.formatters[bestPrefix];
}
return void 0;
}
getUriLabel(resource: URI, options: { relative?: boolean, noPrefix?: boolean } = {}): string {
if (!resource) {
return undefined;
}
const formatter = this.formatters.get(resource.scheme);
const formatter = this.findFormatter(resource);
if (!formatter) {
return getPathLabel(resource.path, this.environmentService, options.relative ? this.contextService : undefined);
}
......@@ -117,7 +131,7 @@ export class LabelService implements ILabelService {
// Workspace: Single Folder
if (isSingleFolderWorkspaceIdentifier(workspace)) {
// Folder on disk
const formatter = this.formatters.get(workspace.scheme);
const formatter = this.findFormatter(workspace);
const label = options && options.verbose ? this.getUriLabel(workspace) : basenameOrAuthority(workspace);
if (workspace.scheme === Schemas.file) {
return label;
......@@ -142,12 +156,12 @@ export class LabelService implements ILabelService {
return localize('workspaceName', "{0} (Workspace)", workspaceName);
}
registerFormatter(scheme: string, formatter: LabelRules): IDisposable {
this.formatters.set(scheme, formatter);
this._onDidRegisterFormatter.fire({ scheme, formatter });
registerFormatter(selector: string, formatter: LabelRules): IDisposable {
this.formatters[selector] = formatter;
this._onDidRegisterFormatter.fire({ selector, formatter });
return {
dispose: () => this.formatters.delete(scheme)
dispose: () => delete this.formatters[selector]
};
}
......
......@@ -6,7 +6,6 @@
import * as assert from 'assert';
import { LabelService } from 'vs/platform/label/common/label';
import { TestEnvironmentService, TestContextService } from 'vs/workbench/test/workbenchTestServices';
import { Schemas } from 'vs/base/common/network';
import { TestWorkspace } from 'vs/platform/workspace/test/common/testWorkspace';
import { URI } from 'vs/base/common/uri';
import { nativeSep } from 'vs/base/common/paths';
......@@ -21,7 +20,7 @@ suite('URI Label', () => {
});
test('file scheme', function () {
labelService.registerFormatter(Schemas.file, {
labelService.registerFormatter('file://', {
uri: {
label: '${path}',
separator: nativeSep,
......@@ -39,7 +38,7 @@ suite('URI Label', () => {
});
test('custom scheme', function () {
labelService.registerFormatter(Schemas.vscode, {
labelService.registerFormatter('vscode://', {
uri: {
label: 'LABEL/${path}/${authority}/END',
separator: '/',
......
......@@ -40,8 +40,8 @@ export class MainThreadFileSystem implements MainThreadFileSystemShape {
this._fileProvider.delete(handle);
}
$setUriFormatter(scheme: string, formatter: LabelRules): void {
this._labelService.registerFormatter(scheme, formatter);
$setUriFormatter(selector: string, formatter: LabelRules): void {
this._labelService.registerFormatter(selector, formatter);
}
$onFileSystemChange(handle: number, changes: IFileChangeDto[]): void {
......
......@@ -35,7 +35,6 @@ import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IEditorGroupsService } from 'vs/workbench/services/group/common/editorGroupsService';
import { ILabelService } from 'vs/platform/label/common/label';
import { Schemas } from 'vs/base/common/network';
import { nativeSep } from 'vs/base/common/paths';
import { IPartService } from 'vs/workbench/services/part/common/partService';
......@@ -58,7 +57,7 @@ export class OpenExplorerViewletAction extends ShowViewletAction {
class FileUriLabelContribution implements IWorkbenchContribution {
constructor(@ILabelService labelService: ILabelService) {
labelService.registerFormatter(Schemas.file, {
labelService.registerFormatter('file://', {
uri: {
label: '${authority}${path}',
separator: nativeSep,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册