How to detect what is expected element type at current position inside CompletionContributor?

How to detect what is expected element type at current position inside CompletionContributor?

The problem is, CompletionContributor doesn't reacts to patterns, which contain non-final language types (which are corresponded with non-final BNF rules in grammar).
And there is no obvious way to detect inside addCompletions() method (or inside fillCompletionVariants()) what is expected element type at current position according to Grammar.
Does CompletionContributor work only with Lexer results?

5 comments

I'm not sure what you mean by "CompletionContributor doesn't reacts to patterns, which contain non-final language types". Also, there is no concept of "expected element type" in the IntelliJ IDEA PSI structure. In a CompletionContributor, you have full access to the PSI tree and can look at all the elemens currently present in the file, but there is no one who could tell you about "expected" element types.

0

Well, actually I mean, that at current position grammar "knows" which kind of tokens are expected. Using this info IDEA build error messages ("blabla is expected, but found gigigi").
But I don't see any way how I can get this information and associate current position and grammar inside CompletionContributor.
Futhermore, it always mark my current position as IDENTIFIER, while I have in my lexer such rule:

IDENTIFIER=[a-zA-Z$_][a-zA-Z$_0-9]*

%%

...
{IDENTIFIER}                   { yybegin(YYINITIAL); return MyTypes.IDENTIFIER; }
...

0

The grammar is only used to generate the lexer and parser. It does not exist in a generated plugin in any form accessible to other code.

The completion works by inserting a dummy identifier at the caret position. See the javadocs of CompletionContributor for some more info on how completion works.

0

Dmitry, thanks for reply. Yes, I've read about dummy identifier. That is why I asked this question here.

Do I correctly understand, that if I want to make specific list of variants for completion depending on my current position, I should actually implement some kind of my grammatic parser inside CompletionContributor?

More specifically. Let's imagine, I have grammar (just an example):

code ::= function*

function ::= function_name COLON function_body

function_body ::= operator+

operator ::= OPERATOR_1 | OPERATOR_2 | ....

So, I want to be able show to user list of all available operators, when his current position is something like:

function1: _ <-- here

If I correctly understand You, the only way how I could do this,  - I should programmically analyze my current position and detect, that I'm after "function_name", so I can show list "OPERATOR_1, OPERATOR_2 ...".

0

Essentially yes. You can look at JavaCompletionData, for example, to see how this is done in our code.

0

Please sign in to leave a comment.