tree-sitter-grammars / tree-sitter-markdown

Markdown grammar for tree-sitter
MIT License
411 stars 52 forks source link

Expose delimiters for `uri_autolink` #109

Open musjj opened 1 year ago

musjj commented 1 year ago

I want to make a query to conceal the delimiters for uri_autolink (the angle brackets in <https://www.google.com>), but the delimiters are not exposed in the tree.

For example for emphasis markers, you have this tree:

emphasis [12, 2] - [12, 8]
  emphasis_delimiter [12, 2] - [12, 3]
  emphasis_delimiter [12, 7] - [12, 8]

Can we have something similar for uri_autolink?

clason commented 1 year ago

If you're using :InspectTree, you need to toggle anonymous nodes:

(section) ; [1:1 - 2:0]
 (paragraph) ; [1:1 - 2:0]
  (inline) ; [1:1 - 24]
   (uri_autolink) ; [1:1 - 24]
   "<" ; [1:1 - 1]
   ":" ; [1:7 - 7]
   "/" ; [1:8 - 8]
   "/" ; [1:9 - 9]
   "." ; [1:13 - 13]
   "." ; [1:20 - 20]
   ">" ; [1:24 - 24]

so you can just match them literally.

musjj commented 1 year ago

Hmm, is there a way to match literal characters in treesitter queries? I tried:

(uri_autolink
  [
    "<"
    ">"
  ] @conceal
  (#set! conceal ""))

But it's giving me errors.

clason commented 1 year ago

they're not children of uri_autolink, so you'd have to write a more complicated query using anchors

musjj commented 1 year ago

I tried to use match?, but it doesn't really help because it will still apply @conceal to the whole node. Is there really a way to apply it partially? I also tried offset!, but I don't think you can capture the individual delimiters this way.

clason commented 1 year ago

Yes, you need to capture the anonymous nodes themselves. You might need two individual queries, one for < and one for >. (#offset! could help with that).

Of course, you could also make a PR adding uri_autolink_delimiter nodes to the grammar.

MDeiml commented 1 year ago

I think implementing a uri_autolink_delimiter would be quite complex, so I recommend sticking with the #offset! plan. If I see correctly though offset doesn't quite do what you want so the best way to go is probably to [write your own directive](https://neovim.io/doc/user/treesitter.html#vim.treesitter.query.add_directive()).

clason commented 1 year ago

There's also #gsub! for more complex transformations. (Not cheap, though!)