Move Caret to Next Blank Line

Hi, I'm used to the keyboard shortcut "Ctrl DOWN" in many editors that will jump the caret (cursor) to the next blank line in the code. This allows for quick navigation of the code.

Perhaps I completely missed it, but the action of "move caret to next blank line" or something like that is not in the Keymap list for the editor.

Do you know if such an option is there, and if not, can you recommend a good way to navigate the code? "Move Caret to Code Block Start" is not very intuitive to me when I'm moving about the code and the simple page-up, page-down are too coarse.

Thanks

4 comments
Comment actions Permalink

I also posted this question to StackOverflow:
http://stackoverflow.com/questions/7381451/phpstorm-move-caret-to-next-blank-line

If I get any suggestions, I will post them here also.

0
Comment actions Permalink

Yes, thanks Alexey.

If anyone is interested in this feature, please vote it up! It seems that it would be one that would not require an immense time investment, but would definitely add another common and powerful way to navigate the code.

0
Comment actions Permalink

I too wanted this functionality, so I wrote a small plugin. It's my first attempt at writing an Intellij plugin, so the usual caveats apply - untested, no guarantee it'll work for you etc.

It only jumps to the next and previous blank line without selection. I wrote this for the IDEA product, so I'm not sure whether it will work in the PHP IDE. In principle it should work. Perhaps if anyone catches any problems/bad practices, they can update this thread to fix the source. The plugin is essentially two small actions:

JumpToPreviousBlankLine:

public class JumpToPreviousBlankLine extends AnAction {
    public void actionPerformed(AnActionEvent e) {
        EditorImpl editor = (EditorImpl) e.getData(PlatformDataKeys.EDITOR);
        DocumentImpl document = (DocumentImpl) editor.getDocument();
        int caretOffset = editor.getCaretModel().getOffset();
        int lineNumber = document.getLineNumber(caretOffset);

        while (lineNumber > 0) {
            lineNumber--;
            if (document.getText(new TextRange(document.getLineStartOffset(lineNumber),
                    document.getLineEndOffset(lineNumber))).length() == 0) {
                editor.getCaretModel().moveToOffset(document.getLineStartOffset(lineNumber));
                editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
                break;
            }
        }
    }
}


and


JumpToNextBlankLine:

public class JumpToNextBlankLine extends AnAction {
    public void actionPerformed(AnActionEvent e) {
        EditorImpl editor = (EditorImpl) e.getData(PlatformDataKeys.EDITOR);
        DocumentImpl document = (DocumentImpl) editor.getDocument();
        int caretOffset = editor.getCaretModel().getOffset();
        int lineNumber = document.getLineNumber(caretOffset);

        while (lineNumber + 1 < document.getLineCount()) {
            lineNumber++;
            if (document.getText(new TextRange(document.getLineStartOffset(lineNumber),
                    document.getLineEndOffset(lineNumber))).length() == 0) {
                editor.getCaretModel().moveToOffset(document.getLineStartOffset(lineNumber));
                editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
                break;
            }
        }
    }
}


I've attached the .jar file - install as a plugin. You can then map the keyboard shortcut as you like in the 'Keymap' preferences. I have it mapped to ctrl+] to go to previous blank line and ctrl+\ to go to next blank line.

Cheers,
irumat



Attachment(s):
idea_blanklinejump.jar
0

Please sign in to leave a comment.