Skip to content

Is period in autocomplete a specially treated character? #162

@distractor

Description

@distractor

I have observed some unexpected behavior with the autocomplete. It might be my setup is incorrect or I'm just not getting it.

I have created my own TextAutoCompleteProvider in the following way:

    class MathAutoCompleteProvider : public edbee::TextAutoCompleteProvider {
    public:
        MathAutoCompleteProvider();

        QList<edbee::TextAutoCompleteItem*> findAutoCompleteItemsForRange(
            edbee::TextDocument* document,
            const edbee::TextRange& range,
            const QString& word) override;

    private:
        QList<edbee::TextAutoCompleteItem*> itemList_;
    };

with the following implementation:

    MathAutoCompleteProvider::MathAutoCompleteProvider()
    {
        const QString prefix = "math.";
        const QString type = "numeric = ";

        auto add = [&](const QString& label, const QString& signature) {
            itemList_.append(new edbee::TextAutoCompleteItem(prefix + label, edbee::TextAutoCompleteKind::Function, type + prefix + signature));
        };

        add("abs", "abs(x)");
        add("acos", "acos(x)");
        add("asin", "asin(x)");
        add("atan", "atan(y [, x])");
        add("ceil", "ceil(x)");
        add("cos", "cos(x)");
        add("deg", "deg(x)");
        add("exp", "exp(x)");
        add("floor", "floor(x)");
        add("fmod", "fmod(x, y)");
        add("frexp", "frexp(x)");
        add("ldexp", "ldexp(m, e)");
        add("log", "log(x [, base])");
        add("max", "max(x, ...)");
        add("min", "min(x, ...)");
        add("modf", "modf(x)");
        add("pow", "pow(x, y)");
        add("rad", "rad(x)");
        add("random", "random([m [, n]])");
        add("randomseed", "randomseed(x)");
        add("sin", "sin(x)");
        add("sqrt", "sqrt(x)");
        add("tan", "tan(x)");
    }

    QList<edbee::TextAutoCompleteItem*> MathAutoCompleteProvider::findAutoCompleteItemsForRange(
    edbee::TextDocument* document,
    const edbee::TextRange& range,
    const QString& word)
    {
        // This will store the matching items.
        QList<edbee::TextAutoCompleteItem*> matches;

        for (auto* item : itemList_) {
            // We will match only if the word is a prefix of the item's label.
            if (item->label().startsWith(word, Qt::CaseInsensitive)) {
                // If it matches, we add it to the matches list.
                int score = item->matchLabelScore(document, range, word);
                if (score > 0) {
                    matches.append(item);
                }
            }
        }

        // Now, sort the matches alphabetically by the label of the item.
        std::sort(matches.begin(), matches.end(), [](edbee::TextAutoCompleteItem* a, edbee::TextAutoCompleteItem* b) {
            return a->label() < b->label(); // Alphabetical sorting
        });

        return matches;
    }

As you can see, the constructor creates a list of all math-related autocomplete words. I have changed the findAutoCompleteItemsForRange to show only words that match the current word being typed and alphabetically sort the autocomplete (something that could be there by default? Not sure, but works for me.).

I then later add the provider to the text document using m_pEditor->textDocument()->autoCompleteProviderList()->giveProvider(pProvider);.

The problem

  • If I start typing "f" nothing is shown. That is OK, all my words start with "math" anyway - assuming the provider above is the only one I have.
  • If I start typing "m" all words that start with "m" are shown. I'm OK with that, even though I would prefer if only "math" was shown. Once "math" or "math." is typed (our triggered to autocomplete), it should show all the functions for math. That would be perfect, but I'm also fine with the current workflow.
  • If I start typing "math". All words are still shown. This is to be expected. And is OK.
  • But is i start typing with "math." (notice the punctuation at the end) the whole autocomplete list is gone. This is NOT OK and is confusing me.

Is the punctuation specially handled?

Expected behavior

If "math." is typed it should still show all the math-related words.

Meaning:

  • if the word being types starts with "math" the autocomplete should show "math.abs", "math.acos", ...
  • when the words starts with "math." it should show the words without a prefix, i.e. "abs", "acos" (or at least something that is relevant).

It actually gets worse

If another provider is added, for example same like MathAutoCompleteProvider but with different words (e.g. "assert", "ablabla"), these words are shown after the punctuation, i.e.\ after "math.a" instead of "abs" or "acos".

ps: Also let me apologize for posting an issue every week now. :D

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions