tree-sitter-grammars / tree-sitter-markdown

Markdown grammar for tree-sitter
MIT License
375 stars 45 forks source link

Inline nodes don't return parent #74

Closed sarmong closed 8 months ago

sarmong commented 1 year ago

Describe the bug

When I am trying to get a parent of node with type inline I get nil. I am using neovim method tsnode:parent(). It works for all nodes but inline ones.

I am not sure though if the bug relates to this parser.

Code example

### Test Heading

- test list

Expected behavior

When you get a parent node of an inline element (text test list and Test Heading) you get paragraph and atx_heading nodes respectively

Actual behavior

You get nil

MDeiml commented 1 year ago

It does relate to the parser. The inline nodes are special, they work kind of like the script node in html, where the contents are parsed as javascript. In this grammar, the content of the inline nodes in the top level grammar are parsed as "inline markdown". This means, that there is one tree for every inline node, and the tsnode in your code is the root node of that tree.

I'm not sure what the most efficient way to get the "parent" is in neovim, i.e. the node that the "inline tree" is attached to. I guess if you have access to the top level markdown tree (let's call it tree) you could do

tree:named_node_for_range(node:range())
sarmong commented 1 year ago

Thanks, this worked, though I am also not sure how efficient this is.

However, what is the reason for having separate trees? I don't think I understand.

MDeiml commented 1 year ago

Markdown does not have a syntax that's nice to parse, wince it's optimized for being readable to humans instead. That's why it's suggested to parse it in two passes. With tree sitter that's not really possible, but the double tree structure is my way of working around it.

MDeiml commented 8 months ago

Closing this as the problem seems to be solved.