Browse Source

Merge pull request #2781 from blink1073/editor-search

Add support for find and replace in the editor
Afshin Darian 7 years ago
parent
commit
3b4c1a3df5

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

@@ -46,6 +46,12 @@ namespace CommandIDs {
 
   export
   const changeMode = 'codemirror:change-mode';
+
+  export
+  const find = 'codemirror:find';
+
+  export
+  const findAndReplace = 'codemirror:find-and-replace';
 };
 
 
@@ -195,6 +201,32 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
       isToggled: args => args['keyMap'] === keyMap
     });
 
+    commands.addCommand(CommandIDs.find, {
+      label: 'Find',
+      execute: () => {
+        let widget = tracker.currentWidget;
+        if (!widget) {
+          return;
+        }
+        let editor = widget.editor as CodeMirrorEditor;
+        editor.execCommand('find');
+      },
+      isEnabled: hasWidget
+    });
+
+    commands.addCommand(CommandIDs.findAndReplace, {
+      label: 'Find & Replace',
+      execute: () => {
+        let widget = tracker.currentWidget;
+        if (!widget) {
+          return;
+        }
+        let editor = widget.editor as CodeMirrorEditor;
+        editor.execCommand('replace');
+      },
+      isEnabled: hasWidget
+    });
+
     commands.addCommand(CommandIDs.changeMode, {
       label: args => args['name'] as string,
       execute: args => {
@@ -264,6 +296,8 @@ function activateEditorCommands(app: JupyterLab, tracker: IEditorTracker, mainMe
 
     menu.addItem({ type: 'submenu', submenu: modeMenu });
     menu.addItem({ type: 'submenu', submenu: tabMenu });
+    menu.addItem({ command: CommandIDs.find });
+    menu.addItem({ command: CommandIDs.findAndReplace });
     menu.addItem({ type: 'separator' });
     menu.addItem({ command: 'fileeditor:toggle-line-numbers' });
     menu.addItem({ command: 'fileeditor:toggle-line-wrap' });

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

@@ -35,6 +35,8 @@ import {
 import 'codemirror/addon/edit/matchbrackets.js';
 import 'codemirror/addon/edit/closebrackets.js';
 import 'codemirror/addon/comment/comment.js';
+import 'codemirror/addon/search/searchcursor';
+import 'codemirror/addon/search/search';
 import 'codemirror/keymap/emacs.js';
 import 'codemirror/keymap/sublime.js';
 import 'codemirror/keymap/vim.js';
@@ -472,6 +474,15 @@ class CodeMirrorEditor implements CodeEditor.IEditor {
     this.doc.setSelections(cmSelections, 0);
   }
 
+  /**
+   * Execute a codemirror command on the editor.
+   *
+   * @param command - The name of the command to execute.
+   */
+  execCommand(command: string): void {
+    this._editor.execCommand(command);
+  }
+
   /**
    * Handle keydown events from the editor.
    */

+ 0 - 1
packages/codemirror/style/index.css

@@ -187,4 +187,3 @@
   background-position: right;
   background-repeat: no-repeat;
 }
-

+ 5 - 0
packages/codemirror/typings/codemirror/codemirror.d.ts

@@ -437,6 +437,11 @@ declare module CodeMirror {
         triggerOnKeyDown(event: Event): void;
         triggerOnKeyPress(event: Event): void;
         triggerOnKeyUp(event: Event): void;
+
+        /**
+         * Execute a command on the editor.
+         */
+        execCommand(command: string): any;
     }
 
     interface EditorFromTextArea extends Editor {