tree-sitter / tree-sitter-c

C grammar for tree-sitter
MIT License
225 stars 100 forks source link

Distinguish between line and block comments #174

Open nacl opened 10 months ago

nacl commented 10 months ago

The current grammar does not distinguish between line comment and block comments. Supporting this would make it more convenient for editors to see in what type of comment they are in, and then make formatting decisions without examining the code themselves.

FWIW, the Java grammar (which has the same comment syntax as C and C++), does this already. Something similar could easily be done here (there's already a skeleton for it). See also https://github.com/tree-sitter/tree-sitter-java/issues/92

savetheclocktower commented 8 months ago

There is a strong convention across lots of parsers to use one comment node for all kinds of comments. Use cases that need to distinguish between comment types can use queries and #match? predicates.

You're right thattree-sitter-java does things differently, but it's the exception, and I'm honestly pretty surprised that that change got landed. (A later proposal to distinguish JavaDoc comments was closed under the “use queries instead” rationale.)

I think it's worth having a philosophical discussion about. I'd be just as happy to have different line_comment and block_comment types for all languages, but I'd much rather have a uniform pattern across all parsers.