为CodeMirror创建新模式 [英] Creating new modes for CodeMirror
问题描述
我只想突出显示如下所示的关键字: {KEYWORD}
(基本上在单个 {}之间包裹大写单词) / code>括号)
I want to highlight only keywords that look like this: {KEYWORD}
(basically UPPERCASE words wrapped between single {}
parentheses)
我通过复制胡子叠印演示,并用单括号替换双括号:
I tried this by copying the code from the Mustache Overlay demo, and by replacing the double brackets with single ones:
CodeMirror.defineMode('mymode', function(config, parserConfig) {
var mymodeOverlay = {
token: function(stream, state) {
if (stream.match("{")) {
while ((ch = stream.next()) != null)
if (ch == "}" && stream.next() == "}") break;
return 'mymode';
}
while (stream.next() != null && !stream.match("{", false)) {}
return null;
}
};
return CodeMirror.overlayParser(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), mymodeOverlay);
});
但效果不佳:)
有什么想法吗?
推荐答案
在Mustache示例中有特殊处理,因为它需要处理2个字符的分隔符(例如,在'{{'
和'}}'
中有两个字符。我以前从未使用过CodeMirror,所以这只是一个猜测,不过请尝试这样的事情:
There is special handling in the Mustache example because it needs to handle 2-character delimiters (e.g. there are two characters in '{{'
and '}}'
). I've never used CodeMirror before, so this is just a guess, but try something like this:
CodeMirror.defineMode("mymode", function(config, parserConfig) {
var mymodeOverlay = {
token: function(stream, state) {
if (stream.match("{")) {
while ((ch = stream.next()) != null)
if (ch == "}") break;
return "mymode";
}
while (stream.next() != null && !stream.match("{", false)) {}
return null;
}
};
return CodeMirror.overlayParser(CodeMirror.getMode(config, parserConfig.backdrop || "text/html"), mymodeOverlay);
});
编辑
有效(尽管它也突出显示小写字母的单词)
it works (though it highlights words with lowercase letters too)
此应该有效:
token: function(stream, state) {
if (stream.match("{")) {
while ((ch = stream.next()) != null && ch === ch.toUpperCase())
if (ch == "}") break;
return "mymode";
}
while (stream.next() != null && !stream.match("{", false)) {}
return null;
}
这篇关于为CodeMirror创建新模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!