diff --git a/README.md b/README.md index 46b044b..5c42cf7 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 99195be..bd621da 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", @@ -5133,6 +5134,12 @@ "icon": "$(copy)" }, { + "command": "gitlens.copyRelativePathToClipboard", + "title": "Copy Relative Path", + "category": "GitLens", + "icon": "$(copy)" + }, + { "command": "gitlens.closeUnchangedFiles", "title": "Close Unchanged Files", "category": "GitLens" @@ -7880,6 +7887,10 @@ "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 9e19888..00ffb54 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 0000000..321b774 --- /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 1fd200a..cd2274f 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',