Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Last active December 19, 2015 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkandalov/5921340 to your computer and use it in GitHub Desktop.
Save dkandalov/5921340 to your computer and use it in GitHub Desktop.
InsertNewLineAbove mini-plugin for https://github.com/dkandalov/live-plugin
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