【发布时间】:2014-03-30 10:55:00
【问题描述】:
我正在使用tern 为使用CodeMirror 运行的窗口提供一些增强的智能感知,它工作正常,但我遇到了一个问题,我想添加一些自定义“types”,所以说话,以便在下拉菜单中它们旁边可以有图标。我已经把代码缩小了一点......
在.json 定义文件中,类型似乎是使用!type 指令声明的,像这样;
"Infinity": {
"!type": "number",
"!url": "https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Infinity",
"!doc": "A numeric value representing infinity."
},
"undefined": {
"!type": "?",
"!url": "https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/undefined",
"!doc": "The value undefined."
}
这些和CSS有点对应,像这样。
.CodeMirror-Tern-completion-object:before {
content: "O";
background: #77c;
}
.CodeMirror-Tern-completion-fn:before {
content: "F";
background: #7c7;
}
.CodeMirror-Tern-completion-array:before {
content: "A";
background: #c66;
}
.CodeMirror-Tern-completion-number:before {
content: "1";
background: #999;
}
好的,这一切都很好。我可以设计那些,没问题。但是我想添加一些我自己的,并且很难做到这一点。例如,如果我只是这样做......
"Character": {
"!type": "entity",
"!url": "some-url",
"!doc": "This is a character entity."
},
"User": {
"!type": "user",
"!url": "some-url-again",
"!doc": "This is a user entity."
}
这只是导致整个事情无法正常工作;就像tern 只是说nope nope nope 而什么都不做。我已经尝试了几个小时,并且已经真正挖掘了源代码,但无法真正找到一种方法来组成我自己的类型。我想简单地给这个特定代码编辑器的用户(它不会是“完全”的 javascript,它将是 javascript,但有一些愚蠢的文字,因为它们不是编码器)一些简单的标志,使其更容易在他们身上。
欢迎提出任何建议!感谢您的宝贵时间。
更新
根据用户的要求,我将发布更多代码以尝试获得更多帮助。这就是我要的;我有一个定义对象布局的.json 文件。该对象将镜像一个实际的 C# 对象/数据库对象。 (那部分不重要,只知道我有具体型号)
我也在使用默认的 ecma5.json 和 jquery.json def 文件。在这里可以找到它们;
https://github.com/marijnh/tern/blob/master/defs/ecma5.json https://github.com/marijnh/tern/blob/master/defs/jquery.json
所以当用户点击CTRL+SPACE 并看到CHARACTER 时,我希望它旁边有一个[CHARACTER] 类型。所以我认为第一步是指定字符作为类型。
character.json
"character": {
"!type": "Character",
"!doc": "Represents the character model.",
"Coefficients":{
"!doc": "The coefficients that govern many of the calculative formulas"
}
}
发生了什么是以下屏幕。
现在我试图追踪它,我在addons/tern.js 中发现了一个名为typeToIcon 的函数(不是实际的 tern 文件,是插入 codemirror 的插件文件)
function typeToIcon(type) {
var suffix;
if (type == "?") suffix = "unknown";
else if (
type == "number" ||
type == "string" ||
type == "bool"
) suffix = type;
else if (/^fn\(/.test(type)) suffix = "fn";
else if (/^\[/.test(type)) suffix = "array";
else suffix = "object";
return cls + "completion " + cls + "completion-" + suffix;
}
所以我虽然AHA! This is it!!,所以我想我可以在这里添加我的类型。
我在函数中添加了console.log(type); 以查看输出是什么样的。虽然这从未触发,即使我点击CTRL+SPACE,我也从未看到控制台输出,所以它告诉我没有到达此代码。我不得不走得更远。
我在def.js 中找到了一个名为TypeParser 的函数,它是实际燕鸥下载的一部分。
var TypeParser = exports.TypeParser = function (spec, start, base, forceNew) {
this.pos = start || 0;
this.spec = spec;
this.base = base;
this.forceNew = forceNew;
};
将控制台日志放在这里给了我一些结果,它们看起来像许多可用的类型,并且在 jquery.json 和 ecma5.json 中定义。
就我所知,我完全不知道如何进行。
【问题讨论】:
-
刚刚碰到这个问题,想知道您是如何解决问题的?
标签: javascript codemirror tern