mistweaverco / kulala.nvim

A minimal 🤏 HTTP-client 🐼 interface 🖥️ for Neovim ❤️.
https://kulala.mwco.app
MIT License
661 stars 31 forks source link

toggle_view treesitter error #128

Closed yujinyuz closed 2 months ago

yujinyuz commented 2 months ago

I'm getting this error when running toggle_view()

This happens when switching from Body to Header view

Error executing vim.schedule lua callback: ...table/share/nvim/runtime/lua/vim/treesitter/language.lua:107: no parser for 'pl
aintext' language, see :help treesitter-parsers
stack traceback:
        [C]: in function 'error'
        ...table/share/nvim/runtime/lua/vim/treesitter/language.lua:107: in function 'add'
        ...e/share/nvim/runtime/lua/vim/treesitter/languagetree.lua:111: in function 'new'
        .../neovim/stable/share/nvim/runtime/lua/vim/treesitter.lua:41: in function '_create_parser'
        .../neovim/stable/share/nvim/runtime/lua/vim/treesitter.lua:108: in function 'get_parser'
        ...m/stable/share/nvim/runtime/lua/vim/treesitter/_fold.lua:117: in function 'compute_folds_levels'
        ...m/stable/share/nvim/runtime/lua/vim/treesitter/_fold.lua:380: in function 'fn'
        ...m/stable/share/nvim/runtime/lua/vim/treesitter/_fold.lua:293: in function <...m/stable/share/nvim/runtime/lua/vim/
treesitter/_fold.lua:289>

Here's my config if it helps

    'mistweaverco/kulala.nvim',
    ft = 'http',
    opts = {
      default_view = 'headers_body',
      winbar = true,
      contenttypes = {
        ['text/plain'] = {
          formatter = function(body)
            return body
          end,
        },
      },
    },
    keys = {
      {
        '<CR>',
        function()
          require('kulala').run()
        end,
        ft = 'http',
      },
      {
        '<leader>r',
        function()
          require('kulala').run()
        end,
        ft = 'http',
      },
      {
        '<leader>v',
        function()
          require('kulala').toggle_view()
        end,
        ft = 'http',
      },
    },
yujinyuz commented 2 months ago

It was a bug in my config..

yujinyuz commented 2 months ago

My config contains

vim.opt.foldmethod = 'expr'
vim.opt.foldexpr = 'v:lua.vim.treesitter.foldexpr()'

Removing that resolves this error.

gorillamoe commented 2 months ago

Can you look if this fixes your issue? https://github.com/neovim/neovim/issues/23964#issuecomment-1584411926\

/edit:

Tried that, didn't work, even if it's supposed to work :see_no_evil:

gorillamoe commented 2 months ago

Okay, you should change your config part to something like this:

local function enable_foldexpr()
  vim.opt_local.foldexpr = "v:lua.vim.treesitter.foldexpr()"
  vim.opt_local.foldmethod = "expr"
end

vim.api.nvim_create_autocmd("FileType", {
  callback = function(ev)
    local has_treesitter = pcall(function()
      vim.treesitter.get_parser(ev.buf)
    end)
    if has_treesitter then
      enable_foldexpr()
    end
  end,
})

This makes sure, that only on buffers with a treesitter parser attached to it your config will get applied.

Then v3.0.1 should work for you :)

yujinyuz commented 2 months ago

Thanks @gorillamoe works for me

EDIT:

I don't think that the method you provided is necessary since the foldmethod and foldexpr is local to the window. So if there are 2 buffers in the window 1 supports TS while the other one does not, it would still set the foldexpr and foldmethod.

But the v3.0.1 works just fine so everything is good

wenjinnn commented 1 month ago

Still have some issue here. the fold level of the result buffer view didn't calculate correctly. so zc zo not work here.

update: opened a PR to fix that, tested on my machine.