From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: john muhl Newsgroups: gmane.emacs.bugs Subject: bug#70785: [PATCH] Improve indentation in 'lua-ts-mode' Date: Sun, 05 May 2024 08:26:44 -0500 Message-ID: <87msp4mlpo.fsf@pub.pink> References: <87zft4mnbn.fsf@pub.pink> Mime-Version: 1.0 Content-Type: text/x-patch Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15094"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.10.8; emacs 30.0.50 To: 70785@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 05 15:30:04 2024 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 1s3bwJ-0003dc-K6 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 05 May 2024 15:30:04 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1s3bvy-0001sD-P2; Sun, 05 May 2024 09:29:42 -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 1s3bvw-0001rl-RU for bug-gnu-emacs@gnu.org; Sun, 05 May 2024 09:29:41 -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 1s3bvv-0007V7-6d for bug-gnu-emacs@gnu.org; Sun, 05 May 2024 09:29:39 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1s3bwI-0005G0-Np for bug-gnu-emacs@gnu.org; Sun, 05 May 2024 09:30: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: Sun, 05 May 2024 13:30:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 70785 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 70785-submit@debbugs.gnu.org id=B70785.171491576620174 (code B ref 70785); Sun, 05 May 2024 13:30:02 +0000 Original-Received: (at 70785) by debbugs.gnu.org; 5 May 2024 13:29:26 +0000 Original-Received: from localhost ([127.0.0.1]:59752 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3bvh-0005FK-3J for submit@debbugs.gnu.org; Sun, 05 May 2024 09:29:26 -0400 Original-Received: from flow6-smtp.messagingengine.com ([103.168.172.141]:56817) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1s3bve-0005FC-5n for 70785@debbugs.gnu.org; Sun, 05 May 2024 09:29:23 -0400 Original-Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailflow.nyi.internal (Postfix) with ESMTP id 290D22003E3 for <70785@debbugs.gnu.org>; Sun, 5 May 2024 09:28:53 -0400 (EDT) Original-Received: from mailfrontend1 ([10.202.2.162]) by compute1.internal (MEProxy); Sun, 05 May 2024 09:28:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pub.pink; h=cc :content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm3; t=1714915733; x=1714922933; bh=OaI03ZJMoO 3sEPUxk21NwqSgfO2tIee73on871MPYfY=; b=eXCgIak/sV9WTWM8qdsH+39ckX hxeH/wZHDYJkTlZy8nW5Mqf5WI7S6VfsIsPF7UIcElqjYpAe59lOZ2DUo87x8Zvr fdbQOcjKb/ea5kIXMeLJYQZXZdzzfNLfiWO0CZpG1RxYfMmY6ADgvKlgX4lqrHjY mhisDYOV/UlATbF3zrW2seXj6DjcWcyRQBuxMl2/NT7DX8zYokQC0OJ9nJOEMKAl mWNYGSh8TBqpaL3SyKyas7IrQtdYZfOsuARs9+BBYMhD2dsmnyjjHGzHLwdUSwjB 7+z385NopyK448MC3IKP4B+yfsy9gzbnehQFYTuiNT3WNlTdt+QnkoPQIYYg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm3; t=1714915733; x=1714922933; bh=OaI03ZJMoO3sEPUxk21NwqSgfO2t Iee73on871MPYfY=; b=ked0Hv3FkkKeTgwoCWgos/7JkWTcEVX+3sGcIVyrtAq0 uMjV2v8fAX0beg2+Td5VFejF6XgTywjU935v+DVx92yuAiVn+iAWzRz9VrQ2ingN vRmPxilQSlf+OooF6qnAe3k76Yu5r31TkyDNSs8BzRmzDhZdPYQ7snWzl+Wsq+4W WmXXiXG71QGRBtDmL5zvC7suhkoOeUxQUIj3GFVQtKl3BsMBWtyiWGf20h+2zvp6 bGRKrfYE38VjD8tLyEDfpQdJstM83Ei8dsZvbafEtYPzgwUmGBD5VbPd+7igSlv2 //3vLg/tssvA/xm8iKl19i0EsIHPmk0nSQLLS/RZNw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrvddvgedgieejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucgovehorghsthgrlhdqffegtdefqddtheculdeftd dtmdenucfjughrpehffgfhvffuffgjkfggtgguseertdertddtredtnecuhfhrohhmpehj ohhhnhcumhhuhhhluceojhhmsehpuhgsrdhpihhnkheqnecuggftrfgrthhtvghrnhepge ehiedvgeekhedugedtveefffeihedtgeehfeetieejvdeffefgffehffehheejnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepjhhmsehpuhgsrd hpihhnkh X-ME-Proxy: Feedback-ID: i74194916:Fastmail Original-Received: by mail.messagingengine.com (Postfix) with ESMTPA for <70785@debbugs.gnu.org>; Sun, 5 May 2024 09:28:52 -0400 (EDT) In-reply-to: <87zft4mnbn.fsf@pub.pink> Content-Disposition: attachment; filename=0001-Improve-indentation-in-lua-ts-mode-bug-70785.patch 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:284497 Archived-At: >From 4d3408607fbf560f5427fbe6193307b16a82ceff Mon Sep 17 00:00:00 2001 From: john muhl Date: Fri, 3 May 2024 15:51:01 -0500 Subject: [PATCH] Improve indentation in 'lua-ts-mode' (bug#70785) * lisp/progmodes/lua-ts-mode.el (lua-ts--simple-indent-rules): - Ignore comments when aligning arguments, parameters and fields. - Apply simpler rules to simpler usage of anonymous functions. - Better handling of table as a function argument. (lua-ts--comment-first-sibling-matcher): (lua-ts--first-real-sibling-anchor): (lua-ts--last-arg-function-call-matcher): (lua-ts--top-level-function-call-matcher): New function. (lua-ts--g-parent): (lua-ts--g-g-parent): New function. (lua-ts--g-g-g-parent): Use it. * test/lisp/progmodes/lua-ts-mode-resources/indent.erts: Add tests. --- lisp/progmodes/lua-ts-mode.el | 63 +++++++++++++++++-- .../lua-ts-mode-resources/indent.erts | 40 ++++++++++++ 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/lisp/progmodes/lua-ts-mode.el b/lisp/progmodes/lua-ts-mode.el index 6b55fefbf84..57aa3db1c85 100644 --- a/lisp/progmodes/lua-ts-mode.el +++ b/lisp/progmodes/lua-ts-mode.el @@ -291,6 +291,14 @@ lua-ts--simple-indent-rules (parent-is "string_content") (node-is "]]")) no-indent 0) + ((and (n-p-gp "field" "table_constructor" "arguments") + lua-ts--multi-arg-function-call-matcher + lua-ts--last-arg-function-call-matcher) + standalone-parent lua-ts-indent-offset) + ((and (n-p-gp "}" "table_constructor" "arguments") + lua-ts--multi-arg-function-call-matcher + lua-ts--last-arg-function-call-matcher) + standalone-parent 0) ((and (n-p-gp "field" "table_constructor" "arguments") lua-ts--multi-arg-function-call-matcher) parent lua-ts-indent-offset) @@ -311,10 +319,15 @@ lua-ts--simple-indent-rules (and (parent-is "parameters") lua-ts--first-child-matcher) (and (parent-is "table_constructor") lua-ts--first-child-matcher)) standalone-parent lua-ts-indent-offset) + ((and (not lua-ts--comment-first-sibling-matcher) + (or (parent-is "arguments") + (parent-is "parameters") + (parent-is "table_constructor"))) + lua-ts--first-real-sibling-anchor 0) ((or (parent-is "arguments") (parent-is "parameters") (parent-is "table_constructor")) - (nth-sibling 1) 0) + standalone-parent lua-ts-indent-offset) ((and (n-p-gp "block" "function_definition" "parenthesized_expression") lua-ts--nested-function-block-matcher lua-ts--nested-function-block-include-matcher) @@ -337,6 +350,9 @@ lua-ts--simple-indent-rules lua-ts--nested-function-end-matcher lua-ts--nested-function-last-function-matcher) parent 0) + ((and (n-p-gp "end" "function_definition" "arguments") + lua-ts--top-level-function-call-matcher) + standalone-parent 0) ((n-p-gp "end" "function_definition" "arguments") parent 0) ((or (match "end" "function_definition") (node-is "end")) @@ -385,24 +401,39 @@ lua-ts--function-definition-p "Return t if NODE is a function_definition." (equal "function_definition" (treesit-node-type node))) +(defun lua-ts--g-parent (node) + "Return the grand-parent of NODE." + (let ((parent (treesit-node-parent node))) + (treesit-node-parent parent))) + +(defun lua-ts--g-g-parent (node) + "Return the great-grand-parent of NODE." + (treesit-node-parent (lua-ts--g-parent node))) + (defun lua-ts--g-g-g-parent (node) "Return the great-great-grand-parent of NODE." - (let* ((parent (treesit-node-parent node)) - (g-parent (treesit-node-parent parent)) - (g-g-parent (treesit-node-parent g-parent))) - (treesit-node-parent g-g-parent))) + (treesit-node-parent (lua-ts--g-g-parent node))) (defun lua-ts--multi-arg-function-call-matcher (_n parent &rest _) "Matches if PARENT has multiple arguments." (> (treesit-node-child-count (treesit-node-parent parent)) 3)) +(defun lua-ts--last-arg-function-call-matcher (node parent &rest _) + "Matches if NODE's PARENT is the last argument in a function call." + (let* ((g-parent (lua-ts--g-parent node)) + (last (1- (treesit-node-child-count g-parent t)))) + (treesit-node-eq parent (seq-elt (treesit-node-children g-parent t) last)))) + (defun lua-ts--nested-function-argument-matcher (node &rest _) "Matches if NODE is in a nested function argument." (save-excursion (goto-char (treesit-node-start node)) (treesit-beginning-of-defun) (backward-char 2) - (not (looking-at ")(")))) + (and (not (looking-at ")(")) + (not (equal "chunk" + (treesit-node-type + (lua-ts--g-parent (treesit-node-at (point))))))))) (defun lua-ts--nested-function-block-matcher (node &rest _) "Matches if NODE is in a nested function block." @@ -438,6 +469,26 @@ lua-ts--nested-function-last-function-matcher (treesit-induce-sparse-tree parent #'lua-ts--function-definition-p))) (= 1 (length (cadr sparse-tree))))) +(defun lua-ts--comment-first-sibling-matcher (node &rest _) + "Matches if NODE if it's previous sibling is a comment." + (let ((sibling (treesit-node-prev-sibling node))) + (equal "comment" (treesit-node-type sibling)))) + +(defun lua-ts--top-level-function-call-matcher (node &rest _) + "Matches if NODE is within a top-level function call." + (let* ((g-g-p (lua-ts--g-g-parent node)) + (g-g-g-p (lua-ts--g-g-g-parent node))) + (and (equal "function_call" (treesit-node-type g-g-p)) + (equal "chunk" (treesit-node-type g-g-g-p))))) + +(defun lua-ts--first-real-sibling-anchor (_n parent _) + "Return the start position of the first non-comment child of PARENT." + (treesit-node-start + (seq-first + (seq-filter + (lambda (n) (not (equal "comment" (treesit-node-type n)))) + (treesit-node-children parent t))))) + (defun lua-ts--variable-declaration-continuation (node &rest _) "Matches if NODE is part of a multi-line variable declaration." (treesit-parent-until node diff --git a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts index 48184160b4d..ba7bad1b452 100644 --- a/test/lisp/progmodes/lua-ts-mode-resources/indent.erts +++ b/test/lisp/progmodes/lua-ts-mode-resources/indent.erts @@ -66,6 +66,10 @@ end return f end +f6(function() +print'ok' +end) + ;(function () return true end)() @@ -118,6 +122,10 @@ function f6(...) return f end +f6(function() + print'ok' +end) + ;(function () return true end)() @@ -406,6 +414,15 @@ a = 1, b = 2, }, nil) + +Test(nil, { + a = 1, + b = 2, + }) + +fn( -- comment + 1, + 2) =-= h( "string", @@ -443,6 +460,15 @@ Test({ b = 2, }, nil) + +Test(nil, { + a = 1, + b = 2, +}) + +fn( -- comment + 1, + 2) =-=-= Name: Parameter Indent @@ -464,6 +490,9 @@ local f3 = function( a, b, c, d ) print(a,b,c,d) end + +local f4 = function(-- comment +a, b, c) =-= function f1( a, @@ -481,6 +510,9 @@ local f3 = function( a, b, c, d ) print(a,b,c,d) end + +local f4 = function(-- comment + a, b, c) =-=-= Name: Table Indent @@ -506,6 +538,10 @@ a = 1, b = 2, c = 3, } + +local a = { -- hello world! + b = 10 +} =-= local Other = { First={up={Step=true,Jump=true}, @@ -527,6 +563,10 @@ local Other = { b = 2, c = 3, } + +local a = { -- hello world! + b = 10 +} =-=-= Name: Continuation Indent -- 2.41.0