为CodeMirror创建新模式 [英] Creating new modes for CodeMirror

查看:203
本文介绍了为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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