11(ns com.github.clojure-lsp.intellij.test-utils
22 (:require
33 [com.github.clojure-lsp.intellij.client :as lsp-client]
4+ [com.github.clojure-lsp.intellij.server :as server]
45 [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]
810 [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]))
1015
1116(set! *warn-on-reflection* true )
1217
1318(defn get-status-bar-widget [project widget-id]
1419 (let [status-bar (.. (WindowManager/getInstance ) (getStatusBar project))]
1520 (.getWidget status-bar widget-id)))
1621
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]
1825 (let [action (.getAction (ActionManager/getInstance ) action-id)
1926 context (.getDataContext (DataManager/getInstance ))]
2027 (println " Running action:" action-id)
2633 (com.intellij.openapi.actionSystem.AnActionEvent/createFromDataContext action-id nil context))))))
2734
2835(defn dispatch-all-until
36+ " Dispatches all events until the LSP server is started or the timeout is reached."
2937 [{:keys [project millis timeout]
3038 :or {millis 1000
3139 timeout 10000 }}]
4856 (do
4957 (clj4intellij.test/dispatch-all )
5058 (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