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.bugs Subject: bug#60496: 29.0.60; c-ts-mode: Broken indentation with linux style conditionals Date: Sun, 15 Jan 2023 01:23:45 -0800 Message-ID: <14D85B96-4968-45D9-8296-819E440F01C3@gmail.com> References: <91001d1f8d7fe3db33aefffaa0829f26@sadiqpk.org> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31277"; mail-complaints-to="usenet@ciao.gmane.io" Cc: bruce.stephens@isode.com, eliz@gnu.org, 60496@debbugs.gnu.org, 59686@debbugs.gnu.org To: Theodor Thornhill Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jan 15 10:24:41 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1pGzFo-0007vz-W0 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 15 Jan 2023 10:24:41 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pGzFH-0001fL-GK; Sun, 15 Jan 2023 04:24:07 -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 1pGzFG-0001dQ-3B for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2023 04:24:06 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pGzFC-0004mL-OT for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2023 04:24:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pGzFC-0003Wd-IJ for bug-gnu-emacs@gnu.org; Sun, 15 Jan 2023 04:24:02 -0500 X-Loop: help-debbugs@gnu.org In-Reply-To: <91001d1f8d7fe3db33aefffaa0829f26@sadiqpk.org> Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 15 Jan 2023 09:24:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60496 X-GNU-PR-Package: emacs Original-Received: via spool by 60496-submit@debbugs.gnu.org id=B60496.167377464013530 (code B ref 60496); Sun, 15 Jan 2023 09:24:02 +0000 Original-Received: (at 60496) by debbugs.gnu.org; 15 Jan 2023 09:24:00 +0000 Original-Received: from localhost ([127.0.0.1]:56102 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pGzFA-0003W9-0N for submit@debbugs.gnu.org; Sun, 15 Jan 2023 04:24:00 -0500 Original-Received: from mail-pl1-f177.google.com ([209.85.214.177]:43528) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pGzF6-0003Ve-AQ; Sun, 15 Jan 2023 04:23:57 -0500 Original-Received: by mail-pl1-f177.google.com with SMTP id d3so27501315plr.10; Sun, 15 Jan 2023 01:23:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:from:to:cc:subject:date:message-id :reply-to; bh=XUt8CmYvOAqndeajKxKqQoDiBG9HvdpRha5qDKw258Y=; b=LoNdkXnWInZWOyUSiz9jjvlDLEaJI3YYhz7Jh16SHWnG2iaCy8LdiHzCefShUrzPtf V2njRovqGxh7ga4S1GxfJ6ToEjUWAEAcKm8tnMGk3Kn+FgBT+slDFLNYgYvdLW2NF+c7 yYw7BbeVTbJE0K+aKj9SFTaytXg5J0yNXIed+hfhqQJzV4vrQ5YjYjXNInFmmXz17Pb7 wtMt9i96h1DVk8iUiflVT91DbsPCEbbQNDpEkFF+t6zUgKtcennOnMKgiRgO+/9ERkRF 3Q88CiqWDPmWEnxCLSq3vm4zORjiLpuGXX97Oid2fJ6AufrXh3x43I2PVZWVBwkuaccL 4xbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:cc:date:message-id:subject:mime-version :content-transfer-encoding:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XUt8CmYvOAqndeajKxKqQoDiBG9HvdpRha5qDKw258Y=; b=PMmMqMDFpaY9hsPOWH+iwobxQA0wXuBaqGReLQDyPcPVkn7UaRCKhf1B04nL/CvTXK kwZkUSiEknF5mLYuBluNBWVVwLDGfNzMIaSKMnm6LI+7+pFkUboVpkejF8YlM7IrJ15/ FfzyUwfRty4CeiN/xxF1jJ8bcZWetrmvpsj1UgmklYYRtJ6KXUy9897CQkDmq76Yt9Kw zoiUzC/5lgID8lQ5/i0o+2f6k/ptLr9clsL855xV9ymeoMToH4/1v+OrzmGsIhq9zwvb RPvLezy0VCaxz+YqktSHW6G+axhXXdssVABvNXRiOcPocxU1Eq83AWLlx8ZAR5lvSe3r B/FA== X-Gm-Message-State: AFqh2krQ5HTS0IFdrtUx2mSem/LbM+D/T7TFljzjWkDnBhL38woeNjVS jax9YxPengPTEytxbo4DW3A= X-Google-Smtp-Source: AMrXdXv4DdrCUVMRZOCYDhE3ISRLMoEjW9CDGEUHknY+YlApmPtWuCis4TKAOt89Z6S7Jy91pxybaw== X-Received: by 2002:a17:903:1d2:b0:194:7aae:f272 with SMTP id e18-20020a17090301d200b001947aaef272mr7630081plh.30.1673774629138; Sun, 15 Jan 2023 01:23:49 -0800 (PST) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id ik9-20020a170902ab0900b00183c67844aesm17081200plb.22.2023.01.15.01.23.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 15 Jan 2023 01:23:48 -0800 (PST) X-Mailer: Apple Mail (2.3696.120.41.1.1) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:253416 Archived-At: Yuan Fu writes: >> On Dec 3, 2022, at 3:08 AM, Theodor Thornhill = wrote: >>=20 >>=20 >>=20 >> On 3 December 2022 11:48:34 CET, Yuan Fu wrote: >>>=20 >>> Theodor Thornhill writes: >>>=20 >>>> Bruce Stephens writes: >>>>=20 >>>>> On 02/12/2022 08:39, Eli Zaretskii wrote: >>>>>=20 >>>>>> FWIW, this is an unusual style, so I see no catastrophe if it is = not 110% >>>>>> according to expectations. Users can easily fix that by tweaking = their BOLs >>>>>> where important. >>>>>=20 >>>>>=20 >>>>> The example I gave would be unusual, I think, but I'd argue that = the >>>>> situations where I saw the problem are quite natural. >>>>>=20 >>>>> For example, >>>>>=20 >>>>> } else if ( MYSTRCMP (attname, = SOME_PREFIX_X400ADDRESS) || >>>>> MYSTRCMP (attname, SOME_PREFIX_X400) ) = { >>>>> FOO_ptr orp =3D foo_std2foo (val); >>>>>=20 >>>>> or a function declaration with several arguments with types that = are >>>>> rather long. >>>>>=20 >>>>> I agree it's not a critical bug but if there's no appropriate = general >>>>> fix it would be helpful to have some guidance for users to resolve = our >>>>> specific cases. >>>>=20 >>>> This is the case I was thinking of. In the for-loop a = grand-parent-bol >>>> on compound_statement rule would match the 'for' keyword, so the >>>> indentation will be correct, but this one will not, IIRC. I plan = to dig >>>> into this some more soon, but motivation left me a little on that = issue. >>>> Maybe we could make a preset like: >>>>=20 >>>> ``` >>>> (seq >>>> (parent-is "compound_statement") parent (parent-is "for_statement") = bol) >>>> ``` >>>>=20 >>>>=20 >>>> In other words, make other presets execute sequentially, move = point, >>>> check again, and if all are true, pick indent offset. Or allow = multiple >>>> captures, like so: >>>>=20 >>>> ``` >>>> (for_statement @offset-anchor >>>> body: (compound_statement (_) @to-indent)) >>>> ``` >>>>=20 >>>> Here the @to-indent capture would get the new indent level based on >>>> treesit-node-start of for_statement. >>>>=20 >>>> What do you think, Yuan? >>>=20 >>> I think we can just test for the grandparent, there is an >>> (undocumented) matcher n-p-gp which matches parent and grandparent. >>>=20 >>> Yuan >>=20 >> Yeah I know, but that doesn't work in every case we see this = behavior. > > I see, but at least it fixes common cases that I can think of right = now, namely if, for, while. What are some other cases? I just pushed a change (189d976dbae) that I think fixes this kind of problems. Instead of trying to figure out the right anchor, we simply count the number of {} blocks between the node at point and the root node, and use that number (multiplied by c-ts-mode-indent-offset) as the indentation. If you think about it, both for (a;b;c) { | } and for (a; b; c) { | =20 } are one block-level deep. So multi-line conditions is not an issue anymore. And int main() { if () { | } } is 2 block-level deep, etc. Yuan