From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.devel Subject: Re: Update on tree-sitter structure navigation Date: Sat, 02 Sep 2023 06:52:41 +0000 Message-ID: <87h6odhxs6.fsf@localhost> References: <5E7F2A94-4377-45C0-8541-7F59F3B54BA1@gmail.com> Mime-Version: 1.0 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="26215"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel , Danny Freeman , Theodor Thornhill , Jostein =?utf-8?Q?Kj=C3=B8nigsen?= , Randy Taylor , Wilhelm Kirschbaum , Perry Smith , Dmitry Gutov To: Yuan Fu Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Sep 02 08:53:19 2023 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 1qcKVS-0006aX-IK for ged-emacs-devel@m.gmane-mx.org; Sat, 02 Sep 2023 08:53:18 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qcKUX-0003Ab-OH; Sat, 02 Sep 2023 02:52:21 -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 1qcKUU-0003AC-Hn for emacs-devel@gnu.org; Sat, 02 Sep 2023 02:52:19 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qcKUR-0005nu-6q for emacs-devel@gnu.org; Sat, 02 Sep 2023 02:52:18 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout02.posteo.de (Postfix) with ESMTPS id 1EB8A240107 for ; Sat, 2 Sep 2023 08:52:06 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1693637527; bh=JKnQXVa9u9XW2EZWVbLWO2w/UEtemVBSULLIDEN6qz0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version: Content-Transfer-Encoding:From; b=mX75v/6DXkh60DV0Tf23qhLuHSGe8i/WAvhKccslJFCxF5sG/uYABkAWweQv5nfN8 8+UPYqISxPnBtB7rMF7cUwFrdVWVcKohTQrhve7sLZeSCjOjhBw47Uxtw2pTZgJGYz DHpGDpjWT1dn//n74tBanSC0PbVI4wPmASOcyyFV/jYPL1WyL+9fj3utOGneXqwDav HmO/CA3LXE2qOBIYdzOPbNZwnUa/U8W46RP9/bqtVJlDghCK95r3BrzSbaOrdN1CXS 8ug+6ytSMmstQUUJdmldvwDYA1gs9k0xrt6fOI5Gx+PZnnC7tDEuGfaBYmLcq54uFZ 7jwe349q254Iw== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Rd5DP30Mkz9rxG; Sat, 2 Sep 2023 08:52:05 +0200 (CEST) In-Reply-To: <5E7F2A94-4377-45C0-8541-7F59F3B54BA1@gmail.com> Received-SPF: pass client-ip=185.67.36.66; envelope-from=yantar92@posteo.net; helo=mout02.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, 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:309851 Archived-At: Yuan Fu writes: > In the months after wrapping up tree-sitter stuff in emacs-29, I was > thinking about how to implement structural navigation and extracting > information from the parser with tree-sitter. In emacs-29 we have > things like treesit-beginning/end-of-defun, and treesit-defun-name. I > was thinking maybe we can generalize this to support getting arbitrary > =E2=80=9Cthing=E2=80=9D at point, move around them, and getting informati= on like the > name of a defun, its arglist, parent of a class, type of an variable > declaration, etc, in a language-agnostic way. Note that Org mode also does all of these using https://orgmode.org/worg/dev/org-element-api.html It would be nice if we could converge to more consistent interface across all the modes. For example, by extending `thing-at-point' to handle parsed elements, not just simplistic regexp-based "thing" boundaries exposed by `thing-at-point' now. Org approaches getting name/begin/end/arguments using a common API: (org-element-property :begin NODE) (org-element-property :end NODE) (org-element-property :contents-begin NODE) (org-element-property :contents-end NODE) (org-element-property :name NODE) (org-element-property :args NODE) Language-agnostic "thing"s will certainly be welcome, especially given that tree-sitter grammars use inconsistent naming schemes, which have to be learned separately, and may even change with grammar versions. I think that both NODE types and attributes can be standardized. > Also, at the time, we only support defining things by a regexp > matching a node=E2=80=99s type, which is often not enough. > > And it would be nice to somehow take advantage of the tree-sitter > queries for the features I mentioned above. Tree-sitter query is what > every other editor are using for virtually all tree-sitter related > features. But in Emacs, we mostly only use it for font-lock. I recall one user asking about something like VIM's textobjects via tree-sitter queries. Example: https://github.com/nvim-treesitter/nvim-treesitter-textobjects/blob/master/= queries/cpp/textobjects.scm > Here=E2=80=99s the progress as of now: > > - Functions like treesit-search-forward, treesit-induce-sparse-tree, > treesit-thing-at-point, treesit--navigate-thing, etc, support a richer > set of predicates now. Besides regexp matching the type, the predicate > can also be a predication function, or (REGEP . FUNC), or compound > predicates like (or PRED PRED) or (not PRED). Slightly unrelated, but do you have any idea if it can be faster to use Emacs' regexp search combined with treesit-thing-at-point vs. pure tree-sitter query? > - There=E2=80=99s now a variable treesit-thing-settings, which holds > definition for things. Then, instead of passing the predicate to the > functions I mentioned above, you can save the predicate in > treesit-thing-settings under a symbol, say =E2=80=98sexp', and pass the s= ymbol > instead, just like thing-at-point.el. (We=E2=80=99ll work on integrating = with > thing-at-point.el later.) This sounds similar to textobjects I linked above. One question: how will it integrate with multiple parsers in one buffer? > - I can=E2=80=99t think of a good way to integrate tree-sitter queries wi= th > the navigation functions we have right now. Most importantly, > tree-sitter query always search top-down, and you can=E2=80=99t limit the > depth it searches. OTOH, our navigation functions work by traversing > the tree node-to-node. May you elaborate about the difficulties you encountered? > Some other things on the TODO list that people can take a jab at: > > - Solve the grammar versioning/breaking-change problem: tree-sitter gramm= ar don=E2=80=99t have a version number, so every time the author changes th= e grammar, our queries break, and loading the mode only produces a giant er= ror. May we somehow get a hash of the library? That way, we can at least detect if something has changed. > - Major mode fallback/inheritance, this has been discussed many times, no= good solution emerged. I think that integration of tree-sitter with navigation functions might be a step towards solving this problem. If common Emacs commands can automatically choose between tree-sitter and classic implementations, it might become easier to unify foo-ts-mode with foo-mode. > - Isolated ranges. For many embedded languages, each blocks should be ind= ependent from another, but currently all the embedded blocks are connected = together and parsed by a single parser. We probably need to spawn a parser = for each block. I=E2=80=99ll probably work on this one next. Do you mean that a single parser sees subsequent block as a continuation of the previous? --=20 Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at