1
1
(ns com.github.clojure-lsp.intellij.test-utils
2
2
(:require
3
3
[com.github.clojure-lsp.intellij.client :as lsp-client]
4
+ [com.github.clojure-lsp.intellij.server :as server]
4
5
[com.github.ericdallo.clj4intellij.app-manager :as app-manager]
5
- [com.github.ericdallo.clj4intellij.test :as clj4intellij.test])
6
- (:import
7
- [com.intellij.openapi.wm WindowManager]
6
+ [com.github.ericdallo.clj4intellij.test :as clj4intellij.test]
7
+ [com.github.clojure-lsp.intellij.db :as db])
8
+ (:import
9
+ [com.github.clojure_lsp.intellij.extension SettingsState]
8
10
[com.intellij.ide DataManager]
9
- [com.intellij.openapi.actionSystem ActionManager]))
11
+ [com.intellij.openapi.actionSystem ActionManager]
12
+ [com.intellij.openapi.components ServiceManager]
13
+ [com.intellij.openapi.editor LogicalPosition]
14
+ [com.intellij.openapi.wm WindowManager]))
10
15
11
16
(set! *warn-on-reflection* true )
12
17
13
18
(defn get-status-bar-widget [project widget-id]
14
19
(let [status-bar (.. (WindowManager/getInstance ) (getStatusBar project))]
15
20
(.getWidget status-bar widget-id)))
16
21
17
- (defn run-editor-action [action-id project]
22
+ (defn run-editor-action
23
+ " Runs an editor action with the given ID for the specified project."
24
+ [action-id project]
18
25
(let [action (.getAction (ActionManager/getInstance ) action-id)
19
26
context (.getDataContext (DataManager/getInstance ))]
20
27
(println " Running action:" action-id)
26
33
(com.intellij.openapi.actionSystem.AnActionEvent/createFromDataContext action-id nil context))))))
27
34
28
35
(defn dispatch-all-until
36
+ " Dispatches all events until the LSP server is started or the timeout is reached."
29
37
[{:keys [project millis timeout]
30
38
:or {millis 1000
31
39
timeout 10000 }}]
48
56
(do
49
57
(clj4intellij.test/dispatch-all )
50
58
(Thread/sleep millis)
51
- (recur )))))))
59
+ (recur )))))))
60
+
61
+ (defn teardown-test-project
62
+ " Shuts down all resources for the given project."
63
+ [project]
64
+ (server/shutdown! project))
65
+
66
+ (defn setup-test-project
67
+ " Sets up a test project with the given name and optional deps.edn content.
68
+ Returns a map with :fixture, :project, and :deps-file."
69
+ ([project-name]
70
+ (setup-test-project project-name " {}" ))
71
+ ([project-name deps-content]
72
+ (let [fixture (clj4intellij.test/setup project-name)
73
+ deps-file (.createFile fixture " deps.edn" deps-content)
74
+ _ (.setTestDataPath fixture " testdata" )
75
+ project (.getProject fixture)]
76
+ {:fixture fixture
77
+ :project project
78
+ :deps-file deps-file})))
79
+
80
+ (defn open-file-in-editor
81
+ " Opens a file in the editor and returns the editor instance."
82
+ [fixture file]
83
+ (let [project (.getProject fixture)]
84
+ (app-manager/write-command-action
85
+ project
86
+ (fn [] (.openFileInEditor fixture file)))
87
+ (.getEditor fixture)))
88
+
89
+ (defn move-caret-to-position
90
+ " Moves the caret to the specified logical position in the editor."
91
+ [editor line column]
92
+ (let [caret (.getCaretModel editor)
93
+ new-position (LogicalPosition. line column)]
94
+ @(app-manager/invoke-later!
95
+ {:invoke-fn (fn [] (.moveToLogicalPosition caret new-position))})))
96
+
97
+ (defn get-editor-text
98
+ " Returns the text content of the editor's document."
99
+ [fixture]
100
+ (-> fixture .getEditor .getDocument .getText))
101
+
102
+ (defn check-result-by-file
103
+ " Checks if the current editor content matches the expected file."
104
+ [fixture expected-file]
105
+ (.checkResultByFile fixture expected-file))
106
+
107
+ (defn setup-lsp-server
108
+ " Sets up and waits for the LSP server to be ready."
109
+ [project]
110
+ (let [my-settings (ServiceManager/getService SettingsState)]
111
+ (.loadState my-settings my-settings)
112
+ (clj4intellij.test/dispatch-all )
113
+ (dispatch-all-until {:project project})
114
+ (println " status LSP >> " (db/get-in project [:status ]))))
0 commit comments