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.devel Subject: New defun navigation for tree-sitter (Was: Code navigation for sh-mode with Tree-sitter) Date: Mon, 12 Dec 2022 21:20:17 -0800 Message-ID: <5BB6E79A-B1FA-40F4-B48A-DADF63A30DDC@gmail.com> References: <1B9F9B3A-A757-4A65-9653-CD0112EB8895@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) 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="9633"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Stefan Monnier , Alan Mackenzie , emacs-devel@gnu.org To: =?utf-8?Q?Jo=C3=A3o_Paulo_Labegalini_de_Carvalho?= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Dec 13 06:21:13 2022 Return-path: Envelope-to: ged-emacs-devel@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 1p4xj7-0002Hh-Ni for ged-emacs-devel@m.gmane-mx.org; Tue, 13 Dec 2022 06:21:13 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p4xiQ-0001Fr-Uq; Tue, 13 Dec 2022 00:20:30 -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 1p4xiM-0001FF-2l for emacs-devel@gnu.org; Tue, 13 Dec 2022 00:20:26 -0500 Original-Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p4xiK-0007V2-Fi for emacs-devel@gnu.org; Tue, 13 Dec 2022 00:20:25 -0500 Original-Received: by mail-pg1-x529.google.com with SMTP id 142so9704773pga.1 for ; Mon, 12 Dec 2022 21:20:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; 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=5rUXph4Wb4Ew5CO0Mvduk9CIVSxewjZCCr3XX8XXzqM=; b=qLR9XpHCo4eDEeGESx4rcKeaFIe0/YvMN7dghJsSrgcJ5G+DG1cOlMiAcYA5/+RLTr pDH+zWv2KhMvjq4zDreQYahm0P5AKv7o2OfeAWHzFyr9JS9v44xtcwF+M+4RFZJbIx8x S8WNQEen6HdCWWarHiG6y901Aao0DkuvECrcS/HBhES5/AtO7Gykz/1NYx+pvzt1M9dq lL9Z6HSpEpcZTOvnZrfZ++BIP3kBm26ZfOus8IzjRymQzqip2hh7sII1mkJ1SJ1mRoht T1wc695swBvJx5VDspM6d3/eYM6mbFDjGtBUWRw+CanDufmqM0my9M43Fz1hZrcOflhI a0OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=5rUXph4Wb4Ew5CO0Mvduk9CIVSxewjZCCr3XX8XXzqM=; b=1QdcXq/uYN+vhRxwZalAKs2baH1GF4E2vDvez3VyxlgTT9T28oWycykfsghFQGDOeL Xak3EeXeDe0uxzakTtmAnvjcuaU8VdkmrDOBPHy44QG7wmuvoROtc1P5gnoRquhrEuTV izSnlOYknDFkc8AqX1+bSPLIrfJrdXNf4I0ukQ5T7FyJkC/efqoXT5rHlZ4N9fBo/mCW braUK+QSoyvx7PxRlfMv1TqhoSTnx5feVhKlbfG/V6xF0VpslXCxz+UlWfo2CCK2aG1T ftM3pBU3IrUDxVyZ2zohrGV9OKuQzPZp+wYVi1hZcwu/uboFerQrztFhFVyyrei1aNDx nq7Q== X-Gm-Message-State: ANoB5pmC8PmxyKfarL8LN1ZLc8WuWIQBu5SmkF+toOgw3Anf2gLik9hF e9TwSpi+rosLceOfoY0I5gw= X-Google-Smtp-Source: AA0mqf6vbbi3RFpJm43DjN37nS8c7E/3vKHx9LKzqORduNgJt6xkAc3PFAuh8ozwybEcgWFR4nWvVg== X-Received: by 2002:aa7:8252:0:b0:574:c62c:2f55 with SMTP id e18-20020aa78252000000b00574c62c2f55mr18033117pfn.3.1670908822572; Mon, 12 Dec 2022 21:20:22 -0800 (PST) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id b133-20020a621b8b000000b00574e4fe6118sm6679356pfb.162.2022.12.12.21.20.20 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Dec 2022 21:20:22 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=casouri@gmail.com; helo=mail-pg1-x529.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:301299 Archived-At: Thanks to Alan and Jo=C3=A3o, I have revamped the defun navigation of = tree-sitter, now major modes can get accurate defun navigation by = setting treesit-defun-type-regexp (as before), and switch between = top-level-only mode and nested defun mode by treesit-defun-tactic (new). The change I pushed doesn=E2=80=99t replace the old functions, my plan = is to replace the existing ones if we think this change is ok for the = release branch. After experimenting and implementing this, I don=E2=80=99t= think the current beginning/end-of-defun can support nested navigation = without large changes. So I=E2=80=99d prefer we use new beg/end-of-defun = commands in tree-sitter major modes. And we later think about merging = the two. I also added comprehensive tests to the defun navigation[1], which = should give us more peace in mind. Although this is a large change, I = hope it can stay on the release branch because (1) it fixes current bugs = (2) it has a comprehensive test. Yuan [1] For tests, see=20 treesit-defun-navigation-nested-1 treesit-defun-navigation-nested-2 treesit-defun-navigation-nested-3 treesit-defun-navigation-top-level It tests all possible defun navigations starting from all possible = starting positions (that I can think of), so it should be pretty = comprehensive. I currently have test for python, js, and bash. New tests = can be easily added. The test program is like the following, we mark positions with markers = like [100] [100] [101]class Class1(): [999] prop =3D 0 [102] [103]class Class2():[0] [104] [1]def method1(): [999] [2]return 0[3] [105] [4] [106] [5]def method2(): [999] [6]return 0[7] [107] [8] [999] prop =3D 0[9] [108] [109]class Class3(): [999] prop =3D 0[10] [110] Then we have a list of positions like this, basically saying =E2=80=9Cif = point is at marker X, and we find the next/previous beginning/end of = defun, point should end up at Y=E2=80=9D, and we have X for all the = possible positions in a nested defun, and Y for all four operations. So = the first line says =E2=80=9Cif we start at marker 0, and find = prev-beg-of-defun, we should end up at marker 103=E2=80=9D. ;; START PREV-BEG NEXT-END PREV-END NEXT-BEG '((0 103 105 102 106) ; Between Beg of parent & 1st sibling. (1 103 105 102 106) ; Beg of 1st sibling. (2 104 105 102 106) ; Inside 1st sibling. (3 104 107 102 109) ; End of 1st sibling. (4 104 107 102 109) ; Between 1st sibling & 2nd sibling. (5 104 107 102 109) ; Beg of 2nd sibling. (6 106 107 105 109) ; Inside 2nd sibling. (7 106 108 105 109) ; End of 2nd sibling. (8 106 108 105 109) ; Between 2nd sibling & end of parent. (9 103 110 102 nil) ; End of parent. (100 nil 102 nil 103) ; Before 1st parent. (101 nil 102 nil 103) ; Beg of 1st parent. (102 101 108 nil 109) ; Between 1st & 2nd parent. (103 101 108 nil 109) ; Beg of 2nd parent. (110 109 nil 108 nil) ; After 3rd parent. )