Use tree-sitter to detect java test classes/methods

This commit is contained in:
Jeremy Dormitzer 2023-04-20 12:35:49 -04:00
parent c5bb3799a0
commit d18a5befcb
3 changed files with 49 additions and 8 deletions

View File

@ -256,17 +256,19 @@
(let ((case-fold-search nil))
(string-match-p ".*\\(Test\\|IT\\).*\\.java$"
(buffer-file-name)))
(fboundp 'dap-java-test-class)
(dap-java-test-class))))
(tree-sitter-fully-qualified-class-name
(point)))))
(list :command-name "test this class"
:command-line (format "mvn test -DfailIfNoTests=false -Dtest=%s" test-class)
:working-dir local-pom-dir))
(when-let ((test-method (and (buffer-file-name)
(let ((case-fold-search nil))
(string-match-p ".*\\(Test\\|IT\\).*\\.java$"
(buffer-file-name)))
(fboundp 'dap-java-test-method-at-point)
(dap-java-test-method-at-point t))))
;; 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)))
(format "%s#%s" test-class method))))
(list :command-name "test this method"
:command-line (format "mvn test -DfailIfNoTests=false -Dtest=%s" test-method)
:working-dir local-pom-dir)))))

View File

@ -0,0 +1,38 @@
;; -*- lexical-binding: t; -*-
;; Tree sitter is a fast parser that supports a number of programming languages
(use-package tree-sitter
:hook (after-init . global-tree-sitter-mode)
:config
(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?
(condition-case nil
(let* ((root (tsc-root-node tree-sitter-tree))
(child (tsc-get-nth-named-child root 0)))
(when (eq (tsc-node-type child) 'package_declaration)
(tsc-node-text (tsc-get-nth-child child 1))))
(error nil)))
(defun tree-sitter-get-enclosing-function-name (pos)
"Gets the identifier name of the method or function enclosing the position `pos', if it exists."
(condition-case nil
(let ((func (tree-sitter-node-at-pos 'method_declaration pos)))
(tsc-node-text (tsc-get-child-by-field func :name)))
(error nil)))
(defun tree-sitter-get-enclosing-class-name (pos)
"Gets the identifier name of the class enclosing the position `pos', if it exists."
(condition-case nil
(let ((class (tree-sitter-node-at-pos 'class_declaration pos)))
(tsc-node-text (tsc-get-child-by-field class :name)))
(error nil)))
(defun tree-sitter-fully-qualified-class-name (pos)
"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))))
(use-package tree-sitter-langs)
(provide 'init-tree-sitter)

View File

@ -87,6 +87,7 @@
(require 'init-vterm)
(require 'init-eshell)
(require 'init-help)
(require 'init-tree-sitter)
(require 'init-python)
(require 'init-clojure)
(require 'init-lisp)