mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-01-21 09:45:37 -05:00
Adds copyMessageToClipboard command
Adds copyMessageToClipboard to the editor content menu Adds copyMessageToClipboard to showQuickCommitDetails quick pick
This commit is contained in:
14
package.json
14
package.json
@@ -386,6 +386,11 @@
|
||||
"command": "gitlens.copyShaToClipboard",
|
||||
"title": "Copy Commit Sha to Clipboard",
|
||||
"category": "GitLens"
|
||||
},
|
||||
{
|
||||
"command": "gitlens.copyMessageToClipboard",
|
||||
"title": "Copy Commit Message to Clipboard",
|
||||
"category": "GitLens"
|
||||
}
|
||||
],
|
||||
"menus": {
|
||||
@@ -441,6 +446,10 @@
|
||||
{
|
||||
"command": "gitlens.copyShaToClipboard",
|
||||
"when": "gitlens:enabled"
|
||||
},
|
||||
{
|
||||
"command": "gitlens.copyMessageToClipboard",
|
||||
"when": "gitlens:enabled"
|
||||
}
|
||||
],
|
||||
"explorer/context": [
|
||||
@@ -535,6 +544,11 @@
|
||||
"command": "gitlens.copyShaToClipboard",
|
||||
"when": "editorTextFocus && gitlens:enabled",
|
||||
"group": "9_gitlens@1"
|
||||
},
|
||||
{
|
||||
"command": "gitlens.copyMessageToClipboard",
|
||||
"when": "editorTextFocus && gitlens:enabled",
|
||||
"group": "9_gitlens@2"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
75
src/commands/copyMessageToClipboard.ts
Normal file
75
src/commands/copyMessageToClipboard.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
'use strict';
|
||||
import { Iterables } from '../system';
|
||||
import { TextEditor, Uri, window } from 'vscode';
|
||||
import { ActiveEditorCommand } from './commands';
|
||||
import { Commands } from '../constants';
|
||||
import GitProvider, { GitUri } from '../gitProvider';
|
||||
import { Logger } from '../logger';
|
||||
import { copy } from 'copy-paste';
|
||||
|
||||
export default class CopyMessageToClipboardCommand extends ActiveEditorCommand {
|
||||
|
||||
constructor(private git: GitProvider, public repoPath: string) {
|
||||
super(Commands.CopyMessageToClipboard);
|
||||
}
|
||||
|
||||
async execute(editor: TextEditor, uri?: Uri, sha?: string, message?: string): Promise<any> {
|
||||
if (!(uri instanceof Uri)) {
|
||||
uri = editor && editor.document && editor.document.uri;
|
||||
}
|
||||
|
||||
try {
|
||||
// If we don't have an editor then get the message of the last commit to the repository
|
||||
if (!uri) {
|
||||
const log = await this.git.getLogForRepo(this.repoPath, undefined, 1);
|
||||
if (!log) return undefined;
|
||||
|
||||
message = Iterables.first(log.commits.values()).message;
|
||||
copy(message);
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const line = editor.selection.active.line;
|
||||
const gitUri = GitUri.fromUri(uri, this.git);
|
||||
|
||||
if (!message) {
|
||||
if (!sha) {
|
||||
const blameline = line - gitUri.offset;
|
||||
if (blameline < 0) return undefined;
|
||||
|
||||
try {
|
||||
const blame = await this.git.getBlameForLine(gitUri.fsPath, blameline, gitUri.sha, gitUri.repoPath);
|
||||
if (!blame) return undefined;
|
||||
|
||||
if (blame.commit.isUncommitted) return undefined;
|
||||
|
||||
sha = blame.commit.sha;
|
||||
if (!gitUri.repoPath) {
|
||||
gitUri.repoPath = blame.commit.repoPath;
|
||||
}
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('[GitLens.CopyMessageToClipboardCommand]', `getBlameForLine(${blameline})`, ex);
|
||||
return window.showErrorMessage(`Unable to copy message. See output channel for more details`);
|
||||
}
|
||||
}
|
||||
|
||||
// Get the full commit message -- since blame only returns the summary
|
||||
const log = await this.git.getLogForFile(gitUri.fsPath, sha, gitUri.repoPath, undefined, 1);
|
||||
if (!log) return undefined;
|
||||
|
||||
const commit = log.commits.get(sha);
|
||||
if (!commit) return undefined;
|
||||
|
||||
message = commit.message;
|
||||
}
|
||||
|
||||
copy(message);
|
||||
return undefined;
|
||||
}
|
||||
catch (ex) {
|
||||
Logger.error('GitLens.CopyMessageToClipboardCommand', ex);
|
||||
return window.showErrorMessage(`Unable to copy message. See output channel for more details`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,11 @@ export class CommitQuickPick {
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 $(git-commit) ${commit.sha}`
|
||||
}, Commands.CopyShaToClipboard, [uri, commit.sha]));
|
||||
|
||||
items.push(new CommandQuickPickItem({
|
||||
label: `$(clippy) Copy Commit Message to Clipboard`,
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 $(git-commit) ${commit.message}`
|
||||
}, Commands.CopyMessageToClipboard, [uri, commit.sha, commit.message]));
|
||||
|
||||
items.push(new CommandQuickPickItem({
|
||||
label: `$(diff) Show Changed Files`,
|
||||
description: `\u00a0 \u2014 \u00a0\u00a0 $(git-commit) ${commit.sha}`,
|
||||
|
||||
@@ -15,8 +15,9 @@ export const BuiltInCommands = {
|
||||
ToggleRenderWhitespace: 'editor.action.toggleRenderWhitespace' as BuiltInCommands
|
||||
};
|
||||
|
||||
export type Commands = 'gitlens.copyShaToClipboard' | 'gitlens.diffWithPrevious' | 'gitlens.diffLineWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.diffLineWithWorking' | 'gitlens.showBlame' | 'gitlens.showBlameHistory' | 'gitlens.showFileHistory' | 'gitlens.showQuickCommitDetails' | 'gitlens.showQuickFileHistory' | 'gitlens.showQuickRepoHistory' | 'gitlens.toggleBlame' | 'gitlens.toggleCodeLens';
|
||||
export type Commands = 'gitlens.copyMessageToClipboard' | 'gitlens.copyShaToClipboard' | 'gitlens.diffWithPrevious' | 'gitlens.diffLineWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.diffLineWithWorking' | 'gitlens.showBlame' | 'gitlens.showBlameHistory' | 'gitlens.showFileHistory' | 'gitlens.showQuickCommitDetails' | 'gitlens.showQuickFileHistory' | 'gitlens.showQuickRepoHistory' | 'gitlens.toggleBlame' | 'gitlens.toggleCodeLens';
|
||||
export const Commands = {
|
||||
CopyMessageToClipboard: 'gitlens.copyMessageToClipboard' as Commands,
|
||||
CopyShaToClipboard: 'gitlens.copyShaToClipboard' as Commands,
|
||||
DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands,
|
||||
DiffLineWithPrevious: 'gitlens.diffLineWithPrevious' as Commands,
|
||||
|
||||
@@ -3,6 +3,7 @@ import { commands, ExtensionContext, languages, window, workspace } from 'vscode
|
||||
import BlameActiveLineController from './blameActiveLineController';
|
||||
import BlameAnnotationController from './blameAnnotationController';
|
||||
import { configureCssCharacters } from './blameAnnotationFormatter';
|
||||
import CopyMessageToClipboardCommand from './commands/copyMessageToClipboard';
|
||||
import CopyShaToClipboardCommand from './commands/copyShaToClipboard';
|
||||
import DiffLineWithPreviousCommand from './commands/diffLineWithPrevious';
|
||||
import DiffLineWithWorkingCommand from './commands/diffLineWithWorking';
|
||||
@@ -77,6 +78,7 @@ export async function activate(context: ExtensionContext) {
|
||||
const activeLineController = new BlameActiveLineController(context, git, annotationController);
|
||||
context.subscriptions.push(activeLineController);
|
||||
|
||||
context.subscriptions.push(new CopyMessageToClipboardCommand(git, repoPath));
|
||||
context.subscriptions.push(new CopyShaToClipboardCommand(git));
|
||||
context.subscriptions.push(new DiffWithWorkingCommand(git));
|
||||
context.subscriptions.push(new DiffLineWithWorkingCommand(git));
|
||||
|
||||
Reference in New Issue
Block a user