Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom identifierRegexps is not working #2532

Closed
ghost opened this issue Jun 6, 2015 · 7 comments
Closed

Custom identifierRegexps is not working #2532

ghost opened this issue Jun 6, 2015 · 7 comments

Comments

@ghost
Copy link

ghost commented Jun 6, 2015

This seems to be a recurrent issue, but maybe someone can give a bit of help figuring this out.

The idea is to be able to provide a custom identifierRegex in order to change the regex that is used in retrievePrecedingIdentifier.

This should be possible giving an identifierRegexps to the completers so that this line takes effect:

editor.completers.forEach(function(completer) {
        if (completer.identifierRegexps) {
            completer.identifierRegexps.forEach(function(identifierRegex) {
                if (!prefix && identifierRegex)
                    prefix = util.retrievePrecedingIdentifier(line, pos.column, identifierRegex);
            });
        }

However, I haven't been able to do that using the following (naive) approach:

    var rhymeCompleter = {
        identifierRegexps: [/[a-zA-Z_0-9\$\.\-\u00A2-\uFFFF]/],
        getCompletions: function(editor, session, pos, prefix, callback) {
            if (prefix.length === 0) { callback(null, []); return }
            $.getJSON(
                "http://rhymebrain.com/talk?function=getRhymes&word=" + prefix,
                function(wordList) {
                    alert(prefix);
                    // wordList like [{"word":"flow","freq":24,"score":300,"flags":"bc","syllables":"1"}]
                    callback(null, wordList.map(function(ea) {
                        return {name: ea.word, value: ea.word, score: ea.score, meta: "rhyme"}
                    }));
                })
        }
    }

    langTools.addCompleter(rhymeCompleter);

You can see the code in this plnkr. I simply added a dot in the regex but it looks like the previous prefix is being used as a conditional to run the line prefix = util.retrievePrecedingIdentifier(line, pos.column, identifierRegex);. Therefore, hitting Ctrl+space after a dot (e.g. will.) doesn't produce the correct result because there is no prefix under the previous rule. Unfortunately, even after including additional characters (such as will.a), the prefix is set as a and that line doesn't run again. Please, let me know if there is a fix. Hopefully, I'm just misunderstanding this snippet.

This issue is basically the same as this SO question. It would be nice to give an authoritative answer. Also, issues #1670 and #2225 and open pull request #2352 are related to this issue.

@sugang
Copy link

sugang commented Jul 1, 2015

I was able to reproduce your issue. For some reason the custom regexp doesn't work at all. I am researching a solution right now.

@ghost
Copy link
Author

ghost commented Jul 1, 2015

Thanks. Let me know if you find a solution.

@sugang
Copy link

sugang commented Jul 1, 2015

Ok. I figured out what was the problem. Some programmers were definitely drunk.

Here's the solution to the problem: I was using the 'built' version. You need to change the ext-language_tools.js. My fix is not perfect, but it's much better than modifying the ID_REGEX thingy.

  1. Look for the function getCompletionPrefix, as you mentioned. (around line 1900), make a copy. As you mentioned in your original post, this function is actually correct.

  2. Look for the function this.gatherCompletions around line 1400. You will see these two lines:

    var prefix = util.retrievePrecedingIdentifier(line, pos.column); 
   ...
    prefix: util.retrievePrecedingIdentifier(line, pos.column, results[0] && results[0].identifierRegex),`

These are apparently wrong, as they are both not using the correct prefix which can be generated with getCompletionPrefix function. Since it's out of scope, I simply made a copy of this function, and replace the above two lines with

    var prefix = this.getCompletionPrefix(editor);
   ...
    prefix: prefix
  1. Add identifierRegexps to your custom completer. Note that the way ACE is doing autocompletion is it's looking at a single character at a time. Therefore you need to include all the possible permutations of the characters that could be part of the prefix all together. I added the hashtag.

  2. Note if you add hashtag, hashtag then becomes part of the prefix. You need to append that to the results of your rhymeCompleter, otherwise the popup won't work.

$.getJSON(
    "http://rhymebrain.com/talk?function=getRhymes&word=" + prefix.substring(1),
    function(wordList) {
    // wordList like [{"word":"flow","freq":24,"score":300,"flags":"bc","syllables":"1"}]
        console.log(wordList);
        callback(null, wordList.map(function(ea) {
        return {name: '#' + ea.word, value: '#' + ea.word, score: ea.score, meta: "rhyme"}
    }));

I may actually rewrite this part of the library to add a 'trigger' symbol which won't be included as part of the term.

@ghost
Copy link
Author

ghost commented Jul 2, 2015

I'm glad you figured it out. Do you have a branch to test your changes? Even better, you might want to create a pull request to see if this gets merged.

@sugang
Copy link

sugang commented Jul 2, 2015

No.. I simply updated code on my local copy and it's working perfectly now. I think it's not a quite elegant fix as I made a duplicate function. I am still not very familiar with the overall structure of ACE, but I am curious why the getCompletionPrefix function was a local function which can't be referred by other pieces of ACE easily.

How to create a pull request?

@ghost
Copy link
Author

ghost commented Jul 3, 2015

There are a lot of good answers here.

cssherry pushed a commit to cssherry/ace that referenced this issue Dec 11, 2015
- Add util.getCompletionPrefix and use it instead of util.retrievePrecedingIdentifier in gatherCompletions and doLiveAutocomplete functions as suggested at ajaxorg#2532
nightwing added a commit that referenced this issue Dec 11, 2015
Fix custom identifierRegexps (issue #2532)
doog33k pushed a commit to doog33k/ace that referenced this issue Feb 4, 2017
- Add util.getCompletionPrefix and use it instead of util.retrievePrecedingIdentifier in gatherCompletions and doLiveAutocomplete functions as suggested at ajaxorg#2532
@github-actions
Copy link

github-actions bot commented May 9, 2022

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant