unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Perry Smith <pedz@easesoftware.com>
To: emacs-devel <emacs-devel@gnu.org>
Subject: Creating a paradigm for leveraging Tree Sitter's power
Date: Fri, 23 Dec 2022 19:32:13 -0600	[thread overview]
Message-ID: <948662B3-F284-4E89-BFE2-4BCB5007D4EA@easesoftware.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2818 bytes --]

I've seen on this list talk about "adding" Tree Sitter concepts to
such things as mark-defun and other existing Emacs commands.

I've just spent a few days writing rtsn-mark-method that I intend on
adding to ruby-ts-mode which implements all the features of mark-defun
but I did it from scratch.  This is mostly out of ignorance on how to
leverage existing Emacs features and facilities.

One more concrete reason is mark-defun will include the comments
before the defun.  I wanted the same for mark-method but (as far as I
can tell) the hooks back into mark-defun and its associated routines
are simple regular expressions and that, to me, walks away form the
power that Tree Sitter is providing.

I've got rtsn-mark-method working but I plan to rework it over the
next few days.  The "arg" as well as the "interactive" features of
mark-defun which I also put into rtsn-mark-method I believe can be
pulled out to a wrapper routine.  There is actually quite a few
features in the code that I did not know about.  For example, if
mark-defun is called with -1 (or -N) it marks N back.  Ok.  But now if
it is called again with no argument, mark-defun knows to go backwards
to add in the previous defun.  The same is true in the forward
direction as well.  (Although it does have a subtle bug in my opinion
but I digress.)

To repeat myself, I believe these higher level features could be
separated into a wrapper function so that all that would be needed for
the language specific piece is a routine that would be passed a point
and a direction.

I'll call this the "primitive routine".  The routine would be
responsible for returning a beginning and end (in a cons cell) and it
would be the routine's responsibility to make sure that the beginning
and end lie after (in the forward case) or before (in the backward
case) the point that is passed in.

The wrapper routine would then know how to properly adjust the mark
and point, execute counts, add to regions, catch errors, etc.

Broadening the picture: navigation, transpose, and other Emacs
commands could likewise call the same primitive routine to provide
transpose-method, forward-method, etc.  And, broadening the picture
once more: primitive routines could be written not just for methods
(defun) but also for statements, arguments, expressions, classes, etc.
In all cases, the primitive routine would be relatively simple.  It
would be given a point and return a ( begin . end ) cons cell leaving
all the harder work of expanding the region, remembering the
direction, etc to the wrapper routines.

To be clear, there would be a wrapper routine for mark, a wrapper for
forward, transpose, etc.  We would end up with the classic:

For A + B number of routines we would have A x B number of commands.

Does this strike others as a good idea or insanity?

Perry


[-- Attachment #2: Message signed with OpenPGP --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

             reply	other threads:[~2022-12-24  1:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-24  1:32 Perry Smith [this message]
2022-12-24  9:09 ` Creating a paradigm for leveraging Tree Sitter's power Yuan Fu
2022-12-24 10:07   ` Theodor Thornhill
2022-12-24 14:57     ` Perry Smith
2022-12-24 15:13       ` Theodor Thornhill
2022-12-24 14:32   ` Perry Smith
2022-12-24 16:52     ` [External] : " Drew Adams
2022-12-24 17:29       ` Perry Smith

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=948662B3-F284-4E89-BFE2-4BCB5007D4EA@easesoftware.com \
    --to=pedz@easesoftware.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).