|
| 1 | +(ns com.github.clojure-lsp.intellij.extension.new-file |
| 2 | + (:require |
| 3 | + [clojure.java.io :as io] |
| 4 | + [clojure.string :as string] |
| 5 | + [com.github.clojure-lsp.intellij.client :as lsp-client] |
| 6 | + [com.rpl.proxy-plus :refer [proxy+]]) |
| 7 | + (:import |
| 8 | + [com.github.clojure_lsp.intellij Icons] |
| 9 | + [com.intellij.ide.actions CreateFileFromTemplateAction CreateFileFromTemplateDialog$Builder] |
| 10 | + [com.intellij.ide.fileTemplates FileTemplate] |
| 11 | + [com.intellij.psi PsiDirectory])) |
| 12 | + |
| 13 | +(set! *warn-on-reflection* true) |
| 14 | + |
| 15 | +(defn ^:private filename->source-path [filename project] |
| 16 | + (let [source-paths (get-in (lsp-client/server-info project) [:final-settings "source-paths"])] |
| 17 | + (first (filter #(string/starts-with? filename %) source-paths)))) |
| 18 | + |
| 19 | +(defn ^:private dir->partial-namespace |
| 20 | + [filename project] |
| 21 | + (when-let [current-source-path (filename->source-path filename project)] |
| 22 | + (some-> filename |
| 23 | + (string/replace-first (re-pattern current-source-path) "") |
| 24 | + (string/replace (System/getProperty "file.separator") ".") |
| 25 | + (string/replace #"_" "-") |
| 26 | + not-empty |
| 27 | + (subs 1) |
| 28 | + (str ".")))) |
| 29 | + |
| 30 | +(defn ^:private dialog [project ^PsiDirectory dir ^CreateFileFromTemplateDialog$Builder builder] |
| 31 | + (let [filename (.getPath (.getVirtualFile dir)) |
| 32 | + namespace (dir->partial-namespace filename project)] |
| 33 | + (-> builder |
| 34 | + (.setTitle "New Clojure namespace") |
| 35 | + (.setDefaultText (or namespace "")) |
| 36 | + (.addKind "Clojure (.clj)" Icons/CLOJURE "ClojureNamespace") |
| 37 | + (.addKind "ClojureScript (.cljs)" Icons/CLOJURE_SCRIPT "ClojureScriptNamespace") |
| 38 | + (.addKind "CLJC (.cljc)" Icons/CLOJURE "CLJCNamespace") |
| 39 | + (.addKind "ClojureDart (.cljd)" Icons/CLOJURE_DART "ClojureDartNamespace")))) |
| 40 | + |
| 41 | +(defn ^:private create-file-from-template |
| 42 | + [project ^String ns ^FileTemplate template ^PsiDirectory dir] |
| 43 | + (let [dir-filename (.getPath (.getVirtualFile dir)) |
| 44 | + separator (System/getProperty "file.separator") |
| 45 | + source-path (filename->source-path dir-filename project) |
| 46 | + ns-path (-> ns |
| 47 | + (string/replace "." separator) |
| 48 | + (string/replace "-" "_")) |
| 49 | + new-name (string/replace-first (.getCanonicalPath (io/file source-path ns-path)) |
| 50 | + (str dir-filename separator) |
| 51 | + "")] |
| 52 | + (CreateFileFromTemplateAction/createFileFromTemplate new-name template dir nil true))) |
| 53 | + |
| 54 | +(defn ->ClojureNewFileAction [project] |
| 55 | + (proxy+ |
| 56 | + ["Clojure namespace" "Create a new Clojure namespace" Icons/CLOJURE] |
| 57 | + CreateFileFromTemplateAction |
| 58 | + (buildDialog [_ project dir builder] |
| 59 | + (dialog project dir builder)) |
| 60 | + (getActionName [_ _dir new-name _template-name] |
| 61 | + (str "Create Clojure namespace: " new-name)) |
| 62 | + (createFileFromTemplate [_ name template dir] |
| 63 | + (create-file-from-template project name template dir)))) |
0 commit comments