commands.ts 3.0 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
function resolveURI<R>(command: (t: SCMResource | SCMResourceGroup | undefined) => R): (uri: Uri) => R | undefined {
J
Joao Moreno 已提交
52 53 54 55 56 57 58 59 60 61
	return uri => {
		if (uri.authority !== 'git') {
			return;
		}

		const result = scm.getResourceFromURI(uri);

		if (!result) {
			return;
		}
J
Joao Moreno 已提交
62

J
Joao Moreno 已提交
63 64
		return command(result);
	};
J
Joao Moreno 已提交
65 66
}

J
Joao Moreno 已提交
67
// TODO: do more with these errors
J
Joao Moreno 已提交
68 69
function catchErrors<T, R>(command: (...args: any[]) => Promise<R>): (...args: any[]) => void {
	return (...args) => command(...args).catch(err => console.error(err));
J
Joao Moreno 已提交
70 71
}

J
Joao Moreno 已提交
72 73
function compose(command: Command, ...args: Function[]): Command {
	return args.reduce((r, fn) => fn(r), command) as Command;
J
Joao Moreno 已提交
74 75
}

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

J
Joao Moreno 已提交
79
	const disposables = [
J
Joao Moreno 已提交
80
		commands.registerCommand('git.refresh', compose(refresh, bindModel)),
J
Joao Moreno 已提交
81 82 83 84 85 86 87 88
		commands.registerCommand('git.openChange', compose(openChange, bindModel, resolveURI)),
		commands.registerCommand('git.openFile', compose(openFile, bindModel, resolveURI)),
		commands.registerCommand('git.stage', compose(stage, catchErrors, bindModel, resolveURI)),
		commands.registerCommand('git.stageAll', compose(stageAll, catchErrors, bindModel)),
		commands.registerCommand('git.unstage', compose(unstage, catchErrors, bindModel, resolveURI)),
		commands.registerCommand('git.unstageAll', compose(unstageAll, catchErrors, bindModel)),
		commands.registerCommand('git.clean', compose(clean, bindModel, resolveURI)),
		commands.registerCommand('git.cleanAll', compose(cleanAll, bindModel, resolveURI)),
J
Joao Moreno 已提交
89 90 91
	];

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