Skip to content

Commit 421cb12

Browse files
committed
Fixed behavior when there are multiple folders in the workspace. It will now try to find the workspace that corresponds with the targeted file.
1 parent 4742d96 commit 421cb12

3 files changed

Lines changed: 34 additions & 24 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "git-diff-and-merge-tool",
33
"displayName": "Git Diff and Merge Tool",
44
"description": "Launch git difftool and mergetool from VSCode.",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"author": {
77
"name": "David Rickard",
88
"email": "david.rickard@gmail.com"

src/extension.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,49 @@ import * as vscode from 'vscode';
55
// This method is called when your extension is activated
66
// Your extension is activated the very first time the command is executed
77
export function activate(context: vscode.ExtensionContext) {
8-
function getItemPath(projectPath: string, param: any): string | null
9-
{
10-
var targetFile = param._resourceUri.fsPath.replace(projectPath, '');
8+
function getRelativeItemPath(projectPath: string, fullTargetFilePath: string): string | null {
9+
var relativeFilePath = fullTargetFilePath.replace(projectPath, '');
1110
// remove first / or \
12-
if(targetFile[0] === '/' || targetFile[0] === '\\') {
13-
targetFile = targetFile.slice(1, targetFile.length );
11+
if(relativeFilePath[0] === '/' || relativeFilePath[0] === '\\') {
12+
relativeFilePath = relativeFilePath.slice(1, relativeFilePath.length );
1413
}
1514

16-
return targetFile;
15+
return relativeFilePath;
1716
}
1817

1918
async function executeOperation(
2019
commandParam: any,
2120
gitArgumentsFunc: (targetFile: string) => string[],
22-
infoMessageFunc: (targetFile: string) => string) {
21+
infoMessageFunc: (targetFile: string) => string): Promise<void> {
22+
const fullTargetFilePath: string = commandParam.resourceUri.fsPath;
23+
2324
const simpleGit = await import('simple-git');
2425
if (vscode.workspace.workspaceFolders) {
25-
const projectPath = vscode.workspace.workspaceFolders[0].uri.fsPath;
26-
var targetFile = getItemPath(projectPath, commandParam);
27-
if (targetFile === null) {
28-
vscode.window.showWarningMessage('Could not get target path.');
29-
return;
30-
}
26+
// Look through the workspace folders and find the one that has our file.
27+
for (let workspaceFolder of vscode.workspace.workspaceFolders) {
28+
const projectPath = workspaceFolder.uri.fsPath;
29+
if (fullTargetFilePath.startsWith(projectPath)) {
30+
var targetFile = getRelativeItemPath(projectPath, fullTargetFilePath);
31+
if (targetFile === null) {
32+
vscode.window.showWarningMessage('Could not get target path.');
33+
return;
34+
}
35+
36+
vscode.window.showInformationMessage(infoMessageFunc(targetFile));
37+
38+
simpleGit(projectPath).raw(
39+
gitArgumentsFunc(targetFile),
40+
(err: any, result: any) => {
41+
if(err) {
42+
vscode.window.showWarningMessage(err);
43+
}
44+
});
45+
46+
return;
47+
}
48+
}
3149

32-
vscode.window.showInformationMessage(infoMessageFunc(targetFile));
33-
34-
simpleGit(projectPath).raw(
35-
gitArgumentsFunc(targetFile),
36-
(err: any, result: any) => {
37-
if(err) {
38-
vscode.window.showWarningMessage(err);
39-
}
40-
});
50+
vscode.window.showWarningMessage('Could not find workspace for ' + fullTargetFilePath);
4151
}
4252
}
4353

0 commit comments

Comments
 (0)