tree-sitter-grammars / tree-sitter-svelte

Svelte grammar for tree-sitter
MIT License
11 stars 7 forks source link

feature: ability to target @component comments #12

Open AlbertMarashi opened 2 months ago

AlbertMarashi commented 2 months ago

Did you check the tree-sitter docs?

Is your feature request related to a problem? Please describe.

Currently, there exists no way to distinguish the special @component markdown comment for markdown injections/highlighting.

Describe the solution you'd like

Would like the ability to highlight <!-- @component this is a markdown component documentation comment used by language servers -->

Additional context

AlbertMarashi commented 2 months ago

@savetheclocktower interested in giving this a shot?

savetheclocktower commented 2 months ago

Couldn't you do something like this?

((comment) @component-comment
  (#match? @component-comment "^<!--\\s*@component")
)
AlbertMarashi commented 2 months ago

Couldn't you do something like this?

((comment) @component-comment
  (#match? @component-comment "^<!--\\s*@component")
)

Unfortunately I don't think we have a way to get the raw text of the comment (ie I think it will end up injecting the <!-- and --> parts)

savetheclocktower commented 2 months ago

There's lots of precedent against having “special” types of comments in the parser. For instance, tree-sitter-java assigns both /* Foo */ and /** Foo */ a node type of comment, even though the latter is the convention of JavaDoc and the former is an ordinary comment.

You'll probably think it ridiculous, but one answer here would be to write a very small Tree-sitter grammar that takes such a comment and separates it into <!--, @component, (everything else), and -->. You could then use it as the injection language for the comment in my example, and inject Markdown into the (everything else) block in turn.

AlbertMarashi commented 2 months ago

@savetheclocktower in JS, JSDoc comments are parsed as JSDocs, as well as with rust the doc comments have their own unique nodes

image

image

savetheclocktower commented 2 months ago

@savetheclocktower in JS, JSDoc comments are parsed as JSDocs, as well as with rust the doc comments have their own unique nodes

I'm sure you're right about Rust, but JSDoc comments in tree-sitter-javascript do not have a special kind of node. They're just injected into with a #match? predicate, as I proposed above.

AlbertMarashi commented 2 months ago

@savetheclocktower in JS, JSDoc comments are parsed as JSDocs, as well as with rust the doc comments have their own unique nodes

I'm sure you're right about Rust, but JSDoc comments in tree-sitter-javascript do not have a special kind of node. They're just injected into with a #match? predicate, as I proposed above.

You're right.

However, unlike Markdown, JSDoc expects the /** aspects of the comment I think.

I think we need a way to target the comment contents within the open/close tags

savetheclocktower commented 2 months ago

I think we need a way to target the comment contents within the open/close tags

Right, that's what I was saying here:

You'll probably think it ridiculous, but one answer here would be to write a very small Tree-sitter grammar that takes such a comment and separates it into <!--, @component, (everything else), and -->. You could then use it as the injection language for the comment in my example, and inject Markdown into the (everything else) block in turn.

If you've never written a Tree-sitter grammar from scratch before, it'd be a very good candidate.

AlbertMarashi commented 2 months ago

oh i'm pretty familiar with writing tree-sitter grammars at this point, was just curious if you wanted to implement it

savetheclocktower commented 2 months ago

I wouldn't hate doing it, but it'd probably be a while before I got around to it.