From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.bugs Subject: bug#74386: Tree-sitter javascript indentation Date: Wed, 11 Dec 2024 21:28:58 -0800 Message-ID: References: <2ce8c98e-c399-46f1-a930-04f27a3d56dd@gutov.dev> <389b6090-6ae9-433a-85cc-a2d2eb84751f@gutov.dev> <86r06t82gl.fsf@gnu.org> <4B4B99E7-C4A1-4124-BC85-2AD66EF0871B@gmail.com> <35B61F44-C551-44DB-A334-A893991BC799@gmail.com> <8A7428E6-50EE-4783-82FF-3A62C4756C56@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3776.700.51\)) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16900"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 74386@debbugs.gnu.org, Eli Zaretskii , Theodor Thornhill , marius.kjeldahl@gmail.com To: Dmitry Gutov Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Dec 12 06:31:19 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 1tLbnC-0004Bt-IB for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 12 Dec 2024 06:31:18 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tLbmz-00028V-VO; Thu, 12 Dec 2024 00:31:06 -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 1tLbmy-00028L-DN for bug-gnu-emacs@gnu.org; Thu, 12 Dec 2024 00:31:04 -0500 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 1tLbmw-0000Rc-NF for bug-gnu-emacs@gnu.org; Thu, 12 Dec 2024 00:31:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=References:Date:In-Reply-To:From:Mime-Version:To:Subject; bh=y1muzZElkitfuyDvzTKjQ7Jo+zVBE7LLjxAviLivzOg=; b=mI2jSEDN3nXncOC22KEssoXQPahBAfblBtfwnJjnRMW6VwKKAWPioSL1KotzVwe//bSf/XCybm8MB8VMHMWoSh9ZHYs5YkEMqyGMSehq0CiCu2aJuOJ6psnBjTmvh6tBsiuOoWhbk8ow71Xu0JJ5NQhqySUVHDlP5jKPsyvv31EouK7KR7jfhfLrPUH2n4GR71Srk7uAzsrFwWlxqVG7dgtsJCcWqjV6xCnMKRUr/xZtyUBa6ialCwf9eW8zmGPHd7OIO/82xNlIIrwMoP3Z27X4AFo9soHG8jMEhRpZNATqbRV3qw6pnEQo4G9dj6YCtZzZTtUyTCJnC4zQkf0Apg==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tLbmw-0002rn-8U for bug-gnu-emacs@gnu.org; Thu, 12 Dec 2024 00:31:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Yuan Fu Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 12 Dec 2024 05:31:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 74386 X-GNU-PR-Package: emacs Original-Received: via spool by 74386-submit@debbugs.gnu.org id=B74386.173398142010895 (code B ref 74386); Thu, 12 Dec 2024 05:31:02 +0000 Original-Received: (at 74386) by debbugs.gnu.org; 12 Dec 2024 05:30:20 +0000 Original-Received: from localhost ([127.0.0.1]:37244 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tLbmF-0002pd-CW for submit@debbugs.gnu.org; Thu, 12 Dec 2024 00:30:19 -0500 Original-Received: from mail-pg1-f176.google.com ([209.85.215.176]:55559) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tLbmD-0002ir-2A for 74386@debbugs.gnu.org; Thu, 12 Dec 2024 00:30:18 -0500 Original-Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-7f46d5d1ad5so182342a12.3 for <74386@debbugs.gnu.org>; Wed, 11 Dec 2024 21:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1733981351; x=1734586151; darn=debbugs.gnu.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=y1muzZElkitfuyDvzTKjQ7Jo+zVBE7LLjxAviLivzOg=; b=P+4OCrkiNu8VFfRt7omn95nONnqTLKvuLtIXrxuEGKrTZ0FujeKuzcP/bnd0/eyccy RC0THFx8fr+JtQCdD+Vs90YS+0DsGjln/yQ8YbRT1F9CRwYgV6XdClEOPIfRbKKaqDcJ wvc6BWnJuLaVT/HwyzKRHIotDtd7cOsgGfJ6YraFqjzjPYx9muH+iAlUH0uDuuWSx1JR WnThd9BmJaaCqsTkFGBK4WG617Agk7Ng+CehkuMUBVLlzMG89n5681jSvc3obl2icWVq 39BYQogrReR0MR258hT1j6rHeJLUqrTxrfYUVcznn0DhvvlsyYCyNgkcZ0nkEnbBX9iV f8GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733981351; x=1734586151; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y1muzZElkitfuyDvzTKjQ7Jo+zVBE7LLjxAviLivzOg=; b=kWyhDLGdfyzdCP2U9DPIyGYQT7UhR21Juvf3sz2ARDojjogsTl6aUA/dDFM9kXCYD5 /G0Chwb2PaIbuhhY00cvvpSFaS20FadWTGyex/G107h3cJtfYQr6qkHddUCwuScfSJfS Woh5nOC+bEm4oTO4qUOxPs17OCpNPnsNy0dQo4BGGP5RvvbTXQ5Y/yW0xc+q+QrCMOwq TQ8CjGVwn046lNP4D2stW33Md340cCgHhKLU6MWqPitl7QCSpeo7uoT/bBDf0tXADIXA 5fnN9klJYZ6ESEO8qA+PvVQgV7opZs5Y3QN3H51bXXN7JLOOs/CAwdnhZnrH4GJ/J7nN 6Fqw== X-Forwarded-Encrypted: i=1; AJvYcCWPF4UeALgEM8otH2meoTz9K0gYTfmFoJbXtXQyRpgY9snHueTJTOkqedI/RGVaGU0t+PhawQ==@debbugs.gnu.org X-Gm-Message-State: AOJu0Yx635GjWo3kU6Ne1qykQN22oqnAVkzeAyn55tITqQqVYM+FOtqI 5aEjn+aR9qox1TBDaRxnG99MmlepteRRc03FypbohJXohkuhrMMD X-Gm-Gg: ASbGncv82PV+Y2IjU3gzG3cKI+Vy/yE+4HfDGhxW8j/3c+RgBEbRtWJhlIMcVcCSXN9 T53YT0QGiTdUvrjfO1SKPYxU1l7CkbRPWQkKwAnSJ7Z+l0wKt6583VDCTAn7w9IDH3Z6wS3k/MZ VAEBDRz5FTLaE4pE1lt0OtwYOjGNMnnpZayUX5PL/lrLFjk3/LRgk7sJDWQoqdvOxCcRbF5KoFX x2zZ9/kpdS1E4P0Pm2WiToPr5Q6n8QkL3WMPvp/rUQrVTfUi1eU4k9WzTE6wY9XIUEBZdLusKRk Q2zH X-Google-Smtp-Source: AGHT+IHDGeUWw+1+WBRtT0NmrXh31qhtp2mSRxNQKhOsMS97lliKbS4JK/Ts3KTEKwzo3kgk1SSZnQ== X-Received: by 2002:a05:6a21:9988:b0:1e1:ae9a:6311 with SMTP id adf61e73a8af0-1e1cea5705cmr2954392637.4.1733981351073; Wed, 11 Dec 2024 21:29:11 -0800 (PST) Original-Received: from smtpclient.apple ([2601:646:8f81:6120:e40f:e83b:f0f0:1e5e]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7fd55eb21e9sm5021428a12.84.2024.12.11.21.29.09 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 11 Dec 2024 21:29:10 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3776.700.51) 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:296885 Archived-At: > On Dec 11, 2024, at 7:20=E2=80=AFPM, Dmitry Gutov = wrote: >=20 > On 11/12/2024 08:18, Yuan Fu wrote: >>>> Maybe not by hardcoding this in inside the 'standalone-parent' = matcher, but writing this in the indentation rules? Different languages = might have differing ASTs for such construct. >>>>=20 >>>> Or if you meant to do a text search, a period might start a method = call, but it could also continue a "range" literal in some other = language, or some struct initializer (I think?) in C/C++. Also, some = languages allow (and style guides suggest) to have the previous at the = end of the line, then followed by newline and then the method name. >>> Not hard-coded, but customizable, like this: >>>=20 >>> Yuan >>>=20 >>> >> Circling back on this. WDYT? I think this would benefit all = =E2=80=9Cmodern=E2=80=9D languages with chaining method calls. >=20 > It's an interesting suggestion - and the docstring is very readable. >=20 > I'd be great to see how it works with some existing modes' indentation = code - e.g. to try to rewrite any of the rules in ruby-ts-mode (do we = have any other ts mode with as many options affecting indentation?). >=20 > OT2H the use of 'standalone-parent' is optional, and it's combined = with a matcher anyway, so it's good even if it covers like 80% of the = cases. >=20 > What would be our next step in this? Replacing all 'parent-bol' = anchors with 'standalone-parent' across most ts modes? Speaking of next step, I recently added another handy tool for languages = with C-like syntax: c-ts-common-baseline-indent-rule. I figured out an = indent logic that can work on all C-like languages and covers a wide = range of cases. This one rule can give you all theses indentation: 1. Statements align to their previous sibling: int main() { int a =3D 1; int b =3D 2; <-- Align to prev line=E2=80=99s sibling. } 2. Indents one level for blocks: function, if, for, struct, etc. int main() { return 0; <-- Indent one level. { <-- Align to prev line=E2=80=99s sibling. return 1; <-- Indent one level. } } 3. Elements in parenthesis and brackets: return [1, 2, 3, 4, 5, 6]; <-- Align to first sibling. return [ 1, 2, 3, <-- Indent one level (option 1). 4, 5, 6, <-- Align to prev line=E2=80=99s sibling. ]; return [ 1, 2, 3, <-- Align to opening bracket (option 2). 4, 5, 6, <-- Align to prev line=E2=80=99s sibling. ]; <-- Align to opening bracket. for (int i =3D 0; i < 10; <-- Align to first sibling. i++) { <-- Align to prev line=E2=80=99s sibling. continue; } 4. Statement expressions indent one level when it=E2=80=99s broken into = two lines: int main() { int var =3D 1287; <-- Indent one level. int var =3D 1287; <-- Indent one level. } Then a C-like language=E2=80=99s major mode only need to add special = cases over the baseline indent rule. And if we add the configurable = heuristic for standalone-parent, the baseline indent rules would make = use of it. I brought it up because if we=E2=80=99re going to do some renovations to = indent rules, might as well make use of = c-ts-common-baseline-indent-rule, and we probably don=E2=80=99t even = need to replace parent-box with standalone-parent, because the baseline = indent rule would cover most cases.=20 I=E2=80=99ve already used it to rewrite c-ts-mode indent rules and = it=E2=80=99s been a success; this baseline + override approach has been = very helpful. c-ts-mode still has a lot of indent rules because of = things like preproc directive, etc, but it=E2=80=99s much more = manageable than before. I don=E2=80=99t know how much it would help modes that has simpler = indent rules. Go-ts-mode and rust-ts-mode only has a handful of indent = rules, maybe they don=E2=80=99t really need this baseline rule. OTOH Lua = and Ruby has more involved indent rules, maybe they can benefit and = reduce the number of rules they need to define. Yuan=