Skip to content

Commit 757eae1

Browse files
committed
Fix command execution for refactors
1 parent 0c76894 commit 757eae1

File tree

3 files changed

+38
-47
lines changed

3 files changed

+38
-47
lines changed

src/main/clojure/com/github/clojure_lsp/intellij/client.clj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[com.intellij.openapi.project Project]
44
[com.redhat.devtools.lsp4ij LanguageServerManager]
55
[com.redhat.devtools.lsp4ij.commands CommandExecutor LSPCommandContext]
6+
[java.util List]
67
[org.eclipse.lsp4j Command]))
78

89
(set! *warn-on-reflection* true)
@@ -11,8 +12,9 @@
1112
(when-let [manager (LanguageServerManager/getInstance project)]
1213
(keyword (.toString (.getServerStatus manager "clojure-lsp")))))
1314

14-
(defn execute-command [^String name ^String text ^Project project]
15+
(defn execute-command [^String name ^String text ^List args ^Project project]
1516
(-> (CommandExecutor/executeCommand
16-
(doto (LSPCommandContext. (Command. text name) project)
17+
(doto (LSPCommandContext. (Command. text name args) project)
1718
(.setPreferredLanguageServerId "clojure-lsp")))
18-
(.response)))
19+
(.response)
20+
deref))

src/main/clojure/com/github/clojure_lsp/intellij/editor.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
[com.github.clojure-lsp.intellij.editor :as editor])
44
(:import
55
[com.intellij.openapi.editor Editor]
6+
[com.intellij.openapi.fileEditor FileDocumentManager]
67
[com.intellij.openapi.util.text StringUtil]))
78

89
(set! *warn-on-reflection* true)
910

11+
(defn editor->uri [^Editor editor]
12+
;; TODO sanitize URL, encode, etc
13+
(.getUrl (.getFile (FileDocumentManager/getInstance) (.getDocument editor))))
14+
1015
(defn offset->cursor-position [^Editor editor offset]
1116
(let [text (.getCharsSequence (.getDocument editor))
1217
line-col (StringUtil/offsetToLineColumn text offset)]

src/main/clojure/com/github/clojure_lsp/intellij/extension/register_actions_startup.clj

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
:implements [com.intellij.openapi.startup.StartupActivity
55
com.intellij.openapi.project.DumbAware])
66
(:require
7+
[camel-snake-kebab.core :as csk]
78
[com.github.clojure-lsp.intellij.client :as lsp-client]
9+
[com.github.clojure-lsp.intellij.db :as db]
10+
[com.github.clojure-lsp.intellij.editor :as editor]
811
[com.github.ericdallo.clj4intellij.action :as action]
912
[com.github.ericdallo.clj4intellij.logger :as logger]
10-
[com.rpl.proxy-plus :refer [proxy+]])
13+
[com.github.ericdallo.clj4intellij.tasks :as tasks]
14+
[com.github.ericdallo.clj4intellij.util :as util])
1115
(:import
1216
[com.github.clojure_lsp.intellij Icons]
13-
[com.intellij.openapi.actionSystem ActionManager AnActionEvent KeyboardShortcut]
14-
[com.intellij.openapi.keymap KeymapManager]
15-
[com.intellij.openapi.project Project]
16-
[com.redhat.devtools.lsp4ij.commands LSPCommandAction]
17-
[javax.swing Icon KeyStroke]))
17+
[com.intellij.openapi.actionSystem AnActionEvent CommonDataKeys]
18+
[com.intellij.openapi.editor Editor]
19+
[com.intellij.openapi.project Project]))
1820

1921
(set! *warn-on-reflection* true)
2022

@@ -65,50 +67,32 @@
6567
{:name "raise-sexp" :text "Raise sexpr" :description "Raise current sexpr (Paredit)" :keyboard-shortcut {:first "alt R" :replace-all true}}
6668
{:name "kill-sexp" :text "Kill sexpr" :description "Kill current sexpr (Paredit)" :keyboard-shortcut {:first "alt K" :replace-all true}}])
6769

