Browse Source

Merge pull request #2868 from blink1073/cm-tab

Add a command for intelligent tab
Brian E. Granger 7 years ago
parent
commit
7b0a8d28db
2 changed files with 34 additions and 3 deletions
  1. 32 1
      packages/codemirror/src/editor.ts
  2. 2 2
      packages/codemirror/src/factory.ts

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

@@ -1033,6 +1033,29 @@ namespace Private {
     }
   }
 
+  /**
+   * Indent or insert a tab as appropriate.
+   */
+  export
+  function indentMoreOrinsertTab(cm: CodeMirror.Editor): void {
+    let doc = cm.getDoc();
+    let from = doc.getCursor('from');
+    let to = doc.getCursor('to');
+    let sel = !posEq(from, to);
+    if (sel) {
+      CodeMirror.commands['indentMore'](cm);
+      return;
+    }
+    // Check for start of line.
+    let line = doc.getLine(from.line);
+    let before = line.slice(0, from.ch);
+    if (/^\s*$/.test(before)) {
+      CodeMirror.commands['indentMore'](cm);
+    } else {
+      CodeMirror.commands['insertSoftTab'](cm);
+    }
+  }
+
   /**
    * Delete spaces to the previous tab stob in a codemirror editor.
    */
@@ -1061,7 +1084,7 @@ namespace Private {
     } else {
       CodeMirror.commands['delCharBefore'](cm);
     }
-  };
+  }
 
   /**
    * Test whether two CodeMirror positions are equal.
@@ -1134,3 +1157,11 @@ CodeMirrorEditor.addCommand(
   'delSpaceToPrevTabStop', Private.delSpaceToPrevTabStop
 );
 
+
+/**
+ * Add a CodeMirror command to indent or insert a tab as appropriate.
+ */
+CodeMirrorEditor.addCommand(
+  'indentMoreOrinsertTab', Private.indentMoreOrinsertTab
+);
+

+ 2 - 2
packages/codemirror/src/factory.ts

@@ -25,7 +25,7 @@ class CodeMirrorEditorFactory implements IEditorFactoryService {
         'Cmd-Right': 'goLineRight',
         'End': 'goLineRight',
         'Cmd-Left': 'goLineLeft',
-        'Tab': 'indentMore',
+        'Tab': 'indentMoreOrinsertTab',
         'Shift-Tab': 'indentLess',
         'Cmd-Alt-[': 'indentAuto',
         'Ctrl-Alt-[': 'indentAuto',
@@ -37,7 +37,7 @@ class CodeMirrorEditorFactory implements IEditorFactoryService {
     this.documentCodeMirrorConfig = {
       ...CodeMirrorEditor.defaultConfig,
       extraKeys: {
-        'Tab': 'indentMore',
+        'Tab': 'indentMoreOrinsertTab',
         'Shift-Enter': () => { /* no-op */ }
       },
       lineNumbers: true,