Skip to content

Commit a013cad

Browse files
committed
Improve find definition of external files to consider opened files, avoiding exceptions.
1 parent 8e20c68 commit a013cad

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
- Only log server communication when trace-level matches.
6+
- Improve find definition of external files to consider opened files, avoiding exceptions.
7+
58
## 2.4.4
69

710
- Fix freeze after using paredit raise.

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

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
(let [response
3131
(case message-type
3232
(:parse-error :invalid-request)
33-
(protocols.endpoint/log client :red "Error reading message" message)
33+
(protocols.endpoint/log client :error "Error reading message" message)
3434
:request
3535
(protocols.endpoint/receive-request client context message)
3636
(:response.result :response.error)
@@ -41,18 +41,19 @@
4141
(when (identical? :request message-type)
4242
response))
4343
(catch Throwable e
44-
(protocols.endpoint/log client :red "Error receiving:" e)
44+
(protocols.endpoint/log client :error "Error receiving:" e)
4545
(throw e)))))
4646

4747
(defrecord Client [client-id
4848
input-ch
4949
output-ch
5050
join
5151
request-id
52-
sent-requests]
52+
sent-requests
53+
trace-level]
5354
protocols.endpoint/IEndpoint
5455
(start [this context]
55-
(protocols.endpoint/log this :white "lifecycle:" "starting")
56+
(protocols.endpoint/log this :verbose "lifecycle:" "starting")
5657
(let [pipeline (async/pipeline-blocking
5758
1 ;; no parallelism preserves server message order
5859
output-ch
@@ -68,23 +69,25 @@
6869
;; shut down
6970
join)
7071
(shutdown [this]
71-
(protocols.endpoint/log this :white "lifecycle:" "shutting down")
72+
(protocols.endpoint/log this :verbose "lifecycle:" "shutting down")
7273
;; closing input will drain pipeline, then close output, then close
7374
;; pipeline
7475
(async/close! input-ch)
7576
(if (= :done (deref join 10e3 :timeout))
76-
(protocols.endpoint/log this :white "lifecycle:" "shutdown")
77-
(protocols.endpoint/log this :red "lifecycle:" "shutdown timed out")))
77+
(protocols.endpoint/log this :verbose "lifecycle:" "shutdown")
78+
(protocols.endpoint/log this :verbose "lifecycle:" "shutdown timed out")))
7879
(log [this msg params]
79-
(protocols.endpoint/log this :white msg params))
80-
(log [_this _color msg params]
81-
;; TODO apply color
82-
(logger/info (string/join " " [msg params])))
80+
(protocols.endpoint/log this :verbose msg params))
81+
(log [_this level msg params]
82+
(when (or (identical? trace-level level)
83+
(identical? trace-level :verbose))
84+
;; TODO apply color
85+
(logger/info (string/join " " [msg params]))))
8386
(send-request [this method body]
8487
(let [req (lsp.requests/request (swap! request-id inc) method body)
8588
p (promise)
8689
start-ns (System/nanoTime)]
87-
(protocols.endpoint/log this :cyan "sending request:" req)
90+
(protocols.endpoint/log this :messages "sending request:" req)
8891
;; Important: record request before sending it, so it is sure to be
8992
;; available during receive-response.
9093
(swap! sent-requests assoc (:id req) {:request p
@@ -93,44 +96,45 @@
9396
p))
9497
(send-notification [this method body]
9598
(let [notif (lsp.requests/notification method body)]
96-
(protocols.endpoint/log this :blue "sending notification:" notif)
99+
(protocols.endpoint/log this :messages "sending notification:" notif)
97100
(async/>!! output-ch notif)))
98101
(receive-response [this {:keys [id] :as resp}]
99102
(if-let [{:keys [request start-ns]} (get @sent-requests id)]
100103
(let [ms (float (/ (- (System/nanoTime) start-ns) 1000000))]
101-
(protocols.endpoint/log this :green (format "received response (%.0fms):" ms) resp)
104+
(protocols.endpoint/log this :messages (format "received response (%.0fms):" ms) resp)
102105
(swap! sent-requests dissoc id)
103106
(deliver request (if (:error resp)
104107
resp
105108
(:result resp))))
106-
(protocols.endpoint/log this :red "received response for unmatched request:" resp)))
109+
(protocols.endpoint/log this :error "received response for unmatched request:" resp)))
107110
(receive-request [this context {:keys [id method params] :as req}]
108-
(protocols.endpoint/log this :magenta "received request:" req)
111+
(protocols.endpoint/log this :messages "received request:" req)
109112
(when-let [response-body (case method
110113
"window/showMessageRequest" (show-message-request params)
111114
"window/showDocument" (show-document context params)
112115
"workspace/applyEdit" (workspace-apply-edit context params)
113116
(logger/warn "Unknown LSP request method" method))]
114117
(let [resp (lsp.responses/response id response-body)]
115-
(protocols.endpoint/log this :magenta "sending response:" resp)
118+
(protocols.endpoint/log this :messages "sending response:" resp)
116119
resp)))
117120
(receive-notification [this context {:keys [method params] :as notif}]
118-
(protocols.endpoint/log this :blue "received notification:" notif)
121+
(protocols.endpoint/log this :messages "received notification:" notif)
119122
(case method
120123
"window/showMessage" (show-message context params)
121124
"$/progress" (progress context params)
122125
"textDocument/publishDiagnostics" (publish-diagnostics context params)
123126

124127
(logger/warn "Unknown LSP notification method" method))))
125128

