emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: More Tree Sitter Questions / Problems.


From: Stefan Monnier
Subject: Re: More Tree Sitter Questions / Problems.
Date: Wed, 14 Dec 2022 18:53:08 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

> You might be misunderstanding my concern (but I do appreciate all of
> your examples and thoughts).
>
> My concern is if Tree Sitter modes deviate too much from the old way,
> they may not catch on.  Perhaps I should not worry about that.
> The old modes are not going anywhere so people can keep which ever
> they prefer.  But, that is where my worry is coming from.

Makes sense.  There's a delicate balance sometimes.  Some indentation
styles don't "make sense" if you look a them from the point of view of
an AST, yet people like them and in some cases like them enough that
they'd rather use another tool for that.

We'll need to come up with ways to accommodate them, but accommodating
them still requires "making sense of those non-sensical styles".
My examples and thoughts are just trying to help you make sense of the
case you're bumping into.

> On a slightly different topic but only slightly, I discovered that my first 
> draft also does this:
>
> if 12 * 18 +
>    45 - 19
>      frog = 12
> end
>
> Rather than this:
>
> if 12 * 18 +
>    45 - 19
>   frog = 12
> end
>
> I can change the code but I mention it because I bet others will be making 
> the same mistake.
>
> “frog” (in the first example) is indented to the bol of parent.
> The parent is “then” (not if) and the “then” is on the 2nd line, not
> the first.  So instead of indenting two spaces from the bol of the
> “if", it is indented two spaces from the bol of the “then” which is
> the 45.

I don't see a "then" keyword above, so I'll assume that the "then" is
implicit (can be taken as being represented by the newline or somesuch).

In SMIE this is usually handled so as to allow "frog" to be indented as follows:

    if 12 * 18 +
       45 - 19 then
      frog = 12
    end
or
    if 12 * 18 +
       45 - 19
    then
      frog = 12
    end
or
    if 12 * 18 +
       45 - 19
      then
        frog = 12
      end

IOW, "frog" here is *always* indented 2 columns deeper than "then".
This is true even for the first case above because in the first case
above, the *virtual* indentation of "then" is the column of "if" rather
than the column where "then" is actually placed in the file.  This is
defined by specifying in the indentation rules how "then" is (virtually)
indented when it is "hanging" (i.e. the last (and not only) token on
a line).

I've found this idea of virtual indentation to be very convenient,
arguably the most important idea behind SMIE's indentation rules.
For example, it lets me say that "fn x => ..." is virtually indented
like its parent when it parent is another "fn x =>", so that we get:

    fn x => fn y => fn z =>
      <BODY>

instead of

    fn x => fn y => fn z =>
                      <BODY>

[ In the above example I presumed that "end" is defined to be aligned
  with "then" but of course, another valid choice would be to align it
  with "if".  ]


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]