Last active
December 19, 2015 07:49
-
-
Save dkandalov/5921340 to your computer and use it in GitHub Desktop.
InsertNewLineAbove mini-plugin for https://github.com/dkandalov/live-plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.intellij.openapi.actionSystem.{AnActionEvent, AnAction} | |
import com.intellij.openapi.application.ApplicationManager | |
import com.intellij.openapi.command.{UndoConfirmationPolicy, CommandProcessor} | |
import groovy.lang.Closure | |
import liveplugin.PluginUtil._ | |
// This action inserts new line above current line. | |
// It's a follow-up for these posts: | |
// http://martinfowler.com/bliki/InternalReprogrammability.html | |
// http://nealford.com/memeagora/2013/01/22/why_everyone_eventually_hates_maven.html | |
// Note that there is "Start New Line Before Current" action (ctrl + alt + enter) which does almost the same thing. | |
implicit def asGroovyClosure_WithActionEventParam[F](f: (AnActionEvent) => F) = new Closure[F]() { def doCall(event: AnActionEvent): F = f(event) } | |
implicit def asGroovyClosure_WithNoParams[F](f: () => F) = new Closure[F]() { def doCall(arg: Any): F = f() } | |
registerAction("InsertNewLineAbove", "alt shift ENTER", (event: AnActionEvent) => { | |
runDocumentWriteAction(event.getProject, () => { | |
val editor = currentEditorIn(event.getProject) | |
val offset = editor.getCaretModel.getOffset | |
val currentLine = editor.getCaretModel.getLogicalPosition.line | |
val lineStartOffset = editor.getDocument.getLineStartOffset(currentLine) | |
editor.getDocument.insertString(lineStartOffset, "\n") | |
editor.getCaretModel.moveToOffset(offset + 1) | |
}) | |
}) | |
show("Loaded 'InsertNewLineAbove' action<br/>Use 'Alt+Shift+Enter' to run it") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment