Skip to content

Commit

Permalink
Use new lsp4ij install api
Browse files Browse the repository at this point in the history
  • Loading branch information
ericdallo committed Feb 13, 2025
1 parent db38460 commit 824e9fe
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@
[com.redhat.devtools.lsp4ij LSPIJUtils ServerStatus]
[com.redhat.devtools.lsp4ij.client LanguageClientImpl]
[com.redhat.devtools.lsp4ij.client.features LSPClientFeatures LSPProgressFeature]
[com.redhat.devtools.lsp4ij.installation LanguageServerInstallerBase]
[com.redhat.devtools.lsp4ij.server OSProcessStreamConnectionProvider]
[java.io File]
[java.util List]
[org.eclipse.lsp4j InitializeParams]))

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

(defonce ^:private server (atom {:status :not-found
:path nil}))
(defonce ^:private server-path* (atom nil))
(defonce ^:private server-installing* (atom false))

(defn -createConnectionProvider [_ ^Project _project]
(let [server-path (loop []
(Thread/sleep 100)
(or (settings/server-path)
(some-> ^File (:path @server) .getCanonicalPath)
(recur)))
command [server-path "listen"]]
(let [path (loop []
(Thread/sleep 100)
(or (settings/server-path)
(some-> ^File @server-path* .getCanonicalPath)
(recur)))
command [path "listen"]]
(doto (proxy+
[]
OSProcessStreamConnectionProvider)
Expand All @@ -48,14 +49,6 @@
(defn -getServerInterface [_]
com.github.clojure_lsp.intellij.ClojureLanguageServer)

(defn ^:private install-server [project]
(swap! server assoc :status :installing)
(server/install-server
project
(fn [{:keys [status path]}]
(swap! server assoc :status status :path path)
(server/start! project))))

(defn ^:private create-temp-file ^VirtualFile
[^Project project ^String path ^String text]
(let [temp-file (io/file (config/project-cache-path project) path)]
Expand Down Expand Up @@ -96,28 +89,30 @@
(defn -createClientFeatures [_]
(doto
(proxy+ [] LSPClientFeatures
(isEnabled [_this ^VirtualFile file]
(case (:status @server)
:installing
false

:installed
true

:not-found
(do (install-server (editor/guess-project-for file))
false)))
(keepServerAlive [_] true)
(initializeParams [_ ^InitializeParams params]
(.setWorkDoneToken params "clojure-lsp-startup")
(.setInitializationOptions params {"dependency-scheme" "jar"
"hover" {"arity-on-same-line?" true}}))
(findFileByUri ^VirtualFile [_ ^String uri]
(find-file-by-uri uri))
(keepServerAlive [_] true)
(handleServerStatusChanged [^LSPClientFeatures this ^ServerStatus server-status]
(let [status (keyword (.toString server-status))]
(db/assoc-in (.getProject this) [:status] status)
(run! #(% status) (db/get-in (.getProject this) [:on-status-changed-fns])))))
(.setProgressFeature (proxy+ [] LSPProgressFeature
(updateMessage [_ ^String message ^ProgressIndicator indicator]
(.setText indicator (str "LSP: " message)))))))
(.setText indicator (str "LSP: " message)))))
(.setServerInstaller (proxy+ [] LanguageServerInstallerBase
(checkServerInstalled [_ _indicator]
(let [{:keys [status path]} (server/server-install-status)]
(if (identical? :installed status)
(do
(when-not @server-path* (reset! server-path* path))
true)
false)))
(install [^LSPClientFeatures this indicator]
(when-not @server-installing*
(reset! server-installing* true)
(reset! server-path* (server/install-server! (.getProject this) indicator))
(reset! server-installing* false)))))))
54 changes: 29 additions & 25 deletions src/main/clojure/com/github/clojure_lsp/intellij/server.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,38 @@
(db/assoc-in project [:downloaded-server-path] dest-path)
(logger/info "Downloaded clojure-lsp to" dest-path)))

(defn install-server [project installed-fn]
(tasks/run-background-task!
project
"LSP: install clojure-lsp"
(fn [indicator]
(let [download-path (config/download-server-path)
server-version-path (config/download-server-version-path)
latest-version* (delay (try (string/trim (slurp latest-version-uri)) (catch Exception _ nil)))
custom-server-path (settings/server-path)]
(cond
custom-server-path
(installed-fn {:status :installed :path custom-server-path})
(defn server-install-status []
(let [download-path (config/download-server-path)
server-version-path (config/download-server-version-path)
latest-version* (delay (try (string/trim (slurp latest-version-uri)) (catch Exception _ nil)))
custom-server-path (settings/server-path)]
(cond
custom-server-path
{:status :installed :path custom-server-path}

(and (.exists download-path)
(or (not @latest-version*) ;; on network connection issues we use any downloaded server
(= (try (slurp server-version-path) (catch Exception _ :error-checking-local-version))
@latest-version*)))
{:status :installed :path download-path}

(and (.exists download-path)
(or (not @latest-version*) ;; on network connection issues we use any downloaded server
(= (try (slurp server-version-path) (catch Exception _ :error-checking-local-version))
@latest-version*)))
(installed-fn {:status :installed :path download-path})
@latest-version*
{:status :not-installed :path download-path :version @latest-version* :version-path server-version-path}

@latest-version*
(do (download-server! project indicator download-path server-version-path @latest-version*)
(installed-fn {:status :installed :path download-path}))
:else
{:status :error})))

:else
(notification/show-notification! {:project project
:type :error
:title "Clojure LSP download error"
:message "There is no server downloaded and there was a network issue to download the latest one"}))))))
(defn install-server! [project indicator]
(Thread/sleep 3000)
(let [{:keys [status path version version-path]} (server-install-status)]
(case status
:installed path
:not-installed (do (download-server! project indicator path version-path version)
path)
(notification/show-notification! {:project project
:type :error
:title "Clojure LSP download error"
:message "There is no server downloaded and there was a network issue to download the latest one"}))))

(defn start! [^Project project]
(.start (LanguageServerManager/getInstance project) "clojure-lsp"))
Expand Down

0 comments on commit 824e9fe

Please sign in to comment.