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#61558: 29.0.60; Indentation with c-ts-mode doesn't work in code guarded by #ifdef..#endif Date: Sat, 18 Feb 2023 22:30:06 +0100 Message-ID: <878rgu64rl.fsf@thornhill.no> References: <835yc12v7a.fsf@gnu.org> <87cz68p03u.fsf@thornhill.no> <83zg9cytni.fsf@gnu.org> <877cwgoyws.fsf@thornhill.no> <83ilfzz4cy.fsf@gnu.org> <87bklq65m2.fsf@thornhill.no> 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="3472"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 61558@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Feb 18 22:31:26 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 1pTUnl-0000jE-J1 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Feb 2023 22:31:25 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pTUnS-00048C-K4; Sat, 18 Feb 2023 16:31:06 -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 1pTUnP-00047F-4A for bug-gnu-emacs@gnu.org; Sat, 18 Feb 2023 16:31:03 -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 1pTUnO-0000Nn-N9 for bug-gnu-emacs@gnu.org; Sat, 18 Feb 2023 16:31:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pTUnO-0002sb-B2 for bug-gnu-emacs@gnu.org; Sat, 18 Feb 2023 16:31:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Theodor Thornhill Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Feb 2023 21:31:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61558 X-GNU-PR-Package: emacs Original-Received: via spool by 61558-submit@debbugs.gnu.org id=B61558.167675581211001 (code B ref 61558); Sat, 18 Feb 2023 21:31:02 +0000 Original-Received: (at 61558) by debbugs.gnu.org; 18 Feb 2023 21:30:12 +0000 Original-Received: from localhost ([127.0.0.1]:45090 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pTUmZ-0002rM-HX for submit@debbugs.gnu.org; Sat, 18 Feb 2023 16:30:12 -0500 Original-Received: from out0.migadu.com ([94.23.1.103]:30617) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pTUmW-0002qo-EC for 61558@debbugs.gnu.org; Sat, 18 Feb 2023 16:30:09 -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=1676755807; 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=vYrRx/WxKDHk8hrSala50ZuyYhaIrxh3Q8J2bb1cbvQ=; b=FyYN+bBBqIqXTbtHu1NDQ68r++SNuhsJcPEkgkcJO3uN+tSTfkCqhpQvcd6TvktYc2JITw UUI4XBQaWlLzWPmOu/HtenNrhol/m2qQzePZoZtPOAbEaoqqTDS6oyeTH36WTQHDvCfep7 6iHFv7CuLoWZfGvZuvL3BBPcnKlSslOZDnFahaaWHIl5IcCrPhzrqkGLFpayuewfszUY/2 OhGXznxIhesF3SLvBsBiyiYZNocrkYJxndurNZDjQKv1jScMgTsASpLLrM6IL2cf4MGcCh 4lNKF1wnXUiWqVZzVd89Nwg4Yp4+dXzw5MjCMH6W843U6oP6Khn8zbFcKR7z4g== In-Reply-To: <87bklq65m2.fsf@thornhill.no> X-Migadu-Flow: FLOW_OUT 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:255996 Archived-At: --=-=-= Content-Type: text/plain Theodor Thornhill writes: > Eli Zaretskii writes: > >>> From: Theodor Thornhill >>> Cc: 61558@debbugs.gnu.org >>> Date: Fri, 17 Feb 2023 20:49:23 +0100 >>> >>> Can you test this patch? It seems to work for me, but I'm no C expert. >> >> Thanks, this is much better. However, the handling of #if (as opposed >> to #ifdef) is still incorrect. For example, go to the end of line >> 3376 of dispnew.c and type RET: point goes to column zero, not column >> 2. If you convert "#if" to #ifdef", the behavior becomes correct. >> Same problem exists with #elif. >> >> Doesn't tree-sitter grammar consider #if and #elif preprocessor nodes? > > It seems we are at the mercy of this[0] issue here, in all the cases > you've described. > >> >> Also, there's a problem with #define in the middle of code. For >> example, here: >> >> /* Put a `display' property on the string for the captions to display, >> put a `menu_item' property on tab-bar items with a value that >> is the index of the item in F's tab-bar item vector. */ >> for (i = 0; i < f->n_tab_bar_items; ++i) >> { >> #define PROP(IDX) \ >> AREF (f->tab_bar_items, i * TAB_BAR_ITEM_NSLOTS + (IDX)) >> >> caption = Fcopy_sequence (PROP (TAB_BAR_ITEM_CAPTION)); <<<<<<<<<< >> >> /* Put a `display' text property on the string for the caption to >> display. Put a `menu-item' property on the string that gives >> the start of this item's properties in the tab-bar items >> vector. */ >> AUTO_LIST2 (props, Qmenu_item, make_fixnum (i * TAB_BAR_ITEM_NSLOTS)); >> >> Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)), >> props, caption); >> >> f->desired_tab_bar_string = >> concat2 (f->desired_tab_bar_string, caption); >> >> #undef PROP <<<<<<<<<<<<<<<<<<<< >> } >> >> Typing RET at the end of the two marked lines goes to column zero >> instead of the expected non-zero column. So it sounds like #define >> and #undef are also not handled correctly yet. > > Yeah, luckily it indents correctly after you start to type. I'll try to > see if I can make some specific handling for this. > > Theo > Scratch that. Can you test this for me? I think I got it. Theo --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Cleanup-preproc-indent-for-c-ts-mode.patch >From ba9fc0542351506e2271a5edd47f00feb2163245 Mon Sep 17 00:00:00 2001 From: Theodor Thornhill Date: Fri, 17 Feb 2023 20:46:19 +0100 Subject: [PATCH] Cleanup preproc indent for c-ts-mode * lisp/progmodes/c-ts-mode.el (c-ts-mode--indent-styles): Make sure we indent to grand-parent if inside an #ifdef...#endif block. If grandparent is root node, then don't indent one step. --- lisp/progmodes/c-ts-mode.el | 11 ++++++----- lisp/treesit.el | 5 +++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el index 05875e9267..9c28fc4ad4 100644 --- a/lisp/progmodes/c-ts-mode.el +++ b/lisp/progmodes/c-ts-mode.el @@ -265,13 +265,14 @@ c-ts-mode--indent-styles ((match nil "do_statement" "body") parent-bol c-ts-mode-indent-offset) ((match nil "for_statement" "body") parent-bol c-ts-mode-indent-offset) - ((match "preproc_ifdef" "compound_statement") point-min 0) - ((match "#endif" "preproc_ifdef") point-min 0) - ((match "preproc_if" "compound_statement") point-min 0) - ((match "#endif" "preproc_if") point-min 0) - ((match "preproc_function_def" "compound_statement") point-min 0) + ((node-is "preproc") point-min 0) + ((node-is "#endif") point-min 0) ((match "preproc_call" "compound_statement") point-min 0) + ((n-p-gp nil "preproc" "translation_unit") point-min 0) + ((n-p-gp nil "\n" "preproc") great-grand-parent c-ts-mode-indent-offset) + ((parent-is "preproc") grand-parent c-ts-mode-indent-offset) + ((parent-is "function_definition") parent-bol 0) ((parent-is "conditional_expression") first-sibling 0) ((parent-is "assignment_expression") parent-bol c-ts-mode-indent-offset) diff --git a/lisp/treesit.el b/lisp/treesit.el index 09531b838a..be864d8f1f 100644 --- a/lisp/treesit.el +++ b/lisp/treesit.el @@ -1204,6 +1204,11 @@ treesit-simple-indent-presets (cons 'grand-parent (lambda (_n parent &rest _) (treesit-node-start (treesit-node-parent parent)))) + (cons 'great-grand-parent + (lambda (_n parent &rest _) + (treesit-node-start + (treesit-node-parent + (treesit-node-parent parent))))) (cons 'parent-bol (lambda (_n parent &rest _) (save-excursion (goto-char (treesit-node-start parent)) -- 2.34.1 --=-=-= Content-Type: text/plain > > [0]: https://github.com/tree-sitter/tree-sitter-c/issues/97 --=-=-=--