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.
197190If TEST is non nil, it will check if contains any test specific name
198191to 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
0 commit comments