tree-sitter / tree-sitter-scala

Scala grammar for tree-sitter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`end` clause capture/grouping

KaranAhlawat opened this issue · comments

Commit of tree-sitter-scala you tested this on

236c7b2

A code sample showing the error

object C:
  given C =
    new C:
      def f = "!"
      end f
    end new
  end given
end C

Show the error node

(object_definition object body: (identifier)
  (template_body :
   (given_definition given return_type: (type_identifier) =
    body: 
     (instance_expression new arguments: (type_identifier)
      (template_body :
       (function_definition def name: (identifier) = body: (string))
       end _end_ident)))
   end new end given))
 end _end_ident

What do you expect the tree to look like

There is no ERROR node, but I strongly believe that the end clause of a given node should be captured as a child of that node (or some similar construct).
Particularly, looking at the second last line of the output, we can see that end new and end given are siblings. Meanwhile, the code suggests that new block is a child of the given block.
The improper grouping of the end clauses makes it so that a lot more of the parse tree has to be examined to find the proper indentation for that end clause.

For instance, you have to check if you're not already correctly indented by checking if the previous sibling is a matching block to the end clause. But since multiple nested end clauses are just represented as sibling nodes, you have to walk through all the previous children, and then you go one level up and so on.

Where are you experiencing this error?

treesit (GNU Emacs 29+)

Thank you for reporting the issue @KaranAhlawat!

Digging a bit more into it, it seems like nesting isn't the actual problem, as in some places that does work fine (nesting vals, within an object etc).
So, starting with looking at the given_definition and instance_expression might be a viable option.