Override marker click action

2019-07-17 04:02发布

I want to add a custom action when the user clicks on a marker from the left vertical ruler of the editor

enter image description here

I managed to run a custom action by adding in the plugin.xml the following code:

<extension point="org.eclipse.ui.editorActions">
    <editorContribution targetID="org.eclipse.cdt.ui.editor.CEditor"
        id="org.eclipse.cdt.debug.ui.CEditor.MyRulerActions">
        <action label="%Dummy.label"
            class="com.example.MarkerClickAction"
            actionID="RulerClick"
            id="com.example.MarkerClickAction">
        </action>
    </editorContribution>
</extension>

I want to call a custom implementation of the IQuickFixProcessor, but the implemented method getCorrections requires an IInvocationContext and IProblemLocation[]. How can I get those informations?

A really bad implentation which I thought of was to simulate the Ctrl+1 shortcut press, but if the carret is not positioned on the same line with the clicked marker, it will show quick fixes for the one from the carret:

public class MarkerClickAction implements IEditorActionDelegate {

    @Override
    public void run(IAction action) {
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_1);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_1);
    }

    @Override
    public void selectionChanged(IAction action, ISelection selection) {
        // TODO Auto-generated method stub

    }

    @Override
    public void setActiveEditor(IAction action, IEditorPart targetEditor) {
        // TODO Auto-generated method stub

    }

}

If it's not possible to call the methods behind the Ctrl + 1 shortcut, how can I position the carret to the marker's line?

0条回答
登录 后发表回答