Skip to content

Commit

Permalink
Fixes #969 adds copy relative path to context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
d13 committed Mar 13, 2023
1 parent 5883120 commit 10ec66b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion package.json
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -7879,6 +7886,10 @@
"command": "gitlens.copyShaToClipboard",
"when": "gitlens:activeFileStatus =~ /blameable/"
},
{
"command": "gitlens.copyRelativePathToClipboard",
"when": "gitlens:enabled"
},
{
"command": "gitlens.closeUnchangedFiles",
"when": "gitlens:enabled"
Expand Down Expand Up @@ -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": [
Expand Down
1 change: 1 addition & 0 deletions src/commands.ts
Expand Up @@ -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';
Expand Down
26 changes: 26 additions & 0 deletions 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;
}
}
1 change: 1 addition & 0 deletions src/constants.ts
Expand Up @@ -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',
Expand Down

0 comments on commit 10ec66b

Please sign in to comment.