commands.ts 3.1 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

J
Joao Moreno 已提交
8 9
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource } from 'vscode';
import { Model, Resource, ResourceGroup } from './model';
J
Joao Moreno 已提交
10 11
import { log } from './util';

J
Joao Moreno 已提交
12 13
type Command = (...args: any[]) => any;

J
Joao Moreno 已提交
14 15 16
function refresh(model: Model): void {
	log('refresh');
	model.update();
J
Joao Moreno 已提交
17
}
J
Joao Moreno 已提交
18

J
Joao Moreno 已提交
19
function openChange(model: Model, resource: Resource): void {
J
Joao Moreno 已提交
20
	log('open change', resource);
J
Joao Moreno 已提交
21 22
}

J
Joao Moreno 已提交
23
function openFile(model: Model, resource: Resource): void {
J
Joao Moreno 已提交
24
	log('open file', resource);
J
Joao Moreno 已提交
25 26
}

J
Joao Moreno 已提交
27 28
async function stage(model: Model, resource: Resource): Promise<void> {
	return await model.stage(resource);
J
Joao Moreno 已提交
29 30
}

J
Joao Moreno 已提交
31 32
async function stageAll(model: Model): Promise<void> {
	return await model.stage();
J
Joao Moreno 已提交
33 34
}

J
Joao Moreno 已提交
35 36
async function unstage(model: Model, resource: Resource): Promise<void> {
	return await model.unstage(resource);
J
Joao Moreno 已提交
37 38
}

J
Joao Moreno 已提交
39 40
async function unstageAll(model: Model): Promise<void> {
	return await model.unstage();
J
Joao Moreno 已提交
41 42
}

J
Joao Moreno 已提交
43
function clean(model: Model, resource: Resource): void {
J
Joao Moreno 已提交
44 45 46
	log('clean', resource);
}

J
Joao Moreno 已提交
47
function cleanAll(model: Model, resourceGroup: ResourceGroup): void {
J
Joao Moreno 已提交
48 49
	log('clean all', resourceGroup);
}
J
Joao Moreno 已提交
50

J
Joao Moreno 已提交
51 52 53 54 55 56 57 58
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);
}

J
Joao Moreno 已提交
59 60 61 62 63
// TODO: do more with these errors
function catchErrors<T, R>(command: (t: T) => Promise<R>): (t: T) => void {
	return t => command(t).catch(err => console.error(err));
}

J
Joao Moreno 已提交
64 65
function compose(command: Command, ...args: Function[]): Command {
	return args.reduce((r, fn) => fn(r), command) as Command;
J
Joao Moreno 已提交
66 67
}

68
export function registerCommands(model: Model): Disposable {
J
Joao Moreno 已提交
69 70
	const bindModel = command => (...args: any[]) => command(model, ...args);

J
Joao Moreno 已提交
71
	const disposables = [
J
Joao Moreno 已提交
72 73 74
		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)),
J
Joao Moreno 已提交
75 76 77 78
		commands.registerCommand('git.stage', compose(stage, bindModel, resolveURI, skipUndefined, catchErrors)),
		commands.registerCommand('git.stageAll', compose(stageAll, bindModel, catchErrors)),
		commands.registerCommand('git.unstage', compose(unstage, bindModel, resolveURI, skipUndefined, catchErrors)),
		commands.registerCommand('git.unstageAll', compose(unstageAll, bindModel, catchErrors)),
J
Joao Moreno 已提交
79 80
		commands.registerCommand('git.clean', compose(clean, bindModel, resolveURI, skipUndefined)),
		commands.registerCommand('git.cleanAll', compose(cleanAll, bindModel, resolveURI, skipUndefined)),
J
Joao Moreno 已提交
81 82 83
	];

	return Disposable.from(...disposables);
J
Joao Moreno 已提交
84
}