renzmann / treesit-auto

Automatic installation, usage, and fallback for tree-sitter major modes in Emacs 29
GNU General Public License v3.0
369 stars 28 forks source link

Automatically use bash-ts-mode? #21

Closed noctuid closed 1 year ago

noctuid commented 1 year ago

Unfortunately, this would have to be handled differently in some way (e.g. checking sh-shell) since there is no bash-mode.

renzmann commented 1 year ago

Did you already try this recommendation in the README?

noctuid commented 1 year ago

Yes, I saw that, but I also have zsh and other non-bash scripts.

Understandable if you don't want to add support directly in the package since you can't use the same method used for other modes. Maybe add another example in the reame that checks sh-shell. I currently have this in my config:

  (defun noct-maybe-enable-bash-ts-mode ()
    (when (and (eq major-mode 'sh-mode)
               (eq sh-shell 'bash))
      (unless (treesit-ready-p 'bash t)
        (treesit-auto--prompt-to-install-package 'bash))
      (bash-ts-mode)))
  (add-hook 'sh-mode-hook #'noct-maybe-enable-bash-ts-mode)
renzmann commented 1 year ago

I see what you mean now, I'll admit I was ignorant to the role sh-shell could play. I'm open to having this kind of behavior, since it seems likely each shell will get its own tree-sitter mode later on, and it would be pretty handy to have the tree-sitter mode automatically show up when setting the shell. It would also certainly make incremental support of new modes as they appear easier.

As you mentioned though, I'd like to branch off and add a couple functions that would handle that. In that process I'd also want to make sure we consider the private namespacing, since I wouldn't want to recommend "private" functions for documentation snippets.

An easy first step in that direction would be a patch to treesit-auto--maybe-install-grammar https://github.com/renzmann/treesit-auto/blob/603de37a7ce2e7ad1698d3f26924b1ea518c6fa1/treesit-auto.el#L215-L226,

adding an optional argument mode-or-lang (falling back to major-mode) and marking the function public so that you could use it in lieu of the unless block. Then we could set up something similar to noct-maybe-enable-bash-ts-mode in a slightly more generalized format for handling the different shell modes altogether within treesit-auto.el

renzmann commented 1 year ago

@noctuid After looking into this a bit more, I'm wondering if we need to write the upgrade/fallback at all. From the documentation:

(bash-ts-mode)

Major mode for editing Bash shell scripts. This mode automatically falls back to ‘sh-mode’ if the buffer is not written in Bash or sh.

Experimenting with this, by using sh-shell the mode line will update to represent the current shell type we're editing:

different-shells

In your usage of non-bash/sh files, are there features that the built-in fallback isn't providing?

noctuid commented 1 year ago

Thanks, I hadn't seen that. I can just do that for now, but if/when zsh-ts-mode and similar are added, a more complex check may be needed if there isn't some generic sh-ts-mode. For now, I don't think treesit-auto needs to do anything though.

renzmann commented 1 year ago

I agree - if each shell gets its own ts mode then I'll look for ways to incorporate that behavior. Since it seems we don't need to worry about it just yet, though, I'm going to close the issue, but leave the thread open for later in case it comes up again.