Skip to content

Commit f8c7106

Browse files
committed
refactor some functions
1 parent ed7e2ac commit f8c7106

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
(ns com.github.clojure-lsp.intellij.editor
22
(:require
33
[com.github.clojure-lsp.intellij.db :as db]
4-
[com.github.clojure-lsp.intellij.editor :as editor])
4+
[com.github.clojure-lsp.intellij.editor :as editor]
5+
[com.github.ericdallo.clj4intellij.app-manager :as app-manager])
56
(:import
6-
[com.intellij.openapi.editor Editor]
7+
[com.intellij.openapi.editor CaretModel Editor LogicalPosition]
78
[com.intellij.openapi.fileEditor FileDocumentManager]
89
[com.intellij.openapi.project ProjectLocator]
910
[com.intellij.openapi.util.text StringUtil]
@@ -23,3 +24,11 @@
2324
(defn guess-project-for [^VirtualFile file]
2425
(or (.guessProjectForFile (ProjectLocator/getInstance) file)
2526
(first (db/all-projects))))
27+
28+
(defn move-caret-to-position
29+
"Moves the caret to the specified logical position in the editor."
30+
[^Editor editor line column]
31+
(let [caret ^CaretModel (.getCaretModel editor)
32+
new-position (LogicalPosition. line column)]
33+
@(app-manager/invoke-later!
34+
{:invoke-fn (fn [] (.moveToLogicalPosition caret new-position))})))
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
(ns com.github.clojure-lsp.intellij.foo-test
22
(:require
33
[clojure.test :refer [deftest is]]
4+
[com.github.clojure-lsp.intellij.editor :as editor]
45
[com.github.clojure-lsp.intellij.test-utils :as test-utils]
5-
[com.github.ericdallo.clj4intellij.app-manager :as app-manager]
66
[com.github.ericdallo.clj4intellij.test :as clj4intellij.test]))
77

88
(set! *warn-on-reflection* true)
99

1010
(deftest foo-test
11+
"Tests the Forward Slurp editor action functionality in Clojure LSP.
12+
This test:
13+
1. Sets up a test project with a Clojure file
14+
2. Opens the file in the editor
15+
3. Sets up the LSP server
16+
4. Moves the caret to a specific position
17+
5. Executes the Forward Slurp action
18+
6. Verifies the resulting text matches the expected output
19+
20+
The test ensures that the Forward Slurp action correctly modifies the code structure
21+
by moving the closing parenthesis forward."
1122
(let [project-name "clojure.core"
1223
{:keys [fixture project deps-file]} (test-utils/setup-test-project project-name)
1324
clj-file (.copyFileToProject fixture "foo.clj")]
@@ -16,9 +27,9 @@
1627

1728
(let [editor (test-utils/open-file-in-editor fixture clj-file)]
1829
(test-utils/setup-lsp-server project)
19-
(test-utils/move-caret-to-position editor 2 8)
30+
(editor/move-caret-to-position editor 2 8)
2031
(test-utils/run-editor-action "ClojureLSP.ForwardSlurp" project)
2132
(clj4intellij.test/dispatch-all)
2233
(println (test-utils/get-editor-text fixture))
23-
(test-utils/check-result-by-file fixture "foo_expected.clj")
34+
(.checkResultByFile fixture "foo_expected.clj")
2435
(test-utils/teardown-test-project project))))

src/test/clojure/com/github/clojure_lsp/intellij/test_utils.clj

+14-33
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99
[com.github.clojure_lsp.intellij.extension SettingsState]
1010
[com.intellij.ide DataManager]
1111
[com.intellij.openapi.actionSystem ActionManager]
12-
[com.intellij.openapi.components ServiceManager]
13-
[com.intellij.openapi.editor LogicalPosition]
14-
[com.intellij.openapi.wm WindowManager]))
12+
[com.intellij.openapi.components ServiceManager]))
1513

1614
(set! *warn-on-reflection* true)
1715

18-
(defn get-status-bar-widget [project widget-id]
19-
(let [status-bar (.. (WindowManager/getInstance) (getStatusBar project))]
20-
(.getWidget status-bar widget-id)))
16+
(defn get-editor-text
17+
"Returns the text content of the editor's document."
18+
[fixture]
19+
(-> fixture .getEditor .getDocument .getText))
20+
21+
(defn open-file-in-editor
22+
"Opens a file in the editor and returns the editor instance."
23+
[fixture file]
24+
(let [project (.getProject fixture)]
25+
(app-manager/write-command-action
26+
project
27+
(fn [] (.openFileInEditor fixture file)))
28+
(.getEditor fixture)))
2129

2230
(defn run-editor-action
2331
"Runs an editor action with the given ID for the specified project."
@@ -77,33 +85,6 @@
7785
:project project
7886
:deps-file deps-file})))
7987

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-
10788
(defn setup-lsp-server
10889
"Sets up and waits for the LSP server to be ready."
10990
[project]

0 commit comments

Comments
 (0)