Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Created February 12, 2019 13:02
Show Gist options
  • Save dkandalov/56482a6b1d5b00702d66545c7c303eb9 to your computer and use it in GitHub Desktop.
Save dkandalov/56482a6b1d5b00702d66545c7c303eb9 to your computer and use it in GitHub Desktop.
Paragraph navigation for IJ
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.editor.EditorModificationUtil
import com.intellij.openapi.editor.LogicalPosition
import static liveplugin.PluginUtil.currentEditorIn
import static liveplugin.PluginUtil.registerAction
static def process(AnActionEvent event, Closure callback) {
def editor = currentEditorIn(event.project)
def lines = editor.document.text.readLines()
def caretModel = editor.caretModel
def lineIndex = callback(caretModel.logicalPosition.line, lines)
def column = 0
if (lineIndex > 0) column = lines[lineIndex].chars.findIndexOf { !it.isWhitespace() }
if (column == -1) column = 0
caretModel.currentCaret.removeSelection()
caretModel.moveToLogicalPosition(new LogicalPosition(lineIndex, column))
EditorModificationUtil.scrollToCaret(editor)
}
registerAction("PrevParagraph", "alt ctrl I") { AnActionEvent event ->
process(event) { Integer lineIndex, List<String> lines ->
lineIndex--
if (lineIndex <= 0) return 0
while (lineIndex > 0 && lines[lineIndex].trim().isEmpty()) lineIndex--
while (lineIndex > 0 && !lines[lineIndex].trim().isEmpty()) lineIndex--
if (lines[lineIndex].trim().isEmpty()) lineIndex++
lineIndex
}
}
registerAction("NextParagraph", "alt ctrl K") { AnActionEvent event ->
process(event) { Integer lineIndex, List<String> lines ->
while (lineIndex < lines.size() && !lines[lineIndex].trim().isEmpty()) lineIndex++
while (lineIndex < lines.size() && lines[lineIndex].trim().isEmpty()) lineIndex++
if (lineIndex >= lines.size() - 1) return lines.size() - 1
lineIndex
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment