admin管理员组文章数量:1026989
- I know how to use ace editor in web page
- I know how to add a pleter
I want to do like this, input a mand, a blank space, then press -
(dash),
let ace editor autoplete options(parameters), what should I do?
For example, here is a mand Print
with options -a|-b|-c|-d
,
when I input Print -
, how can trigger autoplete, and let you select
-a
or -b
or -c
or -d
?
- I know how to use ace editor in web page
- I know how to add a pleter
I want to do like this, input a mand, a blank space, then press -
(dash),
let ace editor autoplete options(parameters), what should I do?
For example, here is a mand Print
with options -a|-b|-c|-d
,
when I input Print -
, how can trigger autoplete, and let you select
-a
or -b
or -c
or -d
?
2 Answers
Reset to default 7I solve it by myself. code as follows:
var langTools = ace.require("ace/ext/language_tools");
var editor = ace.edit("stepEditor");
editor.setTheme("ace/theme/chrome");
editor.getSession().setMode("ace/mode/tcl");
editor.setOptions({
enableBasicAutopletion: true,
enableSnippets: false,
enableLiveAutopletion: true
});
var wordList = [];
var icc2Commands = null;
jQuery.getJSON("auto_pletion.json",function(obj){
icc2Commands = obj;
for(var name in obj){
wordList.push(name);
}
for(var i = 0; i < 5; i++)
{
console.log(wordList[i]);
}
});
var icc2Completer = {
getCompletions: function(editor, session, pos, prefix, callback) {
var curLine = session.getDocument().getLine(pos.row);
var curTokens = curLine.slice(0, pos.column).split(/\s+/);
var curCmd = curTokens[0];
if (!curCmd) return;
var lastToken = curTokens[curTokens.length-1];
var candidates = [];
if (lastToken && lastToken.match(/^-/)) {
for (var option of icc2Commands[curCmd]) {
if (option.startsWith(lastToken.slice(1))) {
candidates.push("-"+option);
}
}
callback(null, candidates.map(function(ea) {
return {name: ea, value: ea, score: 300, meta: "ICC2Option"};
}));
}
else{
callback(null, wordList.map(function(word) {
return {
caption: word,
value: word,
meta: "ICC2Command"
};
}));
}
}
}
langTools.addCompleter(icc2Completer);
enableLiveAutopletion
seems to work in many cases, but I found out that for some reason it does not show the popup if you type a word, followed by a space and then another word. What worked for me was calling:
sourceEditor.execCommand('startAutoplete');
This is the programmatic way of triggering the ctrl+space action which seems to show popup in some cases that are not covered by enableLiveAutopletion
.
- I know how to use ace editor in web page
- I know how to add a pleter
I want to do like this, input a mand, a blank space, then press -
(dash),
let ace editor autoplete options(parameters), what should I do?
For example, here is a mand Print
with options -a|-b|-c|-d
,
when I input Print -
, how can trigger autoplete, and let you select
-a
or -b
or -c
or -d
?
- I know how to use ace editor in web page
- I know how to add a pleter
I want to do like this, input a mand, a blank space, then press -
(dash),
let ace editor autoplete options(parameters), what should I do?
For example, here is a mand Print
with options -a|-b|-c|-d
,
when I input Print -
, how can trigger autoplete, and let you select
-a
or -b
or -c
or -d
?
2 Answers
Reset to default 7I solve it by myself. code as follows:
var langTools = ace.require("ace/ext/language_tools");
var editor = ace.edit("stepEditor");
editor.setTheme("ace/theme/chrome");
editor.getSession().setMode("ace/mode/tcl");
editor.setOptions({
enableBasicAutopletion: true,
enableSnippets: false,
enableLiveAutopletion: true
});
var wordList = [];
var icc2Commands = null;
jQuery.getJSON("auto_pletion.json",function(obj){
icc2Commands = obj;
for(var name in obj){
wordList.push(name);
}
for(var i = 0; i < 5; i++)
{
console.log(wordList[i]);
}
});
var icc2Completer = {
getCompletions: function(editor, session, pos, prefix, callback) {
var curLine = session.getDocument().getLine(pos.row);
var curTokens = curLine.slice(0, pos.column).split(/\s+/);
var curCmd = curTokens[0];
if (!curCmd) return;
var lastToken = curTokens[curTokens.length-1];
var candidates = [];
if (lastToken && lastToken.match(/^-/)) {
for (var option of icc2Commands[curCmd]) {
if (option.startsWith(lastToken.slice(1))) {
candidates.push("-"+option);
}
}
callback(null, candidates.map(function(ea) {
return {name: ea, value: ea, score: 300, meta: "ICC2Option"};
}));
}
else{
callback(null, wordList.map(function(word) {
return {
caption: word,
value: word,
meta: "ICC2Command"
};
}));
}
}
}
langTools.addCompleter(icc2Completer);
enableLiveAutopletion
seems to work in many cases, but I found out that for some reason it does not show the popup if you type a word, followed by a space and then another word. What worked for me was calling:
sourceEditor.execCommand('startAutoplete');
This is the programmatic way of triggering the ctrl+space action which seems to show popup in some cases that are not covered by enableLiveAutopletion
.
本文标签:
版权声明:本文标题:javascript - How to trigger autocomplete dynamically with ace editor: input commands and complete options - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745667953a2162284.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论