如何为新语言编写括号扩展?

How to write a brackets extension for a new language?

我正在为一种新的文本语言编写括号扩展,它是 html 和车把 + javascript 的混合体。我想使用现有的 CodeMirror 模式编写代码模式,例如html混合,javascript,车把。

这是我目前拥有的 (main.js),

define(function (require, exports, module) {

    var CodeMirror = brackets.getModule("thirdparty/CodeMirror/lib/codemirror"),
        LanguageManager = brackets.getModule("language/LanguageManager");

    CodeMirror.defineMode('htmlbars', function (config, parserConfig) {

        var htmlMode = CodeMirror.getMode(config, "htmlmixed"),
            javascriptMode = CodeMirror.getMode(config, "javascript"),
            handlebarsMode = CodeMirror.getMode(config, "handlebars");

        function html(stream, state) {
            var style = htmlMode.token(stream, state.htmlState);
            return style; 
        }

        return {
            startState: function() {
                var state = htmlMode.startState();
                return {token: html, localMode: null, localState: null, htmlState: state};
            },

            copyState: function(state) {
                if (state.localState)
                    var local = CodeMirror.copyState(state.localMode, state.localState);
                    return { token: state.token, localMode: state.localMode, localState: local,
                             htmlState: CodeMirror.copyState(htmlMode, state.htmlState)};
            },

            token: function(stream, state) {
                return state.token(stream, state);
            },

            indent: function(state, textAfter) {
                if (!state.localMode || /^\s*<\//.test(textAfter))
                    return htmlMode.indent(state.htmlState, textAfter);
                else if (state.localMode.indent)
                    return state.localMode.indent(state.localState, textAfter);
                else
                    return CodeMirror.Pass;
            },

            innerMode: function(state) {
                return {state: state.localState || state.htmlState, mode: state.localMode || htmlMode};
            }
        };
    }); 

    CodeMirror.defineMIME("text/x-xhbs", "htmlbars");

    LanguageManager.defineLanguage("htmlbars", {
        name: "Htmlbars",
        mode: ["htmlbars", "text/x-hbs"],
        fileExtensions: ["xhbs"]
    });

});

所以我想知道的是如果元素以 <% 开头并以 %> 结尾等,如何在这里定义使用 javascript 模式...目前它仅适用于 html

如果需要更多详细信息,请告诉我。

谢谢! :)

您可以使用 CodeMirror 轻松实现此目的 Multiplex Mode,

例如

define(function (require, exports, module) {

    var CodeMirror = brackets.getModule("thirdparty/CodeMirror/lib/codemirror"),
        LanguageManager = brackets.getModule("language/LanguageManager");

    brackets.getModule(["thirdparty/CodeMirror/mode/handlebars/handlebars", "thirdparty/CodeMirror/mode/javascript/javascript"], function () {
        CodeMirror.defineMode("htmlbars", function (config) {
            return CodeMirror.multiplexingMode(
                CodeMirror.getMode(config, "text/html"),
                {
                    open: "<%",
                    close: "%>",
                    mode: CodeMirror.getMode(config, "javascript"),
                    parseDelimiters: true
                },
                {
                    open: "{{",
                    close: "}}",
                    mode: CodeMirror.getMode(config, "handlebars"),
                    parseDelimiters: true
                }
            );
        });

        CodeMirror.defineMIME("text/x-xhbs", "htmlbars");

        LanguageManager.defineLanguage("htmlbars", {
            name: "Htmlbars",
            mode: ["htmlbars", "text/x-hbs"],
            fileExtensions: ["xhbs"]
        });
    });

});

官方演示:https://codemirror.net/demo/multiplex.html

希望这对其他人有所帮助。 :)