3

我想定义我的自定义模式以扩展一种可用模式(例如 javascript)?我尝试使用下面的代码,但没有效果。

CodeMirror.defineMode("foo", function (config, parserConfig) {
        var fooOverlay = {
            token: function (stream, state) {
                var ch;
                if (stream.match("{{")) {
                    while ((ch = stream.next()) != null)
                        if (ch == "}" && stream.next() == "}") {
                            stream.eat("}");
                            return "foo";
                        }
                }
                while (stream.next() != null && !stream.match("{{", false)) { }
                return null;
            }
        };
        return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "javascript"), fooOverlay);
    });

this.config = {
        lineNumbers: true,
        mode: 'foo'
    };

更新:

CodeMirror.defineSimpleMode("foo", {
        start = [
            { regex: /"(?:[^\\]|\\.)*?(?:"|$)/, token: "string" },
            { regex: /'(?:[^\\]|\\.)*?(?:'|$)/, token: "string" },
            {
                regex: /(?:if|foreach|in|while)\b/,
                token: "keyword"
            },
            { regex: /true|false|null|undefined/, token: "atom" },
        ];
    });
4

0 回答 0