From c32ce5aef2c8692cb93e6d91a7dc08af4b50c210 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. - 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 | 26 ++- .../lua-ts-mode-resources/indent.erts | 179 ++++++++++++++++++ 2 files changed, 201 insertions(+), 4 deletions(-) diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el index 030a3585158..fcab8ec610b 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,10 @@ 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) + ((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 +263,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..b0cfe11ef6f 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,136 @@ 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