Codemirror自动完成 - 建议来源

时间:2013-07-25 05:52:50

标签: javascript codemirror

我正在使用codemirror自动完成演示。它显示了一些javascript关键字,如应用程序缓存,defaultStatus和framenet以及更多建议。我希望它将我的关键字显示为建议。但我无法找到这些javascript关键字的来源。请帮帮我...

3 个答案:

答案 0 :(得分:3)

Javascript模式很难理解。所以我采取了python模式并且更改python-hint.js非常简单,我得到了我想要的输出。谢谢Eliran ..

答案 1 :(得分:2)

查看javascript-hint.js,其中包含一些关键字,可在源代码中看到。例如javascript关键字(第96行):

var javascriptKeywords = 
    ("break case catch continue debugger default delete do else false finally for function " +
     "if in instanceof new null return switch throw true try typeof var void while with")
    .split(" ");

这应该让您开始编写自己的*-hint.js文件。

答案 2 :(得分:1)

在我遇到问题时,我实际上是在查询旧的CodeMirror自动填充问题。

Javascript完成来自各种来源,您可以在源代码中看到这一点,因为Eliran指出javascript-hint.js

这是此时的相关功能:

    function getCompletions(token, context, keywords, options) {
    var found = [], start = token.string, global = options && options.globalScope || window;
    function maybeAdd(str) {
      if (str.lastIndexOf(start, 0) == 0 && !arrayContains(found, str)) found.push(str);
    }
    function gatherCompletions(obj) {
      if (typeof obj == "string") forEach(stringProps, maybeAdd);
      else if (obj instanceof Array) forEach(arrayProps, maybeAdd);
      else if (obj instanceof Function) forEach(funcProps, maybeAdd);
      for (var name in obj) maybeAdd(name);
    }

    if (context && context.length) {
      // If this is a property, see if it belongs to some object we can
      // find in the current environment.
      var obj = context.pop(), base;
      if (obj.type && obj.type.indexOf("variable") === 0) {
        if (options && options.additionalContext)
          base = options.additionalContext[obj.string];
        if (!options || options.useGlobalScope !== false)
          base = base || global[obj.string];
      } else if (obj.type == "string") {
        base = "";
      } else if (obj.type == "atom") {
        base = 1;
      } else if (obj.type == "function") {
        if (global.jQuery != null && (obj.string == '$' || obj.string == 'jQuery') &&
            (typeof global.jQuery == 'function'))
          base = global.jQuery();
        else if (global._ != null && (obj.string == '_') && (typeof global._ == 'function'))
          base = global._();
      }
      while (base != null && context.length)
        base = base[context.pop().string];
      if (base != null) gatherCompletions(base);
    } else {
      // If not, just look in the global object and any local scope
      // (reading into JS mode internals to get at the local and global variables)
      for (var v = token.state.localVars; v; v = v.next) maybeAdd(v.name);
      for (var v = token.state.globalVars; v; v = v.next) maybeAdd(v.name);
      if (!options || options.useGlobalScope !== false)
        gatherCompletions(global);
      forEach(keywords, maybeAdd);
    }
    return found;
  }

在这里你可以看到一些消息来源。 关键字是调用函数(未显示)提供的参数,在这种情况下,它是Eliran提到的拆分字符串:

 var javascriptKeywords = ("break case catch continue debugger default delete do else false finally for function " +
              "if in instanceof new null return switch throw true try typeof var void while with").split(" ");

然后逻辑中有一个分支:if (context && context.length) {

在真实情况下,它使用调用者传递的上下文对象(在本例中是一个属性链)。它可以使用可选的 additionalContext 对象,或者如果未在选项中明确禁用useGlobalScope,则可以使用全局对象作为基础来启动查找(字符串,原子和函数类型具有特殊处理逻辑)。然后,它使用while循环来解析上下文链并调用gatherCompletions。根据传递给gatherCompletions的对象类型,它可以尝试添加拆分字符串提供的其他硬编码列表:

  var stringProps = ("charAt charCodeAt indexOf lastIndexOf substring substr slice trim trimLeft trimRight " +
                     "toUpperCase toLowerCase split concat match replace search").split(" ");
  var arrayProps = ("length concat join splice push pop shift unshift slice reverse sort indexOf " +
                    "lastIndexOf every some filter forEach map reduce reduceRight ").split(" ");
  var funcProps = "prototype apply call bind".split(" ");

或者通过迭代javascript对象本身的属性。

最后在else案例token.state.localVarstoken.state.globalVars中,如果未明确禁用,则再次使用全局对象作为gatherCompletions函数的对象。您还可以在此处注明上述关键字。请注意,global在options.globalScope中提供,或者它采用window的值。

摘要

只是为了快速显示所有来源:

  1. javascriptKeywords
  2. funcProps
  3. arrayProps
  4. stringProps
  5. 全球对象
  6. options.additionalContext
  7. token.state.localVars
  8. token.state.globalVars
  9. 上下文属性链