vrischmann / tree-sitter-templ

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inconsistency between $.element, $.style_element and $.script_element

vrischmann opened this issue · comments

Investigating #22 and implementing #23 made me realize we have some inconsistencies between $.element, $.style_element and $.script_element.

An standard element creates these nodes:

<div></div>

(element
  (tag_start
    name: (element_identifier))
  (tag_end
    name: (element_identifier)))

a style element (normal or self-closing) creates these nodes:

<style></style>
<style />

(style_element)
(style_element)

a script element (normal or self-closing) creates these nodes:

<script></script>
<script />

(script_element)
(script_element)

We have no way to know if a style or script element is for a tag_start/tag_end tuple or a self-closing tag, and I think we should have one.

I'm thinking style_element and script_element should produce tag_start and tag_end nodes but I don't know if I can do that without introducing a conflict.

If the name needs to be unique we could use style_tag_start, style_tag_end, script_tag_start and script_tag_end. That will require an update the nvim-treesitter queries though.

I'll have to experiment.

fwiw, this plan sounds okay to me, and I can update the queries in the emacs package if necessary.

Finally got back to this.

I did some digging in https://github.com/vrischmann/tree-sitter-templ/tree/fix-inconsistencies and I can't use tag_start and tag_end, it breaks the parsing.
So the only way that works is to add:

  • style_tag_start
  • style_tag_end

That gives something like this:

(style_element
  (style_tag_start)
  (style_element_text)
  (style_tag_end))

But this doesn't give us the ability to target the node representing the "style" string in the tags because it's anonymous if written like this:

style_tag_start: $ => seq(
    '<',
    field('name', 'style'),
    repeat($._attribute),
    '>'
  ),

When using tag_start we can target the name, to do that with our new style_tag_start we need to define a new node style_element_identifier like this:

style_element_identifier: $ => /style/,

style_tag_start: $ => seq(
    '<',
    field('name', $.style_element_identifierm),
    repeat($._attribute),
    '>'
  ),

which works but I'm not sure if there's a better way to make the 'style' string non anonymous.