GNU bug report logs - #68493
Got to be a better way to customize c-ts-mode indentation

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Daniel Colascione <dancol@HIDDEN>; dated Mon, 15 Jan 2024 23:52:02 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.

Message received at 68493 <at> debbugs.gnu.org:


Received: (at 68493) by debbugs.gnu.org; 16 Jan 2024 12:19:35 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Tue Jan 16 07:19:35 2024
Received: from localhost ([127.0.0.1]:47974 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1rPiPn-0006Yi-EF
	for submit <at> debbugs.gnu.org; Tue, 16 Jan 2024 07:19:35 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10]:53926)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eliz@HIDDEN>) id 1rPiPl-0006YS-DW
 for 68493 <at> debbugs.gnu.org; Tue, 16 Jan 2024 07:19:34 -0500
Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@HIDDEN>)
 id 1rPiPf-00073h-4q; Tue, 16 Jan 2024 07:19:27 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=NnOcI5WL3vZw7oOuGPef5WWQWKsX3JasSVgb5qGM1z8=; b=g1qD/QCE/YBF
 pePIkaqleudsTLyF++eGJLeaBcpQHd4b06keONql6lhMClX4TSf9VU3O8h6/cjYJE+Ij/BB8yLscD
 o+e5eQWTd7z8JylxAc+et1I708CbFQBBxun6FzivRveyGUCFjlQYsWIuy4pEAE8styN8M6YKJk/4c
 YmjRzF+N+npFy+tDGFeLl8ZqoO18ijweCPVihU/sQ99+H92bu+EBGyuysBcufgAGAVA/m46iGW4eF
 BOxtfFMOFP08VJSl3O7RyQfdC4qwr9Ui5lJgR/eH64N1e3JA+O/JHeT/tnudfRrdmGpYJD7qwmJO3
 PyslgcUONK41vA4ycd+mag==;
Date: Tue, 16 Jan 2024 14:19:13 +0200
Message-Id: <83il3tcvy6.fsf@HIDDEN>
From: Eli Zaretskii <eliz@HIDDEN>
To: Daniel Colascione <dancol@HIDDEN>
In-Reply-To: <87jzoagnpe.fsf@HIDDEN> (message from Daniel Colascione on
 Mon, 15 Jan 2024 18:51:25 -0500)
Subject: Re: bug#68493: Got to be a better way to customize c-ts-mode
 indentation
References: <87jzoagnpe.fsf@HIDDEN>
X-Spam-Score: -2.3 (--)
X-Debbugs-Envelope-To: 68493
Cc: 68493 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -3.3 (---)

> From: Daniel Colascione <dancol@HIDDEN>
> Date: Mon, 15 Jan 2024 18:51:25 -0500
> 
> There's got to be a way to customize tree-sitter-based indentation
> that's cleaner than the patching I'm doing below.

Agreed.  Patches welcome.

> Wouldn't it be nice if we could use cc-mode indentation rules with
> c-ts-mode?

It would, indeed.  Of course, it isn't easy: CC Mode's indentation
rules are defined in terms of pseudo-syntactic parameters that don't
map easily (to say the least) into what a parser returns, and many of
the rules invoke CC Mode functions (as opposed to reference variables
and literals) that are specific to CC Mode and its analysis of the C
source.  Again, patches welcome.




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#68493; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 15 Jan 2024 23:51:50 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Mon Jan 15 18:51:50 2024
Received: from localhost ([127.0.0.1]:47267 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1rPWk9-0002zS-Fx
	for submit <at> debbugs.gnu.org; Mon, 15 Jan 2024 18:51:49 -0500
Received: from lists.gnu.org ([2001:470:142::17]:55036)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <dancol@HIDDEN>) id 1rPWk7-0002zE-3x
 for submit <at> debbugs.gnu.org; Mon, 15 Jan 2024 18:51:47 -0500
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 <dancol@HIDDEN>) id 1rPWk1-0008P2-2g
 for bug-gnu-emacs@HIDDEN; Mon, 15 Jan 2024 18:51:41 -0500
Received: from dancol.org ([2600:3c01:e000:3d8::1])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <dancol@HIDDEN>) id 1rPWjx-0007VK-UZ
 for bug-gnu-emacs@HIDDEN; Mon, 15 Jan 2024 18:51:40 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=dancol.org; 
 s=x;
 h=Content-Type:MIME-Version:Message-ID:Date:Subject:To:From:Sender:
 Reply-To:Cc:Content-Transfer-Encoding:Content-ID:Content-Description:
 Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:
 In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe:
 List-Post:List-Owner:List-Archive;
 bh=/vCamFzUB8Cz4FOW+ywovr5O2WOKiNE+JJMTXM5M3oU=; b=YZPC+oe64tIs4lbXk82A7Nh9Wp
 qF1kMM1upjY1Ys4B9Xt1j5fdJeEhkbtUkJnx0ceZ6WOySsViy6+2alPoBdXrfd+nNNs8NYOFOVtGE
 NnZLUHzOeF7NF7KwrGL5hvW9rI7KCyEihkV3kIvLt6N3cTUxKVDS4vAUsGK3sfRgRfWKxUjmPHaJR
 BIuGJpiKPNHG9/ZLL6fDoebtnjvcFv3DrO+J02DzVeuUSGJCOvSSxsC8Vmpb5UFK3j7IypLblRZxJ
 3+UxCEeNc/YjS8h2Wbb2Hnmmh7Q7OJtV/4YRJoeiCylVB73DokvSrzYLrkbfdYjsn9RDnBrtCqqqD
 DbtEcICw==;
