提交 e0195d75 编写于 作者: I isidor

simplify copy path command

上级 1df5a2f9
......@@ -16,7 +16,7 @@ import { ltrim } from 'vs/base/common/strings';
export interface IUriLabelService {
_serviceBrand: any;
getLabel(resource: URI, relative?: boolean): string;
getLabel(resource: URI, relative?: boolean, forceNoTildify?: boolean): string;
registerFormater(schema: string, formater: UriLabelRules): IDisposable;
onDidRegisterFormater: Event<{ scheme: string, formater: UriLabelRules }>;
}
......@@ -52,7 +52,7 @@ export class UriLabelService implements IUriLabelService {
return this._onDidRegisterFormater.event;
}
getLabel(resource: URI, relative: boolean): string {
getLabel(resource: URI, relative: boolean, forceNoTildify?: boolean): string {
if (!resource) {
return undefined;
}
......@@ -68,8 +68,8 @@ export class UriLabelService implements IUriLabelService {
if (isEqual(baseResource.uri, resource, !isLinux)) {
relativeLabel = ''; // no label if resources are identical
} else {
const baseResourceLabel = this.formatUri(baseResource.uri, formater);
relativeLabel = ltrim(this.formatUri(resource, formater).substring(baseResourceLabel.length), formater.separator);
const baseResourceLabel = this.formatUri(baseResource.uri, formater, forceNoTildify);
relativeLabel = ltrim(this.formatUri(resource, formater, forceNoTildify).substring(baseResourceLabel.length), formater.separator);
}
const hasMultipleRoots = this.contextService.getWorkspace().folders.length > 1;
......@@ -82,7 +82,7 @@ export class UriLabelService implements IUriLabelService {
}
}
return this.formatUri(resource, formater);
return this.formatUri(resource, formater, forceNoTildify);
}
registerFormater(scheme: string, formater: UriLabelRules): IDisposable {
......@@ -94,7 +94,7 @@ export class UriLabelService implements IUriLabelService {
};
}
private formatUri(resource: URI, formater: UriLabelRules): string {
private formatUri(resource: URI, formater: UriLabelRules, forceNoTildify: boolean): string {
let label = formater.label.replace(labelMatchingRegexp, match => {
switch (match) {
case '${scheme}': return resource.scheme;
......@@ -109,7 +109,7 @@ export class UriLabelService implements IUriLabelService {
label = label.charAt(1).toUpperCase() + label.substr(2);
}
if (formater.tildify) {
if (formater.tildify && !forceNoTildify) {
label = tildify(label, this.environmentService.userHome);
}
......
......@@ -8,7 +8,6 @@
import * as nls from 'vs/nls';
import * as paths from 'vs/base/common/paths';
import { TPromise } from 'vs/base/common/winjs.base';
import * as labels from 'vs/base/common/labels';
import URI from 'vs/base/common/uri';
import { toResource, IEditorCommandsContext } from 'vs/workbench/common/editor';
import { IWindowsService, IWindowService } from 'vs/platform/windows/common/windows';
......@@ -31,7 +30,7 @@ import { IEditorViewState } from 'vs/editor/common/editorCommon';
import { getCodeEditor } from 'vs/editor/browser/editorBrowser';
import { KeybindingsRegistry, KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { KeyMod, KeyCode, KeyChord } from 'vs/base/common/keyCodes';
import { isWindows, isMacintosh, isLinux } from 'vs/base/common/platform';
import { isWindows, isMacintosh } from 'vs/base/common/platform';
import { ITextModelService } from 'vs/editor/common/services/resolverService';
import { sequence } from 'vs/base/common/async';
import { getResourceForCommand, getMultiSelectedResources } from 'vs/workbench/parts/files/browser/files';
......@@ -42,8 +41,7 @@ import { INotificationService } from 'vs/platform/notification/common/notificati
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
import { IEditorGroupsService } from 'vs/workbench/services/group/common/editorGroupsService';
import { isEqual, basenameOrAuthority } from 'vs/base/common/resources';
import { ltrim } from 'vs/base/common/strings';
import { IUriLabelService } from 'vs/platform/uriLabel/common/uriLabel';
// Commands
......@@ -389,28 +387,12 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
}
});
function resourcesToClipboard(resources: URI[], clipboardService: IClipboardService, notificationService: INotificationService, contextService?: IWorkspaceContextService): void {
function resourcesToClipboard(resources: URI[], relative: boolean, clipboardService: IClipboardService, notificationService: INotificationService, uriLabelService: IUriLabelService): void {
if (resources.length) {
const lineDelimiter = isWindows ? '\r\n' : '\n';
const text = resources.map(resource => {
if (contextService) {
const workspaceFolder = contextService.getWorkspaceFolder(resource);
if (workspaceFolder) {
if (isEqual(workspaceFolder.uri, resource, !isLinux)) {
return basenameOrAuthority(workspaceFolder.uri);
}
return paths.normalize(ltrim(resource.path.substr(workspaceFolder.uri.path.length), paths.sep), true);
}
}
if (resource.scheme === Schemas.file) {
return paths.normalize(labels.normalizeDriveLetter(resource.fsPath), true);
}
return resource.toString();
}).join(lineDelimiter);
const text = resources.map(resource => uriLabelService.getLabel(resource, relative, true))
.join(lineDelimiter);
clipboardService.writeText(text);
} else {
notificationService.info(nls.localize('openFileToCopy', "Open a file first to copy its path"));
......@@ -427,7 +409,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
id: COPY_PATH_COMMAND_ID,
handler: (accessor, resource: URI | object) => {
const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IEditorService));
resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService));
resourcesToClipboard(resources, false, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IUriLabelService));
}
});
......@@ -441,7 +423,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
id: COPY_RELATIVE_PATH_COMMAND_ID,
handler: (accessor, resource: URI | object) => {
const resources = getMultiSelectedResources(resource, accessor.get(IListService), accessor.get(IEditorService));
resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IWorkspaceContextService));
resourcesToClipboard(resources, true, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IUriLabelService));
}
});
......@@ -454,7 +436,7 @@ KeybindingsRegistry.registerCommandAndKeybindingRule({
const editorService = accessor.get(IEditorService);
const activeInput = editorService.activeEditor;
const resources = activeInput && activeInput.getResource() ? [activeInput.getResource()] : [];
resourcesToClipboard(resources, accessor.get(IClipboardService), accessor.get(INotificationService));
resourcesToClipboard(resources, false, accessor.get(IClipboardService), accessor.get(INotificationService), accessor.get(IUriLabelService));
}
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册