Diff view: Pass line number to inline editor

In I9825c01ed0c9 Edit button was added to the diff view. In
I0e58a78eb93a codemirror-editor plugin was extended to scroll to
specific line. In Id9d64c0b9c2 gerrit core was extended to pass line
number to codemirror editor.

In this change we put all pieces together and pass in the selected line
number from diff view to the inline editor.

Bug: Issue 11493
Bug: Issue 12364
Bug: Issue 13041
Change-Id: Iba3bf76b21200759a2d2f619eb2157bbebb5a405
diff --git a/polygerrit-ui/app/elements/core/gr-navigation/gr-navigation.js b/polygerrit-ui/app/elements/core/gr-navigation/gr-navigation.js
index 24438eb..2f32706 100644
--- a/polygerrit-ui/app/elements/core/gr-navigation/gr-navigation.js
+++ b/polygerrit-ui/app/elements/core/gr-navigation/gr-navigation.js
@@ -471,11 +471,12 @@
    * @param {{ _number: number, project: string }} change The change object.
    * @param {string} path The file path.
    * @param {number=} opt_patchNum
+   * @param {number=} opt_lineNum
    * @return {string}
    */
-  getEditUrlForDiff(change, path, opt_patchNum) {
+  getEditUrlForDiff(change, path, opt_patchNum, opt_lineNum) {
     return this.getEditUrlForDiffById(change._number, change.project, path,
-        opt_patchNum);
+        opt_patchNum, opt_lineNum);
   },
 
   /**
@@ -484,15 +485,17 @@
    * @param {string} path The file path.
    * @param {number|string=} opt_patchNum The patchNum the file content
    *    should be based on, or ${EDIT_PATCHNUM} if left undefined.
+   * @param {number=} opt_lineNum The line number to pass to the inline editor.
    * @return {string}
    */
-  getEditUrlForDiffById(changeNum, project, path, opt_patchNum) {
+  getEditUrlForDiffById(changeNum, project, path, opt_patchNum, opt_lineNum) {
     return this._getUrlFor({
       view: GerritNav.View.EDIT,
       changeNum,
       project,
       path,
       patchNum: opt_patchNum || EDIT_PATCHNUM,
+      lineNum: opt_lineNum,
     });
   },
 
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
index 33f7cc5..ad72ee1 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view.js
@@ -677,10 +677,12 @@
 
   _goToEditFile() {
     // TODO(taoalpha): add a shortcut for editing
+    const cursorAddress = this.$.cursor.getAddress();
     const editUrl = GerritNav.getEditUrlForDiff(
         this._change,
         this._path,
-        this._patchRange.patchNum
+        this._patchRange.patchNum,
+        cursorAddress && cursorAddress.number
     );
     return GerritNav.navigateToRelativeUrl(editUrl);
   }
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js
index 73e6f0c..8646336 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-view/gr-diff-view_test.js
@@ -507,6 +507,43 @@
       });
     });
 
+    test('edit should redirect to edit page with line number', done => {
+      const lineNumber = 42;
+      element._loggedIn = true;
+      element._path = 't.txt';
+      element._patchRange = {
+        basePatchNum: PARENT,
+        patchNum: '1',
+      };
+      element._change = {
+        _number: 42,
+        project: 'gerrit',
+        status: ChangeStatus.NEW,
+        revisions: {
+          a: {_number: 1, commit: {parents: []}},
+          b: {_number: 2, commit: {parents: []}},
+        },
+      };
+      sinon.stub(element.$.cursor, 'getAddress')
+          .returns({number: lineNumber, isLeftSide: false});
+      const redirectStub = sinon.stub(GerritNav, 'navigateToRelativeUrl');
+      flush(() => {
+        const editBtn = element.shadowRoot
+            .querySelector('.editButton gr-button');
+        assert.isTrue(!!editBtn);
+        MockInteractions.tap(editBtn);
+        assert.isTrue(redirectStub.called);
+        assert.isTrue(redirectStub.lastCall.calledWithExactly(
+            GerritNav.getEditUrlForDiff(
+                element._change,
+                element._path,
+                element._patchRange.patchNum,
+                lineNumber
+            )));
+        done();
+      });
+    });
+
     function isEditVisibile({loggedIn, changeStatus}) {
       return new Promise(resolve => {
         element._loggedIn = loggedIn;