You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
which is then rejected by the OCaml tree-sitter grammar.
Note that the former piece of code is not valid top-level OCaml. It does get accepted by the tree-sitter grammar, which probably is a feature so that we can highlight snippets? in which case it would make sense to also be able to format such snippets?
@Xophmeister> An opinion on this “formatting snippets” question?
The text was updated successfully, but these errors were encountered:
In general, I completely agree with you: de-contextualised snippets should be formattable, even if the result is not valid top-level code. The Tree-sitter grammar for all languages (AIUI) will always,1 however, assume it's valid top-level code and the root node of the tree will be something like program or source (in OCaml's case, it is compilation_unit).
As such, the formatting queries we write should, in principle, accommodate snippets.
However, having said that, in this case, while Tree-sitter parses your snippet, the parse tree is different to one that is correct top-level OCaml.
Parse tree for snippet:
structtypet = unitend
Parse tree for top-level version2 (highlight mine):
moduleFoo=structtypet = unitend
This is why the formatting is wrong, in this case: The snippet version gives a completely different parse tree to what's defined by the formatting queries. The type_definition node is close to what it should be, but even then that end token forces the parser to parse the RHS into something unexpected.
So, to extend my answer: the formatting queries we write should accommodate snippets, providing their parse tree doesn't change due to context. Otherwise, we'd have to potentially duplicate each set of queries for in-context and out-of-context, which would be a lot of work and may cause problematic interactions.
Footnotes
I suppose it might be possible to define multiple valid root nodes and for Tree-sitter to assign the right one. Maybe; IDK 🤷 ↩
As of 9b2de13,
gets formatted once as
which is then rejected by the OCaml tree-sitter grammar.
Note that the former piece of code is not valid top-level OCaml. It does get accepted by the tree-sitter grammar, which probably is a feature so that we can highlight snippets? in which case it would make sense to also be able to format such snippets?
@Xophmeister> An opinion on this “formatting snippets” question?
The text was updated successfully, but these errors were encountered: