emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Tree-sitter integration on feature/tree-sitter


From: Yuan Fu
Subject: Re: Tree-sitter integration on feature/tree-sitter
Date: Fri, 13 May 2022 17:03:17 -0700


> On May 9, 2022, at 2:33 PM, Theodor Thornhill <theo@thornhill.no> wrote:
> 
>> 
>> +(defun js-treesit-move-to-node (fn)
>> + (when-let ((found-node (treesit-parent-until
>> + (treesit-node-at (point) (point) 'javascript)
>> + (lambda (parent)
>> + (let ((parent-type (treesit-node-type parent)))
>> + (or (equal "function_declaration" parent-type)
>> + ;;; More declarations here
>> + ))))))
>> + (goto-char (funcall fn found-node))))
>> +
>> +(defun js-treesit-beginning-of-defun (&optional arg)
>> + (js-treesit-move-to-node #'treesit-node-start))
>> +
>> +(defun js-treesit-end-of-defun (&optional arg)
>> + (js-treesit-move-to-node #'treesit-node-end))
>> 
>> Maybe I could extract this into treesit.el, so major modes can specify
>> simply the node name for a function definition and get function
>> traversal for free.
>> 
> 
> Yeah! My plan was to use `M-a` and `M-e` for siblings and `C-M-a` and
> `C-M-e` for beginning/end-of-defun. Some way of supplying "defun-nodes"
> and make treesit handle the rest would be awesome.

Now there is treesit-beginning/end-of-defun. You just need to set 
treesit-defun-query and everything else come for free. I needed to invent some 
heavy machinery for that, resulting in some new handy functions:

- treesit-traverse-depth-first
- treesit-traverse-breadth-first
- treesit-traverse-forward-depth-first (maybe this should be named simply 
treesit-traverse-forward?)

- treesit-search-forward
- treesit-search-beginning
- treesit-search-end

They are untested & undocumented (in manual), so please play with them and 
report problems :-)

(BREAKING) I also changed the semantic of treesit-node-at, the old semantic 
sometimes returns unexpected result and it is best to change it to something 
more intuitive. The old semantic can still be found in treesit-node-on.

Yuan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]