From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: How does c-ts-mode, tree-sitter indentation, and preprocessor directives work? Date: Thu, 28 Nov 2024 02:03:44 -0800 Message-ID: <946BE9A2-08E8-466F-BD0A-39D504B09CE9@gmail.com> References: <86plmferwu.fsf@gnu.org> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_BA534F01-AAA7-49F6-9C2E-481940D2EEBE" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11073"; mail-complaints-to="usenet@ciao.gmane.io" Cc: =?utf-8?Q?Bj=C3=B6rn_Lindqvist?= , Emacs Devel To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Nov 28 11:04:42 2024 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1tGbO5-0002hg-D8 for ged-emacs-devel@m.gmane-mx.org; Thu, 28 Nov 2024 11:04:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tGbNS-0001C2-UM; Thu, 28 Nov 2024 05:04:02 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tGbNQ-0001AV-D0 for emacs-devel@gnu.org; Thu, 28 Nov 2024 05:04:00 -0500 Original-Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tGbNO-000482-T0; Thu, 28 Nov 2024 05:04:00 -0500 Original-Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-21269c8df64so6012185ad.2; Thu, 28 Nov 2024 02:03:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732788236; x=1733393036; darn=gnu.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=m8KH6EeIJf1QvRzAm8nJNIL94NATZtd1M2ouitMmjhI=; b=k6EwZ+e14MmHLGlb4zHbrLPfjFaIs4Py0ZApBudo5HSJae2Qub5jpHMGsSaeg/E8pA /I7HMBLoVthJ7OLqyHC5BTQgIQUJDQ5kaJfYTyMGXJv1jgpj2zxshTnqaO5cFOsrvtvc FaWzuy5a6jd0NBYGjg47V3PqRAylwRsAKBySr4WwE5ftR1cIQxjiUrqbehfC/EzQqEeD wkETu9L+DCJyDVXbrUtsh0RmySp/VLLsx4oYXnsuWlGBYnl7pEw8OJOaZIqbY7oO+Nws /9bs7RKzGXm6iFMZvbaT2jkiDVg9lAt2d4wHPZXVhb3Pr+CrZQl9sZvePsugbWiJeYO8 l5bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732788236; x=1733393036; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=m8KH6EeIJf1QvRzAm8nJNIL94NATZtd1M2ouitMmjhI=; b=NHmRTlR6awR4vo2dltVZaNR1DKN2EsUqtHoWBcuXQRT/sh7BkN+gthZf6LRlA/Obwd MGkNZboRSJmUhh7U92ftxfI9Idg8mpaF/UKW3Po8NDlI1/PO37JYROraIFvNKyRZxz2O z29KRj9JzbWoT8U6zYQ+H0nUoQOg5dJAOHLoqRwa9Ziohjcet3j5AV8FLEi8KZNBrzG1 Gzd1B8ku5L1a+Nrt9S/3iJpOj8N9+zjxBmaNRd35TalAhTbS3ihHlhcU3AJdCkxj6n6m acVVXeuwDYckmTwnxqSKynhOSzuKn53cLAjKIYRru4SfOguTE41Qlm2mVxQQSK5NYLC3 Mbag== X-Forwarded-Encrypted: i=1; AJvYcCV+kdDjY0UqsdtYWXcvVsIa9PpiNXtp4EQW82V4IFFjQ+m2LZPT8tMxE5dZOjT/OPMXlV1l1d2h1Pgn3A==@gnu.org X-Gm-Message-State: AOJu0YyoU2/Auf+HRFB2uMMJPtY1tD0gzJLBIF9RFow5FJgBDrDE8OWX OFb4r97dpmDLBTXRF1VnyarwsoTrp5MYUeZJBx8lR9njG0k4+fzjjmYI+g== X-Gm-Gg: ASbGncsq0Ss0KhQ1hxM2EP0/GWdSnDWBFjv9+OHVyj+hlLk2XEKvHV0ONSqHU2nsQHX 7E/9VjEDBAb3SOaCdm/shcSDmE4elfHokrlTZbB5S1iw9th54g/tFHVGzT6xDdytFOEZ2JiOGGS liLARJP0x0Bzf1ReWtscATR+sFD2MMMXYly5qR0Zjkj+lrD7RwZ7Jt/3cr2rLNeK/N5SWG0ch1t Z1wJHYg564Waow81fE8TC6hFxR4W+9MLMkDvpACa5px9PUqS6z1xc51QDA+JGEFcN4IMgHKKQ== X-Google-Smtp-Source: AGHT+IGVFNwUC5DopTJeSaWJEAq/J6Jh5TkIohFuBLX/gCNm237G5BRUIbowEGxPyyCYYTKwV4MdVA== X-Received: by 2002:a17:902:d4cc:b0:215:28c2:6665 with SMTP id d9443c01a7336-21528c26ca9mr7787025ad.51.1732788236237; Thu, 28 Nov 2024 02:03:56 -0800 (PST) Original-Received: from smtpclient.apple ([2601:646:8f81:6120:c885:af5e:9679:e419]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21521983068sm9886585ad.186.2024.11.28.02.03.55 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 28 Nov 2024 02:03:55 -0800 (PST) In-Reply-To: <86plmferwu.fsf@gnu.org> X-Mailer: Apple Mail (2.3776.700.51) Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=casouri@gmail.com; helo=mail-pl1-x62c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:325803 Archived-At: --Apple-Mail=_BA534F01-AAA7-49F6-9C2E-481940D2EEBE Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Nov 27, 2024, at 11:30=E2=80=AFPM, Eli Zaretskii = wrote: >=20 >> From: Bj=C3=B6rn Lindqvist >> Date: Thu, 28 Nov 2024 00:27:17 +0100 >>=20 >> I've been trying to get c-ts-mode to indent like I want, but I'm >> running into problems related to preprocessor directives. >=20 > Preprocessor directives are difficult because the tree-sitter C/C++ > grammars include only partial support for them. >=20 >> For >> example, consider a type definition nested in two #ifdefs: >>=20 >> #ifdef X >> #ifdef Y >> typedef int foo; >> #endif >> #endif >>=20 >> Since both the parent and grand parent of the type_definition is a >> preproc_ifdef no rule matches. >=20 > But if you go back (up) the parent-child hierarchy, you will > eventually find a node which is not a preproc_SOMETHING, and can go > from there, no? >=20 >> Another issue is that I want my >> preprocessor directives kept at column 0, which unfortunately screws >> up all rules that refer to the parent. E.g.: >>=20 >> ((parent-is "if_statement") standalone-parent 4) >>=20 >> Doesn't work for >>=20 >> int main() { >> if (true) >> #ifdef A >> prutt(); >> #else >> fis(); >> #endif >> } >>=20 >> The rule I'd like to express is "take the indent of the closest >> *indenting* parent and add one indent". That rule would match whether >> that parent is a "while_statement", "if_statement", "for_statement", >> etc. You can't express such rules with tree-sitter, can you? >=20 > Not sure, but Yuan will know. Everything is possible, it=E2=80=99s just elisp. The only problem is how = generic you can make the rule. Here=E2=80=99s a POC that only works for = this example; specifically, it only works for if statements and #ifdef = directives. It should be extendable to for statement, while statement, = etc, and maybe other directives too. Speaking of indent, we need to do something with c-ts-mode=E2=80=99s = indentation rules. It=E2=80=99s getting too long and too complex. But I = don=E2=80=99t have any great idea at this point. Maybe we can replace = the rules with a hand-rolled function so it has more structure, or try = nvim=E2=80=99s query approach. Yuan --Apple-Mail=_BA534F01-AAA7-49F6-9C2E-481940D2EEBE Content-Disposition: attachment; filename=preproc-indent.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="preproc-indent.patch" Content-Transfer-Encoding: quoted-printable =46rom=2025de026b3eb32e7457270cd199fe0902876a2715=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20Yuan=20Fu=20=0ADate:=20Thu,=20= 28=20Nov=202024=2001:51:44=20-0800=0ASubject:=20[PATCH]=20Preproc=20= indent=20POC=0A=0A---=0A=20lisp/progmodes/c-ts-mode.el=20|=2018=20= ++++++++++++++++++=0A=201=20file=20changed,=2018=20insertions(+)=0A=0A= diff=20--git=20a/lisp/progmodes/c-ts-mode.el=20= b/lisp/progmodes/c-ts-mode.el=0Aindex=20c815ee35501..313dcfb5c05=20= 100644=0A---=20a/lisp/progmodes/c-ts-mode.el=0A+++=20= b/lisp/progmodes/c-ts-mode.el=0A@@=20-435,6=20+435,24=20@@=20= c-ts-mode--indent-styles=0A=20=20=20=20=20=20=20=20=20=20=20=20= ((parent-is=20"labeled_statement")=0A=20=20=20=20=20=20=20=20=20=20=20=20= =20c-ts-mode--standalone-grandparent=20c-ts-mode-indent-offset)=0A=20=0A= +=20=20=20=20=20=20=20=20=20=20=20,(let=20(anchor)=0A+=20=20=20=20=20=20=20= =20=20=20=20=20=20=20(list=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (lambda=20(_node=20parent=20&rest=20_)=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20(let=20((anchor-node=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20(cond=0A+=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20((treesit-node-match-p=20= parent=20"preproc_ifdef")=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20(treesit-node-prev-sibling=20parent))=0A= +=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20((treesit-node-match-p=20parent=20"preproc_else")=0A+=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (treesit-node-prev-sibling=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20(treesit-node-parent=20parent))))))=0A= +=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(when=20= anchor-node=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20(setq=20anchor=20(treesit-node-start=20anchor-node))=0A+=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20;;=20If=20parent=20is=20= preproc=20and=20previous=20sibling=20is=0A+=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20;;=20if_statement,=20set=20anchor=20and=20= return=20t.=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20(treesit-node-match-p=20anchor-node=20"if_statement"))))=0A+=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20(lambda=20(&rest=20_)=20anchor)=0A+=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20c-ts-mode-indent-offset))=0A+=0A=20= =20=20=20=20=20=20=20=20=20=20=20;;=20Preproc=20directives=0A=20=20=20=20= =20=20=20=20=20=20=20=20((node-is=20"preproc")=20column-0=200)=0A=20=20=20= =20=20=20=20=20=20=20=20=20((node-is=20"#endif")=20column-0=200)=0A--=20=0A= 2.39.5=20(Apple=20Git-151)=0A=0A= --Apple-Mail=_BA534F01-AAA7-49F6-9C2E-481940D2EEBE--