From df838e883abd31164f659f9fbf3769a740e293ed Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 10 Mar 2017 13:48:01 -0500 Subject: [PATCH] Changes shortcut keys for diff with previous commands Adds diff with next command Fixes #45 - Keyboard Shortcut collision with Project Manager Preps v2.11.2 --- CHANGELOG.md | 9 +++++ package.json | 25 +++++++++--- src/activeEditorTracker.ts | 5 ++- src/commands.ts | 1 + src/commands/commands.ts | 3 +- src/commands/diffLineWithPrevious.ts | 1 + src/commands/diffWithNext.ts | 74 ++++++++++++++++++++++++++++++++++ src/commands/diffWithPrevious.ts | 1 + src/constants.ts | 4 +- src/extension.ts | 7 ++-- src/git/enrichers/logParserEnricher.ts | 2 + src/git/gitEnrichment.ts | 6 +++ 12 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 src/commands/diffWithNext.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b92c67..180964c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ ## Release Notes +### 2.11.2 +- Adds `gitlens.diffWithNext` command to open a diff with the next commit +- Adds `alt+.` shortcut for the `gitlens.diffWithNext` command +- Changes `shift+alt+p` shortcut to `alt+,` for the `gitlens.diffWithPrevious` command +- Changes `alt+p` shortcut to `shift+alt+,` for the `gitlens.diffLineWithPrevious` command +- Removes `gitlens.toggleCodeLens` from Command Palette when not available +- Removes `gitlens.toggleCodeLens` shortcut key when not available +- Fixes (#45)[https://github.com/eamodio/vscode-gitlens/issues/45] - Keyboard Shortcut collision with Project Manager + ### 2.11.1 - Adds blame and active line annotation support to git diff split view (right side) - Adds command (compare, copy sha/message, etc) support to git diff split view (right side) diff --git a/package.json b/package.json index 606f722..9cad3cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gitlens", - "version": "2.11.1", + "version": "2.11.2", "author": { "name": "Eric Amodio", "email": "eamodio@gmail.com" @@ -357,6 +357,11 @@ "category": "GitLens" }, { + "command": "gitlens.diffWithNext", + "title": "Compare with Next Commit", + "category": "GitLens" + }, + { "command": "gitlens.diffWithPrevious", "title": "Compare with Previous Commit", "category": "GitLens" @@ -466,6 +471,10 @@ "when": "gitlens:enabled" }, { + "command": "gitlens.diffWithNext", + "when": "gitlens:enabled" + }, + { "command": "gitlens.diffWithPrevious", "when": "gitlens:enabled" }, @@ -696,15 +705,21 @@ "when": "editorTextFocus && gitlens:enabled" }, { + "command": "gitlens.diffWithNext", + "key": "alt+.", + "mac": "alt+.", + "when": "editorTextFocus && gitlens:enabled" + }, + { "command": "gitlens.diffLineWithPrevious", - "key": "alt+p", - "mac": "alt+p", + "key": "shift+alt+,", + "mac": "shift+alt+,", "when": "editorTextFocus && gitlens:enabled" }, { "command": "gitlens.diffWithPrevious", - "key": "shift+alt+p", - "mac": "shift+alt+p", + "key": "alt+,", + "mac": "alt+,", "when": "editorTextFocus && gitlens:enabled" }, { diff --git a/src/activeEditorTracker.ts b/src/activeEditorTracker.ts index 13fcb25..9ff6ee8 100644 --- a/src/activeEditorTracker.ts +++ b/src/activeEditorTracker.ts @@ -1,6 +1,7 @@ 'use strict'; import { commands, Disposable, TextEditor, window } from 'vscode'; +import { BuiltInCommands } from './constants'; export class ActiveEditorTracker extends Disposable { @@ -28,11 +29,11 @@ export class ActiveEditorTracker extends Disposable { } async close(): Promise<{}> { - return commands.executeCommand('workbench.action.closeActiveEditor'); + return commands.executeCommand(BuiltInCommands.CloseActiveEditor); } async next(): Promise<{}> { - return commands.executeCommand('workbench.action.nextEditor'); + return commands.executeCommand(BuiltInCommands.NextEditor); } async wait(timeout: number = 500): Promise { diff --git a/src/commands.ts b/src/commands.ts index 708e665..7d7b104 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -11,6 +11,7 @@ export { CopyShaToClipboardCommand } from './commands/copyShaToClipboard'; export { DiffDirectoryCommand } from './commands/diffDirectory'; export { DiffLineWithPreviousCommand } from './commands/diffLineWithPrevious'; export { DiffLineWithWorkingCommand } from './commands/diffLineWithWorking'; +export { DiffWithNextCommand } from './commands/diffWithNext'; export { DiffWithPreviousCommand } from './commands/diffWithPrevious'; export { DiffWithWorkingCommand } from './commands/diffWithWorking'; export { OpenChangedFilesCommand } from './commands/openChangedFiles'; diff --git a/src/commands/commands.ts b/src/commands/commands.ts index 722bc21..688fd8c 100644 --- a/src/commands/commands.ts +++ b/src/commands/commands.ts @@ -2,12 +2,13 @@ import { commands, Disposable, TextEditor, TextEditorEdit, Uri, window, workspace } from 'vscode'; import { BuiltInCommands } from '../constants'; -export type Commands = 'gitlens.closeUnchangedFiles' | 'gitlens.copyMessageToClipboard' | 'gitlens.copyShaToClipboard' | 'gitlens.diffDirectory' | 'gitlens.diffWithPrevious' | 'gitlens.diffLineWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.diffLineWithWorking' | 'gitlens.openChangedFiles' | 'gitlens.showBlame' | 'gitlens.showBlameHistory' | 'gitlens.showFileHistory' | 'gitlens.showQuickCommitDetails' | 'gitlens.showQuickCommitFileDetails' | 'gitlens.showQuickFileHistory' | 'gitlens.showQuickRepoHistory' | 'gitlens.showQuickRepoStatus' | 'gitlens.toggleBlame' | 'gitlens.toggleCodeLens'; +export type Commands = 'gitlens.closeUnchangedFiles' | 'gitlens.copyMessageToClipboard' | 'gitlens.copyShaToClipboard' | 'gitlens.diffDirectory' | 'gitlens.diffWithNext' | 'gitlens.diffWithPrevious' | 'gitlens.diffLineWithPrevious' | 'gitlens.diffWithWorking' | 'gitlens.diffLineWithWorking' | 'gitlens.openChangedFiles' | 'gitlens.showBlame' | 'gitlens.showBlameHistory' | 'gitlens.showFileHistory' | 'gitlens.showQuickCommitDetails' | 'gitlens.showQuickCommitFileDetails' | 'gitlens.showQuickFileHistory' | 'gitlens.showQuickRepoHistory' | 'gitlens.showQuickRepoStatus' | 'gitlens.toggleBlame' | 'gitlens.toggleCodeLens'; export const Commands = { CloseUnchangedFiles: 'gitlens.closeUnchangedFiles' as Commands, CopyMessageToClipboard: 'gitlens.copyMessageToClipboard' as Commands, CopyShaToClipboard: 'gitlens.copyShaToClipboard' as Commands, DiffDirectory: 'gitlens.diffDirectory' as Commands, + DiffWithNext: 'gitlens.diffWithNext' as Commands, DiffWithPrevious: 'gitlens.diffWithPrevious' as Commands, DiffLineWithPrevious: 'gitlens.diffLineWithPrevious' as Commands, DiffWithWorking: 'gitlens.diffWithWorking' as Commands, diff --git a/src/commands/diffLineWithPrevious.ts b/src/commands/diffLineWithPrevious.ts index b10a421..71766db 100644 --- a/src/commands/diffLineWithPrevious.ts +++ b/src/commands/diffLineWithPrevious.ts @@ -60,6 +60,7 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand { this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha) ]); await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.uri.fsPath)} (${commit.sha}) ↔ ${path.basename(gitUri.fsPath)} (${gitUri.sha})`); + // TODO: Figure out how to focus the left pane return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' }); } catch (ex) { diff --git a/src/commands/diffWithNext.ts b/src/commands/diffWithNext.ts new file mode 100644 index 0000000..45dfa27 --- /dev/null +++ b/src/commands/diffWithNext.ts @@ -0,0 +1,74 @@ +'use strict'; +import { Iterables } from '../system'; +import { commands, Range, TextEditor, Uri, window } from 'vscode'; +import { ActiveEditorCommand, Commands } from './commands'; +import { BuiltInCommands } from '../constants'; +import { GitLogCommit, GitProvider, GitUri } from '../gitProvider'; +import { Logger } from '../logger'; +// import * as moment from 'moment'; +import * as path from 'path'; + +export class DiffWithNextCommand extends ActiveEditorCommand { + + constructor(private git: GitProvider) { + super(Commands.DiffWithNext); + } + + async execute(editor: TextEditor): Promise; + async execute(editor: TextEditor, uri: Uri): Promise; + async execute(editor: TextEditor, uri: Uri, commit: GitLogCommit, range?: Range): Promise; + async execute(editor: TextEditor, uri: Uri, commit: GitLogCommit, line?: number): Promise; + async execute(editor: TextEditor, uri?: Uri, commit?: GitLogCommit, rangeOrLine?: Range | number): Promise { + if (!(uri instanceof Uri)) { + if (!editor || !editor.document) return undefined; + uri = editor.document.uri; + } + + let line = (editor && editor.selection.active.line) || 0; + if (typeof rangeOrLine === 'number') { + line = rangeOrLine || line; + rangeOrLine = undefined; + } + + if (!commit || !(commit instanceof GitLogCommit) || rangeOrLine instanceof Range) { + const gitUri = await GitUri.fromUri(uri, this.git); + + try { + if (!gitUri.sha) { + // If the file is uncommitted, treat it as a DiffWithWorking + if (await this.git.isFileUncommitted(gitUri.fsPath, gitUri.repoPath)) { + return commands.executeCommand(Commands.DiffWithWorking, uri); + } + } + + const sha = (commit && commit.sha) || gitUri.sha; + + const log = await this.git.getLogForFile(gitUri.fsPath, undefined, gitUri.repoPath, rangeOrLine as Range, sha ? undefined : 2); + if (!log) return window.showWarningMessage(`Unable to open diff. File is probably not under source control`); + + commit = (sha && log.commits.get(sha)) || Iterables.first(log.commits.values()); + } + catch (ex) { + Logger.error('[GitLens.DiffWithNextCommand]', `getLogForFile(${gitUri.fsPath})`, ex); + return window.showErrorMessage(`Unable to open diff. See output channel for more details`); + } + } + + if (!commit.nextSha) { + return commands.executeCommand(Commands.DiffWithWorking, uri); + } + + try { + const [rhs, lhs] = await Promise.all([ + this.git.getVersionedFile(commit.nextUri.fsPath, commit.repoPath, commit.nextSha), + this.git.getVersionedFile(commit.uri.fsPath, commit.repoPath, commit.sha) + ]); + await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.uri.fsPath)} (${commit.sha}) ↔ ${path.basename(commit.nextUri.fsPath)} (${commit.nextSha})`); + return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' }); + } + catch (ex) { + Logger.error('[GitLens.DiffWithNextCommand]', 'getVersionedFile', ex); + return window.showErrorMessage(`Unable to open diff. See output channel for more details`); + } + } +} \ No newline at end of file diff --git a/src/commands/diffWithPrevious.ts b/src/commands/diffWithPrevious.ts index 6b2fd28..0d027f4 100644 --- a/src/commands/diffWithPrevious.ts +++ b/src/commands/diffWithPrevious.ts @@ -64,6 +64,7 @@ export class DiffWithPreviousCommand extends ActiveEditorCommand { this.git.getVersionedFile(commit.previousUri.fsPath, commit.repoPath, commit.previousSha) ]); await commands.executeCommand(BuiltInCommands.Diff, Uri.file(lhs), Uri.file(rhs), `${path.basename(commit.previousUri.fsPath)} (${commit.previousSha}) ↔ ${path.basename(commit.uri.fsPath)} (${commit.sha})`); + // TODO: Figure out how to focus the left pane return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: line, at: 'center' }); } catch (ex) { diff --git a/src/constants.ts b/src/constants.ts index 7673173..8a9623a 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -2,14 +2,16 @@ export const RepoPath = 'repoPath'; -export type BuiltInCommands = 'cursorMove' | 'editor.action.showReferences' | 'editor.action.toggleRenderWhitespace' | 'editorScroll' | 'revealLine' | 'setContext' | 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'vscode.open'; +export type BuiltInCommands = 'cursorMove' | 'editor.action.showReferences' | 'editor.action.toggleRenderWhitespace' | 'editorScroll' | 'revealLine' | 'setContext' | 'vscode.diff' | 'vscode.executeDocumentSymbolProvider' | 'vscode.executeCodeLensProvider' | 'vscode.open' | 'workbench.action.closeActiveEditor' | 'workbench.action.nextEditor'; export const BuiltInCommands = { + CloseActiveEditor: 'workbench.action.closeActiveEditor' as BuiltInCommands, CursorMove: 'cursorMove' as BuiltInCommands, Diff: 'vscode.diff' as BuiltInCommands, EditorScroll: 'editorScroll' as BuiltInCommands, ExecuteDocumentSymbolProvider: 'vscode.executeDocumentSymbolProvider' as BuiltInCommands, ExecuteCodeLensProvider: 'vscode.executeCodeLensProvider' as BuiltInCommands, Open: 'vscode.open' as BuiltInCommands, + NextEditor: 'workbench.action.nextEditor' as BuiltInCommands, RevealLine: 'revealLine' as BuiltInCommands, SetContext: 'setContext' as BuiltInCommands, ShowReferences: 'editor.action.showReferences' as BuiltInCommands, diff --git a/src/extension.ts b/src/extension.ts index 36197e1..5ba3eb8 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -7,7 +7,7 @@ import { configureCssCharacters } from './blameAnnotationFormatter'; import { CommandContext, setCommandContext } from './commands'; import { CloseUnchangedFilesCommand, OpenChangedFilesCommand } from './commands'; import { CopyMessageToClipboardCommand, CopyShaToClipboardCommand } from './commands'; -import { DiffDirectoryCommand, DiffLineWithPreviousCommand, DiffLineWithWorkingCommand, DiffWithPreviousCommand, DiffWithWorkingCommand} from './commands'; +import { DiffDirectoryCommand, DiffLineWithPreviousCommand, DiffLineWithWorkingCommand, DiffWithNextCommand, DiffWithPreviousCommand, DiffWithWorkingCommand} from './commands'; import { ShowBlameCommand, ToggleBlameCommand } from './commands'; import { ShowBlameHistoryCommand, ShowFileHistoryCommand } from './commands'; import { ShowQuickCommitDetailsCommand, ShowQuickCommitFileDetailsCommand, ShowQuickFileHistoryCommand, ShowQuickRepoHistoryCommand, ShowQuickRepoStatusCommand} from './commands'; @@ -93,10 +93,11 @@ export async function activate(context: ExtensionContext) { context.subscriptions.push(new CopyMessageToClipboardCommand(git, repoPath)); context.subscriptions.push(new CopyShaToClipboardCommand(git, repoPath)); context.subscriptions.push(new DiffDirectoryCommand(git, repoPath)); - context.subscriptions.push(new DiffWithWorkingCommand(git)); + context.subscriptions.push(new DiffLineWithPreviousCommand(git)); context.subscriptions.push(new DiffLineWithWorkingCommand(git)); + context.subscriptions.push(new DiffWithNextCommand(git)); context.subscriptions.push(new DiffWithPreviousCommand(git)); - context.subscriptions.push(new DiffLineWithPreviousCommand(git)); + context.subscriptions.push(new DiffWithWorkingCommand(git)); context.subscriptions.push(new ShowBlameCommand(annotationController)); context.subscriptions.push(new ToggleBlameCommand(annotationController)); context.subscriptions.push(new ShowBlameHistoryCommand(git)); diff --git a/src/git/enrichers/logParserEnricher.ts b/src/git/enrichers/logParserEnricher.ts index 9aecc2b..0d0d781 100644 --- a/src/git/enrichers/logParserEnricher.ts +++ b/src/git/enrichers/logParserEnricher.ts @@ -200,9 +200,11 @@ export class GitLogParserEnricher implements IGitEnricher { if (recentCommit) { recentCommit.previousSha = commit.sha; + commit.nextSha = recentCommit.sha; // Only add a filename if this is a file log if (type === 'file') { recentCommit.previousFileName = commit.originalFileName || commit.fileName; + commit.nextFileName = recentCommit.originalFileName || recentCommit.fileName; } } recentCommit = commit; diff --git a/src/git/gitEnrichment.ts b/src/git/gitEnrichment.ts index b7c2ee2..f0eac45 100644 --- a/src/git/gitEnrichment.ts +++ b/src/git/gitEnrichment.ts @@ -108,6 +108,8 @@ export type GitLogType = 'file' | 'repo'; export class GitLogCommit extends GitCommit { fileStatuses: { status: GitFileStatus, fileName: string }[]; + nextSha?: string; + nextFileName?: string; status: GitFileStatus; constructor( @@ -135,6 +137,10 @@ export class GitLogCommit extends GitCommit { this.fileStatuses = [{ status: status, fileName: fileName }]; } } + + get nextUri(): Uri { + return this.nextFileName ? Uri.file(path.join(this.repoPath, this.nextFileName)) : this.uri; + } } export interface IGitCommitLine {