Skip to content

Commit 172ba4d

Browse files
authored
Merge pull request #9 from fabiodomingues/fix-settings-argument
Fixing settings argument
2 parents 97d199b + b8d198d commit 172ba4d

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

Diff for: src/leiningen/clojure_lsp.clj

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,37 @@
11
(ns leiningen.clojure-lsp
22
(:refer-clojure :exclude [run!])
33
(:require
4+
[clojure.edn :as edn]
45
[clojure-lsp.main :as lsp]
56
[leiningen.core.main :as lein-core]))
67

7-
(defn run! [command-and-options options]
8-
(let [result (apply lsp/run! (concat command-and-options ["--settings" (str options)]))]
8+
(defn ^:private has-settings?
9+
[command-and-options]
10+
(let [settings-index (.indexOf command-and-options "--settings")]
11+
(and (>= settings-index 0)
12+
(not-empty (nth command-and-options (inc settings-index))))))
13+
14+
(defn ^:private args-with-merged-settings
15+
[command-and-options project-settings]
16+
(let [settings-index (inc (.indexOf command-and-options "--settings"))
17+
settings (edn/read-string (nth command-and-options settings-index))]
18+
(assoc (vec command-and-options) settings-index (str (merge project-settings settings)))))
19+
20+
(defn args
21+
[command-and-options project-settings]
22+
(cond
23+
(and (not-empty project-settings) (has-settings? command-and-options))
24+
(args-with-merged-settings command-and-options project-settings)
25+
26+
(not-empty project-settings)
27+
(concat command-and-options ["--settings" (str project-settings)])
28+
29+
:else
30+
command-and-options))
31+
32+
(defn ^:private run!
33+
[command-and-options project-settings]
34+
(let [result (apply lsp/run! (args command-and-options project-settings))]
935
(when-let [message-fn (:message-fn result)]
1036
(println (message-fn)))
1137
(when (not= 0 (:result-code result))
@@ -15,8 +41,8 @@
1541
(defn ^:no-project-needed clojure-lsp
1642
"Access clojure-lsp API features"
1743
[project & command-and-options]
18-
(let [project-options (or (:clojure-lsp project) {})]
44+
(let [project-settings (or (:clojure-lsp project) {})]
1945
(if lein-core/*info*
20-
(run! command-and-options project-options)
46+
(run! command-and-options project-settings)
2147
(with-out-str
22-
(run! command-and-options project-options)))))
48+
(run! command-and-options project-settings)))))

Diff for: test/leiningen/clojure_lsp_test.clj

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns leiningen.clojure-lsp-test
2+
(:require [clojure.test :refer :all]
3+
[leiningen.clojure-lsp :as clojure-lsp]))
4+
5+
(deftest args-test
6+
(testing "Should return the arguments when project settings is an empty map"
7+
(is (= ["diagnostics"]
8+
(clojure-lsp/args ["diagnostics"] {}))))
9+
10+
(testing "Should return the arguments when project settings is nil"
11+
(is (= ["diagnostics"]
12+
(clojure-lsp/args ["diagnostics"] nil))))
13+
14+
(testing "Should return the arguments when there is project settings"
15+
(is (= ["diagnostics" "--settings" "{:foo 1}"]
16+
(clojure-lsp/args ["diagnostics"] {:foo 1}))))
17+
18+
(testing "Should return the arguments when there is settings options and also project settings"
19+
(is (= ["diagnostics" "--settings" "{:foo 1, :bar \"Test\"}"]
20+
(clojure-lsp/args ["diagnostics" "--settings" "{:bar \"Test\"}"] {:foo 1}))))
21+
22+
(testing "Should return the arguments when there is settings options and there is no project settings"
23+
(is (= ["diagnostics" "--settings" "{:bar \"Test\"}"]
24+
(clojure-lsp/args ["diagnostics" "--settings" "{:bar \"Test\"}"] {})))))

0 commit comments

Comments
 (0)