Skip to content

Commit 1668527

Browse files
committed
Refactor uncoupling tests with lens
1 parent 8d20488 commit 1668527

File tree

3 files changed

+95
-87
lines changed

3 files changed

+95
-87
lines changed

lsp-dart-code-lens.el

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -131,24 +131,20 @@ If TEST-FILE? debug tests otherwise debug application."
131131
test-file?)
132132
"\n"))))
133133

134-
(lsp-defun lsp-dart-code-lens--build-test-overlay (buffer names kind test-range (range &as &Range :start
135-
(&Position :character beg-position)))
134+
(lsp-defun lsp-dart-code-lens--build-test-overlay (test)
136135
"Build an overlay in BUFFER for a test NAMES of KIND.
137136
TEST-RANGE is the test method range.
138137
RANGE is the overlay range to build."
139-
(-let* (((beg . end) (lsp--range-to-region range))
138+
(-let* (((beg . end) (plist-get test :element-range))
139+
(beg-position (-> (lsp--region-to-range beg end)
140+
(plist-get :start)
141+
(plist-get :character)))
140142
(beg-line (progn (goto-char beg)
141143
(line-beginning-position)))
142144
(spaces (make-string beg-position ?\s))
143-
(overlay (make-overlay beg-line end buffer))
144-
(test (make-lsp-dart-test-len :file-name (buffer-file-name buffer)
145-
:names names
146-
:position beg
147-
:kind kind))
145+
(overlay (make-overlay beg-line end (current-buffer)))
148146
(separator (propertize " " 'font-lock-face 'lsp-dart-code-lens-separator)))
149147
(overlay-put overlay 'lsp-dart-test-code-lens t)
150-
(overlay-put overlay 'lsp-dart-test test)
151-
(overlay-put overlay 'lsp-dart-code-lens-overlay-test-range (lsp--range-to-region test-range))
152148
(overlay-put overlay 'before-string
153149
(concat spaces
154150
(lsp-dart-code-lens--build-action "Run"
@@ -164,21 +160,6 @@ RANGE is the overlay range to build."
164160
test)
165161
"\n"))))
166162

167-
(defun lsp-dart-code-lens--add-test (buffer items &optional names)
168-
"Add test code lens to BUFFER for ITEMS.
169-
NAMES arg is optional and are the group of tests representing a test name."
170-
(seq-doseq (item items)
171-
(-let* (((&Outline :children :code-range test-range :element
172-
(&Element :kind :name :range)) item)
173-
(test-kind? (lsp-dart-test--test-kind-p kind))
174-
(concatened-names (if test-kind?
175-
(append names (list name))
176-
names)))
177-
(when test-kind?
178-
(lsp-dart-code-lens--build-test-overlay buffer (append names (list name)) kind test-range range))
179-
(unless (seq-empty-p children)
180-
(lsp-dart-code-lens--add-test buffer children concatened-names)))))
181-
182163
(lsp-defun lsp-dart--main-code-lens-check ((&OutlineNotification :uri :outline (&Outline :children)))
183164
"Check URI and outline for main method adding lens to it."
184165
(-let* ((buffer (find-buffer-visiting (lsp--uri-to-path uri)))
@@ -190,14 +171,12 @@ NAMES arg is optional and are the group of tests representing a test name."
190171
(save-excursion
191172
(lsp-dart-code-lens--build-main-overlay buffer main-outline)))))))
192173

193-
(lsp-defun lsp-dart--test-code-lens-check ((&OutlineNotification :uri :outline (&Outline :children)))
194-
"Check URI and outline for test adding lens to it."
195-
(when (lsp-dart-test-file-p uri)
196-
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
197-
(with-current-buffer buffer
198-
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
199-
(save-excursion
200-
(lsp-dart-code-lens--add-test buffer children))))))
174+
(lsp-defun lsp-dart--set-test-lens (tests)
175+
"Add lens to the given TESTS."
176+
(save-excursion
177+
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
178+
(seq-doseq (test tests)
179+
(lsp-dart-code-lens--build-test-overlay test))))
201180

202181

203182
;; Public
@@ -225,7 +204,7 @@ NAMES arg is optional and are the group of tests representing a test name."
225204
(lsp-dart-define-key "t L" #'lsp-dart-debug-last-test)
226205
(lsp-dart-define-key "t a" #'lsp-dart-run-all-tests)
227206
(lsp-dart-define-key "t v" #'lsp-dart-visit-last-test)
228-
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--test-code-lens-check nil t))
207+
(add-hook 'lsp-dart-tests-added-hook #'lsp-dart--set-test-lens nil t))
229208
(t
230209
(lsp-dart-define-key "t t" 'ignore)
231210
(lsp-dart-define-key "t T" 'ignore)
@@ -235,7 +214,7 @@ NAMES arg is optional and are the group of tests representing a test name."
235214
(lsp-dart-define-key "t a" 'ignore)
236215
(lsp-dart-define-key "t v" 'ignore)
237216
(remove-overlays (point-min) (point-max) 'lsp-dart-test-code-lens t)
238-
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart--test-code-lens-check t))))
217+
(remove-hook 'lsp-dart-tests-added-hook #'lsp-dart--set-test-lens t))))
239218

240219
(provide 'lsp-dart-code-lens)
241220
;;; lsp-dart-code-lens.el ends here

lsp-dart-test-support.el

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,13 @@
3535

3636
(defconst lsp-dart-test--process-buffer-name "*LSP Dart - tests process*")
3737

38-
(defvar lsp-dart-test--tests nil)
38+
(defvar-local lsp-dart-test--tests nil)
39+
(defvar lsp-dart-test--running-tests nil)
3940

40-
(cl-defstruct lsp-dart-test
41+
(cl-defstruct lsp-dart-running-test
4142
(id nil)
4243
(name nil)
43-
(start-time nil)
44-
(group-ids nil))
45-
46-
(cl-defstruct lsp-dart-test-len
47-
(file-name nil)
48-
(names nil)
49-
(position nil)
50-
(kind nil))
44+
(start-time nil))
5145

5246
(defun lsp-dart-test--test-kind-p (kind)
5347
"Return non-nil if KIND is a test type."
@@ -100,13 +94,13 @@ IGNORE-CASE is a optional arg to ignore the case sensitive on regex search."
10094
"Return non-nil if some test is already running."
10195
(comint-check-proc lsp-dart-test--process-buffer-name))
10296

103-
(defun lsp-dart-test--set-test (id test)
97+
(defun lsp-dart-test--set-running-test (id test)
10498
"Add TEST with key ID."
105-
(setf (alist-get id lsp-dart-test--tests) test))
99+
(setf (alist-get id lsp-dart-test--running-tests) test))
106100

107-
(defun lsp-dart-test--get-test (id)
101+
(defun lsp-dart-test--get-running-test (id)
108102
"Return the test from ID if exists."
109-
(alist-get id lsp-dart-test--tests))
103+
(alist-get id lsp-dart-test--running-tests))
110104

111105
(cl-defgeneric lsp-dart-test--handle-notification (type notification)
112106
"Extension point for handling custom events.
@@ -119,16 +113,15 @@ NOTIFICATION is the event notification.")
119113

120114
(cl-defmethod lsp-dart-test--handle-notification ((_event (eql start)) notification)
121115
"Handle start NOTIFICATION."
122-
(setq lsp-dart-test--tests nil)
116+
(setq lsp-dart-test--running-tests nil)
123117
(run-hook-with-args 'lsp-dart-test-all-start-notification-hook notification))
124118

125119
(cl-defmethod lsp-dart-test--handle-notification ((_event (eql testStart)) notification)
126120
"Handle testStart NOTIFICATION."
127-
(-let (((&TestStartNotification :time :test (&Test :id :group-i-ds :name?)) notification))
128-
(lsp-dart-test--set-test id (make-lsp-dart-test :id id
121+
(-let (((&TestStartNotification :time :test (&Test :id :name?)) notification))
122+
(lsp-dart-test--set-running-test id (make-lsp-dart-running-test :id id
129123
:name name?
130-
:start-time time
131-
:group-ids group-i-ds))
124+
:start-time time))
132125
(run-hook-with-args 'lsp-dart-test-start-notification-hook notification)))
133126

134127
(cl-defmethod lsp-dart-test--handle-notification ((_event (eql allSuites)) _notification)
@@ -145,11 +138,11 @@ NOTIFICATION is the event notification.")
145138
(cl-defmethod lsp-dart-test--handle-notification ((_event (eql testDone)) notification)
146139
"Handle test done NOTIFICATION."
147140
(-let (((&TestDoneNotification :test-id) notification))
148-
(when-let (test (lsp-dart-test--get-test test-id))
141+
(when-let (test (lsp-dart-test--get-running-test test-id))
149142
(run-hook-with-args 'lsp-dart-test-done-notification-hook
150143
notification
151-
(lsp-dart-test-name test)
152-
(lsp-dart-test-start-time test)))))
144+
(lsp-dart-running-test-name test)
145+
(lsp-dart-running-test-start-time test)))))
153146

154147
(cl-defmethod lsp-dart-test--handle-notification ((_event (eql done)) notification)
155148
"Handle done NOTIFICATION."
@@ -197,9 +190,9 @@ If TEST is nil, it will run all tests from project.
197190
If TEST is non nil, it will check if contains any test specific name
198191
to run otherwise run all tests from file-name in TEST."
199192
(if test
200-
(let* ((names (lsp-dart-test-len-names test))
201-
(kind (lsp-dart-test-len-kind test))
202-
(test-file (file-relative-name (lsp-dart-test-len-file-name test)
193+
(let* ((names (plist-get test :names))
194+
(kind (plist-get test :kind))
195+
(test-file (file-relative-name (plist-get test :file-name)
203196
(lsp-dart-get-project-root)))
204197
(test-name (lsp-dart-test--build-test-name names))
205198
(group-kind? (string= kind "UNIT_TEST_GROUP"))
@@ -219,9 +212,9 @@ to run otherwise run all tests from file-name in TEST."
219212

220213
(defun lsp-dart-test--debug (test)
221214
"Debug Dart/Flutter TEST."
222-
(let* ((file-name (lsp-dart-test-len-file-name test))
223-
(names (lsp-dart-test-len-names test))
224-
(kind (lsp-dart-test-len-kind test))
215+
(let* ((file-name (plist-get test :file-name))
216+
(names (plist-get test :names))
217+
(kind (plist-get test :kind))
225218
(test-name (lsp-dart-test--build-test-name names))
226219
(group-kind? (string= kind "UNIT_TEST_GROUP"))
227220
(regex (concat "^"
@@ -233,17 +226,16 @@ to run otherwise run all tests from file-name in TEST."
233226
(lsp-dart-dap-debug-flutter-test file-name test-arg)
234227
(lsp-dart-dap-debug-dart-test file-name test-arg))))
235228

236-
(defun lsp-dart-test--overlay-at-point ()
237-
"Return test overlay at point.
238-
Return the overlay which has the smallest range of all test overlays in
239-
the current buffer."
240-
(-some--> (overlays-in (point-min) (point-max))
241-
(--filter (when (overlay-get it 'lsp-dart-test-code-lens)
242-
(-let* (((beg . end) (overlay-get it 'lsp-dart-code-lens-overlay-test-range)))
243-
(and (>= (point) beg)
244-
(<= (point) end)))) it)
245-
(--min-by (-let* (((beg1 . end1) (overlay-get it 'lsp-dart-code-lens-overlay-test-range))
246-
((beg2 . end2) (overlay-get other 'lsp-dart-code-lens-overlay-test-range)))
229+
(defun lsp-dart-test--test-at-point ()
230+
"Return the test at point.
231+
Return the test which has the smallest range of all tests ranges in the
232+
current buffer."
233+
(-some--> lsp-dart-test--tests
234+
(--filter (-let* (((beg . end) (plist-get it :code-range)))
235+
(and (>= (point) beg)
236+
(<= (point) end))) it)
237+
(--min-by (-let* (((beg1 . end1) (plist-get it :code-range))
238+
((beg2 . end2) (plist-get other :code-range)))
247239
(and (< beg1 beg2)
248240
(> end1 end2))) it)))
249241

@@ -253,6 +245,35 @@ the current buffer."
253245
(lsp-dart-test--handle-notification (intern (lsp-get notification :type)) notification))
254246
(lsp-dart-test--raw->response raw-response)))
255247

248+
(defun lsp-dart-test--add-test (items &optional names)
249+
"Add to test listfor ITEMS.
250+
NAMES arg is optional and are the group of tests representing a test name."
251+
(seq-doseq (item items)
252+
(-let* (((&Outline :children :code-range test-range :element
253+
(&Element :kind :name :range)) item)
254+
(test-kind? (lsp-dart-test--test-kind-p kind))
255+
(concatened-names (if test-kind?
256+
(append names (list name))
257+
names))
258+
(new-test (list :file-name (file-truename (buffer-file-name))
259+
:names (append names (list name))
260+
:kind kind
261+
:code-range (lsp--range-to-region test-range)
262+
:element-range (lsp--range-to-region range))))
263+
(when test-kind?
264+
(add-to-list 'lsp-dart-test--tests new-test))
265+
(unless (seq-empty-p children)
266+
(lsp-dart-test--add-test children concatened-names)))))
267+
268+
(lsp-defun lsp-dart-test--check-tests ((&OutlineNotification :uri :outline (&Outline :children)))
269+
"Check URI and outline for test adding them."
270+
(when (lsp-dart-test-file-p uri)
271+
(when-let (buffer (find-buffer-visiting (lsp--uri-to-path uri)))
272+
(with-current-buffer buffer
273+
(setq lsp-dart-test--tests nil)
274+
(lsp-dart-test--add-test children)
275+
(run-hook-with-args 'lsp-dart-tests-added-hook lsp-dart-test--tests)))))
276+
256277

257278
;;; Public
258279

@@ -265,20 +286,18 @@ the current buffer."
265286

266287
;;;###autoload
267288
(defun lsp-dart-run-test-at-point ()
268-
"Run test at point.
269-
Search for the last test overlay."
289+
"Run test at point."
270290
(interactive)
271-
(if-let (overlay (lsp-dart-test--overlay-at-point))
272-
(lsp-dart-test--run (overlay-get overlay 'lsp-dart-test))
291+
(if-let (test (lsp-dart-test--test-at-point))
292+
(lsp-dart-test--run test)
273293
(lsp-dart-log "No test found at point.")))
274294

275295
;;;###autoload
276296
(defun lsp-dart-debug-test-at-point ()
277-
"Debug test at point.
278-
Search for the last test overlay."
297+
"Debug test at point."
279298
(interactive)
280-
(if-let (overlay (lsp-dart-test--overlay-at-point))
281-
(lsp-dart-test--debug (overlay-get overlay 'lsp-dart-test))
299+
(if-let (test (lsp-dart-test--test-at-point))
300+
(lsp-dart-test--debug test)
282301
(lsp-dart-log "No test found at point.")))
283302

284303
;;;###autoload
@@ -289,7 +308,7 @@ Search for the last test overlay."
289308
(lsp-dart-test--run (->> (current-buffer)
290309
buffer-name
291310
file-truename
292-
(make-lsp-dart-test-len :file-name)))
311+
(list :file-name)))
293312
(lsp-dart-log "Current buffer is not a Dart/Flutter test file.")))
294313

295314
;;;###autoload
@@ -303,17 +322,17 @@ Search for the last test overlay."
303322
"Visit the last ran test going to test definition."
304323
(interactive)
305324
(-if-let* ((test (lsp-workspace-get-metadata "last-ran-test"))
306-
(file-name (lsp-dart-test-len-file-name test))
325+
(file-name (plist-get test :file-name))
307326
(buffer (or (get-file-buffer file-name)
308327
(find-file file-name)))
309-
(position (lsp-dart-test-len-position test)))
328+
(beg-position (car (plist-get test :element-range))))
310329
(if-let ((window (get-buffer-window buffer 'visible)))
311330
(progn
312331
(select-window window)
313-
(goto-char position))
332+
(goto-char beg-position))
314333
(with-current-buffer buffer
315334
(switch-to-buffer buffer nil t)
316-
(goto-char position)))
335+
(goto-char beg-position)))
317336
(lsp-dart-log "No last test found.")))
318337

319338
;;;###autoload
@@ -343,6 +362,15 @@ Search for the last test overlay."
343362
(setenv "PATH" (concat (lsp-dart-pub-command) ":" (getenv "PATH"))))
344363
(setq-local comint-output-filter-functions #'lsp-dart-test--handle-process-response))
345364

365+
(define-minor-mode lsp-dart-test-mode
366+
"Mode for saving tests info for runs."
367+
nil nil nil
368+
(cond
369+
(lsp-dart-test-mode
370+
(add-hook 'lsp-dart-outline-arrived-hook #'lsp-dart-test--check-tests nil t))
371+
(t
372+
(remove-hook 'lsp-dart-outline-arrived-hook #'lsp-dart-test--check-tests t))))
373+
346374

347375
(provide 'lsp-dart-test-support)
348376
;;; lsp-dart-test-support.el ends here

lsp-dart.el

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ PARAMS is the data sent from server."
131131
(when lsp-dart-closing-labels (lsp-dart-closing-labels-mode 1))
132132
(when lsp-dart-outline (lsp-dart-outline-mode 1))
133133
(when lsp-dart-flutter-outline (lsp-dart-flutter-outline-mode 1))
134+
(when (lsp-dart-test-file-p (buffer-file-name)) (lsp-dart-test-mode 1))
134135
(when lsp-dart-main-code-lens (lsp-dart-main-code-lens-mode 1))
135136
(when lsp-dart-test-code-lens (lsp-dart-test-code-lens-mode 1)))
136137

0 commit comments

Comments
 (0)