codemirror:如何在按tab键时缩进整行

codemirror : how to indent the whole line when pressing tab?

本文关键字:缩进 tab codemirror      更新时间:2023-09-26

我正在为代码镜像创建一个新的简单模式。

我希望当用户按下"tab"时,整行都会缩进(而不是只将光标后面的那部分行"一分为二")。

最简单的方法是什么?

注意:不必在模式中定义相应的代码。任何其他方法(例如附加组件或配置)也可以。

只需将选项卡的键映射更改为indentMore:

extraKeys: {
    "Tab": "indentMore"
}

此解决方案也不会破坏选择缩进。

Fiddle

这应该可以工作。jsfiddle

    extraKeys: {
        "Tab": function(cm){
            // get cursor position
            var pos = cm.getCursor();
            // set cursor position to the begining of the line.
            cm.setCursor({ line: pos.line, ch: 0 });
            // insert a tab
            cm.replaceSelection("'t", "end");
            // set cursor position to original.
            cm.setCursor({ line: pos.line, ch: pos.ch + 1 });
        }
     }

关于手册:

extraKeys: {
  'Tab': 'indentAuto'
}
  • 附加密钥:http://codemirror.net/doc/manual.html#option_extraKeys
  • indentAuto命令:http://codemirror.net/doc/manual.html#command_indentAuto