From: "Jørgen Kvalsvik" <j@lambda.is>
To: 74507@debbugs.gnu.org
Subject: bug#74507: [PATCH] Indent compounds in c-ts-mode when { is not BOL
Date: Sat, 30 Nov 2024 16:39:48 +0100 [thread overview]
Message-ID: <8734j8zq63.fsf@lambda.is> (raw)
In-Reply-To: <87r071yov3.fsf@lambda.is>
[-- Attachment #1: Type: text/plain, Size: 1 bytes --]
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Indent-compounds-in-c-ts-mode-when-is-not-BOL.patch --]
[-- Type: text/patch, Size: 3651 bytes --]
Properly indent the body of compound expressions, even when then
compound expression is not at the beginning of line and the parent is
not an if/for/while/etc., and matches the behavior of c-mode.
This fixes a problem that is common with macros and in testing
frameworks. For example, you expect this to indent:
TEST_CASE(1) {
assert (...);
}
If the compound statement is the function body itself, don't apply this
new rule and instead guide by the parent and first sibling.
I'm sure there are subtle interactions that aren't handled properly by
checking for "function_definition" rather than something more general,
but it does fix the test case and the check can be improved as more
cases are found.
* lisp/progmodes/c-ts-mode.el:
(c-ts-mode--parent-is-not-top-compound): New function.
(c-ts-mode--indent-styles): Use it.
* test/lisp/progmodes/c-ts-mode-resources/indent.erts: New
compound statement test.
---
lisp/progmodes/c-ts-mode.el | 9 ++++++
.../progmodes/c-ts-mode-resources/indent.erts | 30 +++++++++++++++++++
2 files changed, 39 insertions(+)
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 3823c553fda..b635e621b03 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -402,6 +402,12 @@ c-ts-mode--first-sibling
(treesit-node-start parent)
(line-end-position))))))
+(defun c-ts-mode--parent-is-not-top-compound (_n parent &rest _)
+ "Matches when PARENT is not the top level compound statement.
+The top-level compound is the {} that immediately follows the function
+signature."
+ (not (equal "function_definition" (treesit-node-type (treesit-node-parent parent)))))
+
(defun c-ts-mode--indent-styles (mode)
"Indent rules supported by `c-ts-mode'.
MODE is either `c' or `cpp'."
@@ -478,6 +484,7 @@ c-ts-mode--indent-styles
;; Closing bracket. This should be before initializer_list
;; (and probably others) rule because that rule (and other
;; similar rules) will match the closing bracket. (Bug#61398)
+ ((and (node-is "}") c-ts-mode--parent-is-not-top-compound) parent-bol 0)
((node-is "}") standalone-parent 0)
,@(when (eq mode 'cpp)
'(((node-is "access_specifier") parent-bol 0)
@@ -497,6 +504,8 @@ c-ts-mode--indent-styles
((parent-is "field_declaration_list") c-ts-mode--anchor-prev-sibling 0)
;; Statement in {} blocks.
+ ((and (parent-is "compound_statement") c-ts-mode--parent-is-not-top-compound)
+ parent-bol c-ts-mode-indent-offset)
((or (and (parent-is "compound_statement")
;; If the previous sibling(s) are not on their
;; own line, indent as if this node is the first
diff --git a/test/lisp/progmodes/c-ts-mode-resources/indent.erts b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
index 2f3540c3970..61e61677ed7 100644
--- a/test/lisp/progmodes/c-ts-mode-resources/indent.erts
+++ b/test/lisp/progmodes/c-ts-mode-resources/indent.erts
@@ -223,6 +223,36 @@ make_pair(int long_identifier_a[], int long_identifier_b[],
=-=-=
+Name: Compound Statement after code
+
+=-=
+#define IOTA(var, n) for (int var = 0; var != (n); ++var)
+int main()
+{
+IOTA (v, 10) {
+printf("%d ", v);
+}
+
+const char *msg = "Hello, world!"; {
+puts("Hello, world!");
+}
+}
+
+=-=
+#define IOTA(var, n) for (int var = 0; var != (n); ++var)
+int main()
+{
+ IOTA (v, 10) {
+ printf("%d ", v);
+ }
+
+ const char *msg = "Hello, world!"; {
+ puts("Hello, world!");
+ }
+}
+
+=-=-=
+
Name: Switch-Case statement
=-=
--
2.39.5
[-- Attachment #3: Type: text/plain, Size: 0 bytes --]
prev parent reply other threads:[~2024-11-30 15:39 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-11-24 9:15 bug#74507: [PATCH] Indent compounds c-ts-mode when { is not BOL Jørgen Kvalsvik
2024-11-29 4:56 ` Yuan Fu
2024-11-29 7:54 ` Eli Zaretskii
2024-11-29 9:04 ` Jørgen Kvalsvik
2024-11-29 7:57 ` Eli Zaretskii
2024-11-29 9:05 ` Jørgen Kvalsvik
2024-11-30 0:16 ` Yuan Fu
2024-11-30 20:49 ` Jørgen Kvalsvik
2024-12-01 9:25 ` Yuan Fu
2024-12-01 9:51 ` Jørgen Kvalsvik
2024-12-01 21:48 ` Yuan Fu
2024-12-01 22:46 ` Jørgen Kvalsvik
2024-12-02 2:13 ` Yuan Fu
2024-11-30 15:39 ` Jørgen Kvalsvik [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8734j8zq63.fsf@lambda.is \
--to=j@lambda.is \
--cc=74507@debbugs.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).