Skip to content

Commit

Permalink
avoid getting queries-dir under with-temp-buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
matth0204 committed Feb 11, 2024
1 parent 220ceae commit 4d779c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
52 changes: 25 additions & 27 deletions evil-textobj-tree-sitter-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -166,53 +166,51 @@ Currently treesit queries are different from queries for elisp-tree-sitter."
nodes)
(lambda (x y) (< (nth 1 x) (nth 1 y)))))

(defun evil-textobj-tree-sitter--get-inherits-line (language)
"Get the inherits line for `LANGUAGE'.
(defun evil-textobj-tree-sitter--get-inherits-line (filename)
"Get the inherits line from `FILENAME'.
It might not be on the fist line and so we cannot just get the first line."
(let ((filename (concat (funcall evil-textobj-tree-sitter--get-queries-dir-func)
language "/textobjects.scm")))
(with-temp-buffer
(if (file-exists-p filename)
(progn
(insert-file-contents filename)
(goto-char (point-min))
(search-forward "; inherits: " nil t)
(let ((line (thing-at-point 'line t)))
(if (string-match "^; inherits: \\([a-z_,()]+\\)$" line)
(match-string 1 line))))))))


(defun evil-textobj-tree-sitter--get-query (language top-level)
"Get tree sitter query for LANGUAGE.
TOP-LEVEL is used to mention if we should load optional inherits.
(with-temp-buffer
(if (file-exists-p filename)
(progn
(insert-file-contents filename)
(goto-char (point-min))
(search-forward "; inherits: " nil t)
(let ((line (thing-at-point 'line t)))
(if (string-match "^; inherits: \\([a-z_,()]+\\)$" line)
(match-string 1 line)))))))

(defun evil-textobj-tree-sitter--get-query-from-dir (language queries-dir top-level)
"Get tree sitter query for `LANGUAGE' from `QUERIES-DIR'.
`TOP-LEVEL' is used to mention if we should load optional inherits.
https://github.com/nvim-treesitter/nvim-treesitter/pull/564"
(let ((filename (concat (funcall evil-textobj-tree-sitter--get-queries-dir-func)
language "/textobjects.scm")))
(let ((filename (concat queries-dir language "/textobjects.scm")))
(with-temp-buffer
(if (file-exists-p filename)
(progn
(insert-file-contents filename)
(goto-char (point-min))
(let ((inherits-line (evil-textobj-tree-sitter--get-inherits-line language)))
(let ((inherits-line (evil-textobj-tree-sitter--get-inherits-line filename)))
(if inherits-line
(insert (string-join (mapcar (lambda (x)
(if (string-prefix-p "(" x)
(if top-level
(evil-textobj-tree-sitter--get-query (substring x 1 -1)
nil))
(evil-textobj-tree-sitter--get-query x nil)))
(evil-textobj-tree-sitter--get-query-from-dir (substring x 1 -1)
queries-dir nil))
(evil-textobj-tree-sitter--get-query-from-dir x queries-dir nil)))
(split-string inherits-line ","))
"\n"))))
(buffer-string))))))

(defun evil-textobj-tree-sitter--get-query (language)
"Get tree sitter query for `LANGUAGE'."
(evil-textobj-tree-sitter--get-query-from-dir language (funcall evil-textobj-tree-sitter--get-queries-dir-func) t))

(defun evil-textobj-tree-sitter--treesit-get-nodes (query)
"Get nodes for `QUERY' using builtin `treesit'."
(let* ((lang-name (alist-get major-mode evil-textobj-tree-sitter-major-mode-language-alist))
(user-query (alist-get major-mode query))
(f-query (if (eq user-query nil)
(evil-textobj-tree-sitter--get-query lang-name t)
(evil-textobj-tree-sitter--get-query lang-name)
user-query))
(root-node (treesit-buffer-root-node))
(captures (treesit-query-capture root-node f-query)))
Expand All @@ -227,7 +225,7 @@ https://github.com/nvim-treesitter/nvim-treesitter/pull/564"
(let* ((lang-name (alist-get major-mode evil-textobj-tree-sitter-major-mode-language-alist))
(user-query (alist-get major-mode query))
(f-query (if (eq user-query nil)
(evil-textobj-tree-sitter--get-query lang-name t)
(evil-textobj-tree-sitter--get-query lang-name)
user-query))
(root-node (tsc-root-node tree-sitter-tree))
(query (tsc-make-query tree-sitter-language f-query))
Expand Down
15 changes: 7 additions & 8 deletions evil-textobj-tree-sitter-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,31 @@ func main() {
(ert-deftest evil-textoj-tree-sitter-check-query-read-simple ()
"Simple query read check."
(let ((evil-textobj-tree-sitter--get-queries-dir-func (lambda () "fixtures/")))
(should (string-prefix-p ";; \"Classes\"" (evil-textobj-tree-sitter--get-query "zig" t)))))
(should (string-prefix-p ";; \"Classes\"" (evil-textobj-tree-sitter--get-query "zig")))))

(ert-deftest evil-textoj-tree-sitter-check-query-read-nocomment ()
"Check a query file with no comment."
(let ((evil-textobj-tree-sitter--get-queries-dir-func (lambda () "fixtures/")))
(should (string-prefix-p "(function_definition" (evil-textobj-tree-sitter--get-query "bash" t)))))
(should (string-prefix-p "(function_definition" (evil-textobj-tree-sitter--get-query "bash")))))

(ert-deftest evil-textoj-tree-sitter-check-query-read-nested ()
"Check a query with nested files to be loaded."
(let ((evil-textobj-tree-sitter--get-queries-dir-func (lambda () "fixtures/")))
(should (string-prefix-p "; inherits: (jsx)" (evil-textobj-tree-sitter--get-query "typescript" t)))))
(should (string-prefix-p "; inherits: (jsx)" (evil-textobj-tree-sitter--get-query "typescript")))))

(ert-deftest evil-textoj-tree-sitter-check-query-read-nested-nofile ()
"Check a file pointing to a non existent file."
(let ((evil-textobj-tree-sitter--get-queries-dir-func (lambda () "fixtures/")))
(should (string-prefix-p "; inherits: (jsx)" (evil-textobj-tree-sitter--get-query "javascript" t)))))
(should (string-prefix-p "; inherits: (jsx)" (evil-textobj-tree-sitter--get-query "javascript")))))

(ert-deftest evil-textoj-tree-sitter-check-query-read-nested-multi ()
"Check a query with multiple nesting items."
(let ((evil-textobj-tree-sitter--get-queries-dir-func (lambda () "fixtures/")))
(should (string-prefix-p "; inherits: (javascript)" (evil-textobj-tree-sitter--get-query "tsx" t)))))
(should (string-prefix-p "; inherits: (javascript)" (evil-textobj-tree-sitter--get-query "tsx")))))

(ert-deftest evil-textoj-tree-sitter-check-query-read-non-top-level ()
"Check a non top level direct query."
(let ((evil-textobj-tree-sitter--get-queries-dir-func (lambda () "fixtures/")))
(should (string-prefix-p "; inherits: (javascript)" (evil-textobj-tree-sitter--get-query "typescript" nil)))))
(should (string-prefix-p "; inherits: (javascript)" (evil-textobj-tree-sitter--get-query-from-dir "typescript" "fixtures/" nil))))

(defun evil-textobj-tree-sitter--goto-test (mode treesit start textobj pos prev end content)
"Check for location of goto actions.
Expand Down Expand Up @@ -252,4 +251,4 @@ func main() {
(evil-textobj-tree-sitter--thing-at-point-test 'c-mode nil 31 'function 'c-unicode selection (cons 11 45))
(evil-textobj-tree-sitter--thing-at-point-test 'c-ts-mode t 31 'function 'c-unicode selection (cons 11 45))))

;;; evil-textobj-tree-sitter-test.el ends here
;;; evil-textobj-tree-sitter-test.el ends here

0 comments on commit 4d779c8

Please sign in to comment.