Make c-ts-mode indent tests side-effect-free

Running indent tests changes the global value of
c-ts-mode-indent-style.  That's not good.  This change fixes that.

I also refactored the indent style functions a bit.

* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--prompt-for-style): New function.
(c-ts-mode-set-local-style): New function.
(c-ts-mode-set-style): Use c-ts-mode--prompt-for-style.  Use
derived-mode-p when testing for major mode.  Remove check of current
buffer's major mode since it doesn't matter.

* test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts:
* test/lisp/progmodes/c-ts-mode-resources/indent.erts: Use
c-ts-mode-set-local-style to set the indent style locally.
This commit is contained in:
Yuan Fu 2023-02-02 18:23:21 -08:00
parent 8a6bdf88b4
commit d963a8f135
No known key found for this signature in database
GPG key ID: 56E19BC57664A442
3 changed files with 35 additions and 27 deletions

View file

@ -100,12 +100,11 @@ the value of SYM in `c-ts-mode' and `c++-ts-mode' buffers to VAL."
(setq-local treesit-simple-indent-rules (setq-local treesit-simple-indent-rules
(treesit--indent-rules-optimize (treesit--indent-rules-optimize
(c-ts-mode--get-indent-style (c-ts-mode--get-indent-style
(if (eq major-mode 'c-ts-mode) 'c 'cpp)))))) (if (derived-mode-p 'c-ts-mode) 'c 'cpp))))))
res) res)
(let ((buffer (car buffers))) (let ((buffer (car buffers)))
(with-current-buffer buffer (with-current-buffer buffer
;; FIXME: Should we use `derived-mode-p' here? (if (derived-mode-p 'c-ts-mode 'c++-ts-mode)
(if (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode))
(loop (append res (list buffer)) (cdr buffers)) (loop (append res (list buffer)) (cdr buffers))
(loop res (cdr buffers)))))))) (loop res (cdr buffers))))))))
@ -134,24 +133,33 @@ MODE is either `c' or `cpp'."
(alist-get c-ts-mode-indent-style (c-ts-mode--indent-styles mode))))) (alist-get c-ts-mode-indent-style (c-ts-mode--indent-styles mode)))))
`((,mode ,@style)))) `((,mode ,@style))))
(defun c-ts-mode-set-style () (defun c-ts-mode--prompt-for-style ()
"Set the indent style of C/C++ modes globally. "Prompt for a indent style and return the symbol for it."
(let ((mode (if (derived-mode-p 'c-ts-mode) 'c 'c++)))
(intern
(completing-read
"Style: "
(mapcar #'car (c-ts-mode--indent-styles mode))
nil t nil nil "gnu"))))
(defun c-ts-mode-set-style (style)
"Set the indent style of C/C++ modes globally to STYLE.
This changes the current indent style of every C/C++ buffer and This changes the current indent style of every C/C++ buffer and
the default C/C++ indent style in this Emacs session." the default C/C++ indent style in this Emacs session."
(interactive) (interactive (list (c-ts-mode--prompt-for-style)))
;; FIXME: Should we use `derived-mode-p' here? (c-ts-mode--indent-style-setter 'c-ts-mode-indent-style style))
(or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode)
(error "Buffer %s is not a c-ts-mode (c-ts-mode-set-style)" (defun c-ts-mode-set-local-style (style)
(buffer-name))) "Set the C/C++ indent style of the current buffer to STYLE."
(c-ts-mode--indent-style-setter (interactive (list (c-ts-mode--prompt-for-style)))
'c-ts-mode-indent-style (if (not (derived-mode-p 'c-ts-mode 'c++-ts-mode))
;; NOTE: We can probably use the interactive form for this. (user-error "The current buffer is not in `c-ts-mode' nor `c++-ts-mode'")
(intern (setq-local c-ts-mode-indent-style style)
(completing-read (setq treesit-simple-indent-rules
"Select style: " (treesit--indent-rules-optimize
(mapcar #'car (c-ts-mode--indent-styles (if (eq major-mode 'c-ts-mode) 'c 'cpp))) (c-ts-mode--get-indent-style
nil t nil nil "gnu")))) (if (derived-mode-p 'c-ts-mode) 'c 'cpp))))))
;;; Syntax table ;;; Syntax table

View file

@ -1,9 +1,9 @@
Code: Code:
(lambda () (lambda ()
(setq indent-tabs-mode nil)
(setq c-ts-mode-indent-offset 2)
(setq c-ts-mode-indent-style 'bsd)
(c-ts-mode) (c-ts-mode)
(setq-local indent-tabs-mode nil)
(setq-local c-ts-mode-indent-offset 2)
(c-ts-mode-set-local-style 'bsd)
(indent-region (point-min) (point-max))) (indent-region (point-min) (point-max)))
Point-Char: | Point-Char: |

View file

@ -1,9 +1,9 @@
Code: Code:
(lambda () (lambda ()
(setq indent-tabs-mode nil)
(setq c-ts-mode-indent-offset 2)
(setq c-ts-mode-indent-style 'gnu)
(c-ts-mode) (c-ts-mode)
(setq-local indent-tabs-mode nil)
(setq-local c-ts-mode-indent-offset 2)
(c-ts-mode-set-local-style 'gnu)
(indent-region (point-min) (point-max))) (indent-region (point-min) (point-max)))
Point-Char: | Point-Char: |
@ -219,10 +219,10 @@ line 2
Code: Code:
(lambda () (lambda ()
(setq indent-tabs-mode nil)
(setq c-ts-mode-indent-offset 8)
(setq c-ts-mode-indent-style 'linux)
(c-ts-mode) (c-ts-mode)
(setq-local indent-tabs-mode nil)
(setq-local c-ts-mode-indent-offset 8)
(c-ts-mode-set-local-style 'linux)
(indent-region (point-min) (point-max))) (indent-region (point-min) (point-max)))
Name: Labels (Linux Style) Name: Labels (Linux Style)