-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
81 lines (68 loc) · 2.94 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
function getRelativeItemPath(projectPath: string, fullTargetFilePath: string): string | null {
var relativeFilePath = fullTargetFilePath.replace(projectPath, '');
// remove first / or \
if (relativeFilePath[0] === '/' || relativeFilePath[0] === '\\') {
relativeFilePath = relativeFilePath.slice(1, relativeFilePath.length);
}
return relativeFilePath;
}
async function executeOperation(
commandParam: any,
gitArgumentsFunc: (targetFile: string) => string[],
infoMessageFunc: (targetFile: string) => string): Promise<void> {
if (!commandParam) {
vscode.window.showErrorMessage('Could not launch merge tool. VSCode did not supply command parameters. Try again in a few seconds.');
return;
}
const fullTargetFilePath: string = commandParam.resourceUri.fsPath;
const simpleGit = await import('simple-git');
if (vscode.workspace.workspaceFolders) {
// Look through the workspace folders and find the one that has our file.
for (let workspaceFolder of vscode.workspace.workspaceFolders) {
const projectPath = workspaceFolder.uri.fsPath;
if (fullTargetFilePath.startsWith(projectPath)) {
var targetFile = getRelativeItemPath(projectPath, fullTargetFilePath);
if (targetFile === null) {
vscode.window.showErrorMessage('Could not get target path.');
return;
}
const notifyOnOpen = vscode.workspace.getConfiguration('git-diff-and-merge-tool').get('showNotificationOnOpen');
if (notifyOnOpen) {
vscode.window.showInformationMessage(infoMessageFunc(targetFile));
}
simpleGit(projectPath).raw(
gitArgumentsFunc(targetFile),
(err: any, result: any) => {
if (err) {
vscode.window.showWarningMessage(err);
}
});
return;
}
}
vscode.window.showErrorMessage('Could not find workspace for ' + fullTargetFilePath);
}
}
let diffCommand = vscode.commands.registerCommand('gitdiffandmergetool.diff', (param: any) => {
executeOperation(
param,
(targetFile: string) => { return ['difftool', '-y', 'HEAD', targetFile]; },
(targetFile: string) => { return 'Launching diff tool for ' + targetFile; });
});
let mergeCommand = vscode.commands.registerCommand('gitdiffandmergetool.merge', async (param: any) => {
executeOperation(
param,
(targetFile: string) => { return ['mergetool', '-y', targetFile]; },
(targetFile: string) => { return 'Launching merge tool for ' + targetFile; });
});
context.subscriptions.push(mergeCommand);
context.subscriptions.push(diffCommand);
}
// This method is called when your extension is deactivated
export function deactivate() { }