简体   繁体   中英

XText QuickFix inserting multiple word token

Given a grammar that includes a rule like:

SomeStatement: ... (tokensExist?=TokensExist)? ...

TokensExist: 'TOKENONE' 'TOKENTWO';

Next I create a QuickFix that detects the missing tokens and proposes to add them by modifying the model:

acceptor.accept(issue, "Add missing tokens", "", null, new ISemanticModification() {
    @Override
    public void apply(EObject element, IModificationContext context) throws Exception {

        // Locate the model element
        EObject goal = element;
        while (goal != null && !(MyStatement.class.isAssignableFrom(goal.getClass()))) {
            goal = goal.eContainer();
        }
        MyStatement stmt = (MyStatement)goal;

        // Set the tokens in the model
        stmt.setTokensExists(true);
    }
});

The problem now is that when the model is updated in the editor it appears as:

TOKENONETOKENTWO

instead of TOKENONE TOKENTWO , eg no space between the tokens.

This seems to be because the generated class MySyntacticSequencer has been generated to return a token this way:

protected String getTokensExist(EObject semanticObject, RuleCall ruleCall, INode node) {
    if (node != null)
        return getTokenText(node);
    return "TOKENONETOKENTWO";
}

I would like to avoid modifying the editor text directly and instead manipulate the model. Is there a way to do this? Can the generated token string be overridden?

We got this to work by extending the MySyntacticSequencer class and then overriding the getTokensExist method to return "TOKENONE TOKENTWO".

Is there a better solution?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM