Skip to content

Instantly share code, notes, and snippets.

@dkandalov
Created July 7, 2013 15:08
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/5943754 to your computer and use it in GitHub Desktop.
Save dkandalov/5943754 to your computer and use it in GitHub Desktop.
InsertNewLineAbove mini-plugin for https://github.com/dkandalov/live-plugin
(ns plugin.clj
(:use clojure.contrib.import-static)
(:import [liveplugin PluginUtil])
(:import [groovy.lang Closure]))
(import-static liveplugin.PluginUtil show registerAction runDocumentWriteAction currentEditorIn)
; 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.
(defn as-groovy-closure [f]
(proxy [Closure] [""]
(call [arg] (f arg))))
(defn insert-new-line [project document]
(let [caret-model (. (currentEditorIn project) getCaretModel)
offset (.. caret-model getOffset)
current-line (.. caret-model getLogicalPosition line)
line-start-offset (. document getLineStartOffset current-line)]
(. document insertString line-start-offset "\n")
(. caret-model moveToOffset (+ offset 1))))
(defn insert-new-line-write-action [action-event]
(let [project (. action-event getProject)]
(runDocumentWriteAction project (as-groovy-closure #(insert-new-line project %)))))
(defn insert-new-line-action [] (as-groovy-closure insert-new-line-write-action))
(registerAction "InsertNewLineAbove" "alt shift ENTER" (insert-new-line-action))
(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