10

次のようなキーワードのみを強調したいと思います: (基本的には、単一の括弧で囲ま{KEYWORD} れた大文字の単語){}

Mustache Overlay demoからコードをコピーし、二重かっこを単一のものに置き換えて、これを試しました。

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);
});

しかし、それはあまりうまくいきません:)

何か案は?

4

2 に答える 2

6

Mustache の例では、2 文字の区切り文字を処理する必要があるため、特別な処理があります (たとえば、 と に 2 つの文字が'{{'あり'}}'ます)。これまで CodeMirror を使用したことがないため、これは単なる推測ですが、次のようにしてみてください。

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);
});

編集

機能します(ただし、小文字の単語も強調表示されます)

これはうまくいくはずです:

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;
}
于 2011-06-15T22:06:40.430 に答える
3

受け入れられた回答は、括弧内のすべての文字を強調表示します。

ここに画像の説明を入力

私のバージョン、他の誰かが同じ問題に遭遇した場合。

ここに画像の説明を入力

CodeMirror.defineMode('mymode', function (config, parserConfig) {
    return {
        /**
         * @param {CodeMirror.StringStream} stream
         */
        token: function (stream) {
            // check for {
            if (stream.match('{')) {
                // trying to find }

                // if not a char
                if (!stream.eatWhile(/[\w]/)) {
                    return null;
                }

                if (stream.match('}')) {
                    return 'mymode';
                }
            }

            while (stream.next() && !stream.match('{', false)) {}

            return null;
        }
    };
});
于 2014-10-13T12:12:03.233 に答える