Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/vs/platform/label/common/label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface ResourceLabelFormatter {
}

export interface ResourceLabelFormatting {
label: string; // myLabel:/${path}
label: string | ((resource: URI) => string); // myLabel:/${path}
separator: '/' | '\\' | '';
tildify?: boolean;
normalizeDriveLetter?: boolean;
Expand Down
47 changes: 47 additions & 0 deletions src/vs/workbench/contrib/scm/browser/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity'
import { stripIcons } from 'vs/base/common/iconLabels';
import { Schemas } from 'vs/base/common/network';
import { Iterable } from 'vs/base/common/iterator';
import { ILabelService } from 'vs/platform/label/common/label';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { URI } from 'vs/base/common/uri';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';

function getCount(repository: ISCMRepository): number {
if (typeof repository.provider.count === 'number') {
Expand Down Expand Up @@ -272,3 +276,46 @@ export class SCMActiveResourceContextKeyController implements IWorkbenchContribu
this.repositoryDisposables.clear();
}
}

class SCMInputTextDocumentLabelFormatter {

readonly separator = '/';

readonly label = (resource: URI) => {
const match = /git\/(?<repositoryId>scm[\d+])\/input/i.exec(resource.path);

if (match?.groups === undefined) {
return resource.toString();
}

const { repositoryId } = match.groups;
const repository = this.scmService.getRepository(repositoryId);

if (repository === undefined || repository.provider.rootUri === undefined) {
return resource.toString();
}

const folder = this.workspaceContextService.getWorkspaceFolder(repository.provider.rootUri);
const repositoryName = folder?.uri.toString() === repository.provider.rootUri.toString() ? folder.name : basename(repository.provider.rootUri);

return `${repositoryName} (${repository.provider.label})`;
};

constructor(
@ISCMService private readonly scmService: ISCMService,
@IWorkspaceContextService private workspaceContextService: IWorkspaceContextService,
) { }
}

export class SCMInputTextDocumentContribution implements IWorkbenchContribution {
constructor(
@IInstantiationService instantiationService: IInstantiationService,
@ILabelService labelService: ILabelService
) {
labelService.registerFormatter({
scheme: Schemas.vscode,
authority: 'scm',
formatting: instantiationService.createInstance(SCMInputTextDocumentLabelFormatter)
});
}
}
5 changes: 4 additions & 1 deletion src/vs/workbench/contrib/scm/browser/scm.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { DirtyDiffWorkbenchController } from './dirtydiffDecorator';
import { VIEWLET_ID, ISCMService, VIEW_PANE_ID, ISCMProvider, ISCMViewService, REPOSITORIES_VIEW_PANE_ID } from 'vs/workbench/contrib/scm/common/scm';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
import { MenuRegistry, MenuId } from 'vs/platform/actions/common/actions';
import { SCMActiveResourceContextKeyController, SCMStatusController } from './activity';
import { SCMActiveResourceContextKeyController, SCMInputTextDocumentContribution, SCMStatusController } from './activity';
import { LifecyclePhase } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { IConfigurationRegistry, Extensions as ConfigurationExtensions, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
import { IContextKeyService, ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
Expand Down Expand Up @@ -115,6 +115,9 @@ Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
.registerWorkbenchContribution(SCMStatusController, LifecyclePhase.Restored);

Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench)
.registerWorkbenchContribution(SCMInputTextDocumentContribution, LifecyclePhase.Restored);

Registry.as<IConfigurationRegistry>(ConfigurationExtensions.Configuration).registerConfiguration({
id: 'scm',
order: 5,
Expand Down
47 changes: 24 additions & 23 deletions src/vs/workbench/services/label/common/labelService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,32 +402,33 @@ export class LabelService extends Disposable implements ILabelService {
}

private formatUri(resource: URI, formatting: ResourceLabelFormatting, forceNoTildify?: boolean): string {
let label = formatting.label.replace(labelMatchingRegexp, (match, token, qsToken, qsValue) => {
switch (token) {
case 'scheme': return resource.scheme;
case 'authority': return resource.authority;
case 'authoritySuffix': {
const i = resource.authority.indexOf('+');
return i === -1 ? resource.authority : resource.authority.slice(i + 1);
}
case 'path':
return formatting.stripPathStartingSeparator
? resource.path.slice(resource.path[0] === formatting.separator ? 1 : 0)
: resource.path;
default: {
if (qsToken === 'query') {
const { query } = resource;
if (query && query[0] === '{' && query[query.length - 1] === '}') {
try {
return JSON.parse(query)[qsValue] || '';
} catch { }
}
let label = typeof formatting.label !== 'string' ? formatting.label(resource) :
formatting.label.replace(labelMatchingRegexp, (match, token, qsToken, qsValue) => {
switch (token) {
case 'scheme': return resource.scheme;
case 'authority': return resource.authority;
case 'authoritySuffix': {
const i = resource.authority.indexOf('+');
return i === -1 ? resource.authority : resource.authority.slice(i + 1);
}
case 'path':
return formatting.stripPathStartingSeparator
? resource.path.slice(resource.path[0] === formatting.separator ? 1 : 0)
: resource.path;
default: {
if (qsToken === 'query') {
const { query } = resource;
if (query && query[0] === '{' && query[query.length - 1] === '}') {
try {
return JSON.parse(query)[qsValue] || '';
} catch { }
}
}

return '';
return '';
}
}
}
});
});

// convert \c:\something => C:\something
if (formatting.normalizeDriveLetter && hasDriveLetterIgnorePlatform(label)) {
Expand Down
12 changes: 12 additions & 0 deletions src/vs/workbench/services/label/test/browser/label.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ suite('URI Label', () => {
assert.strictEqual(labelService.getUriLabel(uri1, { relative: false }), 'LABEL: /END');
});

test('custom formatting function', function () {
labelService.registerFormatter({
scheme: 'vscode',
formatting: {
label: (resource) => { return resource.toString(); },
separator: '/',
}
});

const uri1 = URI.parse('vscode://microsoft.com/1/2/3/4/5');
assert.strictEqual(labelService.getUriLabel(uri1), uri1.toString());
});
Comment thread
lszomoru marked this conversation as resolved.

test('label caching', () => {
const m = new Memento('cachedResourceLabelFormatters', storageService).getMemento(StorageScope.PROFILE, StorageTarget.MACHINE);
Expand Down