Skip to content

Commit b64a2cd

Browse files
committed
clojure/DependencyContents
1 parent e3c6b0d commit b64a2cd

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
(into {})
2424
walk/keywordize-keys))))
2525

26+
(defn dependency-contents [^String uri ^Project project]
27+
(when-let [manager (LanguageServerManager/getInstance project)]
28+
(when-let [server (.getServer ^LanguageServerItem @(.getLanguageServer manager "clojure-lsp"))]
29+
(some->> (.dependencyContents ^ClojureLanguageServer server {"uri" uri})
30+
deref))))
31+
2632
(defn execute-command [^String name ^String text ^List args ^Project project]
2733
(-> (CommandExecutor/executeCommand
2834
(doto (LSPCommandContext. (Command. text name args) project)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require
33
[clojure.java.io :as io])
44
(:import
5+
[com.intellij.openapi.project Project]
56
[java.io File]))
67

78
(set! *warn-on-reflection* true)
@@ -16,3 +17,6 @@
1617

1718
(defn download-server-version-path ^File []
1819
(io/file (plugin-path) "clojure-lsp-version"))
20+
21+
(defn project-cache-path ^File [^Project project]
22+
(io/file (plugin-path) "cache" (.getName project)))

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
(ns com.github.clojure-lsp.intellij.editor
22
(:require
3+
[com.github.clojure-lsp.intellij.db :as db]
34
[com.github.clojure-lsp.intellij.editor :as editor])
45
(:import
56
[com.intellij.openapi.editor Editor]
67
[com.intellij.openapi.fileEditor FileDocumentManager]
7-
[com.intellij.openapi.util.text StringUtil]))
8+
[com.intellij.openapi.project ProjectLocator]
9+
[com.intellij.openapi.util.text StringUtil]
10+
[com.intellij.openapi.vfs VirtualFile]))
811

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

@@ -16,3 +19,7 @@
1619
(let [text (.getCharsSequence (.getDocument editor))
1720
line-col (StringUtil/offsetToLineColumn text offset)]
1821
[(.line line-col) (.column line-col)]))
22+
23+
(defn guess-project-for [^VirtualFile file]
24+
(or (.guessProjectForFile (ProjectLocator/getInstance) file)
25+
(first (db/all-projects))))

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

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
:name com.github.clojure_lsp.intellij.extension.LanguageServerFactory
44
:implements [com.redhat.devtools.lsp4ij.LanguageServerFactory])
55
(:require
6+
[clojure.java.io :as io]
67
[clojure.string :as string]
7-
[com.github.clojure-lsp.intellij.db :as db]
8+
[com.github.clojure-lsp.intellij.client :as lsp-client]
9+
[com.github.clojure-lsp.intellij.config :as config]
10+
[com.github.clojure-lsp.intellij.editor :as editor]
811
[com.github.clojure-lsp.intellij.server :as server]
912
[com.rpl.proxy-plus :refer [proxy+]])
1013
(:import
1114
[com.intellij.execution.configurations GeneralCommandLine]
1215
[com.intellij.openapi.progress ProgressIndicator]
13-
[com.intellij.openapi.project Project ProjectLocator]
14-
[com.intellij.openapi.vfs VirtualFile]
16+
[com.intellij.openapi.project Project]
17+
[com.intellij.openapi.vfs LocalFileSystem VirtualFile]
1518
[com.redhat.devtools.lsp4ij LSPIJUtils]
1619
[com.redhat.devtools.lsp4ij.client LanguageClientImpl]
1720
[com.redhat.devtools.lsp4ij.client.features LSPClientFeatures LSPProgressFeature]
@@ -50,6 +53,41 @@
5053
(swap! server assoc :status status :path path)
5154
(server/start! project))))
5255

56+
(defn ^:private create-temp-file ^VirtualFile
57+
[^Project project ^String path ^String text]
58+
(let [temp-file (io/file (config/project-cache-path project) path)]
59+
(io/make-parents temp-file)
60+
(spit temp-file text)
61+
(proxy+ [] VirtualFile
62+
(getName [_] (.getName temp-file))
63+
(getFileSystem [_] (LocalFileSystem/getInstance))
64+
(getPath [_] (.getCanonicalPath temp-file))
65+
(isWritable [_] false)
66+
(isDirectory [_] false)
67+
(isValid [_] true)
68+
(getParent [_] nil)
69+
(getChildren [_] [])
70+
(contentsToByteArray [_] (.getBytes text))
71+
(getTimeStamp [_] 0)
72+
(getLength [_] (count (.getBytes text)))
73+
(refresh [_ _ _ _])
74+
(getInputStream [_] (io/input-stream temp-file))
75+
(getOutputStream [_ _ _ _] (io/output-stream temp-file))
76+
(getModificationStamp [_] -1))))
77+
78+
(defn ^:private find-file-by-uri [^String uri]
79+
(if (and (string/starts-with? uri "file:")
80+
(string/includes? uri ".jar!"))
81+
(let [fixed-uri (string/replace-first uri "file:" "jar:file:")
82+
old-vfile (LSPIJUtils/findResourceFor fixed-uri)
83+
project (editor/guess-project-for old-vfile)
84+
dependency-contents (lsp-client/dependency-contents fixed-uri project)
85+
jar-pattern (re-pattern (str "^(jar|zip):(file:.+)!" (System/getProperty "file.separator") "(.+)"))
86+
path (last (re-find jar-pattern fixed-uri))
87+
tmp-file (create-temp-file project path dependency-contents)]
88+
tmp-file)
89+
(LSPIJUtils/findResourceFor uri)))
90+
5391
(defn -createClientFeatures [_]
5492
(doto
5593
(proxy+ [] LSPClientFeatures
@@ -62,18 +100,14 @@
62100
true
63101

64102
:not-found
65-
(do (install-server (or (.guessProjectForFile (ProjectLocator/getInstance) file)
66-
(first (db/all-projects))))
103+
(do (install-server (editor/guess-project-for file))
67104
false)))
68105
(initializeParams [_ ^InitializeParams params]
69106
(.setWorkDoneToken params "clojure-lsp-startup")
70107
(.setInitializationOptions params {"dependency-scheme" "jar"
71108
"hover" {"arity-on-same-line?" true}}))
72-
(findFileByUri [_ ^String uri]
73-
(if (and (string/starts-with? uri "file:")
74-
(string/includes? uri ".jar!"))
75-
(LSPIJUtils/findResourceFor (string/replace-first uri "file:" "jar:file:"))
76-
(LSPIJUtils/findResourceFor uri))))
109+
(findFileByUri ^VirtualFile [_ ^String uri]
110+
(find-file-by-uri uri)))
77111
(.setProgressFeature (proxy+ [] LSPProgressFeature
78112
(updateMessage [_ ^String message ^ProgressIndicator indicator]
79113
(.setText indicator (str "LSP: " message)))))))

src/main/kotlin/clojure_language_server.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import java.util.concurrent.CompletableFuture
66

77
interface ClojureLanguageServer : LanguageServer {
88

9+
@JsonRequest("clojure/dependencyContents")
10+
fun dependencyContents(params: Any): CompletableFuture<String>
11+
912
@JsonRequest("clojure/serverInfo/raw")
10-
fun serverInfo(): CompletableFuture<Object>
13+
fun serverInfo(): CompletableFuture<Any>
1114
}

0 commit comments

Comments
 (0)