Skip to content

Commit e0195d7

Browse files
committed
simplify copy path command
1 parent 1df5a2f commit e0195d7

2 files changed

Lines changed: 15 additions & 33 deletions

File tree

src/vs/platform/uriLabel/common/uriLabel.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ltrim } from 'vs/base/common/strings';
1616

1717
export interface IUriLabelService {
1818
_serviceBrand: any;
19-
getLabel(resource: URI, relative?: boolean): string;
19+
getLabel(resource: URI, relative?: boolean, forceNoTildify?: boolean): string;
2020
registerFormater(schema: string, formater: UriLabelRules): IDisposable;
2121
onDidRegisterFormater: Event<{ scheme: string, formater: UriLabelRules }>;
2222
}
@@ -52,7 +52,7 @@ export class UriLabelService implements IUriLabelService {
5252
return this._onDidRegisterFormater.event;
5353
}
5454

55-
getLabel(resource: URI, relative: boolean): string {
55+
getLabel(resource: URI, relative: boolean, forceNoTildify?: boolean): string {
5656
if (!resource) {
5757
return undefined;
5858
}
@@ -68,8 +68,8 @@ export class UriLabelService implements IUriLabelService {
6868
if (isEqual(baseResource.uri, resource, !isLinux)) {
6969
relativeLabel = ''; // no label if resources are identical
7070
} else {
71-
const baseResourceLabel = this.formatUri(baseResource.uri, formater);
72-
relativeLabel = ltrim(this.formatUri(resource, formater).substring(baseResourceLabel.length), formater.separator);
71+
const baseResourceLabel = this.formatUri(baseResource.uri, formater, forceNoTildify);
72+
relativeLabel = ltrim(this.formatUri(resource, formater, forceNoTildify).substring(baseResourceLabel.length), formater.separator);
7373
}
7474

7575
const hasMultipleRoots = this.contextService.getWorkspace().folders.length > 1;
@@ -82,7 +82,7 @@ export class UriLabelService implements IUriLabelService {
8282
}
8383
}
8484

85-
return this.formatUri(resource, formater);
85+
return this.formatUri(resource, formater, forceNoTildify);
8686
}
8787

8888
registerFormater(scheme: string, formater: UriLabelRules): IDisposable {
@@ -94,7 +94,7 @@ export class UriLabelService implements IUriLabelService {
9494
};
9595
}
9696

97-
private formatUri(resource: URI, formater: UriLabelRules): string {
97+
private formatUri(resource: URI, formater: UriLabelRules, forceNoTildify: boolean): string {
9898
let label = formater.label.replace(labelMatchingRegexp, match => {
9999
switch (match) {
100100
case '${scheme}': return resource.scheme;
@@ -109,7 +109,7 @@ export class UriLabelService implements IUriLabelService {
109109
label = label.charAt(1).toUpperCase() + label.substr(2);
110110
}
111111

112-
if (formater.tildify) {
112+
if (formater.tildify && !forceNoTildify) {
113113
label = tildify(label, this.environmentService.userHome);
114114
}
115115

src/vs/workbench/parts/files/electron-browser/fileCommands.ts

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import * as nls from 'vs/nls';
99
import * as paths from 'vs/base/common/paths';
1010
import { TPromise } from 'vs/base/common/winjs.base';
11-
import * as labels from 'vs/base/common/labels';
1211
import URI from 'vs/base/common/uri';
1312
import { toResource, IEditorCommandsContext } from 'vs/workbench/common/editor';
1413
import { IWindowsService, IWindowService } from 'vs/platform/windows/common/windows';
@@ -31,7 +30,7 @@ import { IEditorViewState } from 'vs/editor/common/editorCommon';
3130
import { getCodeEditor } from 'vs/editor/browser/editorBrowser';
3231
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
3332
import { KeyMod, KeyCode, KeyChord } from 'vs/base/common/keyCodes';
34-
import { isWindows, isMacintosh, isLinux } from 'vs/base/common/platform';
33+
import { isWindows, isMacintosh } from 'vs/base/common/platform';
3534
import { ITextModelService } from 'vs/editor/common/services/resolverService';
3635
import { sequence } from 'vs/base/common/async';
3736
import { getResourceForCommand, getMultiSelectedResources } from 'vs/workbench/parts/files/browser/files';
@@ -42,8 +41,7 @@ import { INotificationService } from 'vs/platform/notification/common/notificati
4241
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
4342
import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
4443
import { IEditorGroupsService } from 'vs/workbench/services/group/common/editorGroupsService';
45-
import { isEqual, basenameOrAuthority } from 'vs/base/common/resources';
46-
import { ltrim } from 'vs/base/common/strings';
44+
import { IUriLabelService } from 'vs/platform/uriLabel/common/uriLabel';
4745

4846
// Commands
4947

@@ -389,28 +387,12 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
389387
}
390388
});
391389

392-
function resourcesToClipboard(resources: URI[], clipboardService: IClipboardService, notificationService: INotificationService, contextService?: IWorkspaceContextService): void {
390+
function resourcesToClipboard(resources: URI[], relative: boolean, clipboardService: IClipboardService, notificationService: INotificationService, uriLabelService: IUriLabelService): void {
393391
if (resources.length) {
394392
const lineDelimiter = isWindows ? '\r\n' : '\n';
395393

396-
const text = resources.map(resource => {
397-
if (contextService) {
398-
const workspaceFolder = contextService.getWorkspaceFolder(resource);
399-
if (workspaceFolder) {
400-
if (isEqual(workspaceFolder.uri, resource, !isLinux)) {
401-
return basenameOrAuthority(workspaceFolder.uri);
402-
}
403-
404-
return paths.normalize(ltrim(resource.path.substr(workspaceFolder.uri.path.length), paths.sep), true);
405-
}
406-
}
407-
408-
if (resource.scheme === Schemas.file) {
409-
return paths.normalize(labels.normalizeDriveLetter(resource.fsPath), true);
410-
}
411-
412-
return resource.toString();
413-
}).join(lineDelimiter);
394+
const text = resources.map(resource => uriLabelService.getLabel(resource, relative, true))
395+
.join(lineDelimiter);
414396
clipboardService.writeText(text);
415397
} else {
416398
notificationService.info(nls.localize('openFileToCopy', "Open a file first to copy its path"));
@@ -427,7 +409,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
427409
id: COPY_PATH_COMMAND_ID,
428410
handler: (accessor, resource: URI | object) => {
429411
const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IEditorService));
430-
resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService));
412+
resourcesToClipboard(resources, false, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IUriLabelService));
431413
}
432414
});
433415

@@ -441,7 +423,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
441423
id: COPY_RELATIVE_PATH_COMMAND_ID,
442424
handler: (accessor, resource: URI | object) => {
443425
const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IEditorService));
444-
resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IWorkspaceContextService));
426+
resourcesToClipboard(resources, true, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IUriLabelService));
445427
}
446428
});
447429

@@ -454,7 +436,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
454436
const editorService = accessor.get(IEditorService);
455437
const activeInput = editorService.activeEditor;
456438
const resources = activeInput && activeInput.getResource() ? [activeInput.getResource()] : [];
457-
resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService));
439+
resourcesToClipboard(resources, false, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IUriLabelService));
458440
}
459441
});
460442

0 commit comments

Comments
 (0)