CodeMirror:缩进不适用于字符串

时间:2018-06-16 05:13:02

标签: javascript java codemirror

我想在文本区域编写代码时应用缩进,为此我有这样的renderLine事件。

setTimeout(function(test) {
     var editor = CodeMirror.fromTextArea(document.getElementById('javaBody'), {
         lineNumbers: true,
         matchBrackets: true,
         styleActiveLine: true,
         extraKeys: {
             ".": function(editor) {
                 setTimeout(function() {
                     editor.execCommand("autocomplete");
                 }, 100);
                 throw CodeMirror.Pass; // tell CodeMirror we didn't handle the key
             }
         },
         gutters: ["CodeMirror-lint-markers"],
         lint: true,
         mode: "text/x-java"
     });
     editor.markClean();
     editor.on("keyup", function(cm, event) {
         var keyCode = event.keyCode || event.which;
         if (!ExcludedIntelliSenseTriggerKeys[(event.keyCode || event.which).toString()]) {
             if (timeout) clearTimeout(timeout);
             timeout = setTimeout(function() {
                 editor.showHint({
                     hint: CodeMirror.hint.auto,
                     completeSingle: false
                 });
             }, 150);
         }
     });
     var charWidth = editor.defaultCharWidth(),
         basePadding = 4;
     editor.on("renderLine", function(cm, line, elt) {
         var off = CodeMirror.countColumn(line.text, null, cm.getOption("tabSize")) * charWidth;
         elt.style.textIndent = "-" + off + "px";
         elt.style.paddingLeft = (basePadding + off) + "px";
     });
     editor.refresh();
     globalEditor1 = $('.CodeMirror')[0].CodeMirror;
     if (localStorage && localStorage.getItem('javaEditorTheme')) {
         globalEditor1.setOption("theme", localStorage.getItem('javaEditorTheme'));
     }
     //globalEditor1.(localStorage.getItem('javaEditorTheme'));
 }), 2000

我甚至在保存时应用了自动注释,如下所示:

editor.indentSelection("smart");

但它仍然没有纠正字符串参数:

例如:

public static String getAnimalNameById(Integer x) {
    Http h = new Http();
    HttpRequest req = new HttpRequest();

    //req.setEndpoint("https://th-apex-http-callout.herokuapp.com/animals/" + x);
    req.setEndpoint("https://th-apex-http-callout.herokuapp.com/animals/ad as dnaskd nlas fnasklnflaksn falsk fna;ksnflajsbfnaksn flasn fkasnf ansfkansfans f.ans .fnas. fnas fnas fnasnf asnf asnfa jsbfaks bfkasb fkasbf kasbfk absfba sf" + x);

    req.setMethod("GET");


    HttpResponse res = h.send(req);
    Map < String, Object > results = (Map < String, Object > ) JSON.deserializeUntyped(res.getBody());
    Map < String, Object > animal = (Map < String, Object > ) results.get('animal');
    return (String) animal.get('name');
}

在上面的代码中,它会缩进代码,但不会尝试将setEndPoint字符串分成两行'+'符号。 有没有办法实现这个?

0 个答案:

没有答案