Search code examples
eclipseautocompletextextcontent-assist

How can I customize the autocompletion created by Xtext?


Introduction: I have written a simple grammar in Xtext which is working apart from the autocompletion. It's not like it's not working at all but it doesn't work like I want it to so I started to search for a possibility to customize the autocompletion. I found out that there are a lot of possibilities but I couldn't find any concrete explanations to do so.
And that's what I'm asking for here. I hope you can help me.

My Grammar:

grammar sqf.Sqf with org.eclipse.xtext.common.Terminals

generate sqf "http://www.Sqf.sqf" 

Model:
    elements += Element*
;

    Element:
        Declaration ";" | Command ";"
    ;

        Declaration:
            Variable | Array
        ;


            Variable:
                name=ID "=" content=VARCONTENT (("+"|"-"|"*"|"/") content2+=VARCONTENT)*
            ;


            Array:
                name=ID "=" contet=ArrayLiteral | name=ID "=" "+" content2+=[Array]
            ;

                ArrayLiteral:
                    "[" (content += ArrayContent)* "]" (("+"|"-")content1+=Extension)*
                ;

                    ArrayContent:
                        content01=Acontent ("," content02+=Acontent)*
                    ;

                        Acontent:
                            STRING | INT | Variable | ref=[Declaration] | ArrayLiteral
                        ;

                    Extension:
                        STRING | INT | ref=[Declaration]
                    ;




    Command:
        Interaction
    ;

        Interaction:
            hint
        ;

            hint:
                Normal | Format | Special
            ;

                Normal:
                    name=HintTypes content=STRING
                ;

                Format:
                    name=HintTypes "format" "[" content=STRING "," variable=[Declaration] "]"
                ;

                    HintTypes:
                        "hint" | "hintC" | "hintCadet" | "hintSilent"
                    ;

                Special:
                    hintCArray
                ;

                    hintCArray:
                        title=STRING "hintC" (content1=ArrayLiteral | content=STRING)
                    ;

    VARCONTENT:
    STRING | INT | ref=[Declaration] | "true" | "false" | "nil"
;

My Problem: The problem is that when I' hitting ctrl+space in the eclipse-editor(with my plug-in loaded) I only get the proposal "title"-STRING (from the hintCArray-rule) and name-ID (from the Declaration-rule).
What I would expect is that it also offers me the possibility to choose between my hint-commands. I also tried to hit ctrl+space after having begun typing the command (hin) and as a result the autocomletion put a "=" next to it so it would be a Variable- or Array-declaration.

What I have found out is that I have to edit some of the proposalProvider.java-Files (I think it's the AbstractProposalProvider) in the .ui-section.
I now have the problem that I don't know what I have to write in there.

In this AbstractProposalProvider.java there's stuff like this:
public void completeModel_Elements(EObject model, Assignment assignment, ContentAssistContext context, ICompletionProposalAcceptor acceptor) { completeRuleCall(((RuleCall)assignment.getTerminal()), context, acceptor); }

Greetings Raven


Solution

  • there are several problems with your grammar but when only concentrating on your actual question:

    Stuff that is inside a DataTypeRule will never be shown in the proposals out of the box. (HintTypes) To make it happen without coding something you could inine the possible types as keywords like this:

    Normal: name=("hint" | "hintC" | "hintCadet" | "hintSilent") content=STRING;

    Format: name=("hint" | "hintC" | "hintCadet" | "hintSilent") "format" "[" content=STRING "," variable=[Declaration] "]";

    Cheers, Holger