Skip to content

Commit 08a8206

Browse files
committed
Fix code lens references command
1 parent 2db388d commit 08a8206

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
(ns com.github.clojure-lsp.intellij.client
22
(:require
33
[clojure.walk :as walk]
4-
[com.github.clojure-lsp.intellij.client :as lsp-client])
4+
[com.github.clojure-lsp.intellij.client :as lsp-client]
5+
[com.github.ericdallo.clj4intellij.logger :as logger])
56
(:import
67
[com.github.clojure_lsp.intellij ClojureLanguageServer]
78
[com.intellij.openapi.project Project]
89
[com.redhat.devtools.lsp4ij LanguageServerItem LanguageServerManager]
910
[com.redhat.devtools.lsp4ij.commands CommandExecutor LSPCommandContext]
1011
[java.util List]
11-
[org.eclipse.lsp4j Command]))
12+
[org.eclipse.lsp4j
13+
Command
14+
Position
15+
ReferenceContext
16+
ReferenceParams
17+
TextDocumentIdentifier]))
1218

1319
(set! *warn-on-reflection* true)
1420

@@ -31,9 +37,18 @@
3137
(some->> (.dependencyContents ^ClojureLanguageServer server {"uri" uri})
3238
deref))))
3339

40+
(defn references [^String uri line character ^Project project]
41+
(when-let [manager (LanguageServerManager/getInstance project)]
42+
(when-let [server (.getServer ^LanguageServerItem @(.getLanguageServer manager "clojure-lsp"))]
43+
(some-> (.getTextDocumentService ^ClojureLanguageServer server)
44+
(.references (ReferenceParams. (TextDocumentIdentifier. uri)
45+
(Position. line character)
46+
(ReferenceContext. false)))
47+
deref))))
48+
3449
(defn execute-command [^String name ^String text ^List args ^Project project]
3550
(-> (CommandExecutor/executeCommand
36-
(doto (LSPCommandContext. (Command. text name args) project)
37-
(.setPreferredLanguageServerId "clojure-lsp")))
51+
(doto (LSPCommandContext. (Command. text name args) project)
52+
(.setPreferredLanguageServerId "clojure-lsp")))
3853
(.response)
3954
deref))

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,21 @@
1111
[com.github.ericdallo.clj4intellij.action :as action]
1212
[com.github.ericdallo.clj4intellij.logger :as logger]
1313
[com.github.ericdallo.clj4intellij.tasks :as tasks]
14-
[com.github.ericdallo.clj4intellij.util :as util])
14+
[com.github.ericdallo.clj4intellij.util :as util]
15+
[com.rpl.proxy-plus :refer [proxy+]])
1516
(:import
1617
[com.github.clojure_lsp.intellij Icons]
17-
[com.intellij.openapi.actionSystem AnActionEvent CommonDataKeys]
18+
[com.google.gson JsonPrimitive]
19+
[com.intellij.openapi.actionSystem
20+
ActionManager
21+
ActionUpdateThread
22+
AnActionEvent
23+
CommonDataKeys]
1824
[com.intellij.openapi.editor Editor]
19-
[com.intellij.openapi.project Project]))
25+
[com.intellij.openapi.project Project]
26+
[com.redhat.devtools.lsp4ij LanguageServerManager]
27+
[com.redhat.devtools.lsp4ij.commands LSPCommand LSPCommandAction]
28+
[com.redhat.devtools.lsp4ij.usages LSPUsageType LSPUsagesManager LocationData]))
2029

2130
(set! *warn-on-reflection* true)
2231

@@ -76,6 +85,29 @@
7685
(fn [_]
7786
(lsp-client/execute-command command-name text [(editor/editor->uri editor) line character] project))))))
7887

88+
(defn register-command!
89+
[& {:keys [id on-performed]}]
90+
(let [manager (ActionManager/getInstance)
91+
action (proxy+ [] LSPCommandAction
92+
(commandPerformed [_ command event] (on-performed command event))
93+
(getCommandPerformedThread [_] ActionUpdateThread/EDT))]
94+
(when-not (.getAction manager id)
95+
(.registerAction manager id action)
96+
action)))
97+
98+
(defn ^:private code-lens-references-performed [^Project project ^LSPCommand command ^AnActionEvent event]
99+
(let [uri (.getAsString ^JsonPrimitive (.getArgumentAt command 0))
100+
line (dec (.getAsInt ^JsonPrimitive (.getArgumentAt command 1)))
101+
character (dec (.getAsInt ^JsonPrimitive (.getArgumentAt command 2)))
102+
references (lsp-client/references uri line character project)
103+
language-server @(.getLanguageServer (LanguageServerManager/getInstance project) "clojure-lsp")]
104+
(.findShowUsagesInPopup
105+
(LSPUsagesManager/getInstance project)
106+
(mapv #(LocationData. % language-server) references)
107+
LSPUsageType/References
108+
(.getDataContext event)
109+
(.getInputEvent event))))
110+
79111
(defn -runActivity [_this ^Project project]
80112
(doseq [{:keys [name text description use-shortcut-of keyboard-shortcut]} clojure-lsp-commands]
81113
(action/register-action! :id (str "ClojureLSP." (csk/->PascalCase name))
@@ -85,6 +117,8 @@
85117
:keyboard-shortcut keyboard-shortcut
86118
:use-shortcut-of use-shortcut-of
87119
:on-performed (partial on-action-performed name text project)))
120+
(register-command! :id "code-lens-references"
121+
:on-performed (partial code-lens-references-performed project))
88122
(action/register-group! :id "ClojureLSP.Refactors"
89123
:popup true
90124
:text "Clojure refactors"

src/main/resources/META-INF/plugin.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727
<incompatible-with>com.cursiveclojure.cursive</incompatible-with>
2828

2929
<extensions defaultExtensionNs="com.intellij">
30+
<!-- Clojure language / lexer, syntax parser -->
3031
<fileType name="clojure" language="clojure" extensions="clj;cljs;cljc;cljd;edn;bb;clj_kondo"
3132
implementationClass="com.github.clojure_lsp.intellij.ClojureFileType" fieldName="INSTANCE"/>
3233

33-
<!-- syntax parse -->
3434
<lang.parserDefinition language="clojure" implementationClass="com.github.clojure_lsp.intellij.language.parser.ClojureParserDefinition"/>
3535
<lang.syntaxHighlighterFactory language="clojure" implementationClass="com.github.clojure_lsp.intellij.extension.SyntaxHighlighter"/>
3636
<colorSettingsPage implementation="com.github.clojure_lsp.intellij.extension.ColorSettingsPage"/>
3737

38-
<!-- LSP features -->
39-
38+
<!-- Features -->
4039
<postStartupActivity implementation="com.github.ericdallo.clj4intellij.extension.NREPLStartup"/>
4140
<postStartupActivity implementation="com.github.clojure_lsp.intellij.extension.InitDBStartup"/>
4241
<postStartupActivity implementation="com.github.clojure_lsp.intellij.extension.RegisterActionsStartup"/>

0 commit comments

Comments
 (0)