lukas-reineke / headlines.nvim

This plugin adds horizontal highlights for text filetypes, like markdown, orgmode, and neorg.
MIT License
688 stars 28 forks source link

got "Error executing vim.schedule lua callback", and error related to treesitter. #68

Closed hopezh closed 7 months ago

hopezh commented 7 months ago

I was using headlines.nvim previously to highlight the headers and code blocks in qmd file, a type of markdown file basically for the [quarto](https://quarto.org/) publication system, which works perfect.

However, I got the following error message recently, trying to edit a qmd file, which is probably related to the callback function in line 26.

Error executing vim.schedule lua callback: ...hare/oatVim05/lazy/headlines.nvim/lua/headlines/init.lua:279: attempt to get length of field 'bullet_highlights' (a nil value)
stack traceback:
    ...hare/oatVim05/lazy/headlines.nvim/lua/headlines/init.lua:279: in function 'refresh'
    /Users/oatmini/.config/oatVim05/lua/plugins/headlines.lua:26: in function </Users/oatmini/.config/oatVim05/lua/plugins/headlines.lua:2

Commenting out these lines related to the vim.schedule function will generate new error messages as shown below.

12:21:42 msg_show Error executing lua callback: ...vim-treesitter-context/lua/treesitter-context/render.lua:94: CursorMoved Autocommands for "*"..FileType Autocommands for "*": Vim(append):Error executing lua callback: ....5/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:119: CursorMoved Autocommands for "*"..FileType Autocommands for "*"..Syntax Autocommands for "*": Vim(lua):E5108: Error executing lua ...hare/oatVim05/lazy/headlines.nvim/lua/headlines/init.lua:279: attempt to get length of field 'bullet_highlights' (a nil value)
stack traceback:
    ...hare/oatVim05/lazy/headlines.nvim/lua/headlines/init.lua:279: in function 'refresh'
    [string ":lua"]:1: in main chunk
    [C]: in function '__newindex'
    ....5/share/nvim/runtime/lua/vim/treesitter/highlighter.lua:119: in function 'new'
    ...r/neovim/0.9.5/share/nvim/runtime/lua/vim/treesitter.lua:461: in function 'start'
    ...5/lazy/nvim-treesitter/lua/nvim-treesitter/highlight.lua:20: in function 'attach'
    ...m05/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:509: in function 'attach_module'
    ...m05/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:532: in function 'reattach_module'
    ...m05/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:133: in function <...m05/lazy/nvim-treesitter/lua/nvim-treesitter/configs.lua:132>
    [C]: in function '__newindex'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:94: in function 'copy_option'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:122: in function 'highlight_contexts'
    ...vim-treesitter-context/lua/treesitter-context/render.lua:367: in function 'open'
    .../lazy/nvim-treesitter-context/lua/treesitter-context.lua:50: in function 'open'
    .../lazy/nvim-treesitter-context/lua/treesitter-context.lua:111: in function 'f'
    .../lazy/nvim-treesitter-context/lua/treesitter-context.lua:27: in function <.../lazy/nvim-treesitter-context/lua/treesitter-context.lua:21>
...(and many more lines like this)

Changing the .qmd suffix to .md for the same file will make headlines.nvim works again.

May I ask:

  1. What could be the source of error in this case, and how to solve this issue?
  2. How to make headlines.nvim to support qmd markdown files?

Thanks.

My headlines.lua config file is shown below, which is from my lazyvim fork: oatVim05

return {
    "lukas-reineke/headlines.nvim",
    -- enabled = false, -- suddently not working

    opts = function()
        local opts = {}
        for _, ft in ipairs({ "markdown", "quarto" }) do
            opts[ft] = {
                headline_highlights = {},
            }
            for i = 1, 6 do
                local hl = "Headline" .. i
                vim.api.nvim_set_hl(0, hl, { link = "Headline", default = true })
                table.insert(opts[ft].headline_highlights, hl)
            end
        end
        return opts
    end,

    ft = { "markdown", "quarto" },

    config = function(_, opts)
        -- PERF: schedule to prevent headlines slowing down opening a file
        vim.schedule(function()
            require("headlines").setup(opts)
            require("headlines").refresh()
        end)

        -- highlight color for headlines.nvim ----------------------------------
        vim.cmd([[highlight Headline1 guibg=#999999 guifg=#000000]])
        vim.cmd([[highlight Headline2 guibg=#777777 guifg=#000000]])
        vim.cmd([[highlight Headline3 guibg=#555555 guifg=#000000]])
        vim.cmd([[highlight CodeBlock guibg=#252525]])
        -- vim.cmd([[highlight Dash guibg=#202020 gui=bold]])

        -- custom config for each file types -----------------------------------
        require("headlines").setup({
            quarto = {
                query = vim.treesitter.query.parse(
                    "markdown",
                    [[
                        (atx_heading [
                            (atx_h1_marker)
                            (atx_h2_marker)
                            (atx_h3_marker)
                            (atx_h4_marker)
                            (atx_h5_marker)
                            (atx_h6_marker)
                        ] @headline)

                        (thematic_break) @dash

                        (fenced_code_block) @codeblock

                        (block_quote_marker) @quote
                        (block_quote (paragraph (inline (block_continuation) @quote)))
                    ]]
                ),

                treesitter_language = "markdown",
                headline_highlights = {
                    "Headline1",
                    "Headline2",
                    "Headline3",
                },
                codeblock_highlight = "CodeBlock",
                dash_highlight = "Dash",
                dash_string = "-",
                quote_highlight = "Quote",
                quote_string = "ā”ƒ",
                fat_headlines = true,
                fat_headline_upper_string = "",
                fat_headline_lower_string = "",
            },

            markdown = {
                query = vim.treesitter.query.parse(
                    "markdown",
                    [[
                        (atx_heading [
                            (atx_h1_marker)
                            (atx_h2_marker)
                            (atx_h3_marker)
                            (atx_h4_marker)
                            (atx_h5_marker)
                            (atx_h6_marker)
                        ] @headline)

                        (thematic_break) @dash

                        (fenced_code_block) @codeblock

                        (block_quote_marker) @quote
                        (block_quote (paragraph (inline (block_continuation) @quote)))
                    ]]
                ),

                -- headline_highlights = { "Headline" },
                headline_highlights = {
                    "Headline1",
                    "Headline2",
                    "Headline3",
                },

                codeblock_highlight = "CodeBlock",
                dash_highlight = "Dash",
                dash_string = "-",
                quote_highlight = "Quote",
                quote_string = "ā”ƒ",
                fat_headlines = true,
                -- fat_headline_upper_string = "ā–ƒ",
                -- fat_headline_lower_string = "šŸ¬‚",
                fat_headline_upper_string = "",
                fat_headline_lower_string = "",
            },
        })
    end,
}
lukas-reineke commented 7 months ago

Version 4 had a breaking change that added new config values. You need to add them to your filetypes as well. :help headlines-bullet_highlights and :help headlines-bullets.

You can set them to empty lists if you don't want them, but they need to be defined.