ACE editor autocompletion keywords in upper case

老子叫甜甜 提交于 2019-12-13 18:40:51

问题


I have an ACE editor with a custom highlight mode, based on sql mode, and all is working fine, but I am defining keywords in upper case, and when autocompleter shows the available options, all of them are in lower case.

I have checked the old sql mode (before my modifications) and the behaviour is exactly the same.

Is there any way of converting this options to upper case?

I have review this question, but I have been unable to find a way to do it. I have also tried to remove all toLowerCase() functions into ext-language-tools.js, but still showing options in lower case.

Thank you!


回答1:


It's a very dirty workaround, but you can try tweaking ext-language_tools.js by adding the following.

Where the code reads:

this.filterCompletions = function(items, needle) {
    var results = [];
    var upper = needle.toUpperCase();
    var lower = needle.toLowerCase();
    loop: for (var i = 0, item; item = items[i]; i++) {       
        var caption = item.value || item.caption || item.snippet;
        if (!caption) continue;

Change it to read the following, where 'keyword' and 'builtinFunctions' are the groups of completions you want to capitalize:

this.filterCompletions = function(items, needle) {
    var results = [];
    var upper = needle.toUpperCase();
    var lower = needle.toLowerCase();
    loop: for (var i = 0, item; item = items[i]; i++) {
        if (item.meta === 'keyword' || item.meta === 'builtinFunctions'){
            items[i].name = items[i].name.toUpperCase();
            items[i].value = items[i].value.toUpperCase();
        }
        var caption = item.value || item.caption || item.snippet;
        if (!caption) continue;



回答2:


Here is a clean solution using a custom completer:

const customKeyWordCompleter = {
  getCompletions(editor, session, pos, prefix, callback) {
    if (session.$mode.completer) {
      return session.$mode.completer.getCompletions(editor, session, pos, prefix, callback);
    }
    const state = editor.session.getState(pos.row);
    let keywordCompletions;
    if (prefix === prefix.toUpperCase()) {
      keywordCompletions = session.$mode.getCompletions(state, session, pos, prefix);
      keywordCompletions = keywordCompletions.map((obj) => {
        const copy = obj;
        copy.value = obj.value.toUpperCase();
        return copy;
      });
    } else {
      keywordCompletions = session.$mode.getCompletions(state, session, pos, prefix);
    }
    return callback(null, keywordCompletions);
  },
};

this.editor.completers = [
  customKeyWordCompleter,
  customCompleter,
];


来源:https://stackoverflow.com/questions/35183311/ace-editor-autocompletion-keywords-in-upper-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!