emacs-devel
[Top][All Lists]
Advanced

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

Re: hideshow support for treesitter


From: Yuan Fu
Subject: Re: hideshow support for treesitter
Date: Sat, 7 Sep 2024 20:41:19 -0700

> 
> On Fri, 6 Sept 2024 at 03:04, Juri Linkov <juri@linkov.net> wrote:
> >>>>> Are there any plans to add treesitter support for hideshow, it seems 
> >>>>> easy
> >>>>> enough to do and also very helpful, especially for langs that don't
> >>>>> excessively use parens, such as python
> >>>>
> >>>> Indeed, it's easy to do this.  You just need to override
> >>>> hs-find-block-beginning-func, hs-find-next-block-func, etc.
> >>>> with treesit functions like 'treesit-outline-search'.
> >>>
> >>> Ah I see, then I think treesit.el should contain code for this, because
> >>> this seems extremely useful task that would not make sense if each tree
> >>> sitter based major mode was to implement this separately
> >>
> >> Or hideshow code for treesit could be in hideshow.el as well.
> >> The reason why 'treesit-outline-search' was added to treesit.el
> >> was because treesit.el already contained imenu support.  But
> >> imenu support could be in imenu.el as well.
> >
> > Is there a preferred way to handle this treesit wise. I know some packages
> > (eg imenu) require major modes to define their support for it, but others
> > like ispell do it themselves. I personally thing the latter would be better
> > in this case.
> 
> I looked at hideshow.el and found such a list:
> 
> (defvar hs-special-modes-alist
>   (mapcar #'purecopy
>   '((c-mode "{" "}" "/[*/]" nil nil)
>     (c-ts-mode "{" "}" "/[*/]" nil nil)
>     (c++-mode "{" "}" "/[*/]" nil nil)
>     (c++-ts-mode "{" "}" "/[*/]" nil nil)
>     (bibtex-mode ("@\\S(*\\(\\s(\\)" 1))
>     (java-mode "{" "}" "/[*/]" nil nil)
>     (java-ts-mode "{" "}" "/[*/]" nil nil)
>     (js-mode "{" "}" "/[*/]" nil)
>     (js-ts-mode "{" "}" "/[*/]" nil)
>     (lua-ts-mode "{\\|\\[\\[" "}\\|\\]\\]" "--" nil)
>     (mhtml-mode "{\\|<[^/>]*?" "}\\|</[^/>]*[^/]>" "<!--" mhtml-forward nil)
> 
> But this is useless for tree-sitter.
> 
> Line-oriented treesit-outline-search relied on treesit-simple-imenu-settings
> that defines header lines for imenu.  And every ts-mode sets own value of
> treesit-simple-imenu-settings.  But this can't be used for hideshow.
> 
> So whoever will implement hideshow support for treesitter
> will need to design a setting like buffer-local
> treesit-simple-imenu-settings for ts-modes.

> On Sep 5, 2024, at 6:18 PM, Pranshu Sharma <pranshusharma198@gmail.com> wrote:
> 
> I am working to try get the hide show working for tree sitter, however one 
> thing that is missing in treesit is sexp navigation functions, hideshow 
> requires sexp function.

Master branch should have them, you can check out treesit-forward-sexp. Major 
modes need to define sexp in treesit-thing-settings for it to work though. 
Also, hideshow can just use forward-sexp, it’ll use treesit-forward-sexp behind 
the scenes.

With that said, last time I tried to make hideshow work with tree-sitter, I 
found that hideshow is not generic enough, it insists on its way of using 
regexp and search back and forth for block beginning and ends. If you want to 
make it work for tree-sitter, you might need to add another layer of 
abstraction and make hideshow more generic on how it finds the region to fold.

Also just a note, for tree-sitter integration, I always try to make tree-sitter 
work for another package/library/feature, rather than  the other way around. So 
if possible, hideshow.el should not require treesit or even be aware of it, 
treesit should set the necessary variables to work with hideshow.

Yuan


reply via email to

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