Ace:在自定义自动完成后将插入符移到一对括号中
Ace: move caret into the pair of brackets after custom autocompletion
我是 Ace 的新手,我正在用它制作一个 JavaScript 编辑器。我向编辑器添加了自动完成器:
var functionCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {
var funcList = ["foo", "bar"]
callback(null, funcList.map(function(word) {
return {
caption: word,
value: word + "()",
meta: "Custom Functions"
};
}));
}
}
editor.completers.push(functionCompleter);
自动完成后:
但是我希望插入符号在完成后位于圆括号之间,如下所示:
这样添加函数参数会更方便
在 JavaScript 中有没有办法做到这一点?谢谢
在auto-completion之后,您可以使用Ace的goToLine函数将光标设置在括号之间。
//Once you Insert the brackets ()
var pos = editor.selection.getCursor(); //Take the latest position on the editor
editor.gotoLine(pos.row + 1, pos.column + 2); //This will set your cursor in between the brackets
对于回调,您可以使用自动完成 insertMatch
var functionCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {
var funcList = ["foo", "bar"]
callback(null, funcList.map(function(word) {
return {
caption: word,
value: word + "()",
meta: "Custom Functions"
completer: {
insertMatch: function(editor, data) {
console.log(data.value); // This should give the completed keyword
// Here you can get the position and set the cursor
}
}
};
}));
}
}
editor.completers.push(functionCompleter);
我是 Ace 的新手,我正在用它制作一个 JavaScript 编辑器。我向编辑器添加了自动完成器:
var functionCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {
var funcList = ["foo", "bar"]
callback(null, funcList.map(function(word) {
return {
caption: word,
value: word + "()",
meta: "Custom Functions"
};
}));
}
}
editor.completers.push(functionCompleter);
自动完成后:
但是我希望插入符号在完成后位于圆括号之间,如下所示:
这样添加函数参数会更方便
在 JavaScript 中有没有办法做到这一点?谢谢
在auto-completion之后,您可以使用Ace的goToLine函数将光标设置在括号之间。
//Once you Insert the brackets ()
var pos = editor.selection.getCursor(); //Take the latest position on the editor
editor.gotoLine(pos.row + 1, pos.column + 2); //This will set your cursor in between the brackets
对于回调,您可以使用自动完成 insertMatch
var functionCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {
var funcList = ["foo", "bar"]
callback(null, funcList.map(function(word) {
return {
caption: word,
value: word + "()",
meta: "Custom Functions"
completer: {
insertMatch: function(editor, data) {
console.log(data.value); // This should give the completed keyword
// Here you can get the position and set the cursor
}
}
};
}));
}
}
editor.completers.push(functionCompleter);