Last active
November 28, 2023 23:53
-
-
Save yuvalherziger/aa48782568c6914b55066d290ff88360 to your computer and use it in GitHub Desktop.
Sample Ace Editor custom completer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// fetch ace's language tools module: | |
var langTools = ace.require('ace/ext/language_tools'); | |
// data stub: | |
var sqlTables = [ | |
{ name: 'users', description: 'Users in the system' }, | |
{ name: 'userGroups', description: 'User groups to which users belong' }, | |
{ name: 'customers', description: 'Customer entries' }, | |
{ name: 'companies', description: 'Legal entities of customers' }, | |
{ name: 'loginLog', description: 'Log entries for user log-ins' }, | |
{ name: 'products', description: 'Products offered in the system' }, | |
{ name: 'productCategories', description: 'Different product categories' } | |
]; | |
// create a completer object with a required callback function: | |
var sqlTablesCompleter = { | |
getCompletions: function(editor, session, pos, prefix, callback) { | |
callback(null, sqlTables.map(function(table) { | |
return { | |
caption: table.description, | |
value: table.name, | |
meta: "Table" | |
}; | |
})); | |
} | |
}; | |
// finally, bind to langTools: | |
langTools.addCompleter(sqlTablesCompleter); |
@mahdi943 You want to clear the completers list before adding one. So change the last line of this gist:
langTools.setCompleters([]);
langTools.addCompleter(sqlTablesCompleter);
hi there, var langTools = ace.require('ace/ext/language_tools');
ace.require
returns none for me
For anyone curious how to set this only to work to autocomplete for SQL mode -
var sqlTablesCompleter = {
getCompletions: function(editor, session, pos, prefix, callback) {
// Check if the editor's session mode is SQL
if (session.$mode && session.$mode.$id === 'ace/mode/sql') {
callback(null, sqlTables.map(function(table) {
return {
caption: table.description,
value: table.name,
meta: "Table"
};
}));
}
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I am using Ace editor in my project and also used CustomeCompleter to have my own list.
Could you please help me to delete the old list if I need to replace it with a new one?
Thanks.