Skip to content

Commit

Permalink
Add test-helper for loading a lot of code and returning cursor positi…
Browse files Browse the repository at this point in the history
…ons within it
  • Loading branch information
snoe committed Jan 17, 2021
1 parent 4ea0930 commit f536a59
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
5 changes: 2 additions & 3 deletions src/clojure_lsp/handlers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@
:text-document {:version version :uri doc-id}}))

(defn rename [{:keys [textDocument position newName]}]
(let [row (-> position :line inc)
col (-> position :character inc)
(let [[row col] (shared/position->line-column position)
references (q/find-references-from-cursor (:analysis @db/db) (shared/uri->filename textDocument) row col true)
definition (first (filter (comp #{:locals :var-definitions :namespace-definitions} :bucket) references))
can-rename? (and (not= :namespace-definitions (:bucket definition))
(string/starts-with? (:filename definition) (get-in @db/db [:project-settings :source-paths])))]
(some #(string/starts-with? (:filename definition) %) (get-in @db/db [:settings :source-paths])))]
(when (and (seq references) can-rename?)
(let [changes (mapv
(fn [r]
Expand Down
5 changes: 1 addition & 4 deletions src/clojure_lsp/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,8 @@
[ppid server]
(async/go-loop []
(async/<! (async/timeout 5000))
(log/debug "Checking parent process" ppid "liveness")
(if (shared/process-alive? ppid)
(do
(log/debug "Parent process" ppid "is running")
(recur))
(recur)
(do
(log/info "Parent process" ppid "is not running - exiting server")
(.exit server)))))
Expand Down
1 change: 1 addition & 0 deletions test/clojure_lsp/handlers_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@



#_
(deftest hover
(let [start-code "```clojure"
end-code "```"
Expand Down
24 changes: 21 additions & 3 deletions test/clojure_lsp/queries_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
(ns clojure-lsp.queries-test
(:require
[clojure.test :refer [deftest is]]
[clojure-lsp.queries :as q]))
[clojure-lsp.db :as db]
[clojure-lsp.queries :as q]
[clojure-lsp.test-helper :as h]
[clojure.test :refer [deftest]]
[clojure.tools.logging :as log]))

(deftest find-element-under-cursor []
(is (= [] (q/find-element-under-cursor [{:name-row 1 :name-col 1 :name-end-row 1 :name-end-col 10}] "filename" 1 10))))
(let [code (str "(ns a.b.c (:require [d.e.f :as |f-alias]))\n"
"(defn x [file|name] filename)\n"
"un|known")
[[alias-r alias-c]
[param-r param-c]
[unknown-r unknown-c]] (h/load-code-and-locs code)
ana (:analysis @db/db)]
(h/assert-submap
'{:name filename}
(q/find-element-under-cursor ana "/a.clj" alias-r alias-c))
(h/assert-submap
'{:name filename}
(q/find-element-under-cursor ana "/a.clj" param-r param-c))
(h/assert-submap
'{:name unknown}
(q/find-element-under-cursor ana "/a.clj" unknown-r unknown-c))))
45 changes: 34 additions & 11 deletions test/clojure_lsp/test_helper.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
(ns clojure-lsp.test-helper
(:require
[clojure-lsp.handlers :as handlers]
[clojure.pprint :as pprint]
[clojure.string :as string]
[clojure.test :refer [is]]))
[clojure.test :refer [is]]
[clojure.tools.logging :as log]))

(defn assert-submap [expected actual]
(is (= expected (some-> actual (select-keys (keys expected)))) (str "No superset of " (pr-str actual) " found")))

(defmacro assert-submaps
"Asserts that maps are submaps of result in corresponding order and
Expand All @@ -18,12 +22,31 @@
(format "Expected %s results, but got: %s \n--\n%s--"
(count maps#) (count res#) (with-out-str (pprint/pprint res#))))
(doseq [[r# m#] (map vector res# maps#)]
(is (= m# (select-keys r# (keys m#))) (str "No superset of " m# " found"))))))

(defn pos-from-text [text]
[(first
(for [[row line] (map-indexed vector (string/split-lines text))
[col c] (map-indexed vector line)
:when (= \| c)]
[(inc row) (inc col)]))
(string/replace text #"\|" "")])
(assert-submap m# r# (str "No superset of " m# " found"))))))

(defn positions-from-text
"Takes text with a pipe `|` as a placeholder for cursor positions and returns the text without
the pipes alone with a vector of [line column] pairs representing the cursor positions (1-based)"
[text]
(let [[_ _ text positions] (reduce
(fn [[row column text positions] ch]
(cond
(= \| ch)
[row column text (conj positions [row column])]

(= \newline ch)
[(inc row) 1 (str text ch) positions]

:else
[row (inc column) (str text ch) positions]))
[1 1 "" []]
text)]

[text positions]))


(defn load-code-and-locs [code & [filename]]
(let [[code positions] (positions-from-text code)
filename (or filename "file:///a.clj")]
(handlers/did-open {:textDocument {:uri filename :text code}})
positions))

0 comments on commit f536a59

Please sign in to comment.