From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Theodor Thornhill via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#60983: 29.0.60; Tree-sitter user-level control Date: Wed, 25 Jan 2023 21:12:53 +0100 Message-ID: <87lelq8jay.fsf@thornhill.no> References: <83tu0kkuqo.fsf@gnu.org> <87tu0k3y6t.fsf@thornhill.no> <83mt6ckqts.fsf@gnu.org> <87zgacgiwe.fsf@thornhill.no> <834jshhvjh.fsf@gnu.org> <806A8560-A675-423F-9CFC-67D50D14E304@thornhill.no> <833580ipdo.fsf@gnu.org> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="9657"; mail-complaints-to="usenet@ciao.gmane.io" Cc: casouri@gmail.com, 60983@debbugs.gnu.org To: Eli Zaretskii , Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Jan 25 21:14:33 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 1pKmAD-0002MU-1k for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 25 Jan 2023 21:14:33 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pKm9o-0002yA-9N; Wed, 25 Jan 2023 15:14:08 -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 1pKm9j-0002xj-6t for bug-gnu-emacs@gnu.org; Wed, 25 Jan 2023 15:14:04 -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 1pKm9i-0006lG-Fr for bug-gnu-emacs@gnu.org; Wed, 25 Jan 2023 15:14:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pKm9h-0004VD-RD for bug-gnu-emacs@gnu.org; Wed, 25 Jan 2023 15:14:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 25 Jan 2023 20:14:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 60983 X-GNU-PR-Package: emacs X-Debbugs-Original-Cc: casouri@gmail.com, bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.167467759617222 (code B ref -1); Wed, 25 Jan 2023 20:14:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 25 Jan 2023 20:13:16 +0000 Original-Received: from localhost ([127.0.0.1]:60037 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pKm8y-0004Th-8c for submit@debbugs.gnu.org; Wed, 25 Jan 2023 15:13:16 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:38652) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pKm8s-0004TU-Pv for submit@debbugs.gnu.org; Wed, 25 Jan 2023 15:13:14 -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 1pKm8o-0002tu-ES for bug-gnu-emacs@gnu.org; Wed, 25 Jan 2023 15:13:06 -0500 Original-Received: from out-213.mta0.migadu.com ([91.218.175.213]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pKm8l-0006bl-Qd for bug-gnu-emacs@gnu.org; Wed, 25 Jan 2023 15:13:06 -0500 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thornhill.no; s=key1; t=1674677575; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=kRp9xwVL5EeKdJ7aSSJEb+Hcb+OT0ZmBRg7XGgW4J/U=; b=HgF1QPjfeyOVhPmL3TE1evNKE2Z9ESKJTQ0b7BMyeNtpJTdZgGv6BZqDmbWG6u3JlNYSDW jbQZrlOofTRJpnLTyQj1SvvpT3uz0a0I1vAreUniymfHw6RZP3bwbTL0i+iwKRadpbnKs9 zfcRyYDWEGku62CuCeglSFJOuj4McdeBN3KJ3NyZyhsyjiVsl4Ksqam+v5efbPnd3RGvYM V5/omOKxWQHWQBQtTwEQ76T0aAaBMoJ1IkXZ4RyDMKx0tpk4nSZSur+lb0ChNtCpIpgxqd oRSzOPWkXXwMa19VaDMqHOpRyn5ew8e7711rcfh5rMyOIvw7YzNAzcL0Qg8Rig== In-Reply-To: <833580ipdo.fsf@gnu.org> X-Migadu-Flow: FLOW_OUT Received-SPF: pass client-ip=91.218.175.213; envelope-from=theo@thornhill.no; helo=out-213.mta0.migadu.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, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:254151 Archived-At: --=-=-= Content-Type: text/plain Eli Zaretskii writes: >> Date: Mon, 23 Jan 2023 22:08:27 +0100 >> From: Theodor Thornhill >> CC: bug-gnu-emacs@gnu.org >> >> >> >> On 23 January 2023 20:59:14 CET, Eli Zaretskii wrote: >> >> From: Yuan Fu >> >> Date: Mon, 23 Jan 2023 11:37:24 -0800 >> >> Cc: Eli Zaretskii , >> >> Bug Report Emacs >> >> >> >> Sorry for the delay, overall I agree with your changes. >> > >> >Thanks. What about the questions I asked regarding indentation >> >features, and specifically about c-ts-mode-indent-style? >> >> I am working on that, but I hit some issues where I cannot make treesit recognize the new settings before the whole treesit-major-mode-setup reruns. Just setting the symbol doesn't work, and reenabling the mode inside of the :set function isn't the best idea maybe? >> >> I'd love some pointers to how other modes do similar stuff, but I didn't really find anything. > > Thanks, but can you add some details of what you are trying to do and > what are the difficulties? > > Adding Stefan in case he has some advice. Ok, I added a patch below. try: 1. open some c buffer and make some edits, for example ``` void main() { if (x) { } } ``` This should be the expected output when using the gnu style. 2. M-x c-ts-mode-set-style "bsd" RET 3. C-h o c-ts-mode-indent-style RET Observe variable has changed, but indenting the code does not. 4 C-h o treesit-simple-indent-rules RET Observe variable keeps old value 5. C-x x g Now the bsd style takes effect, and the treesit-simple-indent-rules variable has changed. I'm sure the fix is easy, but I don't see it. I purposely kept the functions simple until I know what approach is best:) Thanks, Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Initial-c-ts-mode-set-style-attempt.patch >From 2a4948bb516435d11245256931eeb9cc0f562aba Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Wed, 25 Jan 2023 21:04:00 +0100 Subject: [PATCH] Initial c-ts-mode-set-style attempt --- lisp/progmodes/c-ts-mode.el | 45 +++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 95f9001e0d..35f076b28d 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -92,7 +92,7 @@ c-ts-mode-indent-offset :safe 'integerp :group 'c) -(defcustom c-ts-mode-indent-style 'gnu +(defcustom c-ts-mode-indent-style "gnu" "Style used for indentation. The selected style could be one of GNU, K&R, LINUX or BSD. If @@ -100,13 +100,33 @@ c-ts-mode-indent-style set instead. This function is expected return a list that follows the form of `treesit-simple-indent-rules'." :version "29.1" - :type '(choice (symbol :tag "Gnu" 'gnu) - (symbol :tag "K&R" 'k&r) - (symbol :tag "Linux" 'linux) - (symbol :tag "BSD" 'bsd) + :type '(choice (string :tag "Gnu" "gnu") + (string :tag "K&R" "k&r") + (string :tag "Linux" "linux") + (string :tag "BSD" "bsd") (function :tag "A function for user customized style" ignore)) + :set #'c-ts-mode--indent-style :group 'c) +(defun c-ts-mode--indent-style (sym val) + "Custom setter for `c-ts-mode-indent-style'." + (set-default sym val)) + +(defun c-ts-mode-set-style () + (interactive) + (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))) + (if-let ((mode (cond ((eq major-mode 'c-ts-mode) 'c) + ((eq major-mode 'c++-ts-mode) 'cpp) + (t nil))) + (choice (completing-read "Select style: " '("gnu" "k&r" "linux" "bsd")))) + (c-ts-mode--indent-style 'c-ts-mode-indent-style choice) + (kill-local-variable 'treesit-simple-indent-rules) + (setq-local treesit-simple-indent-rules + (treesit--indent-rules-optimize + (c-ts-mode--set-indent-style mode))))) + ;;; Syntax table (defvar c-ts-mode--syntax-table @@ -224,19 +244,19 @@ c-ts-mode--indent-styles ((parent-is "do_statement") parent-bol c-ts-mode-indent-offset) ,@(when (eq mode 'cpp) `(((node-is "field_initializer_list") parent-bol ,(* c-ts-mode-indent-offset 2))))))) - `((gnu + `(("gnu" ;; Prepend rules to set highest priority ((match "while" "do_statement") parent 0) (c-ts-mode--top-level-label-matcher point-min 1) ,@common) - (k&r ,@common) - (linux + ("k&r" ,@common) + ("linux" ;; Reference: ;; https://www.kernel.org/doc/html/latest/process/coding-style.html, ;; and script/Lindent in Linux kernel repository. ((node-is "labeled_statement") point-min 0) ,@common) - (bsd + ("bsd" ((node-is "}") parent-bol 0) ((node-is "labeled_statement") parent-bol c-ts-mode-indent-offset) ((parent-is "labeled_statement") parent-bol c-ts-mode-indent-offset) @@ -255,11 +275,8 @@ c-ts-mode--set-indent-style (let ((style (if (functionp c-ts-mode-indent-style) (funcall c-ts-mode-indent-style) - (pcase c-ts-mode-indent-style - ('gnu (alist-get 'gnu (c-ts-mode--indent-styles mode))) - ('k&r (alist-get 'k&r (c-ts-mode--indent-styles mode))) - ('bsd (alist-get 'bsd (c-ts-mode--indent-styles mode))) - ('linux (alist-get 'linux (c-ts-mode--indent-styles mode))))))) + (alist-get c-ts-mode-indent-style + (c-ts-mode--indent-styles mode) nil nil #'equal)))) `((,mode ,@style)))) (defun c-ts-mode--top-level-label-matcher (node &rest _) -- 2.34.1 --=-=-=--