cathaysia / tree-sitter-loongdoc

loongdoc(asciidoc) parser and grammar for tree-sitter
https://cathaysia.github.io/tree-sitter-loongdoc/
Apache License 2.0
17 stars 2 forks source link

Missing highlights #7

Closed DrKGD closed 2 months ago

DrKGD commented 3 months ago

I had some trouble configuring the highlights as it does not install them (highlights.scm is missing); this is not really a github issue, but here's my solution.

My understanding of it is that nvim-tree should have a copy of highlights.scm in the queries folder, which is not yet the case.

As suggested on this very repository, I installed both but I selected the possibly trademarked name as the filetype, thus keeping the original asciidoctor ftplugin as fallback.

loongdoc = {
    install_info = {
        url = 'https://github.com/cathaysia/tree-sitter-loongdoc.git',
        files   = { 'tree-sitter-loongdoc/src/parser.c', 'tree-sitter-loongdoc/src/scanner.c' },
        branch  = 'master',
        generate_requires_npm = false,
        requires_generate_from_grammar = false,
    },
    filetype = 'asciidoc',
},

loongdoc_inline = {
    install_info = {
        url = 'https://github.com/cathaysia/tree-sitter-loongdoc.git',
        files = { 'tree-sitter-loongdoc_inline/src/parser.c', 'tree-sitter-loongdoc_inline/src/scanner.c' },
        branch = 'master',
        generate_requires_npm = false,
        requires_generate_from_grammar = false,
    },
    filetype = 'asciidoc_inline',
}

Thus I cloned this repository as a standalone plugin, as a nvim-treesitter dependency (using lazy).

As treesitter only automatically accepts queries/{language}/{highlights.scm} and such as reported here, I symlink'd the respective queries directories to $HOME/.local/share/nvim/rtp/queries/{lang}, thus I added the path to the nvim runtimepath.

{ 'cathaysia/tree-sitter-loongdoc',
        name = 'treesitter.parser.loongdoc',
        init = function(spec)
            local rtp = ('%s/rtp'):format(vim.fn.stdpath('data'))
            vim.fn.mkdir(rtp, 'p')
            vim.opt.runtimepath:prepend(rtp)

            local queries = ('%s/queries'):format(rtp)
            vim.fn.mkdir(queries, 'p')

            -- Symlink queries folder for language
            vim.iter({ { 'tree-sitter-loongdoc', 'loongdoc' }, { 'tree-sitter-loongdoc_inline', 'loongdoc_inline' } })
                :each(function(tuple)
                    local src = ('%s/%s/queries'):format(spec.dir, tuple[1])
                    local dst = ('%s/%s'):format(queries, tuple[2])

                    -- Path exists, either it is the wrong one, or it is fine as it is
                    if vim.uv.fs_stat(dst) then
                        assert(vim.uv.fs_readlink(dst) == src, ('%s link does not match the right source %q'):format(tuple[1], src))
                        return end

                    -- Symbolic link
                    vim.uv.fs_symlink(src, dst, { dir = true })
                end)
        end },

Also, I tried using this local clone to automatically build the parsers on startup with ensure_installed (install_info also accepts a local url), but as loongdoc and loongdoc_inline shares the same working directory, they conflict with each other during the build phase.

One last thing, thanks a lot for your work and for the parser of course!

cathaysia commented 3 months ago

If you mean that after you installed queries, it still doesn't highlight. You can try using TSToggle highlight twice. This is because when nvim-treesitter is loaded, it determines whether highlighting is supported based on the file name.

If you don't want to run TSToggle highlight manually, you will need to change asciidoc to loongdoc.

DrKGD commented 3 months ago

Mh, I was more reporting the steps to use the plugin as of right now which works for me and also asking if there was a better way to install highlights, I suppose there really isn't :laughing:

Apologies that I couldn't make it more clear :disappointed:

It does indeed work and pretty well too!

cathaysia commented 3 months ago

Yes. I don't have any better ideas. :(

cathaysia commented 2 months ago

Is this what you want?

DrKGD commented 2 months ago

Yes, tysm!