提交 67f09910 编写于 作者: J Joao Moreno

scm: getResourceFromURI

上级 992704e8
......@@ -5,62 +5,74 @@
'use strict';
import { commands, Disposable, SCMResourceGroup, SCMResource } from 'vscode';
import { Model } from './model';
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource } from 'vscode';
import { Model, Resource, ResourceGroup } from './model';
import { log } from './util';
type Command = (...args: any[]) => any;
function refresh(model: Model): void {
log('refresh');
model.update();
}
function openChange(model: Model, resource: SCMResource): void {
function openChange(model: Model, resource: Resource): void {
log('open change', resource);
}
function openFile(model: Model, resource: SCMResource): void {
function openFile(model: Model, resource: Resource): void {
log('open file', resource);
}
function stage(model: Model, resource: SCMResource): void {
function stage(model: Model, resource: Resource): void {
log('stage', resource);
}
function stageAll(model: Model, resourceGroup: SCMResourceGroup): void {
function stageAll(model: Model, resourceGroup: ResourceGroup): void {
log('stageAll', resourceGroup);
}
function unstage(model: Model, resource: SCMResource): void {
function unstage(model: Model, resource: Resource): void {
log('unstage', resource);
}
function unstageAll(model: Model, resourceGroup: SCMResourceGroup): void {
function unstageAll(model: Model, resourceGroup: ResourceGroup): void {
log('unstageAll', resourceGroup);
}
function clean(model: Model, resource: SCMResource): void {
function clean(model: Model, resource: Resource): void {
log('clean', resource);
}
function cleanAll(model: Model, resourceGroup: SCMResourceGroup): void {
function cleanAll(model: Model, resourceGroup: ResourceGroup): void {
log('clean all', resourceGroup);
}
function bind(command: (model: Model, ...args: any[]) => any, model: Model): (...args: any[]) => any {
return command.bind(null, model);
function resolveURI<R>(command: (t: SCMResource | SCMResourceGroup | undefined) => R): (uri: Uri) => R | undefined {
return uri => uri.authority !== 'git' ? undefined : command(scm.getResourceFromURI(uri));
}
function skipUndefined<T, R>(command: (t: T) => R): (t: T | undefined) => R | undefined {
return t => t === undefined ? undefined : command(t);
}
function compose(command: Command, ...args: Function[]): Command {
return args.reduce((r, fn) => fn(r), command) as Command;
}
export function registerCommands(model: Model): Disposable {
const bindModel = command => (...args: any[]) => command(model, ...args);
const disposables = [
commands.registerCommand('git.refresh', bind(refresh, model)),
commands.registerCommand('git.openChange', bind(openChange, model)),
commands.registerCommand('git.openFile', bind(openFile, model)),
commands.registerCommand('git.stage', bind(stage, model)),
commands.registerCommand('git.stageAll', bind(stageAll, model)),
commands.registerCommand('git.unstage', bind(unstage, model)),
commands.registerCommand('git.unstageAll', bind(unstageAll, model)),
commands.registerCommand('git.clean', bind(clean, model)),
commands.registerCommand('git.cleanAll', bind(cleanAll, model)),
commands.registerCommand('git.refresh', compose(refresh, bindModel)),
commands.registerCommand('git.openChange', compose(openChange, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.openFile', compose(openFile, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.stage', compose(stage, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.stageAll', compose(stageAll, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.unstage', compose(unstage, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.unstageAll', compose(unstageAll, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.clean', compose(clean, bindModel, resolveURI, skipUndefined)),
commands.registerCommand('git.cleanAll', compose(cleanAll, bindModel, resolveURI, skipUndefined)),
];
return Disposable.from(...disposables);
......
......@@ -5,10 +5,141 @@
'use strict';
import { EventEmitter, Event } from 'vscode';
import { Uri, EventEmitter, Event, SCMResource, SCMResourceDecorations, SCMResourceGroup } from 'vscode';
import { Repository, IRef, IFileStatus, IRemote } from './git';
import { throttle } from './util';
import { decorate, debounce } from 'core-decorators';
import * as path from 'path';
const iconsRootPath = path.join(path.dirname(__dirname), 'resources', 'icons');
function getIconUri(iconName: string, theme: string): Uri {
return Uri.file(path.join(iconsRootPath, theme, `${iconName}.svg`));
}
export enum Status {
INDEX_MODIFIED,
INDEX_ADDED,
INDEX_DELETED,
INDEX_RENAMED,
INDEX_COPIED,
MODIFIED,
DELETED,
UNTRACKED,
IGNORED,
ADDED_BY_US,
ADDED_BY_THEM,
DELETED_BY_US,
DELETED_BY_THEM,
BOTH_ADDED,
BOTH_DELETED,
BOTH_MODIFIED
}
export class Resource implements SCMResource {
get uri(): Uri { return this._uri; }
get type(): Status { return this._type; }
private static Icons = {
light: {
Modified: getIconUri('status-modified', 'light'),
Added: getIconUri('status-added', 'light'),
Deleted: getIconUri('status-deleted', 'light'),
Renamed: getIconUri('status-renamed', 'light'),
Copied: getIconUri('status-copied', 'light'),
Untracked: getIconUri('status-untracked', 'light'),
Ignored: getIconUri('status-ignored', 'light'),
Conflict: getIconUri('status-conflict', 'light'),
},
dark: {
Modified: getIconUri('status-modified', 'dark'),
Added: getIconUri('status-added', 'dark'),
Deleted: getIconUri('status-deleted', 'dark'),
Renamed: getIconUri('status-renamed', 'dark'),
Copied: getIconUri('status-copied', 'dark'),
Untracked: getIconUri('status-untracked', 'dark'),
Ignored: getIconUri('status-ignored', 'dark'),
Conflict: getIconUri('status-conflict', 'dark')
}
};
private getIconPath(theme: string): Uri | undefined {
switch (this.type) {
case Status.INDEX_MODIFIED: return Resource.Icons[theme].Modified;
case Status.MODIFIED: return Resource.Icons[theme].Modified;
case Status.INDEX_ADDED: return Resource.Icons[theme].Added;
case Status.INDEX_DELETED: return Resource.Icons[theme].Deleted;
case Status.DELETED: return Resource.Icons[theme].Deleted;
case Status.INDEX_RENAMED: return Resource.Icons[theme].Renamed;
case Status.INDEX_COPIED: return Resource.Icons[theme].Copied;
case Status.UNTRACKED: return Resource.Icons[theme].Untracked;
case Status.IGNORED: return Resource.Icons[theme].Ignored;
case Status.BOTH_DELETED: return Resource.Icons[theme].Conflict;
case Status.ADDED_BY_US: return Resource.Icons[theme].Conflict;
case Status.DELETED_BY_THEM: return Resource.Icons[theme].Conflict;
case Status.ADDED_BY_THEM: return Resource.Icons[theme].Conflict;
case Status.DELETED_BY_US: return Resource.Icons[theme].Conflict;
case Status.BOTH_ADDED: return Resource.Icons[theme].Conflict;
case Status.BOTH_MODIFIED: return Resource.Icons[theme].Conflict;
default: return void 0;
}
}
private get strikeThrough(): boolean {
switch (this.type) {
case Status.DELETED:
case Status.BOTH_DELETED:
case Status.DELETED_BY_THEM:
case Status.DELETED_BY_US:
return true;
default:
return false;
}
}
get decorations(): SCMResourceDecorations {
const light = { iconPath: this.getIconPath('light') };
const dark = { iconPath: this.getIconPath('dark') };
return { strikeThrough: this.strikeThrough, light, dark };
}
constructor(private _uri: Uri, private _type: Status) {
}
}
export class ResourceGroup implements SCMResourceGroup {
get id(): string { return this._id; }
get label(): string { return this._label; }
get resources(): SCMResource[] { return this._resources; }
constructor(private _id: string, private _label: string, private _resources: SCMResource[]) {
}
}
export class MergeGroup extends ResourceGroup {
constructor(resources: SCMResource[]) {
super('merge', 'Merge Changes', resources);
}
}
export class IndexGroup extends ResourceGroup {
constructor(resources: SCMResource[]) {
super('index', 'Staged Changes', resources);
}
}
export class WorkingTreeGroup extends ResourceGroup {
constructor(resources: SCMResource[]) {
super('workingTree', 'Changes', resources);
}
}
export class Model {
......
......@@ -6,142 +6,12 @@
'use strict';
import {
Uri, Disposable, SCMProvider, SCMResource, SCMResourceDecorations,
Uri, Disposable, SCMProvider, SCMResource,
SCMResourceGroup, EventEmitter, Event, commands
} from 'vscode';
import { Model } from './model';
import { Model, Status, WorkingTreeGroup, IndexGroup, MergeGroup, Resource, ResourceGroup } from './model';
import * as path from 'path';
const iconsRootPath = path.join(path.dirname(__dirname), 'resources', 'icons');
function getIconUri(iconName: string, theme: string): Uri {
return Uri.file(path.join(iconsRootPath, theme, `${iconName}.svg`));
}
export enum Status {
INDEX_MODIFIED,
INDEX_ADDED,
INDEX_DELETED,
INDEX_RENAMED,
INDEX_COPIED,
MODIFIED,
DELETED,
UNTRACKED,
IGNORED,
ADDED_BY_US,
ADDED_BY_THEM,
DELETED_BY_US,
DELETED_BY_THEM,
BOTH_ADDED,
BOTH_DELETED,
BOTH_MODIFIED
}
export class Resource implements SCMResource {
get uri(): Uri { return this._uri; }
get type(): Status { return this._type; }
private static Icons = {
light: {
Modified: getIconUri('status-modified', 'light'),
Added: getIconUri('status-added', 'light'),
Deleted: getIconUri('status-deleted', 'light'),
Renamed: getIconUri('status-renamed', 'light'),
Copied: getIconUri('status-copied', 'light'),
Untracked: getIconUri('status-untracked', 'light'),
Ignored: getIconUri('status-ignored', 'light'),
Conflict: getIconUri('status-conflict', 'light'),
},
dark: {
Modified: getIconUri('status-modified', 'dark'),
Added: getIconUri('status-added', 'dark'),
Deleted: getIconUri('status-deleted', 'dark'),
Renamed: getIconUri('status-renamed', 'dark'),
Copied: getIconUri('status-copied', 'dark'),
Untracked: getIconUri('status-untracked', 'dark'),
Ignored: getIconUri('status-ignored', 'dark'),
Conflict: getIconUri('status-conflict', 'dark')
}
};
private getIconPath(theme: string): Uri | undefined {
switch (this.type) {
case Status.INDEX_MODIFIED: return Resource.Icons[theme].Modified;
case Status.MODIFIED: return Resource.Icons[theme].Modified;
case Status.INDEX_ADDED: return Resource.Icons[theme].Added;
case Status.INDEX_DELETED: return Resource.Icons[theme].Deleted;
case Status.DELETED: return Resource.Icons[theme].Deleted;
case Status.INDEX_RENAMED: return Resource.Icons[theme].Renamed;
case Status.INDEX_COPIED: return Resource.Icons[theme].Copied;
case Status.UNTRACKED: return Resource.Icons[theme].Untracked;
case Status.IGNORED: return Resource.Icons[theme].Ignored;
case Status.BOTH_DELETED: return Resource.Icons[theme].Conflict;
case Status.ADDED_BY_US: return Resource.Icons[theme].Conflict;
case Status.DELETED_BY_THEM: return Resource.Icons[theme].Conflict;
case Status.ADDED_BY_THEM: return Resource.Icons[theme].Conflict;
case Status.DELETED_BY_US: return Resource.Icons[theme].Conflict;
case Status.BOTH_ADDED: return Resource.Icons[theme].Conflict;
case Status.BOTH_MODIFIED: return Resource.Icons[theme].Conflict;
default: return void 0;
}
}
private get strikeThrough(): boolean {
switch (this.type) {
case Status.DELETED:
case Status.BOTH_DELETED:
case Status.DELETED_BY_THEM:
case Status.DELETED_BY_US:
return true;
default:
return false;
}
}
get decorations(): SCMResourceDecorations {
const light = { iconPath: this.getIconPath('light') };
const dark = { iconPath: this.getIconPath('dark') };
return { strikeThrough: this.strikeThrough, light, dark };
}
constructor(private _uri: Uri, private _type: Status) {
}
}
export class ResourceGroup implements SCMResourceGroup {
get id(): string { return this._id; }
get label(): string { return this._label; }
get resources(): SCMResource[] { return this._resources; }
constructor(private _id: string, private _label: string, private _resources: SCMResource[]) {
}
}
export class MergeGroup extends ResourceGroup {
constructor(resources: SCMResource[]) {
super('merge', 'Merge Changes', resources);
}
}
export class IndexGroup extends ResourceGroup {
constructor(resources: SCMResource[]) {
super('index', 'Staged Changes', resources);
}
}
export class WorkingTreeGroup extends ResourceGroup {
constructor(resources: SCMResource[]) {
super('workingTree', 'Changes', resources);
}
}
export class GitSCMProvider implements SCMProvider {
private disposables: Disposable[] = [];
......
......@@ -121,6 +121,7 @@ declare module 'vscode' {
export namespace scm {
export const onDidChangeActiveProvider: Event<SCMProvider>;
export let activeProvider: SCMProvider | undefined;
export function getResourceFromURI(uri: Uri): SCMResource | SCMResourceGroup | undefined;
export function registerSCMProvider(id: string, provider: SCMProvider): Disposable;
}
}
......@@ -399,6 +399,11 @@ export function createApiFactory(initData: IInitData, threadService: IThreadServ
return extHostSCM.onDidChangeActiveProvider;
}
@proposed(extension)
getResourceFromURI(uri) {
return extHostSCM.getResourceFromURI(uri);
}
@proposed(extension)
registerSCMProvider(id, provider) {
return extHostSCM.registerSCMProvider(id, provider);
......
......@@ -15,7 +15,7 @@ import * as vscode from 'vscode';
function getIconPath(decorations: vscode.SCMResourceThemableDecorations) {
if (!decorations) {
return void 0;
return undefined;
} else if (typeof decorations.iconPath === 'string') {
return URI.file(decorations.iconPath).toString();
} else if (decorations.iconPath) {
......@@ -24,9 +24,11 @@ function getIconPath(decorations: vscode.SCMResourceThemableDecorations) {
}
export interface Cache {
[groupId: string]: {
resourceGroup: vscode.SCMResourceGroup,
resources: { [uri: string]: vscode.SCMResource }
[providerId: string]: {
[groupId: string]: {
resourceGroup: vscode.SCMResourceGroup,
resources: { [uri: string]: vscode.SCMResource }
};
};
}
......@@ -47,15 +49,69 @@ export class ExtHostSCM {
this._proxy = threadService.get(MainContext.MainThreadSCM);
}
registerSCMProvider(id: string, provider: vscode.SCMProvider): Disposable {
if (this._providers[id]) {
throw new Error(`Provider ${id} already registered`);
getResourceFromURI(uri: vscode.Uri): vscode.SCMResource | vscode.SCMResourceGroup | undefined {
if (uri.scheme !== 'scm') {
return undefined;
}
const providerId = uri.authority;
const providerCache = this.cache[providerId];
if (!providerCache) {
return undefined;
}
const match = /^\/([^/]+)(\/(.*))?$/.exec(uri.path);
if (!match) {
return undefined;
}
const resourceGroupId = match[1];
const resourceGroupRef = providerCache[resourceGroupId];
if (!resourceGroupRef) {
return undefined;
}
const rawResourceUri = match[3];
if (!rawResourceUri) {
return resourceGroupRef.resourceGroup;
}
let resourceUri: string;
try {
const rawResource = JSON.parse(rawResourceUri);
const resource = URI.from(rawResource);
resourceUri = resource.toString();
} catch (err) {
resourceUri = undefined;
}
if (!resourceUri) {
return undefined;
}
const resource = resourceGroupRef.resources[resourceUri];
if (!resource) {
return undefined;
}
return resource;
}
registerSCMProvider(providerId: string, provider: vscode.SCMProvider): Disposable {
if (this._providers[providerId]) {
throw new Error(`Provider ${providerId} already registered`);
}
// TODO@joao: should pluck all the things out of the provider
this._providers[id] = provider;
this._providers[providerId] = provider;
this._proxy.$register(id, {
this._proxy.$register(providerId, {
label: provider.label,
supportsCommit: !!provider.commit,
supportsOpen: !!provider.open,
......@@ -65,7 +121,7 @@ export class ExtHostSCM {
const onDidChange = debounceEvent(provider.onDidChange, (l, e) => e, 100);
const onDidChangeListener = onDidChange(resourceGroups => {
this.cache = Object.create(null);
this.cache[providerId] = Object.create(null);
const rawResourceGroups = resourceGroups.map(g => {
const resources: { [id: string]: vscode.SCMResource; } = Object.create(null);
......@@ -91,23 +147,23 @@ export class ExtHostSCM {
return [uri, icons, strikeThrough] as SCMRawResource;
});
this.cache[g.id] = { resourceGroup: g, resources };
this.cache[providerId][g.id] = { resourceGroup: g, resources };
return [g.id, g.label, rawResources] as SCMRawResourceGroup;
});
this._proxy.$onChange(id, rawResourceGroups);
this._proxy.$onChange(providerId, rawResourceGroups);
});
return new Disposable(() => {
onDidChangeListener.dispose();
delete this._providers[id];
this._proxy.$unregister(id);
delete this._providers[providerId];
this._proxy.$unregister(providerId);
});
}
$commit(id: string, message: string): TPromise<void> {
const provider = this._providers[id];
$commit(providerId: string, message: string): TPromise<void> {
const provider = this._providers[providerId];
if (!provider) {
return TPromise.as(null);
......@@ -116,14 +172,15 @@ export class ExtHostSCM {
return asWinJsPromise(token => provider.commit(message, token));
}
$open(id: string, resourceGroupId: string, uri: string): TPromise<void> {
const provider = this._providers[id];
$open(providerId: string, resourceGroupId: string, uri: string): TPromise<void> {
const provider = this._providers[providerId];
if (!provider) {
return TPromise.as(null);
}
const resourceGroup = this.cache[resourceGroupId];
const providerCache = this.cache[providerId];
const resourceGroup = providerCache[resourceGroupId];
const resource = resourceGroup && resourceGroup.resources[uri];
if (!resource) {
......@@ -133,16 +190,17 @@ export class ExtHostSCM {
return asWinJsPromise(token => provider.open(resource, token));
}
$drag(id: string, fromResourceGroupId: string, fromUri: string, toResourceGroupId: string): TPromise<void> {
const provider = this._providers[id];
$drag(providerId: string, fromResourceGroupId: string, fromUri: string, toResourceGroupId: string): TPromise<void> {
const provider = this._providers[providerId];
if (!provider) {
return TPromise.as(null);
}
const fromResourceGroup = this.cache[fromResourceGroupId];
const providerCache = this.cache[providerId];
const fromResourceGroup = providerCache[fromResourceGroupId];
const resource = fromResourceGroup && fromResourceGroup.resources[fromUri];
const toResourceGroup = this.cache[toResourceGroupId];
const toResourceGroup = providerCache[toResourceGroupId];
const resourceGroup = toResourceGroup && toResourceGroup.resourceGroup;
if (!resource || !resourceGroup) {
......
......@@ -10,9 +10,9 @@ import { IDisposable, dispose, empty as EmptyDisposable, toDisposable } from 'vs
import { IContextKeyService, IContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import { IAction } from 'vs/base/common/actions';
import URI from 'vs/base/common/uri';
import { fillInActions } from 'vs/platform/actions/browser/menuItemActionItem';
import { ISCMService, ISCMProvider } from 'vs/workbench/services/scm/common/scm';
import { ISCMService, ISCMProvider, ISCMResource, ISCMResourceGroup } from 'vs/workbench/services/scm/common/scm';
export class SCMMenus implements IDisposable {
......@@ -67,25 +67,41 @@ export class SCMMenus implements IDisposable {
return this.titleSecondaryActions;
}
getResourceGroupActions(resourceGroupId: string): IAction[] {
return this.getActions(MenuId.SCMResourceGroupContext, resourceGroupId).primary;
getResourceGroupActions(group: ISCMResourceGroup): IAction[] {
return this.getActions(MenuId.SCMResourceGroupContext, this.getSCMResourceGroupURI(group), group.id).primary;
}
getResourceGroupContextActions(group: ISCMResourceGroup): IAction[] {
return this.getActions(MenuId.SCMResourceGroupContext, this.getSCMResourceGroupURI(group), group.id).secondary;
}
getResourceGroupContextActions(resourceGroupId: string): IAction[] {
return this.getActions(MenuId.SCMResourceGroupContext, resourceGroupId).secondary;
getResourceActions(resource: ISCMResource): IAction[] {
return this.getActions(MenuId.SCMResourceContext, this.getSCMResourceURI(resource), resource.resourceGroupId).primary;
}
getResourceActions(resourceGroupId: string): IAction[] {
return this.getActions(MenuId.SCMResourceContext, resourceGroupId).primary;
getResourceContextActions(resource: ISCMResource): IAction[] {
return this.getActions(MenuId.SCMResourceContext, this.getSCMResourceURI(resource), resource.resourceGroupId).secondary;
}
getResourceContextActions(resourceGroupId: string): IAction[] {
return this.getActions(MenuId.SCMResourceContext, resourceGroupId).secondary;
private getSCMResourceGroupURI(resourceGroup: ISCMResourceGroup): URI {
return URI.from({
scheme: 'scm',
authority: this.activeProviderContextKey.get(),
path: `/${resourceGroup.id}`
});
}
private getSCMResourceURI(resource: ISCMResource): URI {
return URI.from({
scheme: 'scm',
authority: this.activeProviderContextKey.get(),
path: `/${resource.resourceGroupId}/${JSON.stringify(resource.uri)}`
});
}
private static readonly NoActions = { primary: [], secondary: [] };
private getActions(menuId: MenuId, resourceGroupId: string): { primary: IAction[]; secondary: IAction[]; } {
private getActions(menuId: MenuId, context: URI, resourceGroupId: string): { primary: IAction[]; secondary: IAction[]; } {
if (!this.scmService.activeProvider) {
return SCMMenus.NoActions;
}
......@@ -97,7 +113,7 @@ export class SCMMenus implements IDisposable {
const primary = [];
const secondary = [];
const result = { primary, secondary };
fillInActions(menu, null, result, g => g === 'inline');
fillInActions(menu, context, result, g => g === 'inline');
menu.dispose();
contextKeyService.dispose();
......
......@@ -74,7 +74,7 @@ class ResourceGroupRenderer implements IRenderer<ISCMResourceGroup, ResourceGrou
template.name.textContent = group.label;
template.count.setCount(group.resources.length);
template.actionBar.clear();
template.actionBar.push(this.scmMenus.getResourceGroupActions(group.id));
template.actionBar.push(this.scmMenus.getResourceGroupActions(group));
}
disposeTemplate(template: ResourceGroupTemplate): void {
......@@ -117,7 +117,7 @@ class ResourceRenderer implements IRenderer<ISCMResource, ResourceTemplate> {
renderElement(resource: ISCMResource, index: number, template: ResourceTemplate): void {
template.fileLabel.setFile(resource.uri);
template.actionBar.clear();
template.actionBar.push(this.scmMenus.getResourceActions(resource.resourceGroupId));
template.actionBar.push(this.scmMenus.getResourceActions(resource));
toggleClass(template.name, 'strike-through', resource.decorations.strikeThrough);
const theme = this.themeService.getColorTheme();
......@@ -299,10 +299,10 @@ export class SCMViewlet extends Viewlet {
if ((element as ISCMResource).uri) {
const resource = element as ISCMResource;
actions = this.menus.getResourceContextActions(resource.resourceGroupId);
actions = this.menus.getResourceContextActions(resource);
} else {
const resourceGroup = element as ISCMResourceGroup;
actions = this.menus.getResourceGroupContextActions(resourceGroup.id);
actions = this.menus.getResourceGroupContextActions(resourceGroup);
}
this.contextMenuService.showContextMenu({
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册