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: Sun, 24 Sep 2023 10:06:50 -0500 Message-ID: <87v8bz3905.fsf@pub.pink> References: <87fs36vvxl.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="38464"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 66159@debbugs.gnu.org To: Andrey Listopadov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Sep 24 17:08:15 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 1qkQiU-0009mA-KR for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 24 Sep 2023 17:08:15 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qkQi8-0000IE-Hs; Sun, 24 Sep 2023 11:07:52 -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 1qkQi6-0000HI-Nc for bug-gnu-emacs@gnu.org; Sun, 24 Sep 2023 11:07:50 -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 1qkQi6-0007wD-Fi for bug-gnu-emacs@gnu.org; Sun, 24 Sep 2023 11:07:50 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qkQiH-0004Ah-UI for bug-gnu-emacs@gnu.org; Sun, 24 Sep 2023 11:08:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: john muhl Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 24 Sep 2023 15:08:01 +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.169556806515936 (code B ref 66159); Sun, 24 Sep 2023 15:08:01 +0000 Original-Received: (at 66159) by debbugs.gnu.org; 24 Sep 2023 15:07:45 +0000 Original-Received: from localhost ([127.0.0.1]:43415 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkQi0-00048n-FQ for submit@debbugs.gnu.org; Sun, 24 Sep 2023 11:07:45 -0400 Original-Received: from out-210.mta1.migadu.com ([2001:41d0:203:375::d2]:57170) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qkQhv-00047T-Ts for 66159@debbugs.gnu.org; Sun, 24 Sep 2023 11:07:43 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pub.pink; s=key1; t=1695568045; 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=J6ZjyJGkPMp23nowzxfksU3+TBimmuG6hiM0sXqndBA=; b=g0Seo2+A+VM+0T0sm1v3MyWbOL8z6aP7BSIDWVWfqyytoXEHhtHftzTKoTWSC91R3ddUSG X0EWXWuS3+OMGFLvG7cm5SaiLwciGrawn0mH2ILNdTHwR85eUBnszHZYKmFybKcH3k3MJu nP0auyDXtFbRkab4x1dZolqjiiho7e0= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. In-reply-to: <87fs36vvxl.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:271252 Archived-At: --=-=-= Content-Type: text/plain Andrey Listopadov writes: > I've been editing Lua using the long-existing lua-mode[1] and it works > well enough for me, but it's indentation implementation is very slow. > Indenting a file with just 600 lines takes several seconds on my > machine. So when I saw that Emacs now features a lua-ts-mode package > in its core, I tried to move to it, in hopes that it would be faster. Thanks for trying. > However, a lot of code is now indented differently, not how I used to, > and this creates a lot of noise in commits, as I often re-indent the > whole buffer before saving the file. Here are some examples. The "then" and table examples should indent how you like with the attached patch. Arguments and parameters now also work like your table example so you can have: local function fn(a, b, c, another) end fn(1, 2, 3, {a="first", b="second"}) > Another thing that bothers me is that I prefer Gassanenko-style packing > 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 me 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: > > 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(world.width),math.random(world.height)} > end 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. > Should I expect this to be the default new way of indenting or the > indentation can be made to match the lua-mode where it is sensible? Sure. It's a new mode so nothing is really set in stone. Let me know if you have other suggestions. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Improve-indentation-in-lus-ts-mode-Bug-66159.patch >From d24d1d128fd29ead355334612ccca3f5e2e1fbd2 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 "then" with if/elseif/else/end when it appears at the beginning of a line. - 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--simple-indent-rules): Improve indentation. * test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add tests. --- lisp/progmodes/lua-ts-mode.el | 17 ++- .../lua-ts-mode-resources/indent.erts | 125 ++++++++++++++++++ 2 files changed, 139 insertions(+), 3 deletions(-) diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el index 030a3585158..3d946dd1de2 100644 --- a/lisp/progmodes/lua-ts-mode.el +++ b/lisp/progmodes/lua-ts-mode.el @@ -233,6 +233,10 @@ 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)) + (defvar lua-ts--simple-indent-rules `((lua ((parent-is "chunk") column-0 0) @@ -240,6 +244,7 @@ lua-ts--simple-indent-rules ((parent-is "block") parent-bol 0) ((node-is "}") parent-bol 0) ((node-is ")") parent-bol 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) @@ -251,9 +256,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..999e01e3e37 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 @@ -150,3 +196,82 @@ 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, +} +=-=-= -- 2.41.0 --=-=-=--