Created
July 7, 2013 15:08
-
-
Save dkandalov/5943754 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
(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