emacs-tree-sitter / ts-fold

Code-folding using tree-sitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

global-ts-fold-mode breaks evil folding for unsupported languages

eeshugerman opened this issue · comments

If I enable global-ts-fold-mode and then try to use evil folding (evil-close-fold) with an unsupported major mode (eg sql-mode), I get

Error: (user-error "Ignored, tree-sitter-mode is not enable in the current buffer")

Is there some way this could be avoided?

Alternatively (or in addition), could ts-fold define (and autoload) a list of supported major modes?

My specific use case is a Spacemacs layer (which I hope to get merged upstream) that (optionally) enables ts-fold for all supported languages -- ideally we wouldn't have to maintain that list in the layer. I imagine this would benefit many non-Spacemacs users as well, for basically the same reason.

You might want to enable tree-sitter-mode globally as well? Try

(global-tree-sitter-mode 1)

I have global-tree-sitter-mode enabled. The issue is in major modes that tree-sitter-mode does not support.

I just took a closer look -- global-tree-sitter-mode does not actually enable tree-sitter-mode in major modes that it does not support. If global-ts-fold-mode could do the same, that would resolve this issue.

Sorry for the late reply. Kinda got busy in a time.

Ah, okay. I think the message Ignored, tree-sitter-mode is not enable in the current buffer is just a kind message to inform user about the state of ts-fold. It's should harm you from any aspect.

It does do harm: it breaks evil-fold (in unsupported major modes). Instead of folding, the error is thrown.

Perhaps if it were message instead of user-error logging the message, then folding would still work, but I'm not sure about that.

I assumed you are talking about the fallback scenario? If that's the case, should evil-fold handle this condition itself? 😕

Perhaps if it were message instead of user-error logging the message, then folding would still work, but I'm not sure about that.

user-error will abort the operation for sure.

From what I can tell evil-fold is working as intended.

I tried replacing user-error with message in ts-fold--ensure-ts. This did not fix folding. It turns out this is not surprising if we look at how evil-fold uses evil-fold-list: https://githubt.com/emacs-evil/evil/blob/master/evil-commands.el#L3080-L3101

  • "actions" are performed in a with-demoted-errors form
  • only the first matching action (by mode) is performed (ie there is no fallback scenario -- if that's what you meant?)

So to remedy this, we need some way to enable ts-fold-mode in supported major modes only. Currently, to this end, we have defined a list of supported modes for ts-fold in Spacemacs. This works, but is not ideal -- it would be nice if we (the user) did not have to maintain that list.

@eeshugerman I don't know if this is still relevant to spacemacs, but the commit above should correct the issue you were facing. Now, when global-ts-fold-mode is used, ts-fold will only be activated in buffers where tree-sitter is also enabled AND only when the major mode has defined folds. That way, it shouldn't eat the evil-fold request without knowing that it can be handled.

@samrjack great news! As you can see above, I've opened a PR to take advantage of this in Spacemacs. Thanks for tackling this!