From 10ec66b914cc3e62e2694b616e8679c73a7fd975 Mon Sep 17 00:00:00 2001 From: Keith Daulton Date: Mon, 4 Apr 2022 23:42:48 -0400 Subject: [PATCH] Fixes #969 adds copy relative path to context menu --- README.md | 3 +++ package.json | 18 +++++++++++++- src/commands.ts | 1 + src/commands/copyRelativePathToClipboard.ts | 26 +++++++++++++++++++++ src/constants.ts | 1 + 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/commands/copyRelativePathToClipboard.ts diff --git a/README.md b/README.md index 46b044b1a792a..5c42cf7d22a9f 100644 --- a/README.md +++ b/README.md @@ -675,6 +675,9 @@ Additionally, these integrations provide commands to copy the URL of or open fil - Adds an _Add Co-authors_ command (`gitlens.addAuthors`) to add a co-author to the commit message input box - Adds a _Copy SHA_ command (`gitlens.copyShaToClipboard`) to copy the commit SHA of the current line to the clipboard or from the most recent commit to the current branch, if there is no current editor + +- Adds a _Copy Relative Path_ command (`gitlens.copyRelativePathToClipboard`) to copy the relative path, of the active file in the editor, to the clipboard + - Adds a _Copy Message_ command (`gitlens.copyMessageToClipboard`) to copy the commit message of the current line to the clipboard or from the most recent commit to the current branch, if there is no current editor - Adds a _Copy Current Branch_ command (`gitlens.copyCurrentBranch`) to copy the name of the current branch to the clipboard diff --git a/package.json b/package.json index 99195be91e140..bd621daf03a63 100644 --- a/package.json +++ b/package.json @@ -169,6 +169,7 @@ "onCommand:gitlens.copyCurrentBranch", "onCommand:gitlens.copyMessageToClipboard", "onCommand:gitlens.copyShaToClipboard", + "onCommand:gitlens.copyRelativePathToClipboard", "onCommand:gitlens.closeUnchangedFiles", "onCommand:gitlens.openChangedFiles", "onCommand:gitlens.openBranchesOnRemote", @@ -5132,6 +5133,12 @@ "category": "GitLens", "icon": "$(copy)" }, + { + "command": "gitlens.copyRelativePathToClipboard", + "title": "Copy Relative Path", + "category": "GitLens", + "icon": "$(copy)" + }, { "command": "gitlens.closeUnchangedFiles", "title": "Close Unchanged Files", @@ -7879,6 +7886,10 @@ "command": "gitlens.copyShaToClipboard", "when": "gitlens:activeFileStatus =~ /blameable/" }, + { + "command": "gitlens.copyRelativePathToClipboard", + "when": "gitlens:enabled" + }, { "command": "gitlens.closeUnchangedFiles", "when": "gitlens:enabled" @@ -9354,9 +9365,14 @@ "group": "2_gitlens@1" }, { - "command": "gitlens.copyMessageToClipboard", + "command": "gitlens.copyRelativePathToClipboard", "when": "editorTextFocus && gitlens:activeFileStatus =~ /blameable/ && config.gitlens.menus.editor.clipboard", "group": "2_gitlens@2" + }, + { + "command": "gitlens.copyMessageToClipboard", + "when": "editorTextFocus && gitlens:activeFileStatus =~ /blameable/ && config.gitlens.menus.editor.clipboard", + "group": "2_gitlens@3" } ], "editor/title": [ diff --git a/src/commands.ts b/src/commands.ts index 9e198887ab529..00ffb5453c846 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -6,6 +6,7 @@ export * from './commands/copyCurrentBranch'; export * from './commands/copyDeepLink'; export * from './commands/copyMessageToClipboard'; export * from './commands/copyShaToClipboard'; +export * from './commands/copyRelativePathToClipboard'; export * from './commands/createPullRequestOnRemote'; export * from './commands/openDirectoryCompare'; export * from './commands/diffLineWithPrevious'; diff --git a/src/commands/copyRelativePathToClipboard.ts b/src/commands/copyRelativePathToClipboard.ts new file mode 100644 index 0000000000000..321b774e3ecde --- /dev/null +++ b/src/commands/copyRelativePathToClipboard.ts @@ -0,0 +1,26 @@ +import { env, TextEditor, Uri } from 'vscode'; +import { Commands } from '../constants'; +import type { Container } from '../container'; +import { command } from '../system/command'; +import { ActiveEditorCommand, getCommandUri } from './base'; + +@command() +export class CopyRelativePathToClipboardCommand extends ActiveEditorCommand { + constructor(private readonly container: Container) { + super(Commands.CopyRelativePathToClipboard); + } + + async execute(editor?: TextEditor, uri?: Uri) { + uri = getCommandUri(uri, editor); + let relativePath = ''; + if (uri != null) { + const repoPath = this.container.git.getBestRepository(editor)?.uri; + if (repoPath != null) { + relativePath = this.container.git.getRelativePath(uri, repoPath); + } + } + + void (await env.clipboard.writeText(relativePath)); + return undefined; + } +} diff --git a/src/constants.ts b/src/constants.ts index 1fd200ad17bbd..cd2274f256c78 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -105,6 +105,7 @@ export const enum Commands { CopyRemotePullRequestUrl = 'gitlens.copyRemotePullRequestUrl', CopyRemoteRepositoryUrl = 'gitlens.copyRemoteRepositoryUrl', CopyShaToClipboard = 'gitlens.copyShaToClipboard', + CopyRelativePathToClipboard = 'gitlens.copyRelativePathToClipboard', CreatePullRequestOnRemote = 'gitlens.createPullRequestOnRemote', DiffDirectory = 'gitlens.diffDirectory', DiffDirectoryWithHead = 'gitlens.diffDirectoryWithHead',