Received: from 2603-9001-4203-1ab2-ac30-20a9-5618-a91d.inf6.spectrum.com
 ([2603:9001:4203:1ab2:ac30:20a9:5618:a91d]:40342 helo=localhost)
 by dancol.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
 (Exim 4.94.2) (envelope-from <dancol@HIDDEN>) id 1rPWjm-0002ef-Im
 for bug-gnu-emacs@HIDDEN; Mon, 15 Jan 2024 18:51:27 -0500
From: Daniel Colascione <dancol@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: Got to be a better way to customize c-ts-mode indentation
User-Agent: mu4e 1.11.27; emacs 30.0.50
Date: Mon, 15 Jan 2024 18:51:25 -0500
Message-ID: <87jzoagnpe.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain
Received-SPF: pass client-ip=2600:3c01:e000:3d8::1;
 envelope-from=dancol@HIDDEN; helo=dancol.org
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, SPF_HELO_PASS=-0.001,
 SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 0.9 (/)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.1 (/)


There's got to be a way to customize tree-sitter-based indentation
that's cleaner than the patching I'm doing below.

In particular, we can't just prepend custom rules to those of a base
style because doing so puts those rules ahead of high-priority rules in
the base style that might account for weird syntactic conditions.

In addition, this patching isn't exactly amenable to something checked
into .dir-locals.el or file-local variables.  It's code, not data.

I really think we need a robust, *data*-based approach to describing
indentation schemes, like cc-mode's existing system.  (Wouldn't it be
nice if we could use cc-mode indentation rules with c-ts-mode?)

Maybe we have c-ts-mode--indent-styles "compile" a passive indent schema
data structure (one suitable for dir-locals) into raw tree-sitter
indentation rules?

----------

;; Why edit the K&R rules in particular?  Because the K&R rule-set is
;; the unmolested "common" rule-set.  We want to edit the rules
;; starting from this base instead of from the other styles, which
;; prefix various things to the base rules.

(defun dancol-edit-ts-rules (orig-rules patches)
  "Edit tree-sitter indentation rules

ORIG-RULES is a tree sitter rule set as described in the ELisp
manual.  PATCHES is a sequence of PATCH lists, in which each
PATCH is (:from OLD-RULE :to NEW-RULE).  OLD-RULE is a rule in
ORIG-RULES and NEW-RULE is its replacement.  This function,
unlike just prepending rules to ORIG-RULES, preserves the
ordering of rules and minimizes the degree to which style
customization might break order-dependent matching heuristics in
ORIG-RULES.

Each PATCH is PATCHES must apply at least once or this function
will signal an error.  This way, we don't allow indentation rules
to silently bitrot as the rule set we're patches evolves.

Return the edited rule list."
  (let ((rules (cl-copy-list orig-rules)))
    (pcase-dolist (`(:from ,from :to ,to) patches)
      (unless (member from orig-rules)
        (error "rule %S not in ORIG-RULES" from))
      (setf rules
            (cl-nsubstitute to from rules :test #'equal)))
    rules))

(defun dancol-edit-k&r-rules (rules)
  (dancol-edit-ts-rules rules
    `(;; Namespaces shouldn't add indentation
      (:from
       ((parent-is "declaration_list")
        parent-bol c-ts-mode-indent-offset)
       :to
       ((parent-is "declaration_list") parent-bol 0))
      ;; Don't line up operands --- just indent on continuation: nice
      ;; and simple
      (:from
       ((parent-is "binary_expression") parent 0)
       :to
       ((parent-is "binary_expression")
        parent-bol ,(* c-ts-mode-indent-offset 2)))
      ;; Indent other expression continuations instead of lineup
      ,@(mapcar (lambda (node-name)
                  `(:from
                    ((parent-is ,node-name) first-sibling 1)
                    :to
                    ((parent-is ,node-name)
                     parent-bol ,(* c-ts-mode-indent-offset 2))))
                '("parameter_list"
                  "argument_list"
                  "parenthesized_expression"))
      ,@(mapcar (lambda (node-name)
                  `(:from
                    ((parent-is ,node-name) first-sibling 0)
                    :to
                    ((parent-is ,node-name)
                     parent-bol ,(* c-ts-mode-indent-offset 2))))
                '("concatenated_string"
                  "conditional_expression"
                  "comma_expression")))))

(defun dancol-c-ts-mode--indent-styles (base-styles)
  `((dancol ,@(dancol-edit-k&r-rules
               (cdr (or (assoc 'k&r base-styles)
                        (error "no K&R style")))))
    ,@base-styles))

(advice-add 'c-ts-mode--indent-styles
            :filter-return
            #'dancol-c-ts-mode--indent-styles)

(defun dancol-c-ts-mode-setup ()
  (c-ts-mode-set-style 'dancol))

(add-hook 'c-ts-base-mode-hook #'dancol-c-ts-mode-setup)




Acknowledgement sent to Daniel Colascione <dancol@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#68493; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Sat, 20 Jan 2024 12:30:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.