Browse Source

Merge pull request #6699 from aschlaep/fix-docsearch-no-match-scroll

Eliminate scrolling on search with no match
Jason Grout 5 years ago
parent
commit
daa73f578d

+ 5 - 2
packages/codemirror/src/editor.ts

@@ -522,9 +522,12 @@ export class CodeMirrorEditor implements CodeEditor.IEditor {
    * #### Notes
    * This will remove any secondary cursors.
    */
-  setCursorPosition(position: CodeEditor.IPosition): void {
+  setCursorPosition(
+    position: CodeEditor.IPosition,
+    options?: { bias?: number; origin?: string; scroll?: boolean }
+  ): void {
     const cursor = this._toCodeMirrorPosition(position);
-    this.doc.setCursor(cursor);
+    this.doc.setCursor(cursor, undefined, options);
     // If the editor does not have focus, this cursor change
     // will get screened out in _onCursorsChanged(). Make an
     // exception for this method.

+ 1 - 1
packages/documentsearch/src/providers/codemirrorsearchprovider.ts

@@ -441,7 +441,7 @@ export class CodeMirrorSearchProvider implements ISearchProvider {
       if (!cursor.find(reverse)) {
         // if we don't want to loop, no more matches found, reset the cursor and exit
         if (this.isSubProvider) {
-          this._cm.setCursorPosition(position);
+          this._cm.setCursorPosition(position, { scroll: false });
           this._currentMatch = null;
           return null;
         }

+ 15 - 9
packages/documentsearch/src/providers/notebooksearchprovider.ts

@@ -108,7 +108,7 @@ export class NotebookSearchProvider implements ISearchProvider {
       });
     }
 
-    this._currentMatch = await this._stepNext();
+    this._currentMatch = await this._stepNext(searchTarget.content.activeCell);
 
     return allMatches;
   }
@@ -173,7 +173,9 @@ export class NotebookSearchProvider implements ISearchProvider {
    * @returns A promise that resolves once the action has completed.
    */
   async highlightNext(): Promise<ISearchMatch | undefined> {
-    this._currentMatch = await this._stepNext();
+    this._currentMatch = await this._stepNext(
+      this._searchTarget.content.activeCell
+    );
     return this._currentMatch;
   }
 
@@ -183,7 +185,10 @@ export class NotebookSearchProvider implements ISearchProvider {
    * @returns A promise that resolves once the action has completed.
    */
   async highlightPrevious(): Promise<ISearchMatch | undefined> {
-    this._currentMatch = await this._stepNext(true);
+    this._currentMatch = await this._stepNext(
+      this._searchTarget.content.activeCell,
+      true
+    );
     return this._currentMatch;
   }
 
@@ -270,11 +275,11 @@ export class NotebookSearchProvider implements ISearchProvider {
   readonly isReadOnly = false;
 
   private async _stepNext(
+    activeCell: Cell,
     reverse = false,
     steps = 0
   ): Promise<ISearchMatch | undefined> {
     const notebook = this._searchTarget.content;
-    const activeCell: Cell = notebook.activeCell;
     const cellIndex = notebook.widgets.indexOf(activeCell);
     const numCells = notebook.widgets.length;
     const { provider } = this._cmSearchProviders[cellIndex];
@@ -293,11 +298,11 @@ export class NotebookSearchProvider implements ISearchProvider {
       if (steps === numCells) {
         return undefined;
       }
-      notebook.activeCellIndex =
+      const nextIndex =
         ((reverse ? cellIndex - 1 : cellIndex + 1) + numCells) % numCells;
-      const editor = notebook.activeCell.editor as CodeMirrorEditor;
+      const editor = notebook.widgets[nextIndex].editor as CodeMirrorEditor;
       // move the cursor of the next cell to the start/end of the cell so it can
-      // search the whole thing
+      // search the whole thing (but don't scroll because we haven't found anything yet)
       const newPosCM = reverse
         ? CodeMirror.Pos(editor.lastLine())
         : CodeMirror.Pos(editor.firstLine(), 0);
@@ -305,10 +310,11 @@ export class NotebookSearchProvider implements ISearchProvider {
         line: newPosCM.line,
         column: newPosCM.ch
       };
-      editor.setCursorPosition(newPos);
-      return this._stepNext(reverse, steps + 1);
+      editor.setCursorPosition(newPos, { scroll: false });
+      return this._stepNext(notebook.widgets[nextIndex], reverse, steps + 1);
     }
 
+    notebook.activeCellIndex = cellIndex;
     return match;
   }