diff --git a/emacs/.emacs.d/config/init-run-command.el b/emacs/.emacs.d/config/init-run-command.el index 620e726..bd1f1e6 100644 --- a/emacs/.emacs.d/config/init-run-command.el +++ b/emacs/.emacs.d/config/init-run-command.el @@ -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))))) diff --git a/emacs/.emacs.d/config/init-tree-sitter.el b/emacs/.emacs.d/config/init-tree-sitter.el new file mode 100644 index 0000000..2f9a49f --- /dev/null +++ b/emacs/.emacs.d/config/init-tree-sitter.el @@ -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) diff --git a/emacs/.emacs.d/init.el b/emacs/.emacs.d/init.el index 87ec429..bd8ef86 100644 --- a/emacs/.emacs.d/init.el +++ b/emacs/.emacs.d/init.el @@ -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)