Search code examples
javaintellij-ideawebstormintellij-plugin

Intellij plugin development, overriding CaretImpl


I am tryign to alter the way page up and page down are handled.

I have successfully extended EditorAction and can change the size of the jump it performs.

This is the code that runs for it:

    @Override
    public void execute(Editor editor, DataContext dataContext) {
            boolean $isWithSelection = false;
            int     $divider         = 2;

            int lineHeight = editor.getLineHeight();
            Rectangle visibleArea = editor.getScrollingModel().getVisibleArea();

            int linesIncrement = (visibleArea.height / lineHeight) / $divider;

            editor.getScrollingModel().scrollVertically(visibleArea.y - visibleArea.y % lineHeight - linesIncrement * lineHeight);

            int lineShift = -linesIncrement;

            editor.getCaretModel().moveCaretRelatively(0, lineShift, $isWithSelection, editor.isColumnMode(), true);
    }

I have divided the size of the page up size by two.

But I would also like to change the pay the caret is placed on page up when there is nothing more to page up.

Intellij will annoyingly place the caret on the first line, first character, breaking any flow to page down and return to where the you were last time if you later page down.

You should in fact be able to do page up and page down, and always return to the same line, same column, same character.

Page up and page down are often used to peak at code higher up, but Intellij makes this a real hastle. You page up and you get screwed, all the time.

So the last line:

            editor.getCaretModel().moveCaretRelatively(0, lineShift, $isWithSelection, editor.isColumnMode(), true);

Is responsible to move the caret, but get caretModel is what I believe a singleton, and that's where magic of moving the caret happens.

That method is huge, and I have no clue on what actually moves the caret to the first character, but before I get there:

What do I need to do to able to override moveCaretRelatively in CaretImpl?

Can I participate in the creation of Caret for Editor ?

EDIT


Note! If you page down too much it also moves the caret to the first line, however, page up after that seems to remember your caret position. But page up and then page down does not, so there seems to be a bug since page bottom and then page up does work as I wish


Solution

  • No, you can't participate in the creation of CaretImpl, and you cannot override the moveCaretRelatively() method.

    However, you don't actually need that: instead of calling that method, you can calculate the exact position where you want the caret to be moved and call CaretModel.moveToOffset() or CaretModel.moveToLogicalPosition() to move the caret there.