Skip to content

Commit 7fdaf44

Browse files
authored
Change global mode to turn on with tree-sitter (#41)
The global mode that is currently defined has the issue that it turns on in all buffers which can cause issues with smart folding patterns. By linking the major mode to `tree-sitter`, ts-fold will only turn on when it's in a file where it will actually work.
1 parent a64f525 commit 7fdaf44

File tree

3 files changed

+78
-21
lines changed

3 files changed

+78
-21
lines changed

README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,27 @@ or
8282

8383
The following are the functions provided by `ts-fold-mode`
8484

85-
| Commands | Description |
86-
| -------------------------- | --------------------------------------------------------------------------- |
87-
| `ts-fold-close` | fold the current syntax node. |
88-
| `ts-fold-open` | open all folds inside the current syntax node. |
89-
| `ts-fold-open-recursively` | open the outmost fold of the current syntax node. Keep the sub-folds close. |
90-
| `ts-fold-close-all` | close all foldable syntax nodes in the current buffer. |
91-
| `ts-fold-open-all` | open all folded syntax nodes in the current buffer. |
92-
| `ts-fold-toggle` | toggle the syntax node at `point'. |
85+
Commands for enabling `ts-fold`:
86+
87+
| Commands | Description |
88+
| -------------------------------- | --------------------------------------------------------------------------------------------------- |
89+
| `ts-fold-mode` | enable `ts-fold-mode` in the current buffer. |
90+
| `global-ts-fold-mode` | enable `ts-fold-mode` whenever tree-sitter is turned on and the major mode is supported by ts-fold. |
91+
| `ts-fold-indicators-mode` | enable ts-fold with indicators in the current buffer. See [plugins section](#-indicators-mode). |
92+
| `global-ts-fold-indicators-mode` | enable ts-fold with indicators globally. See [plugins section](#-indicators-mode). |
93+
94+
Commands for using `ts-fold`.
95+
96+
| Commands | Description |
97+
| -------------------------- | ----------------------------------------------------------------------------- |
98+
| `ts-fold-close` | fold the current syntax node. |
99+
| `ts-fold-open` | open the outermost fold of the current syntax node. Keep the sub-folds close. |
100+
| `ts-fold-open-recursively` | open all folds inside the current syntax node. |
101+
| `ts-fold-close-all` | close all foldable syntax nodes in the current buffer. |
102+
| `ts-fold-open-all` | open all folded syntax nodes in the current buffer. |
103+
| `ts-fold-toggle` | toggle the syntax node at `point'. |
104+
105+
If evil mode is loaded, then these commands are also added to the evil folding list.
93106

94107
### 🔨 Supported languages
95108

@@ -340,7 +353,7 @@ basic `ts-fold-range-seq`.
340353
ts-fold comes with a couple of useful little additions that can be used or
341354
turned off as desired.
342355
343-
### ⚖ Indicators Mode
356+
### ⚖ Indicators Mode
344357
345358
<p align="center">
346359
<img src="./etc/indicators.png" width="40%" height=480%"/>
@@ -376,19 +389,28 @@ explicitly declare the package in in your config.
376389
377390
#### 🖥 Usage
378391
379-
You can then enable this manually by doing the following
392+
You can then enable this manually by doing either of the following:
380393
381394
```
382395
M-x ts-fold-indicators-mode
396+
397+
M-x global-ts-fold-indicators-mode
383398
```
384399
385400
Please note that turning on `ts-fold-indicators-mode` automatically turns on
386-
`ts-fold-mode` as well.
401+
`ts-fold-mode` as well. Though, turning off `ts-fold-indicators-mode` does not
402+
turn off `ts-fold-mode`
403+
404+
- To enable this automatically whenever `tree-sitter-mode` is enabled, use the global indicator mode:
405+
406+
```elisp
407+
(global-ts-fold-indicators-mode 1)
408+
```
387409
388-
- To enable this automatically whenever `tree-sitter-mode` is enabled:
410+
Else, a hook can be added to tree-sitter directly.
389411
390412
```elisp
391-
(add-hook 'tree-sitter-after-on-hook #ts-fold-indicators-mode)
413+
(add-hook 'tree-sitter-after-on-hook #'ts-fold-indictors-mode)
392414
```
393415
394416
- To switch to left/right fringe: (Default is `left-fringe`)

ts-fold-indicators.el

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106

107107
(defun ts-fold-indicators--enable ()
108108
"Enable `ts-fold-indicators' mode."
109-
(if (ts-fold-mode 1) ; Enable `ts-fold-mode' automatically
109+
(if (or ts-fold-mode (ts-fold-mode 1)) ; Enable `ts-fold-mode' automatically
110110
(progn
111111
(add-hook 'tree-sitter-after-change-functions #'ts-fold-indicators-refresh nil t)
112112
(add-hook 'after-save-hook #'ts-fold-indicators-refresh nil t)
@@ -130,8 +130,20 @@
130130
(ts-fold-indicators--disable)))
131131

132132
;;;###autoload
133-
(define-global-minor-mode global-ts-fold-indicators-mode ts-fold-indicators-mode
134-
(lambda () (ts-fold-indicators-mode 1)))
133+
(define-minor-mode global-ts-fold-indicators-mode
134+
"Global minor mode for turning on ts-fold with indicators whenever avaliable."
135+
:group 'ts-fold
136+
:lighter nil
137+
:init-value nil
138+
:global t
139+
(if global-ts-fold-indicators-mode
140+
(progn
141+
(add-hook 'ts-fold-mode-hook #'ts-fold-indicators-mode)
142+
(global-ts-fold-mode 1)
143+
(dolist (buf (buffer-list))
144+
(with-current-buffer buf
145+
(when (and ts-fold-mode (not ts-fold-indicators-mode) (ts-fold-indicators-mode))))))
146+
(remove-hook 'ts-fold-mode-hook #'ts-fold-indicators-mode)))
135147

136148
;;
137149
;; (@* "Events" )

ts-fold.el

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,49 @@ the fold in a cons cell. See `ts-fold-range-python' for an example."
144144
:close ts-fold-close
145145
:open-rec ts-fold-open-recursively
146146
:open-all ts-fold-open-all
147-
:close-all ts-fold-close-all)))
148-
149-
(run-hooks 'ts-fold-mode-hook))
147+
:close-all ts-fold-close-all))))
150148

151149
(defun ts-fold--disable ()
152150
"Stop folding minor mode."
153151
(remove-from-invisibility-spec '(ts-fold . t))
154152
(let ((tree-sitter-mode t))
155153
(ts-fold-open-all)))
156154

155+
(defun ts-fold--tree-sitter-trigger ()
156+
"Turn `ts-fold-mode' on and off alongside `tree-sitter-mode'
157+
when in a mode ts-fold can act on."
158+
(if (and tree-sitter-mode (ts-fold-usable-mode-p))
159+
(ts-fold-mode 1)
160+
(ts-fold-mode -1)))
161+
157162
;;;###autoload
158163
(define-minor-mode ts-fold-mode
159164
"Folding code using tree sitter."
165+
:group 'ts-fold
160166
:init-value nil
161167
:lighter "TS-Fold"
162168
(if ts-fold-mode (ts-fold--enable) (ts-fold--disable)))
163169

164170
;;;###autoload
165-
(define-global-minor-mode global-ts-fold-mode ts-fold-mode
166-
(lambda () (ts-fold-mode 1)))
171+
(define-minor-mode global-ts-fold-mode
172+
"Use `ts-fold-mode' wherever possible"
173+
:group 'ts-fold
174+
:init-value nil
175+
:lighter nil
176+
:global t
177+
(if global-ts-fold-mode
178+
(progn
179+
(add-hook 'tree-sitter-mode-hook #'ts-fold--tree-sitter-trigger)
180+
;; try to turn on in all buffers.
181+
(dolist (buf (buffer-list))
182+
(with-current-buffer buf
183+
(ts-fold--tree-sitter-trigger))))
184+
(remove-hook 'tree-sitter-mode-hook #'ts-fold--tree-sitter-trigger)))
185+
186+
(defun ts-fold-usable-mode-p (&optional mode)
187+
"Return non-nil if `ts-fold' has defined folds for MODE."
188+
(let ((mode (or mode major-mode)))
189+
(alist-get mode ts-fold-range-alist)))
167190

168191
;;
169192
;; (@* "Core" )

0 commit comments

Comments
 (0)