Skip to content

Commit

Permalink
Handle cases where ; inherits: is not on the first line
Browse files Browse the repository at this point in the history
In some cases, the inherits line does not show up as the first line.
This improves on our previous method of just assuming the first line
to be the inherits line.
  • Loading branch information
meain committed Nov 15, 2023
1 parent 8180681 commit 9a9edd4
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions evil-textobj-tree-sitter-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ 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'.
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.
Expand All @@ -178,18 +194,15 @@ https://github.com/nvim-treesitter/nvim-treesitter/pull/564"
(progn
(insert-file-contents filename)
(goto-char (point-min))
(let* ((first-line (thing-at-point 'line t))
(first-line-matches (save-match-data (when (string-match "^; *inherits *:? *\\([a-z_,()]+\\) *$"
first-line)
(match-string 1 first-line)))))
(if first-line-matches
(let ((inherits-line (evil-textobj-tree-sitter--get-inherits-line language)))
(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)))
(split-string first-line-matches ","))
(split-string inherits-line ","))
"\n"))))
(buffer-string))))))

Expand Down

0 comments on commit 9a9edd4

Please sign in to comment.