Browse Source

"go to line" action in text editor (#5377)

* fixup! Add a find/replacer semantic extension point.

* Add a go to line semantic extension point.

* codemirror-extension: add go to line

This uses the default "Alt+G" shortcut.
Jérome Perrin 6 years ago
parent
commit
3ea6a39264

+ 28 - 0
packages/codemirror-extension/src/index.ts

@@ -32,6 +32,8 @@ namespace CommandIDs {
   export const find = 'codemirror:find';
 
   export const findAndReplace = 'codemirror:find-and-replace';
+
+  export const goToLine = 'codemirror:go-to-line';
 }
 
 /**
@@ -212,6 +214,19 @@ function activateEditorCommands(
     isEnabled
   });
 
+  commands.addCommand(CommandIDs.goToLine, {
+    label: 'Go to Line...',
+    execute: () => {
+      let widget = tracker.currentWidget;
+      if (!widget) {
+        return;
+      }
+      let editor = widget.content.editor as CodeMirrorEditor;
+      editor.execCommand('jumpToLine');
+    },
+    isEnabled
+  });
+
   commands.addCommand(CommandIDs.changeMode, {
     label: args => args['name'] as string,
     execute: args => {
@@ -308,4 +323,17 @@ function activateEditorCommands(
       editor.execCommand('replace');
     }
   } as IEditMenu.IFindReplacer<IDocumentWidget<FileEditor>>);
+
+  // Add go to line capabilities to the edit menu.
+  mainMenu.editMenu.goToLiners.add({
+    tracker,
+    find: (widget: IDocumentWidget<FileEditor>) => {
+      let editor = widget.content.editor as CodeMirrorEditor;
+      editor.execCommand('jumpToLine');
+    },
+    goToLine: (widget: IDocumentWidget<FileEditor>) => {
+      let editor = widget.content.editor as CodeMirrorEditor;
+      editor.execCommand('jumpToLine');
+    }
+  } as IEditMenu.IGoToLiner<IDocumentWidget<FileEditor>>);
 }

+ 1 - 0
packages/codemirror/src/editor.ts

@@ -31,6 +31,7 @@ import 'codemirror/addon/edit/closebrackets.js';
 import 'codemirror/addon/scroll/scrollpastend.js';
 import 'codemirror/addon/search/searchcursor';
 import 'codemirror/addon/search/search';
+import 'codemirror/addon/search/jump-to-line';
 import 'codemirror/keymap/emacs.js';
 import 'codemirror/keymap/sublime.js';
 import 'codemirror/keymap/vim.js';

+ 8 - 0
packages/mainmenu-extension/src/index.ts

@@ -47,6 +47,8 @@ export namespace CommandIDs {
 
   export const findAndReplace = 'editmenu:find-and-replace';
 
+  export const goToLine = 'editmenu:go-to-line';
+
   export const closeAndCleanup = 'filemenu:close-and-cleanup';
 
   export const persistAndSave = 'filemenu:persist-and-save';
@@ -205,6 +207,12 @@ export function createEditMenu(app: JupyterLab, menu: EditMenu): void {
     [{ command: CommandIDs.find }, { command: CommandIDs.findAndReplace }],
     200
   );
+  commands.addCommand(CommandIDs.goToLine, {
+    label: 'Go to Line…',
+    isEnabled: Private.delegateEnabled(app, menu.goToLiners, 'goToLine'),
+    execute: Private.delegateExecute(app, menu.goToLiners, 'goToLine')
+  });
+  menu.addGroup([{ command: CommandIDs.goToLine }], 200);
 }
 
 /**

+ 24 - 2
packages/mainmenu/src/edit.ts

@@ -20,9 +20,14 @@ export interface IEditMenu extends IJupyterLabMenu {
   readonly clearers: Set<IEditMenu.IClearer<Widget>>;
 
   /**
-   * A set storing IClearers for the Edit menu.
+   * A set storing IFindReplacers for the Edit menu.
    */
   readonly findReplacers: Set<IEditMenu.IFindReplacer<Widget>>;
+
+  /**
+   * A set storing IGoToLiners for the Edit menu.
+   */
+  readonly goToLiners: Set<IEditMenu.IGoToLiner<Widget>>;
 }
 
 /**
@@ -41,6 +46,8 @@ export class EditMenu extends JupyterLabMenu implements IEditMenu {
     this.clearers = new Set<IEditMenu.IClearer<Widget>>();
 
     this.findReplacers = new Set<IEditMenu.IFindReplacer<Widget>>();
+
+    this.goToLiners = new Set<IEditMenu.IGoToLiner<Widget>>();
   }
 
   /**
@@ -54,10 +61,15 @@ export class EditMenu extends JupyterLabMenu implements IEditMenu {
   readonly clearers: Set<IEditMenu.IClearer<Widget>>;
 
   /**
-   * A set storing IClearers for the Edit menu.
+   * A set storing IFindReplacers for the Edit menu.
    */
   readonly findReplacers: Set<IEditMenu.IFindReplacer<Widget>>;
 
+  /**
+   * A set storing IGoToLiners for the Edit menu.
+   */
+  readonly goToLiners: Set<IEditMenu.IGoToLiner<Widget>>;
+
   /**
    * Dispose of the resources held by the edit menu.
    */
@@ -127,4 +139,14 @@ export namespace IEditMenu {
      */
     findAndReplace?: (widget: T) => void;
   }
+
+  /**
+   * Interface for an activity that uses Go to Line.
+   */
+  export interface IGoToLiner<T extends Widget> extends IMenuExtender<T> {
+    /**
+     * Execute a go to line command for the activity.
+     */
+    goToLine?: (widget: T) => void;
+  }
 }