126-
(defn client [in out]
129+
(defn client [in out trace-level]
127130
(map->Client
128131
{:client-id 1
129132
:input-ch (io-chan/input-stream->input-chan out)
130133
:output-ch (io-chan/output-stream->output-chan in)
131134
:join (promise)
132135
:sent-requests (atom {})
133-
:request-id (atom 0)}))
136+
:request-id (atom 0)
137+
:trace-level trace-level}))
134138

135139
(defn start-client! [client context]
136140
(protocols.endpoint/start client context))

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
:name com.github.clojure_lsp.intellij.extension.Definition
44
:extends com.intellij.codeInsight.navigation.actions.GotoDeclarationHandlerBase)
55
(:require
6+
[clojure.java.io :as io]
67
[clojure.string :as string]
78
[com.github.clojure-lsp.intellij.action.references :as action.references]
89
[com.github.clojure-lsp.intellij.client :as lsp-client]
@@ -33,19 +34,23 @@
3334
(let [v-file (file-system/create-temp-file project path text)]
3435
(definition->psi-element v-file project definition text)))))
3536

36-
(defn ^:private show-definition [definition client project]
37+
(defn ^:private show-definition [definition ^VirtualFile current-v-file client project]
3738
(when-let [uri (:uri definition)]
3839
(if (string/starts-with? uri "jar:")
3940
(let [jar-pattern (re-pattern (str "^(jar|zip):(file:.+)!" (System/getProperty "file.separator") "(.+)"))]
4041
(dependency-content client uri project definition (last (re-find jar-pattern uri))))
4142
;; TODO improve this
42-
(if-let [v-file (util/uri->v-file uri)]
43+
(if-let [v-file (or (util/uri->v-file uri)
44+
(when (= uri (.getUrl current-v-file)) current-v-file))]
4345
(definition->psi-element v-file project definition nil)
44-
(dependency-content client uri project definition (string/replace-first uri (str "file://" (config/project-cache-path project)) ""))))))
46+
(dependency-content client uri project definition
47+
(str (.relativize (.toPath (io/file (config/project-cache-path project)))
48+
(.toPath (io/file (java.net.URI. uri))))))))))
4549

4650
(defn -getGotoDeclarationTargets [_ ^PsiElement element _ ^Editor editor]
4751
(let [[line character] (:start (editor/text-range->range (.getTextRange element) editor))
48-
project ^Project (.getProject editor)]
52+
project ^Project (.getProject editor)
53+
current-v-file (some-> element .getContainingFile .getVirtualFile)]
4954
(when-let [client (lsp-client/connected-client project)]
5055
(when-let [definition @(lsp-client/request! client [:textDocument/definition
5156
{:text-document {:uri (editor/editor->uri editor)}
@@ -54,5 +59,5 @@
5459
(when-let [elements (if (and (= line (-> definition :range :start :line))
5560
(= character (-> definition :range :start :character)))
5661
(action.references/get-references editor line character client)
57-
(show-definition definition client project))]
62+
(show-definition definition current-v-file client project))]
5863
(into-array PsiElement elements))))))

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@
9393
(defn ^:private spawn-server! [^Project project indicator server-path]
9494
(logger/info "Spawning LSP server process using path" server-path)
9595
(tasks/set-progress indicator "LSP: Starting server...")
96-
(let [process (p/process [server-path "listen"]
96+
(let [trace-level (keyword (db/get-in project [:settings :trace-level]))
97+
process (p/process [server-path "listen"]
9798
{:dir (.getBasePath project)
9899
:env (EnvironmentUtil/getEnvironmentMap)
99100
:err :string})
100-
client (lsp-client/client (:in process) (:out process))]
101+
client (lsp-client/client (:in process) (:out process) trace-level)]
101102
(db/assoc-in project [:server-process] process)
102103
(lsp-client/start-client! client {:progress-indicator indicator
103104
:project project})

0 commit comments

Comments
 (0)