From 0e338308c68b08f17d05573493742b7922487df7 Mon Sep 17 00:00:00 2001
From: Eric Amodio <eamodio@gmail.com>
Date: Thu, 17 Aug 2017 01:18:00 -0400
Subject: [PATCH] Fixes jumpiness when opening a diff

---
 CHANGELOG.md                         |  3 +++
 src/commands/diffLineWithPrevious.ts | 14 ++++++++------
 src/commands/diffWithBranch.ts       | 14 ++++++++------
 src/commands/diffWithNext.ts         | 12 +++++++-----
 src/commands/diffWithPrevious.ts     | 12 +++++++-----
 src/commands/diffWithRevision.ts     | 14 ++++++++------
 src/commands/diffWithWorking.ts      | 14 ++++++++------
 7 files changed, 49 insertions(+), 34 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3c40f6e..017b0d3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
 - Removes unneeded `gitlens.stashExplorer.enabled` configuration setting since users can add or remove custom views natively now
 - Removes unneeded `Toggle Git Stashed Explorer` command (`gitlens.stashExplorer.toggle`) since users can add or remove custom views natively now
 
+## Fixed
+- Fixes jumpiness when opening a diff to a certain line
+
 ## [4.3.3] - 2017-07-28
 ## Added
 - Adds progress indicator for when computing annotations takes a while
diff --git a/src/commands/diffLineWithPrevious.ts b/src/commands/diffLineWithPrevious.ts
index 255e6ed..a849845 100644
--- a/src/commands/diffLineWithPrevious.ts
+++ b/src/commands/diffLineWithPrevious.ts
@@ -1,5 +1,5 @@
 'use strict';
-import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
+import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
 import { ActiveEditorCommand, Commands, getCommandUri } from './common';
 import { BuiltInCommands, GlyphChars } from '../constants';
 import { DiffWithPreviousCommandArgs } from './diffWithPrevious';
@@ -77,16 +77,18 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
                 this.git.getVersionedFile(args.commit.repoPath, args.commit.uri.fsPath, args.commit.sha)
             ]);
 
+            if (args.line !== undefined && args.line !== 0) {
+                if (args.showOptions === undefined) {
+                    args.showOptions = {};
+                }
+                args.showOptions.selection = new Range(args.line, 0, args.line, 0);
+            }
+
             await commands.executeCommand(BuiltInCommands.Diff,
                 Uri.file(lhs),
                 Uri.file(rhs),
                 `${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(gitUri.fsPath)} (${gitUri.shortSha})`,
                 args.showOptions);
-
-            if (args.line === undefined || args.line === 0) return undefined;
-
-            // TODO: Figure out how to focus the left pane
-            return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
         }
         catch (ex) {
             Logger.error(ex, 'DiffWithPreviousLineCommand', 'getVersionedFile');
diff --git a/src/commands/diffWithBranch.ts b/src/commands/diffWithBranch.ts
index 94a7e34..b4c2f60 100644
--- a/src/commands/diffWithBranch.ts
+++ b/src/commands/diffWithBranch.ts
@@ -1,5 +1,5 @@
 'use strict';
-import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
+import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
 import { ActiveEditorCommand, Commands, getCommandUri } from './common';
 import { BuiltInCommands, GlyphChars } from '../constants';
 import { GitService, GitUri } from '../gitService';
@@ -45,16 +45,18 @@ export class DiffWithBranchCommand extends ActiveEditorCommand {
         try {
             const compare = await this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, branch);
 
+            if (args.line !== undefined && args.line !== 0) {
+                if (args.showOptions === undefined) {
+                    args.showOptions = {};
+                }
+                args.showOptions.selection = new Range(args.line, 0, args.line, 0);
+            }
+
             await commands.executeCommand(BuiltInCommands.Diff,
                 Uri.file(compare),
                 gitUri.fileUri(),
                 `${path.basename(gitUri.fsPath)} (${branch}) ${GlyphChars.ArrowLeftRight} ${path.basename(gitUri.fsPath)}`,
                 args.showOptions);
-
-            if (args.line === undefined || args.line === 0) return undefined;
-
-            // TODO: Figure out how to focus the left pane
-            return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
         }
         catch (ex) {
             Logger.error(ex, 'DiffWithBranchCommand', 'getVersionedFile');
diff --git a/src/commands/diffWithNext.ts b/src/commands/diffWithNext.ts
index edcbd81..593df31 100644
--- a/src/commands/diffWithNext.ts
+++ b/src/commands/diffWithNext.ts
@@ -60,16 +60,18 @@ export class DiffWithNextCommand extends ActiveEditorCommand {
                 this.git.getVersionedFile(args.commit.repoPath, args.commit.uri.fsPath, args.commit.sha)
             ]);
 
+            if (args.line !== undefined && args.line !== 0) {
+                if (args.showOptions === undefined) {
+                    args.showOptions = {};
+                }
+                args.showOptions.selection = new Range(args.line, 0, args.line, 0);
+            }
+
             await commands.executeCommand(BuiltInCommands.Diff,
                 Uri.file(lhs),
                 Uri.file(rhs),
                 `${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(args.commit.nextUri.fsPath)} (${args.commit.nextShortSha})`,
                 args.showOptions);
-
-            if (args.line === undefined || args.line === 0) return undefined;
-
-            // TODO: Figure out how to focus the left pane
-            return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
         }
         catch (ex) {
             Logger.error(ex, 'DiffWithNextCommand', 'getVersionedFile');
diff --git a/src/commands/diffWithPrevious.ts b/src/commands/diffWithPrevious.ts
index cb0af49..c7ea727 100644
--- a/src/commands/diffWithPrevious.ts
+++ b/src/commands/diffWithPrevious.ts
@@ -59,16 +59,18 @@ export class DiffWithPreviousCommand extends ActiveEditorCommand {
                 this.git.getVersionedFile(args.commit.repoPath, args.commit.previousUri.fsPath, args.commit.previousSha)
             ]);
 
