提交 76bfcb0f 编写于 作者: I isidor

label: introduce ResourceLabelFormattersHandler

上级 7bd16e21
......@@ -7,6 +7,8 @@ import { localize } from 'vs/nls';
import { URI } from 'vs/base/common/uri';
import { IDisposable } from 'vs/base/common/lifecycle';
import { Event, Emitter } from 'vs/base/common/event';
import { Extensions as WorkbenchExtensions, IWorkbenchContributionsRegistry, IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { Registry } from 'vs/platform/registry/common/platform';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IWorkspaceContextService, IWorkspace } from 'vs/platform/workspace/common/workspace';
import { isEqual, basenameOrAuthority, basename as resourceBasename } from 'vs/base/common/resources';
......@@ -22,8 +24,9 @@ import { REMOTE_HOST_SCHEME } from 'vs/platform/remote/common/remoteHosts';
import { ILabelService, ResourceLabelFormatter, ResourceLabelFormatting } from 'vs/platform/label/common/label';
import { ExtensionsRegistry } from 'vs/workbench/services/extensions/common/extensionsRegistry';
import { match } from 'vs/base/common/glob';
import { LifecyclePhase } from 'vs/platform/lifecycle/common/lifecycle';
export const labelsExtPoint = ExtensionsRegistry.registerExtensionPoint<ResourceLabelFormatter[]>({
const resourceLabelFormattersExtPoint = ExtensionsRegistry.registerExtensionPoint<ResourceLabelFormatter[]>({
extensionPoint: 'resourceLabelFormatters',
jsonSchema: {
description: localize('vscode.extension.contributes.resourceLabelFormatters', 'Contributes resource label formatting rules.'),
......@@ -74,6 +77,16 @@ function hasDriveLetter(path: string): boolean {
return !!(isWindows && path && path[2] === ':');
}
class ResourceLabelFormattersHandler implements IWorkbenchContribution {
constructor(@ILabelService labelService: ILabelService) {
resourceLabelFormattersExtPoint.setHandler(extensions => {
extensions.forEach(extension => extension.value.forEach(formatter => labelService.registerFormatter(formatter)));
});
}
}
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(ResourceLabelFormattersHandler, LifecyclePhase.Restored);
export class LabelService implements ILabelService {
_serviceBrand: any;
......@@ -84,12 +97,7 @@ export class LabelService implements ILabelService {
@IEnvironmentService private readonly environmentService: IEnvironmentService,
@IWorkspaceContextService private readonly contextService: IWorkspaceContextService,
@IWindowService private readonly windowService: IWindowService
) {
labelsExtPoint.setHandler(extensions => {
extensions.forEach(extension => extension.value.forEach(formatter => this.formatters.push(formatter)));
this._onDidRegisterFormatter.fire();
});
}
) { }
get onDidRegisterFormatter(): Event<void> {
return this._onDidRegisterFormatter.event;
......@@ -100,18 +108,21 @@ export class LabelService implements ILabelService {
this.formatters.forEach(formatter => {
if (formatter.scheme === resource.scheme) {
if (!formatter.authority && !bestResult) {
if (!bestResult) {
bestResult = formatter;
return;
}
if (!formatter.authority) {
return;
}
if (match(resource.authority, formatter.authority) && (!bestResult.authority || formatter.authority.length > bestResult.authority.length)) {
if (match(formatter.authority, resource.authority) && (!bestResult.authority || formatter.authority.length > bestResult.authority.length)) {
bestResult = formatter;
}
}
});
return bestResult.formatting;
return bestResult ? bestResult.formatting : undefined;
}
getUriLabel(resource: URI, options: { relative?: boolean, noPrefix?: boolean } = {}): string {
......
......@@ -21,7 +21,7 @@ suite('URI Label', () => {
test('file scheme', function () {
labelService.registerFormatter({
scheme: 'file://',
scheme: 'file',
formatting: {
label: '${path}',
separator: nativeSep,
......@@ -40,7 +40,7 @@ suite('URI Label', () => {
test('custom scheme', function () {
labelService.registerFormatter({
scheme: 'vscode://',
scheme: 'vscode',
formatting: {
label: 'LABEL/${path}/${authority}/END',
separator: '/',
......@@ -52,4 +52,40 @@ suite('URI Label', () => {
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'LABEL//1/2/3/4/5/microsoft.com/END');
});
test('custom authority', function () {
labelService.registerFormatter({
scheme: 'vscode',
authority: 'micro*',
formatting: {
label: 'LABEL/${path}/${authority}/END',
separator: '/'
}
});
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'LABEL//1/2/3/4/5/microsoft.com/END');
});
test('mulitple authority', function () {
labelService.registerFormatter({
scheme: 'vscode',
authority: 'micro*',
formatting: {
label: 'first',
separator: '/'
}
});
labelService.registerFormatter({
scheme: 'vscode',
authority: 'microsof*',
formatting: {
label: 'second',
separator: '/'
}
});
const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
assert.equal(labelService.getUriLabel(uri1, { relative: false }), 'second');
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册