[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
emacs-29 d963a8f1355 5/6: Make c-ts-mode indent tests side-effect-free
From: |
Yuan Fu |
Subject: |
emacs-29 d963a8f1355 5/6: Make c-ts-mode indent tests side-effect-free |
Date: |
Thu, 2 Feb 2023 21:46:21 -0500 (EST) |
branch: emacs-29
commit d963a8f1355a6d829af3f98182e66705c941e774
Author: Yuan Fu <casouri@gmail.com>
Commit: Yuan Fu <casouri@gmail.com>
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.
---
lisp/progmodes/c-ts-mode.el | 44 +++++++++++++---------
.../progmodes/c-ts-mode-resources/indent-bsd.erts | 6 +--
.../lisp/progmodes/c-ts-mode-resources/indent.erts | 12 +++---
3 files changed, 35 insertions(+), 27 deletions(-)
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 00704337cd9..390f67a8e8c 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -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
(treesit--indent-rules-optimize
(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)
(let ((buffer (car buffers)))
(with-current-buffer buffer
- ;; FIXME: Should we use `derived-mode-p' here?
- (if (or (eq major-mode 'c-ts-mode) (eq major-mode 'c++-ts-mode))
+ (if (derived-mode-p 'c-ts-mode 'c++-ts-mode)
(loop (append res (list buffer)) (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)))))
`((,mode ,@style))))
-(defun c-ts-mode-set-style ()
- "Set the indent style of C/C++ modes globally.
+(defun c-ts-mode--prompt-for-style ()
+ "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
the default C/C++ indent style in this Emacs session."
- (interactive)
- ;; FIXME: Should we use `derived-mode-p' here?
- (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)"
- (buffer-name)))
- (c-ts-mode--indent-style-setter
- 'c-ts-mode-indent-style
- ;; NOTE: We can probably use the interactive form for this.
- (intern
- (completing-read
- "Select style: "
- (mapcar #'car (c-ts-mode--indent-styles (if (eq major-mode 'c-ts-mode) 'c
'cpp)))
- nil t nil nil "gnu"))))
+ (interactive (list (c-ts-mode--prompt-for-style)))
+ (c-ts-mode--indent-style-setter 'c-ts-mode-indent-style style))
+
+(defun c-ts-mode-set-local-style (style)
+ "Set the C/C++ indent style of the current buffer to STYLE."
+ (interactive (list (c-ts-mode--prompt-for-style)))
+ (if (not (derived-mode-p 'c-ts-mode 'c++-ts-mode))
+ (user-error "The current buffer is not in `c-ts-mode' nor `c++-ts-mode'")
+ (setq-local c-ts-mode-indent-style style)
+ (setq treesit-simple-indent-rules
+ (treesit--indent-rules-optimize
+ (c-ts-mode--get-indent-style
+ (if (derived-mode-p 'c-ts-mode) 'c 'cpp))))))
;;; Syntax table
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts
b/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts
index 07698077ffc..ba4f854baf8 100644
--- a/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts
+++ b/test/lisp/progmodes/c-ts-mode-resources/indent-bsd.erts
@@ -1,9 +1,9 @@
Code:
(lambda ()
- (setq indent-tabs-mode nil)
- (setq c-ts-mode-indent-offset 2)
- (setq c-ts-mode-indent-style 'bsd)
(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)))
Point-Char: |
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts
b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
index 0ecbf922b15..3704f06d2ae 100644
--- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
@@ -1,9 +1,9 @@
Code:
(lambda ()
- (setq indent-tabs-mode nil)
- (setq c-ts-mode-indent-offset 2)
- (setq c-ts-mode-indent-style 'gnu)
(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)))
Point-Char: |
@@ -219,10 +219,10 @@ line 2
Code:
(lambda ()
- (setq indent-tabs-mode nil)
- (setq c-ts-mode-indent-offset 8)
- (setq c-ts-mode-indent-style 'linux)
(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)))
Name: Labels (Linux Style)
- emacs-29 updated (88ccf78b206 -> 96ea27278b4), Yuan Fu, 2023/02/02
- emacs-29 b8009bbf2d8 1/6: ; Fix error where we pass t to treesit-node-type in c-ts-common.el, Yuan Fu, 2023/02/02
- emacs-29 8a6bdf88b4b 4/6: Call treesit_record_change in insert_from_gap_1, Yuan Fu, 2023/02/02
- emacs-29 a2b77c79dcc 3/6: Use c-ts-common-statement-offset for closing brackets too, Yuan Fu, 2023/02/02
- emacs-29 74e715cb729 2/6: ; Go back to original point when filling comments in c-ts-mode, Yuan Fu, 2023/02/02
- emacs-29 d963a8f1355 5/6: Make c-ts-mode indent tests side-effect-free,
Yuan Fu <=
- emacs-29 96ea27278b4 6/6: ; Fix c-ts-mode indent test, Yuan Fu, 2023/02/02