68-
#_{:clj-kondo/ignore [:unused-binding]}
69-
(defn register-command!
70-
[& {:keys [project id title description icon args use-shortcut-of keyboard-shortcut on-performed]}]
71-
(let [manager (ActionManager/getInstance)
72-
keymap-manager (KeymapManager/getInstance)
73-
keymap (.getActiveKeymap keymap-manager)
74-
action (proxy+ ClojureLSPCommand [] LSPCommandAction
75-
(commandPerformed [_ _command ^AnActionEvent event]
76-
(lsp-client/execute-command id title)))]
77-
78-
(.setText (.getTemplatePresentation action) ^String title)
79-
(.setIcon (.getTemplatePresentation action) ^Icon icon)
80-
(when-not (.getAction manager id)
81-
(.registerAction manager id action)
82-
(when use-shortcut-of
83-
(.addShortcut keymap
84-
id
85-
(first (.getShortcuts (.getShortcutSet (.getAction manager use-shortcut-of))))))
86-
(when keyboard-shortcut
87-
(let [k-shortcut (KeyboardShortcut. (KeyStroke/getKeyStroke ^String (:first keyboard-shortcut))
88-
(some-> ^String (:second keyboard-shortcut) KeyStroke/getKeyStroke))]
89-
(when (empty? (.getShortcuts keymap id))
90-
(.addShortcut keymap id k-shortcut))
91-
(when (:replace-all keyboard-shortcut)
92-
(doseq [[conflict-action-id shortcuts] (.getConflicts keymap id k-shortcut)]
93-
(doseq [shortcut shortcuts]
94-
(.removeShortcut keymap conflict-action-id shortcut))))))
95-
action)))
70+
(defn ^:private on-action-performed [command-name text project ^AnActionEvent event]
71+
(when-let [editor ^Editor (.getData event CommonDataKeys/EDITOR)]
72+
(let [[line character] (util/editor->cursor-position editor)]
73+
(tasks/run-background-task!
74+
project
75+
"LSP: refactoring"
76+
(fn [_]
77+
(lsp-client/execute-command command-name text [(editor/editor->uri editor) line character] project))))))
9678

9779
(defn -runActivity [_this ^Project project]
98-
(doseq [{:keys [name text args description use-shortcut-of keyboard-shortcut]} clojure-lsp-commands]
99-
(register-command! :id name
100-
:project project
101-
:title text
102-
:description description
103-
:args args
104-
:icon Icons/CLOJURE
105-
:keyboard-shortcut keyboard-shortcut
106-
:use-shortcut-of use-shortcut-of))
80+
(doseq [{:keys [name text description use-shortcut-of keyboard-shortcut]} clojure-lsp-commands]
81+
(action/register-action! :id (str "ClojureLSP." (csk/->PascalCase name))
82+
:title text
83+
:description description
84+
:icon Icons/CLOJURE
85+
:keyboard-shortcut keyboard-shortcut
86+
:use-shortcut-of use-shortcut-of
87+
:on-performed (partial on-action-performed name text project)))
10788
(action/register-group! :id "ClojureLSP.Refactors"
10889
:popup true
10990
:text "Clojure refactors"
11091
:icon Icons/CLOJURE
11192
:children (concat [{:type :add-to-group :group-id "RefactoringMenu" :anchor :first}]
112-
(mapv (fn [{:keys [name]}] {:type :reference :ref name}) clojure-lsp-commands)
93+
(mapv (fn [{:keys [name]}] {:type :reference :ref (str "ClojureLSP." (csk/->PascalCase name))}) clojure-lsp-commands)
11394
[{:type :separator}]))
11495
(logger/info "Actions registered"))
96+
97+
(comment
98+
(-runActivity nil (first (db/all-projects))))

0 commit comments

Comments
 (0)