Skip to content

Commit a20b9e0

Browse files
Copilotletmaik
andauthored
Add Copy Path and Copy Relative Path context menu items (#125)
Co-authored-by: letmaik <530988+letmaik@users.noreply.github.com>
1 parent 80887e9 commit a20b9e0

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

package.json

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@
153153
"title": "Search Changes",
154154
"icon": "$(search)",
155155
"category": "Git Tree Compare"
156+
},
157+
{
158+
"command": "gitTreeCompare.copyPath",
159+
"title": "Copy Path",
160+
"category": "Git Tree Compare"
161+
},
162+
{
163+
"command": "gitTreeCompare.copyRelativePath",
164+
"title": "Copy Relative Path",
165+
"category": "Git Tree Compare"
156166
}
157167
],
158168
"menus": {
@@ -261,19 +271,23 @@
261271
},
262272
{
263273
"command": "gitTreeCompare.openChanges",
264-
"when": "view == gitTreeCompare && viewItem == file && !config.gitTreeCompare.openChanges"
274+
"when": "view == gitTreeCompare && viewItem == file && !config.gitTreeCompare.openChanges",
275+
"group": "1_open"
265276
},
266277
{
267278
"command": "gitTreeCompare.openFile",
268-
"when": "view == gitTreeCompare && viewItem == file && config.gitTreeCompare.openChanges"
279+
"when": "view == gitTreeCompare && viewItem == file && config.gitTreeCompare.openChanges",
280+
"group": "1_open"
269281
},
270282
{
271283
"command": "gitTreeCompare.discardChanges",
272-
"when": "view == gitTreeCompare && viewItem == file"
284+
"when": "view == gitTreeCompare && viewItem == file",
285+
"group": "2_discard"
273286
},
274287
{
275288
"command": "gitTreeCompare.discardChanges",
276-
"when": "view == gitTreeCompare && viewItem == folder"
289+
"when": "view == gitTreeCompare && viewItem == folder",
290+
"group": "2_discard"
277291
},
278292
{
279293
"command": "gitTreeCompare.discardAllChanges",
@@ -335,6 +349,16 @@
335349
{
336350
"command": "gitTreeCompare.switchToMergeDiff",
337351
"when": "view == gitTreeCompare && viewItem == ref && config.gitTreeCompare.diffMode == full"
352+
},
353+
{
354+
"command": "gitTreeCompare.copyPath",
355+
"when": "view == gitTreeCompare && viewItem == file",
356+
"group": "3_copy"
357+
},
358+
{
359+
"command": "gitTreeCompare.copyRelativePath",
360+
"when": "view == gitTreeCompare && viewItem == file",
361+
"group": "3_copy"
338362
}
339363
]
340364
},

src/extension.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ export function activate(context: ExtensionContext) {
9292
commands.registerCommand(NAMESPACE + '.searchChanges', () => {
9393
runAfterInit(() => provider!.searchChanges());
9494
});
95+
commands.registerCommand(NAMESPACE + '.copyPath', node => {
96+
runAfterInit(() => provider!.copyPath(node));
97+
});
98+
commands.registerCommand(NAMESPACE + '.copyRelativePath', node => {
99+
runAfterInit(() => provider!.copyRelativePath(node));
100+
});
95101

96102
createGit(gitApi, outputChannel).then(async git => {
97103
const onOutput = (str: string) => outputChannel.append(str);

src/treeProvider.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as fs from 'fs'
55
import { TreeDataProvider, TreeItem, TreeItemCollapsibleState,
66
Uri, Disposable, EventEmitter, TextDocumentShowOptions,
77
QuickPickItem, ProgressLocation, Memento, OutputChannel,
8-
workspace, commands, window, WorkspaceFoldersChangeEvent, TreeView, ThemeIcon, TreeItemCheckboxState, TreeCheckboxChangeEvent } from 'vscode'
8+
workspace, commands, window, env, WorkspaceFoldersChangeEvent, TreeView, ThemeIcon, TreeItemCheckboxState, TreeCheckboxChangeEvent } from 'vscode'
99
import { NAMESPACE } from './constants'
1010
import { Repository, Git } from './git/git'
1111
import { Ref, RefType } from './git/api/git'
@@ -1215,6 +1215,25 @@ export class GitTreeCompareProvider implements TreeDataProvider<Element>, Dispos
12151215
});
12161216
}
12171217

1218+
async copyPath(fileEntry: FileElement) {
1219+
const diffStatus = this.getDiffStatus(fileEntry);
1220+
if (!diffStatus) {
1221+
return;
1222+
}
1223+
await env.clipboard.writeText(diffStatus.dstAbsPath);
1224+
}
1225+
1226+
async copyRelativePath(fileEntry: FileElement) {
1227+
const diffStatus = this.getDiffStatus(fileEntry);
1228+
if (!diffStatus) {
1229+
return;
1230+
}
1231+
// Calculate relative path from workspace folder root (not git repo root)
1232+
// Note: If the file is outside the workspace folder, the path will start with ../
1233+
const relativePath = path.relative(this.workspaceFolder, diffStatus.dstAbsPath);
1234+
await env.clipboard.writeText(relativePath);
1235+
}
1236+
12181237
dispose(): void {
12191238
this.disposables.forEach(d => d.dispose());
12201239
}

0 commit comments

Comments
 (0)