Search code examples
javaeclipseeclipse-plugineclipse-jdt

ASTRewrite for a QuickFix: How to position the cursor?


I work on a Eclipse Plugin and create a QuickFix using ASTRewrite. Briefly it's constructed in the following way:

public class MyQFXProcessor implements IQuickFixProcessor {
    public IJavaCompletionProposal[] getCorrections(IInvocationContext context,
                                                    IProblemLocation[] locations) {
        AST ast = context.getASTRoot().getAST();
        ASTRewrite rw = ASTRewrite.create(ast);
        ASTNode replacement = ast.newSimpleName("Test");
        rewrite.replace(context.getCoveringNode(), replacement);
        IJavaCompletionProposal p = new ASTRewriteCorrectionProposal("My QFX",
                                                     context.getCompilationUnit(), rw, 10);
        return new IJavaCompletionProposal[]{p};
    }
}

This works fine so far. But what I didn't manage to achieve yet is to set the desired cursor position after the proposal is applied. For example this is how it's solved in JDT for add argument quick fix:

enter image description here

enter image description here

I think there should be an API for doing that, because Eclipse uses this kind of behavior for different use-cases (in auto-completion among others). Does anybody have an idea how to implement that?


Solution

  • You can check the internal implementation and usage of org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal which sets the end position of the linked mode in a quick fix or quick assist via org.eclipse.jdt.internal.ui.text.correction.proposals.LinkedCorrectionProposal.setEndPosition(ITrackedNodePosition position).