+            if (args.line !== undefined && args.line !== 0) {
+                if (args.showOptions === undefined) {
+                    args.showOptions = {};
+                }
+                args.showOptions.selection = new Range(args.line, 0, args.line, 0);
+            }
+
             await commands.executeCommand(BuiltInCommands.Diff,
                 Uri.file(lhs),
                 Uri.file(rhs),
                 `${path.basename(args.commit.previousUri.fsPath)} (${args.commit.previousShortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha})`,
                 args.showOptions);
-
-            if (args.line === undefined || args.line === 0) return undefined;
-
-            // TODO: Figure out how to focus the left pane
-            return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
         }
         catch (ex) {
             Logger.error(ex, 'DiffWithPreviousCommand', 'getVersionedFile');
diff --git a/src/commands/diffWithRevision.ts b/src/commands/diffWithRevision.ts
index 1310702..4954a77 100644
--- a/src/commands/diffWithRevision.ts
+++ b/src/commands/diffWithRevision.ts
@@ -1,5 +1,5 @@
 'use strict';
-import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
+import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
 import { ActiveEditorCommand, Commands, getCommandUri } from './common';
 import { BuiltInCommands, GlyphChars } from '../constants';
 import { GitService, GitUri } from '../gitService';
@@ -48,16 +48,18 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
 
             const compare = await this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, pick.commit.sha);
 
+            if (args.line !== undefined && args.line !== 0) {
+                if (args.showOptions === undefined) {
+                    args.showOptions = {};
+                }
+                args.showOptions.selection = new Range(args.line, 0, args.line, 0);
+            }
+
             await commands.executeCommand(BuiltInCommands.Diff,
                 Uri.file(compare),
                 gitUri.fileUri(),
                 `${path.basename(gitUri.fsPath)} (${pick.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(gitUri.fsPath)}`,
                 args.showOptions);
-
-            if (args.line === undefined || args.line === 0) return undefined;
-
-            // TODO: Figure out how to focus the left pane
-            return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
         }
         catch (ex) {
             Logger.error(ex, 'DiffWithRevisionCommand', 'getVersionedFile');
diff --git a/src/commands/diffWithWorking.ts b/src/commands/diffWithWorking.ts
index ab849cc..0bcd88f 100644
--- a/src/commands/diffWithWorking.ts
+++ b/src/commands/diffWithWorking.ts
@@ -1,5 +1,5 @@
 'use strict';
-import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
+import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
 import { ActiveEditorCommand, Commands, getCommandUri } from './common';
 import { BuiltInCommands, GlyphChars } from '../constants';
 import { GitCommit, GitService, GitUri } from '../gitService';
@@ -51,16 +51,18 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
         try {
             const compare = await this.git.getVersionedFile(args.commit.repoPath, args.commit.uri.fsPath, args.commit.sha);
 
+            if (args.line !== undefined && args.line !== 0) {
+                if (args.showOptions === undefined) {
+                    args.showOptions = {};
+                }
+                args.showOptions.selection = new Range(args.line, 0, args.line, 0);
+            }
+
             await commands.executeCommand(BuiltInCommands.Diff,
                 Uri.file(compare),
                 Uri.file(path.resolve(gitUri.repoPath, workingFileName)),
                 `${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(workingFileName)}`,
                 args.showOptions);
-
-            if (args.line === undefined || args.line === 0) return undefined;
-
-            // TODO: Figure out how to focus the left pane
-            return await commands.executeCommand(BuiltInCommands.RevealLine, { lineNumber: args.line, at: 'center' });
         }
         catch (ex) {
             Logger.error(ex, 'DiffWithWorkingCommand', 'getVersionedFile');