简体   繁体   中英

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

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 (eg add on or configuration) would work as well.

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'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM