diff --git a/emacs/.emacs.d/config/init-run-command.el b/emacs/.emacs.d/config/init-run-command.el index bd1f1e6..bf40cbe 100644 --- a/emacs/.emacs.d/config/init-run-command.el +++ b/emacs/.emacs.d/config/init-run-command.el @@ -261,13 +261,16 @@ (list :command-name "test this class" :command-line (format "mvn test -DfailIfNoTests=false -Dtest=%s" test-class) :working-dir local-pom-dir)) - ;; TODO make this only detect test methods instead of all methods (when-let* ((test-class (tree-sitter-fully-qualified-class-name (point))) (method (tree-sitter-get-enclosing-function-name (point))) (test-method (and (buffer-file-name) (let ((case-fold-search nil)) (string-match-p ".*\\(Test\\|IT\\).*\\.java$" (buffer-file-name))) + (member + "Test" + (tree-sitter-get-enclosing-annotations + (point))) (format "%s#%s" test-class method)))) (list :command-name "test this method" :command-line (format "mvn test -DfailIfNoTests=false -Dtest=%s" test-method) diff --git a/emacs/.emacs.d/config/init-tree-sitter.el b/emacs/.emacs.d/config/init-tree-sitter.el index 2f9a49f..4bf5e04 100644 --- a/emacs/.emacs.d/config/init-tree-sitter.el +++ b/emacs/.emacs.d/config/init-tree-sitter.el @@ -4,6 +4,12 @@ (use-package tree-sitter :hook (after-init . global-tree-sitter-mode) :config + (defun tree-sitter-first-matching-child (node pred) + "Returns the first child of `node' for which `pred' returns true." + (cl-loop for i from 0 below (tsc-count-children node) + for child = (tsc-get-nth-child node i) + if (funcall pred child) return child)) + (defun tree-sitter-get-package-name () "Gets the identifier name of the current buffer's package (for languages that have them)." ;; TODO is there a better way to do this? @@ -31,7 +37,19 @@ "Gets the fully-qualified class name enclosing `pos', if it exists." (when-let* ((package (tree-sitter-get-package-name)) (class (tree-sitter-get-enclosing-class-name pos))) - (format "%s.%s" package class)))) + (format "%s.%s" package class))) + + (defun tree-sitter-get-enclosing-annotations (pos) + "Returns a list of annotation name strings for the current method or class." + (when-let* ((node (or (tree-sitter-node-at-pos 'method_declaration pos) + (tree-sitter-node-at-pos 'class_declaration pos))) + (mods (tree-sitter-first-matching-child + node + (lambda (n) (eq (tsc-node-type n) 'modifiers))))) + (cl-loop for i from 0 below (tsc-count-children mods) + for child = (tsc-get-nth-child mods i) + if (not (null (tsc-get-child-by-field child :name))) + collect (tsc-node-text (tsc-get-child-by-field child :name)))))) (use-package tree-sitter-langs)