From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: john muhl via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#66159: 30.0.50; lua-ts-mode semantic indentation problems Date: Tue, 26 Sep 2023 20:18:07 -0500 Message-ID: <87fs302oz3.fsf@pub.pink> References: <87fs36vvxl.fsf@gmail.com> <87v8bz3905.fsf@pub.pink> <878r8vwm9v.fsf@gmail.com> <87il7z5g3g.fsf@pub.pink> <87lecs4ttm.fsf@gmail.com> Reply-To: john muhl 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="17647"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 66159@debbugs.gnu.org, Philip Kaludercic , Eli Zaretskii To: Andrey Listopadov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 27 06:58:17 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 1qlMcq-0004PZ-Hv for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 27 Sep 2023 06:58:16 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qlMcQ-0002oL-G8; Wed, 27 Sep 2023 00:57:50 -0400 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 1qlMcP-0002o7-GL for bug-gnu-emacs@gnu.org; Wed, 27 Sep 2023 00:57:49 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qlMcP-0000kK-8Z for bug-gnu-emacs@gnu.org; Wed, 27 Sep 2023 00:57:49 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qlMcc-0001sq-BO for bug-gnu-emacs@gnu.org; Wed, 27 Sep 2023 00:58:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: john muhl Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 27 Sep 2023 04:58:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 66159 X-GNU-PR-Package: emacs Original-Received: via spool by 66159-submit@debbugs.gnu.org id=B66159.16957906337176 (code B ref 66159); Wed, 27 Sep 2023 04:58:02 +0000 Original-Received: (at 66159) by debbugs.gnu.org; 27 Sep 2023 04:57:13 +0000 Original-Received: from localhost ([127.0.0.1]:50244 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qlMbo-0001rf-4V for submit@debbugs.gnu.org; Wed, 27 Sep 2023 00:57:12 -0400 Original-Received: from out-195.mta0.migadu.com ([2001:41d0:1004:224b::c3]:14719) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qlMbj-0001rU-UX for 66159@debbugs.gnu.org; Wed, 27 Sep 2023 00:57:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pub.pink; s=key1; t=1695790613; 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=mlX/4EiKUHhGyWKayE2vQb4GkFjuy0bxT2sqUAcUCjY=; b=Ij9oMr+xTqHUUdfy2epJtM4BnXtyEUQsEyPvTEIH35Rk4r0aXTZDfpjBez9d6P2HgsC6ge DTb3Wm4oY8L8UhDhGdNBVfDX/OomlAiCNM3MXej6U7fiR5/mxuVwAvykhhiG6zPO1Sdi6L yF6WV6fM9poAdGzMcho3BiTdQBpGtfc= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <87lecs4ttm.fsf@gmail.com> 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:271408 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Andrey Listopadov writes: > john muhl writes: > >> Eli Zaretskii writes: >> >>>> > Another thing that bothers me is that I prefer Gassanenko-style pack= ing >>>> > of `end' keywords so that they vertically align with the scope of the >>>> > opened block, as it saves so much vertical space and is easier for m= e to >>>> > read, but lua-ts-mode moves it to the latest innermost indentation >>>> > level, as opposed to the outermost depending on the count of ends in= the >>>> > line itself: >>>> >>>> I don't see any reason not to support that style but I'm not sure how = to >>>> do it. A patch would be welcome but I'll try to figure it out sometime. >>> >>> Maybe introduce indentation styles into lua-ts-mode, like CC Mode and >>> c-ts-mode have? >> >> I=E2=80=99ll have a look at what the c-ts-mode styles do and see what mi= ght be >> applicable. In this case the changes can be accommodated by default. > > In my opinion, this isn't distinct enough to introduce a new style. > But it's up to you to decide, of course - I'm all for better editing > experience for Lua! I agree there=E2=80=99s no immediate need. Could you explain what the lua-indent-* options do in lua-mode? I tried toggling them but didn=E2=80=99t see any difference between on/off. >>> A far as I understand it, in the `lua-mode' the overall line indentation >>> is computed via subtracting indentation for every `end' in that line, >>> e.g. `end end end' subtracts `lua-indent-level three times from current >>> indent level. >> >> Thanks for the explanation. The attached patch should make end packing >> work now. > > I've tried your latest patch and indeed `end' now pack themselves as in > the lua-mode. Thank you very much! > >>>> Sure. It's a new mode so nothing is really set in stone. Let me know if >>>> you have other suggestions. > > I noticed that the `do' keyword is indented similarly to `then' before > the patch when put on the new line: > > for i=3D1,10 > do > print(i) > end > > I'm not sure if that's a proper way to indent it or not though, but `do' > usually signifies start of the scope, so perhaps it shouldn't be > indented in this case. Fixed in the attached. > There are also some weirdness in semantic navigation, but it's more > likely that I'm just not used to new ts-backed navigation yet. I=E2=80=99m sure there is room for improvement here too. Suggestions welcom= e. > Thanks for your work on lua-ts-mode, it's much more snappy editing > experience now! Glad to hear it. > If you're willing to dig into some (pretty crazy) involved examples, I > can send here some really convoluted nested anonymous functions that > currently are indented in a weird way in both modes. Neither does it > exactly right in my opiion, but I also don't know if there is the right > way to indent this. I can send these examples later this week once I > finish an article I'm working on rightnow. Sure. Whenever you have the time. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Improve-indentation-in-lus-ts-mode-Bug-66159.patch >From 5d41635442d2c1032d015d90527bc5bf6113d291 Mon Sep 17 00:00:00 2001 From: john muhl Date: Sat, 23 Sep 2023 10:49:11 -0500 Subject: [PATCH] Improve indentation in lus-ts-mode (Bug#66159) - Align lines starting with "then" or "do" to the same level as the corresponding "if" or "for" statement. - Align a single-line of "end"s with the beginning of the outermost block being closed. - Anchor indent of the first child in arguments, parameters and tables to the parent and the other children to the first. * lisp/progmodes/lua-ts-mode.el (lua-ts--first-child) (lua-ts--end-indent-offset) (lua-ts--simple-indent-rules): Improve indentation. * test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add tests. --- lisp/progmodes/lua-ts-mode.el | 27 ++- .../lua-ts-mode-resources/indent.erts | 199 ++++++++++++++++++ 2 files changed, 222 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el index 030a3585158..418d53e0578 100644 --- a/lisp/progmodes/lua-ts-mode.el +++ b/lisp/progmodes/lua-ts-mode.el @@ -233,6 +233,17 @@ lua-ts--font-lock-settings '((ERROR) @font-lock-warning-face)) "Tree-sitter font-lock settings for `lua-ts-mode'.") +(defun lua-ts--first-child (node _p _b &rest _) + "Matches if NODE is the first among its siblings." + (= (treesit-node-index node) 1)) + +(defun lua-ts--end-indent-offset (_n _p _b &rest _) + "Calculate indent offset based on `end' count." + (let* ((beg (line-beginning-position)) + (end (line-end-position)) + (count (count-matches "end" beg end))) + (- (* (1- count) lua-ts-indent-offset)))) + (defvar lua-ts--simple-indent-rules `((lua ((parent-is "chunk") column-0 0) @@ -240,9 +251,11 @@ lua-ts--simple-indent-rules ((parent-is "block") parent-bol 0) ((node-is "}") parent-bol 0) ((node-is ")") parent-bol 0) + ((node-is "do") standalone-parent 0) + ((node-is "then") standalone-parent 0) ((node-is "else_statement") parent-bol 0) ((node-is "elseif_statement") parent-bol 0) - ((node-is "end") parent-bol 0) + ((node-is "end") parent-bol lua-ts--end-indent-offset) ((node-is "until") parent-bol 0) ((parent-is "for_statement") parent-bol lua-ts-indent-offset) ((parent-is "function_declaration") parent-bol lua-ts-indent-offset) @@ -251,9 +264,15 @@ lua-ts--simple-indent-rules ((parent-is "else_statement") parent-bol lua-ts-indent-offset) ((parent-is "repeat_statement") parent-bol lua-ts-indent-offset) ((parent-is "while_statement") parent-bol lua-ts-indent-offset) - ((parent-is "table_constructor") parent-bol lua-ts-indent-offset) - ((parent-is "arguments") parent-bol lua-ts-indent-offset) - ((parent-is "parameters") parent-bol lua-ts-indent-offset) + ((and (parent-is "table_constructor") lua-ts--first-child) + parent-bol lua-ts-indent-offset) + ((parent-is "table_constructor") (nth-sibling 1) 0) + ((and (parent-is "arguments") lua-ts--first-child) + parent-bol lua-ts-indent-offset) + ((parent-is "arguments") (nth-sibling 1) 0) + ((and (parent-is "parameters") lua-ts--first-child) + parent-bol lua-ts-indent-offset) + ((parent-is "parameters") (nth-sibling 1) 0) ((parent-is "ERROR") no-indent 0)))) (defvar lua-ts--syntax-table diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts index 040225c8580..a36a933e640 100644 --- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts @@ -32,6 +32,22 @@ f({ ;(function() return false )() + +function foo (e) + if e == nil + then return 1000 + else return e + end +end + +function foo (e) + if e == nil + then + return 1000 + else + return e + end +end =-= print( 0, @@ -57,6 +73,22 @@ f({ ;(function() return false )() + +function foo (e) + if e == nil + then return 1000 + else return e + end +end + +function foo (e) + if e == nil + then + return 1000 + else + return e + end +end =-=-= Name: Argument Indent @@ -77,6 +109,13 @@ cost = 2, length = 8, parallelism = 4, }) + +fn(1, +2, + 3) + +fn(1, 2, +3) =-= function h( string, @@ -93,6 +132,13 @@ local p = h( length = 8, parallelism = 4, }) + +fn(1, + 2, + 3) + +fn(1, 2, + 3) =-=-= Name: Continuation Indent @@ -130,10 +176,20 @@ for k, v in pairs({}) do print(k, v) end +for i=1,10 + do + print(i) +end + while n < 10 do n = n + 1 end +while n < 10 + do + n = n + 1 +end + repeat z = z * 2 until z > 12 @@ -142,11 +198,154 @@ for k, v in pairs({}) do print(k, v) end +for i=1,10 +do + print(i) +end + while n < 10 do n = n + 1 end +while n < 10 +do + n = n + 1 +end + repeat z = z * 2 until z > 12 =-=-= + +Name: Parameter Indent + +=-= +fn(a, +b) end + +fn(a, b, +c) end + +fn( +a, +b +) end +=-= +fn(a, + b) end + +fn(a, b, + c) end + +fn( + a, + b +) end +=-=-= + +Code: + (lambda () + (setq indent-tabs-mode nil) + (setq lua-ts-indent-offset 4) + (lua-ts-mode) + (indent-region (point-min) (point-max))) + +Name: Table Indent + +=-= +local Recipe = { + Floor={up={Floor=true,Wall=true}, + down={Floor=true,Wall=true}, + left={Floor=true,Wall=true}, + right={Floor=true,Wall=true}}, + Wall={up={Floor=true,Wall=true}, + down={Floor=true,Wall=true}, + left={Floor=true,Wall=true}, + right={Floor=true,Wall=true}}, + Corridor={up={Corridoor=true}, + down={Corridoor=true}, + left={Corridoor=true}, + right={Corridoor=true}} +} + +local Other = { +a = 1, + b = 2, + c = 3, +} +=-= +local Recipe = { + Floor={up={Floor=true,Wall=true}, + down={Floor=true,Wall=true}, + left={Floor=true,Wall=true}, + right={Floor=true,Wall=true}}, + Wall={up={Floor=true,Wall=true}, + down={Floor=true,Wall=true}, + left={Floor=true,Wall=true}, + right={Floor=true,Wall=true}}, + Corridor={up={Corridoor=true}, + down={Corridoor=true}, + left={Corridoor=true}, + right={Corridoor=true}} +} + +local Other = { + a = 1, + b = 2, + c = 3, +} +=-=-= + +Name: Single Line End + +=-= +function lowest_entropy_cell(world) + local lowest,res=math.huge,nil + for y=1,world.height do + for x=1,world.width do + local cell=world:get(x,y) + if cell.is_set then + local e=cell_enthropy(cell) + trace(e) + if e <= lowest then + lowest,res=e,{x,y} + end end end end + return res or {math.random(w.w),math.random(w.h)} +end + +for y=1,world.height do + for x=1,world.width do + local cell=world:get(x,y) + if cell.is_set then + local e=cell_enthropy(cell) + trace(e) + if e <= lowest then + lowest,res=e,{x,y} + end + end end end +=-= +function lowest_entropy_cell(world) + local lowest,res=math.huge,nil + for y=1,world.height do + for x=1,world.width do + local cell=world:get(x,y) + if cell.is_set then + local e=cell_enthropy(cell) + trace(e) + if e <= lowest then + lowest,res=e,{x,y} + end end end end + return res or {math.random(w.w),math.random(w.h)} +end + +for y=1,world.height do + for x=1,world.width do + local cell=world:get(x,y) + if cell.is_set then + local e=cell_enthropy(cell) + trace(e) + if e <= lowest then + lowest,res=e,{x,y} + end +end end end +=-=-= -- 2.41.0 --=-=-=--