最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - codemirror : how to indent the whole line when pressing tab? - Stack Overflow

programmeradmin0浏览0评论

I am creating a new simple mode for codemirror.

I would like that when the user presses "tab", the whole line gets indented (as opposed to only the part of the line that is after the cursor, "splitting" the line in two).

What would be the simplest way to do this ?

note : the corresponding code does not have to be defined in the mode. Any other approach (e.g. add on or configuration) would work as well.

I am creating a new simple mode for codemirror.

I would like that when the user presses "tab", the whole line gets indented (as opposed to only the part of the line that is after the cursor, "splitting" the line in two).

What would be the simplest way to do this ?

note : the corresponding code does not have to be defined in the mode. Any other approach (e.g. add on or configuration) would work as well.

Share Improve this question asked Oct 20, 2014 at 9:16 VinceVince 4,43912 gold badges48 silver badges83 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

Simply change tab's keymap to indentMore:

extraKeys: {
    "Tab": "indentMore"
}

This solution also doesn't break selection indenting.

Fiddle

This should work. 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 });
        }
     }

Regarding the manual:

extraKeys: {
  'Tab': 'indentAuto'
}
  • Extra keys: http://codemirror/doc/manual.html#option_extraKeys
  • indentAuto mand: http://codemirror/doc/manual.html#mand_indentAuto
发布评论

评论列表(0)

  1. 暂无评论