我正在为新的文本语言编写括号扩展名,这是html和把手+ javascript的混合。我想通过使用现有的CodeMirror模式来编写代码模式,例如htmlmixed,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
如果需要更多细节,请告诉我。
谢谢! :)
答案 0 :(得分:1)
您可以使用CodeMirror Multiplex Mode,
轻松实现此目的e.g。
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
希望这会有助于其他人。 :)