From 2bce0e5b74ade5e7974ced877b3d9a284fc02331 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 22 Apr 2019 00:27:24 -0400 Subject: [PATCH] Maintains line when opening changes from hovers --- src/annotations/annotations.ts | 41 ++++++----- src/annotations/blameAnnotationProvider.ts | 7 +- src/annotations/recentChangesAnnotationProvider.ts | 10 +-- src/commands/diffWith.ts | 83 +++++++++------------- src/git/formatters/commitFormatter.ts | 3 +- src/hovers/lineHoverController.ts | 7 +- 6 files changed, 79 insertions(+), 72 deletions(-) diff --git a/src/annotations/annotations.ts b/src/annotations/annotations.ts index 7f2878a..5d0d4e0 100644 --- a/src/annotations/annotations.ts +++ b/src/annotations/annotations.ts @@ -109,7 +109,8 @@ export class Annotations { static getHoverDiffMessage( commit: GitCommit, uri: GitUri, - hunkLine: GitDiffHunkLine | undefined + hunkLine: GitDiffHunkLine | undefined, + editorLine?: number ): MarkdownString | undefined { if (hunkLine === undefined || commit.previousSha === undefined) return undefined; @@ -118,26 +119,33 @@ export class Annotations { let message: string; if (commit.isUncommitted) { if (uri.sha !== undefined && GitService.isStagedUncommitted(uri.sha)) { - message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${ - GlyphChars.Dash - }   [\`${commit.previousShortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs( + message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs( + commit, + editorLine + )} "Open Changes")   ${GlyphChars.Dash}   [\`${ + commit.previousShortSha + }\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs( commit.previousSha! )} "Show Commit Details") ${GlyphChars.ArrowLeftRightLong} _${uri.shortSha}_\n${diff}`; } else { - message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${ - GlyphChars.Dash - }   _uncommitted changes_\n${diff}`; + message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs( + commit, + editorLine + )} "Open Changes")   ${GlyphChars.Dash}   _uncommitted changes_\n${diff}`; } } else { - message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(commit)} "Open Changes")   ${ - GlyphChars.Dash - }   [\`${commit.previousShortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs( - commit.previousSha! - )} "Show Commit Details") ${GlyphChars.ArrowLeftRightLong} [\`${ - commit.shortSha - }\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.sha)} "Show Commit Details")\n${diff}`; + message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs( + commit, + editorLine + )} "Open Changes")   ${GlyphChars.Dash}   [\`${ + commit.previousShortSha + }\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs(commit.previousSha!)} "Show Commit Details") ${ + GlyphChars.ArrowLeftRightLong + } [\`${commit.shortSha}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs( + commit.sha + )} "Show Commit Details")\n${diff}`; } const markdown = new MarkdownString(message); @@ -173,8 +181,9 @@ export class Annotations { const line = editorLine + 1; const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0]; - const hunkLine = await Container.git.getDiffForLine(uri, commitLine.originalLine - 1, ref); - const message = this.getHoverDiffMessage(commit, uri, hunkLine); + const commitEditorLine = commitLine.originalLine - 1; + const hunkLine = await Container.git.getDiffForLine(uri, commitEditorLine, ref); + const message = this.getHoverDiffMessage(commit, uri, hunkLine, commitEditorLine); return { hoverMessage: message diff --git a/src/annotations/blameAnnotationProvider.ts b/src/annotations/blameAnnotationProvider.ts index 3b2747c..497ffe5 100644 --- a/src/annotations/blameAnnotationProvider.ts +++ b/src/annotations/blameAnnotationProvider.ts @@ -226,12 +226,17 @@ export abstract class BlameAnnotationProviderBase extends AnnotationProviderBase } } + let editorLine = this.editor.selection.active.line; + const line = editorLine + 1; + const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0]; + editorLine = commitLine.originalLine - 1; + const message = Annotations.getHoverMessage( logCommit || commit, Container.config.defaultDateFormat, await Container.git.getRemotes(commit.repoPath), this.annotationType, - this.editor.selection.active.line + editorLine ); return new Hover( message, diff --git a/src/annotations/recentChangesAnnotationProvider.ts b/src/annotations/recentChangesAnnotationProvider.ts index 99fbce9..dcd279d 100644 --- a/src/annotations/recentChangesAnnotationProvider.ts +++ b/src/annotations/recentChangesAnnotationProvider.ts @@ -45,12 +45,12 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase { for (const hunk of diff.hunks) { // Subtract 2 because editor lines are 0-based and we will be adding 1 in the first iteration of the loop let count = hunk.currentPosition.start - 2; - for (const line of hunk.lines) { - if (line.current === undefined) continue; + for (const hunkLine of hunk.lines) { + if (hunkLine.current === undefined) continue; count++; - if (line.current.state === 'unchanged') continue; + if (hunkLine.current.state === 'unchanged') continue; const range = this.editor.document.validateRange( new Range(new Position(count, 0), new Position(count, Number.MAX_SAFE_INTEGER)) @@ -66,14 +66,14 @@ export class RecentChangesAnnotationProvider extends AnnotationProviderBase { dateFormat, await Container.git.getRemotes(commit.repoPath), this.annotationType, - this.editor.selection.active.line + count ), range: range }); } if (cfg.hovers.annotations.changes) { - message = Annotations.getHoverDiffMessage(commit, this._uri, line); + message = Annotations.getHoverDiffMessage(commit, this._uri, hunkLine, count); if (message === undefined) continue; } } diff --git a/src/commands/diffWith.ts b/src/commands/diffWith.ts index 7fb8461..961fb83 100644 --- a/src/commands/diffWith.ts +++ b/src/commands/diffWith.ts @@ -15,9 +15,9 @@ export interface DiffWithCommandArgsRevision { } export interface DiffWithCommandArgs { - lhs?: DiffWithCommandArgsRevision; - rhs?: DiffWithCommandArgsRevision; - repoPath?: string; + lhs: DiffWithCommandArgsRevision; + rhs: DiffWithCommandArgsRevision; + repoPath: string | undefined; line?: number; showOptions?: TextDocumentShowOptions; @@ -26,59 +26,43 @@ export interface DiffWithCommandArgs { @command() export class DiffWithCommand extends ActiveEditorCommand { static getMarkdownCommandArgs(args: DiffWithCommandArgs): string; - static getMarkdownCommandArgs(commit1: GitCommit, commit2: GitCommit): string; - static getMarkdownCommandArgs(argsOrCommit1: DiffWithCommandArgs | GitCommit, commit2?: GitCommit): string { + static getMarkdownCommandArgs(commit: GitCommit, line?: number): string; + static getMarkdownCommandArgs(argsOrCommit: DiffWithCommandArgs | GitCommit, line?: number): string { let args: DiffWithCommandArgs | GitCommit; - if (argsOrCommit1 instanceof GitCommit) { - const commit1 = argsOrCommit1; - - if (commit2 === undefined) { - if (commit1.isUncommitted) { - args = { - repoPath: commit1.repoPath, - lhs: { - sha: 'HEAD', - uri: commit1.uri - }, - rhs: { - sha: '', - uri: commit1.uri - } - }; - } - else { - args = { - repoPath: commit1.repoPath, - lhs: { - sha: - commit1.previousSha !== undefined - ? commit1.previousSha - : GitService.deletedOrMissingSha, - uri: commit1.previousUri! - }, - rhs: { - sha: commit1.sha, - uri: commit1.uri - } - }; - } + if (argsOrCommit instanceof GitCommit) { + const commit = argsOrCommit; + + if (commit.isUncommitted) { + args = { + repoPath: commit.repoPath, + lhs: { + sha: 'HEAD', + uri: commit.uri + }, + rhs: { + sha: '', + uri: commit.uri + }, + line: line + }; } else { args = { - repoPath: commit1.repoPath, + repoPath: commit.repoPath, lhs: { - sha: commit1.sha, - uri: commit1.uri + sha: commit.previousSha !== undefined ? commit.previousSha : GitService.deletedOrMissingSha, + uri: commit.previousUri! }, rhs: { - sha: commit2.sha, - uri: commit2.uri - } + sha: commit.sha, + uri: commit.uri + }, + line: line }; } } else { - args = argsOrCommit1; + args = argsOrCommit; } return super.getMarkdownCommandArgsCore(Commands.DiffWith, args); @@ -88,14 +72,17 @@ export class DiffWithCommand extends ActiveEditorCommand { super(Commands.DiffWith); } - async execute(editor?: TextEditor, uri?: Uri, args: DiffWithCommandArgs = {}): Promise { + async execute(editor?: TextEditor, uri?: Uri, args?: DiffWithCommandArgs): Promise { + if (args === undefined || args.lhs === undefined || args.rhs === undefined) return undefined; + args = { ...args, lhs: { ...(args.lhs as DiffWithCommandArgsRevision) }, rhs: { ...(args.rhs as DiffWithCommandArgsRevision) }, - showOptions: { ...args.showOptions } + showOptions: args.showOptions === undefined ? undefined : { ...args.showOptions } }; - if (args.repoPath === undefined || args.lhs === undefined || args.rhs === undefined) return undefined; + + if (args.repoPath === undefined) return undefined; try { let lhsSha = args.lhs.sha; diff --git a/src/git/formatters/commitFormatter.ts b/src/git/formatters/commitFormatter.ts index c9d0bd3..285f20b 100644 --- a/src/git/formatters/commitFormatter.ts +++ b/src/git/formatters/commitFormatter.ts @@ -123,7 +123,8 @@ export class CommitFormatter extends Formatter { let commands = `[\`${this.id}\`](${ShowQuickCommitDetailsCommand.getMarkdownCommandArgs( this._item.sha )} "Show Commit Details") [\`${GlyphChars.MuchGreaterThan}\`](${DiffWithCommand.getMarkdownCommandArgs( - this._item + this._item, + this._options.line )} "Open Changes") `; if (this._item.previousSha !== undefined) { diff --git a/src/hovers/lineHoverController.ts b/src/hovers/lineHoverController.ts index f2498e7..6506cf6 100644 --- a/src/hovers/lineHoverController.ts +++ b/src/hovers/lineHoverController.ts @@ -107,6 +107,11 @@ export class LineHoverController implements Disposable { } } + let editorLine = position.line; + const line = editorLine + 1; + const commitLine = commit.lines.find(l => l.line === line) || commit.lines[0]; + editorLine = commitLine.originalLine - 1; + const trackedDocument = await Container.tracker.get(document); if (trackedDocument === undefined) return undefined; @@ -115,7 +120,7 @@ export class LineHoverController implements Disposable { Container.config.defaultDateFormat, await Container.git.getRemotes(commit.repoPath), fileAnnotations, - position.line + editorLine ); return new Hover(message, range); }