2424
2525; ;; Code:
2626
27+ (require 'cl-lib )
2728(require 'lsp-mode )
2829
2930(require 'lsp-dart-project )
3334
3435; ;; Internal
3536
37+ (cl-defstruct lsp-dart-test
38+ (file-name nil )
39+ (names nil )
40+ (position nil )
41+ (kind nil ))
42+
3643(defun lsp-dart-test-support--test-kind-p (kind )
3744 " Return non-nil if KIND is a test type."
3845 (or (string= kind " UNIT_TEST_TEST" )
@@ -92,20 +99,25 @@ otherwise the dart command."
9299 escaped-str nil t )))
93100 escaped-str))
94101
95- (defun lsp-dart-test-support-run (buffer &optional names kind )
102+ (defun lsp-dart-test-support-run (test )
96103 " Run Dart/Flutter test command in a compilation buffer for BUFFER file.
97- If NAMES is non nil, it will run only for KIND the test joining the name
98- from NAMES."
104+ If TEST is non nil, it will run only this test."
99105 (interactive )
100106 (lsp-dart-test-support--from-project-root
101- (let* ((test-file (file-relative-name (buffer-file-name buffer)
107+ (let* ((file-name (lsp-dart-test-file-name test))
108+ (buffer (get-file-buffer file-name))
109+ (names (lsp-dart-test-names test))
110+ (kind (lsp-dart-test-kind test))
111+ (test-file (file-relative-name file-name
102112 (lsp-dart-project-get-root)))
103113 (test-name (lsp-dart-test-support--build-test-name names))
104114 (group-kind? (string= kind " UNIT_TEST_GROUP" ))
105115 (test-arg (when test-name
106116 (concat " --name '^"
107117 (lsp-dart-test-support--escape-test-name test-name)
108118 (if group-kind? " '" " $'" )))))
119+ (when names
120+ (lsp-workspace-set-metadata " last-ran-test" test))
109121 (compilation-start (format " %s test %s %s "
110122 (lsp-dart-test-support--build-command buffer)
111123 (or test-arg " " )
@@ -122,10 +134,13 @@ TEST-RANGE is the test method range."
122134 (beg-line (progn (goto-char beg)
123135 (line-beginning-position )))
124136 (spaces (make-string beg-position ?\s ))
125- (overlay (make-overlay beg-line end buffer)))
137+ (overlay (make-overlay beg-line end buffer))
138+ (test (make-lsp-dart-test :file-name (buffer-file-name buffer)
139+ :names names
140+ :position beg
141+ :kind kind)))
126142 (overlay-put overlay 'lsp-dart-test-code-lens t )
127- (overlay-put overlay 'lsp-dart-test-names names)
128- (overlay-put overlay 'lsp-dart-test-kind kind)
143+ (overlay-put overlay 'lsp-dart-test test)
129144 (overlay-put overlay 'lsp-dart-test-overlay-test-range (lsp--range-to-region test-range))
130145 (overlay-put overlay 'before-string
131146 (concat spaces
@@ -135,7 +150,7 @@ TEST-RANGE is the test method range."
135150 'local-map (-doto (make-sparse-keymap )
136151 (define-key [mouse-1] (lambda ()
137152 (interactive )
138- (lsp-dart-test-support-run buffer names kind ))))
153+ (lsp-dart-test-support-run test ))))
139154 'font-lock-face 'lsp-lens-face )))))
140155
141156(defun lsp-dart-test-support--add-code-lens (buffer items &optional names )
@@ -168,6 +183,28 @@ PARAMS is the notification data from outline."
168183 " Return non-nil if FILE-NAME is a dart test files."
169184 (string-match " _test.dart" file-name))
170185
186+ (defun lsp-dart-test-support-run-last-test ()
187+ " Visit the last ran test going to the test definition."
188+ (if-let ((test (lsp-workspace-get-metadata " last-ran-test" )))
189+ (lsp-dart-test-support-run test)
190+ (lsp-dart-project-log " No last test found." )))
191+
192+ (defun lsp-dart-test-support-visit-last-test ()
193+ " Visit the last ran test going to the test definition."
194+ (-if-let* ((test (lsp-workspace-get-metadata " last-ran-test" ))
195+ (file-name (lsp-dart-test-file-name test))
196+ (buffer (or (get-file-buffer file-name)
197+ (find-file file-name)))
198+ (position (lsp-dart-test-position test)))
199+ (if-let ((window (get-buffer-window buffer 'visible )))
200+ (progn
201+ (select-window window)
202+ (goto-char position))
203+ (with-current-buffer buffer
204+ (switch-to-buffer buffer nil t )
205+ (goto-char position)))
206+ (lsp-dart-project-log " No last test found." )))
207+
171208
172209(provide 'lsp-dart-test-support )
173210; ;; lsp-dart-test-support.el ends here
0 commit comments