stevearc / aerial.nvim

Neovim plugin for a code outline window
MIT License
1.55k stars 76 forks source link

Help with xml query #334

Closed petrisch closed 6 months ago

petrisch commented 6 months ago

Language: [xml]

I'm quite new to aerial so bare with me if I'm doing stupid.

<assembly type="ru_derive">
    <morph type="fs_derive">
        <\morph>
<\assembly>

This is not a attempt to make a general purpose xml query, since I'm not shure if this is of any use. In my case I deal with very large files which do have a structure as of above. Now I could query for "assembly" or "morph", but the real structure comes from the "ru_" stuff.

So this works fine.

What I am curious though is, how is the fold structure been made. Like this I always get the symbols on the same level.

stevearc commented 6 months ago

The problem with the nesting is that apparently treesitter doesn't parse the XML document into a nested structure. When I run :InspectTree, I see this:

(STag) ; [1:1 - 27]
 (Name) ; [1:2 - 9]
 (Attribute) ; [1:11 - 26]
  (Name) ; [1:11 - 14]
  (AttValue) ; [1:16 - 26]
(CharData) ; [1:28 - 2:1]
(STag) ; [2:2 - 25]
 (Name) ; [2:3 - 7]
 (Attribute) ; [2:9 - 24]
  (Name) ; [2:9 - 12]
  (AttValue) ; [2:14 - 24]
(CharData) ; [2:26 - 3:8]
(STag) ; [3:9 - 16]
 (ERROR) ; [3:10 - 10]
 (Name) ; [3:11 - 15]
(CharData) ; [3:17 - 4:0]
(STag) ; [4:1 - 11]
 (ERROR) ; [4:2 - 2]
 (Name) ; [4:3 - 10]
(content) ; [4:12 - 5:0]
 (CharData) ; [4:12 - 5:0]

Since treesitter doesn't nest the parsed AST, the symbols won't be nested either. There are ways around it. For other, similar filetypes I've added custom logic to enforce the nesting based on some other properties I can detect. Here's how it works for markdown:

https://github.com/stevearc/aerial.nvim/blob/e2e3bc2df4490690ea005395eecdc8eeb30c4def/lua/aerial/backends/treesitter/extensions.lua#L111-L124