From aaa1b78d29cae24981731e329a2a73cc3026077c Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Sun, 12 Feb 2017 23:33:30 -0500 Subject: [PATCH] Adds copy sha to clipboard - fixes #28 Rearranges menu structure --- package.json | 76 ++++++++++++++++++++++---------------- src/commands/copyShaToClipboard.ts | 49 ++++++++++++++++++++++++ src/constants.ts | 3 +- src/extension.ts | 6 ++- 4 files changed, 100 insertions(+), 34 deletions(-) create mode 100644 src/commands/copyShaToClipboard.ts diff --git a/package.json b/package.json index c4cd228..69307f2 100644 --- a/package.json +++ b/package.json @@ -355,79 +355,91 @@ "command": "gitlens.showQuickRepoHistory", "title": "Show Repository History", "category": "GitLens" + }, + { + "command": "gitlens.copyShaToClipboard", + "title": "Copy Commit Sha to Clipboard", + "category": "GitLens" } ], "menus": { "explorer/context": [ { - "command": "gitlens.diffWithWorking", - "when": "config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "2_gitlens-file" + "command": "gitlens.showQuickFileHistory", + "when": "config.git.enabled", + "group": "gitlens" }, { "command": "gitlens.diffWithPrevious", "when": "config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "2_gitlens-file" + "group": "gitlens_diff" }, { - "command": "gitlens.showQuickFileHistory", - "when": "config.git.enabled", - "group": "3_gitlens-file" + "command": "gitlens.diffWithWorking", + "when": "config.gitlens.menus.diff.enabled && config.git.enabled", + "group": "gitlens_diff" } ], "editor/title": [ { - "command": "gitlens.diffWithWorking", - "when": "config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "2_gitlens" + "command": "gitlens.toggleBlame", + "when": "config.git.enabled", + "group": "1_gitlens@1" + }, + { + "command": "gitlens.showQuickFileHistory", + "when": "config.git.enabled", + "group": "1_gitlens@2" }, { "command": "gitlens.diffWithPrevious", "when": "config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "2_gitlens" + "group": "1_gitlens_diff" }, { - "command": "gitlens.showQuickFileHistory", - "when": "editorTextFocus && config.git.enabled", - "group": "3_gitlens" - }, + "command": "gitlens.diffWithWorking", + "when": "config.gitlens.menus.diff.enabled && config.git.enabled", + "group": "1_gitlens_diff" + } + ], + "editor/title/context": [ { "command": "gitlens.toggleBlame", - "when": "editorTextFocus && config.git.enabled", - "group": "3_gitlens" + "when": "config.git.enabled", + "group": "gitlens@1" + }, + { + "command": "gitlens.showQuickFileHistory", + "when": "config.git.enabled", + "group": "gitlens@2" } ], "editor/context": [ { - "command": "gitlens.diffLineWithWorking", - "when": "editorTextFocus && config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "2_gitlens@1.0" - }, - { "command": "gitlens.diffLineWithPrevious", "when": "editorTextFocus && config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "2_gitlens@1.1" + "group": "1_gitlens@1" }, { - "command": "gitlens.diffWithWorking", + "command": "gitlens.diffLineWithWorking", "when": "editorTextFocus && config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "3_gitlens@1.0" + "group": "1_gitlens@2" }, { - "command": "gitlens.diffWithPrevious", - "when": "editorTextFocus && config.gitlens.menus.diff.enabled && config.git.enabled", - "group": "3_gitlens@1.1" + "command": "gitlens.toggleBlame", + "when": "editorTextFocus && config.git.enabled", + "group": "2_gitlens@1" }, { "command": "gitlens.showQuickFileHistory", "alt": "gitlens.showFileHistory", "when": "config.git.enabled", - "group": "4_gitlens" + "group": "2_gitlens@2" }, { - "command": "gitlens.toggleBlame", + "command": "gitlens.copyShaToClipboard", "when": "editorTextFocus && config.git.enabled", - "group": "4_gitlens" + "group": "9_gitlens@1" } ] }, @@ -498,6 +510,7 @@ "vscode:prepublish": "tsc -p ./" }, "dependencies": { + "copy-paste": "^1.3.0", "ignore": "^3.2.2", "lodash.debounce": "^4.0.8", "lodash.escaperegexp": "^4.1.2", @@ -508,6 +521,7 @@ "tmp": "^0.0.31" }, "devDependencies": { + "@types/copy-paste": "^1.1.30", "@types/node": "^7.0.5", "@types/mocha": "^2.2.39", "@types/tmp": "^0.0.32", diff --git a/src/commands/copyShaToClipboard.ts b/src/commands/copyShaToClipboard.ts new file mode 100644 index 0000000..92bb049 --- /dev/null +++ b/src/commands/copyShaToClipboard.ts @@ -0,0 +1,49 @@ +'use strict'; +import { TextEditor, TextEditorEdit, Uri, window } from 'vscode'; +import { EditorCommand } from './commands'; +import { Commands } from '../constants'; +import GitProvider, { GitUri } from '../gitProvider'; +import { Logger } from '../logger'; +import { copy } from 'copy-paste'; + +export default class CopyShaToClipboard extends EditorCommand { + + constructor(private git: GitProvider) { + super(Commands.CopyShaToClipboard); + } + + async execute(editor: TextEditor, edit: TextEditorEdit, uri?: Uri, sha?: string): Promise { + if (!(uri instanceof Uri)) { + if (!editor.document) return undefined; + uri = editor.document.uri; + } + + const line = editor.selection.active.line; + const gitUri = GitUri.fromUri(uri, this.git); + + try { + 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; + + sha = blame.commit.sha; + } + catch (ex) { + Logger.error('[GitLens.CopyShaToClipboard]', `getBlameForLine(${blameline})`, ex); + return window.showErrorMessage(`Unable to copy sha. See output channel for more details`); + } + } + + copy(sha); + return undefined; + } + catch (ex) { + Logger.error('GitLens.CopyShaToClipboard', ex); + return window.showErrorMessage(`Unable to copy sha. See output channel for more details`); + } + } +} \ No newline at end of file diff --git a/src/constants.ts b/src/constants.ts index 52eeb51..50de002 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -14,8 +14,9 @@ export const BuiltInCommands = { ToggleRenderWhitespace: 'editor.action.toggleRenderWhitespace' as BuiltInCommands }; -export type Commands = '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.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 = { + CopyShaToClipboard: 'gitlens.copyShaToClipboard' as Commands, DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands, DiffLineWithPrevious: 'gitlens.diffLineWithPrevious' as Commands, DiffWithWorking: 'gitlens.diffWithWorking' as Commands, diff --git a/src/extension.ts b/src/extension.ts index 75e4759..fb7207d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -3,6 +3,7 @@ import { ExtensionContext, languages, window, workspace } from 'vscode'; import BlameActiveLineController from './blameActiveLineController'; import BlameAnnotationController from './blameAnnotationController'; import { configureCssCharacters } from './blameAnnotationFormatter'; +import CopyShaToClipboardCommand from './commands/copyShaToClipboard'; import DiffLineWithPreviousCommand from './commands/diffLineWithPrevious'; import DiffLineWithWorkingCommand from './commands/diffLineWithWorking'; import DiffWithPreviousCommand from './commands/diffWithPrevious'; @@ -63,9 +64,10 @@ export async function activate(context: ExtensionContext) { const annotationController = new BlameAnnotationController(context, git); context.subscriptions.push(annotationController); - const statusBarController = new BlameStatusBarController(context, git, annotationController); - context.subscriptions.push(statusBarController); + const activeLineController = new BlameActiveLineController(context, git, annotationController); + context.subscriptions.push(activeLineController); + context.subscriptions.push(new CopyShaToClipboardCommand(git)); context.subscriptions.push(new DiffWithWorkingCommand(git)); context.subscriptions.push(new DiffLineWithWorkingCommand(git)); context.subscriptions.push(new DiffWithPreviousCommand(git));