Codemirror, defineMode在双引号内

Codemirror, defineMode inside doublequotes

本文关键字:defineMode Codemirror      更新时间:2023-09-26

我正在编写一个自定义覆盖来创建类型为engage的令牌,用于一些自定义功能/样式。

我现在在双引号创建令牌,如"EXP=SOMETHING"我需要虽然只有在双引号之间:EXP=SOMETHING,我可以轻松地跳过第一个报价并得到类似EXP=SOMETHING"但我似乎无法找到一个可行的方法,跳过最后一个引用,在这个问题上我一直敲我的头这么久我开始认为这是不可能的,备份的字符返回EXCEPTION: Uncaught (in promise): Error: Mode engage failed to advance stream.这是有意义的。我肯定我遗漏了一些东西,我想要一些输入。

跟随产生EXP=SOMETHING"的代码谢谢你的帮助:-)

    CodeMirror.defineMode("engage", function(config, parserConfig) {
  var engageOverlay = {
    startState: function() {return {inString: false};},
    token: function(stream, state) {
      // If we are not inside the engage token and we are peeking a "
      if (!state.inString && stream.peek() == '"') {
        // We move the stream to the next char
        // Then mark the start of the string
        // Then return null to avoid including the first " as part of the token
        stream.next();
        state.inString = true;
        return null;
      }
      // We are inside the target token
      if (state.inString)
      {
        if (stream.skipTo('"'))
        {
          stream.next();
          state.inString = false;
        }
        else
        {
          stream.skipToEnd();
        }
        return "engage";
      }
      else
      {
        stream.skipTo('"') || stream.skipToEnd();
        return null;
      }
    }
  };
  return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || "xml"), engageOverlay);
});

如果有人无意中发现了这个问题,下面是上述问题的解决方案:

// If we are not inside the engage token and we are peeking a "
      if ( !state.inString && stream.match(/="/, true) ) {
        state.inString = true;
        return null;
      }
      // We are inside the target token
      if (state.inString)
      {
        if (stream.skipTo('"'))
        {
          state.inString = false;
          return "engage";
        }
        else
        {
          stream.skipToEnd();
          return null;
        }
      }
      stream.next();
      return null;

我们基本上只是区分双引号的开始和结束,在我的特殊情况下,我总是在第一个"之前有一个=,如果不是这种情况,你可以很容易地设置另一个标志