Javascript ace编辑器中的自定义自动完成在“后”不起作用&引用;
我想在ace编辑器中使用autocomplete。在用户键入Javascript ace编辑器中的自定义自动完成在“后”不起作用&引用;,javascript,ace-editor,Javascript,Ace Editor,我想在ace编辑器中使用autocomplete。在用户键入foo.之后,我想建议foo.bar 实际上,我使用了以下代码: var langTools = ace.require("ace/ext/language_tools"); var staticWordCompleter = { identifierRegexps: [/[\.]/], getCompletions: function(editor, session, pos, prefix, callback) {
foo.
之后,我想建议foo.bar
实际上,我使用了以下代码:
var langTools = ace.require("ace/ext/language_tools");
var staticWordCompleter = {
identifierRegexps: [/[\.]/],
getCompletions: function(editor, session, pos, prefix, callback) {
console.log(prefix);
if (prefix == "foo.") {
var wordList = ["baar", "bar", "baz"];
callback(null, wordList.map(function(word) {
return {
caption: word,
value: word,
meta: "static"
};
}
}));
}
}
langTools.setCompleters([staticWordCompleter])
如果我删除identifierRegexps
和If
子句,则自动完成会起作用,但在“.”之后不会起作用
我也读过这个解决方案,但它已经不起作用了:您可以绑定“.”,然后构建您的单词列表。您可以将单词列表设置为全局,并在getCompletions中使用,或者在绑定“.”后使用此代码获取before项ie foo,然后将值插入编辑器
self.editor.commands.addCommand({
name: "dotCommand1",
bindKey: { win: ".", mac: "." },
exec: function () {
var pos = editor.selection.getCursor();
var session = editor.session;
var curLine = (session.getDocument().getLine(pos.row)).trim();
var curTokens = curLine.slice(0, pos.column).split(/\s+/);
var curCmd = curTokens[0];
if (!curCmd) return;
var lastToken = curTokens[curTokens.length - 1];
editor.insert(".");
if (lastToken === "foo") {
// Add your words to the list or then insert into the editor using editor.insert()
}
}
});
您可以绑定“.”,然后构建单词列表。您可以将单词列表设置为全局,并在getCompletions中使用,或者在绑定“.”后使用此代码获取before项ie foo,然后将值插入编辑器
self.editor.commands.addCommand({
name: "dotCommand1",
bindKey: { win: ".", mac: "." },
exec: function () {
var pos = editor.selection.getCursor();
var session = editor.session;
var curLine = (session.getDocument().getLine(pos.row)).trim();
var curTokens = curLine.slice(0, pos.column).split(/\s+/);
var curCmd = curTokens[0];
if (!curCmd) return;
var lastToken = curTokens[curTokens.length - 1];
editor.insert(".");
if (lastToken === "foo") {
// Add your words to the list or then insert into the editor using editor.insert()
}
}
});
谢谢你的回答!我觉得我太蠢了,不能把你的榜样付诸实践。你能看一下这里的例子吗:嘿,对不起,我对这里的代码做了一个小改动。您是否可以将您的单词列表作为一个全局数组,并在lastToken与“foo”匹配后更新工作列表。您可以为autocomplete创建一个通用函数,并在最后一次令牌匹配时调用该函数。如果您能将此应用到@Harshapps的工作示例中,那就太好了。从这个“已接受”的答案来看,由于原始海报的代码不适用,因此不清楚完整的解决方案是什么。你错过了胶水:)谢谢你的回答!我觉得我太蠢了,不能把你的榜样付诸实践。你能看一下这里的例子吗:嘿,对不起,我对这里的代码做了一个小改动。您是否可以将您的单词列表作为一个全局数组,并在lastToken与“foo”匹配后更新工作列表。您可以为autocomplete创建一个通用函数,并在最后一次令牌匹配时调用该函数。如果您能将此应用到@Harshapps的工作示例中,那就太好了。从这个“已接受”的答案来看,由于原始海报的代码不适用,因此不清楚完整的解决方案是什么。你缺少胶水:)也许试试这个:也许试试这个: