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#61502: 29.0.60; c-ts-mode auto-indent not working Date: Tue, 14 Feb 2023 21:21:33 +0100 Message-ID: <875yc40zhu.fsf@thornhill.no> References: <87v8k4g8xu.fsf@codeisgreat.org> <83a61ge69s.fsf@gnu.org> <87edqs11db.fsf@thornhill.no> <83a61gc8xx.fsf@gnu.org> Reply-To: Theodor Thornhill Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="2444"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 61502@debbugs.gnu.org, casouri@gmail.com, pankaj@codeisgreat.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Feb 14 21:22:18 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 1pS1of-0000VF-P1 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 14 Feb 2023 21:22:17 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pS1oS-0005Pw-N6; Tue, 14 Feb 2023 15:22:04 -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 1pS1oS-0005Pi-2I for bug-gnu-emacs@gnu.org; Tue, 14 Feb 2023 15:22: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 1pS1oQ-0007fK-5v for bug-gnu-emacs@gnu.org; Tue, 14 Feb 2023 15:22:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pS1oP-0002e5-Lz for bug-gnu-emacs@gnu.org; Tue, 14 Feb 2023 15:22: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: Tue, 14 Feb 2023 20:22:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 61502 X-GNU-PR-Package: emacs Original-Received: via spool by 61502-submit@debbugs.gnu.org id=B61502.167640609910135 (code B ref 61502); Tue, 14 Feb 2023 20:22:01 +0000 Original-Received: (at 61502) by debbugs.gnu.org; 14 Feb 2023 20:21:39 +0000 Original-Received: from localhost ([127.0.0.1]:57108 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pS1o3-0002dP-3Q for submit@debbugs.gnu.org; Tue, 14 Feb 2023 15:21:39 -0500 Original-Received: from out-111.mta1.migadu.com ([95.215.58.111]:36852) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pS1o0-0002dE-O8 for 61502@debbugs.gnu.org; Tue, 14 Feb 2023 15:21:38 -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=1676406095; 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=WZA+RzEgvPyvD5w+4IMWGCQ1uC+gaODhQE1i8yl7Ouw=; b=WB21VrFca0rEFrrhqfQNpqge3FJAsaptJominPmAqbaHjtuMgHN88GV8NYuTbKZsWpZnvY 6xrrFkAH21dAX7bgkCa7LPS+v0hM4+qsa34K9nuGZCFFlhWghooNlACJrab7kVZ+kHN+wA GXlCKaWORFjoHOHm1SV+XLTQlCvG85S5XAoI/U4hk6EsrOZ/b4NYX6Sv3bOYd5U6D93IUC o4MI1wQCQgZKbu3l5U5kc5ygrw2AYZgpJ0VpJgmcpBEn41YiAoribhqAytTal3GqofkUfz iRU6uJvAgRxYfFnLq3C/s88/WibUq0zyx6Y5q3Z3CmHn9nI1sSKYHRbZPjrQJA== In-Reply-To: <83a61gc8xx.fsf@gnu.org> 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:255629 Archived-At: Eli Zaretskii writes: >> From: Theodor Thornhill >> Cc: 61502@debbugs.gnu.org >> Date: Tue, 14 Feb 2023 20:41:04 +0100 >> >> Eli Zaretskii writes: >> >> > Keep typing whatever code you wan "int main" to include, and it will >> > auto-indent soon enough. >> >> Yeah, but. > > My point is that what we are used to from CC mode does not necessarily > have to work the same way with tree-sitter based modes. As long as > the indentation fixes itself soon enough, we are still fine, I think. > >> int >> main >> { >> for (;;) >> {| >> } >> ``` >> >> If you press RET if point at | you'll see we indent immediately, even >> though there is no closing bracket. This is because of how >> treesit-indent defaults to treesit-node-on when there is no node at >> point. So in the example without the for loop the parent is then set to >> whatever treesit-node-on returns, which in this case is the root >> node. That means that the rule for translation_unit is selected, which >> is: >> >> `(((parent-is "translation_unit") point-min 0) >> >> However, what's interesting here is that treesit-indent selects an >> "unexisting" node as the "smallest-node". Specifically that is: >> >> # >> >> This node in turn will return "compound_statement" if you look for its >> parent. It seems some parsers detects these nodes, so maybe we should >> add some handling for that? Some "block-closers" code in >> treesit-node-on, so that treesit-node-on doesn't default to the root >> node, but rather the compound_statement? > > AFAIU, you are talking about hitting RET in the following situation > (where "|" stands for point): > > int main () > {| > } > > However, the OP presented a slightly different situation: > > int main () > {| > > That is, without the closing brace. In that case, there's no "}" in > the source. Are you saying that the tree-sitter's parser "invents" > such a node? That's correct. In tree-sitter-c at least that's the case. > > And why does treesit-indent select that "unexisting" node in the first > place? > This code: (smallest-node (cond ((null (treesit-parser-list)) nil) ((eq 1 (length (treesit-parser-list))) (treesit-node-at bol)) ((treesit-language-at (point)) (treesit-node-at bol (treesit-language-at (point)))) (t (treesit-node-at bol)))) treesit-node-at selects the "invented" node. >> I'm not sure this explanation was easy to follow at all, but I'll add a >> hack in a diff to make the point hopefully a little clearer. >> >> What do you think? > > How well did you test that? Not well at all. I just created that hack to make the example a little clearer. I think the change probably should go into treesit-node-on. > Does it fix similar problems with struct > definition at top-level? Are there any regressions elsewhere in the > indentation? Not that I found, but I'll experiment some more. > > There are also other similar cases, but with code on deeper levels. > Try this, for example (where "|" again stands for point): > > int > main > { > for (;;)| > } > > Now press RET and observe the result: > > int > main > { > for (;;) > | > } > > instead of the expected > > int > main > { > for (;;) > | > } > > Why? If I'm not mistaken the same "problem". Treesit-node-on selects the surrounding compound_statement, so it only indents one step from column 0. > > (Of course, as soon as you type ";", the code is automatically > reindented to yield the correct indentation. Which was my point.) Yeah, but consider the same example of yours without the closing brace: ``` int main { for (;;)| ``` Now type RET ``` int main { for (;;) | ``` Now type { ``` int main { for (;;) {| ``` Now type RET ``` int main { for (;;) { | ``` Which is what I consider a little confusing. We get different indentation with and without the closed scope. Theo