From 25a880605fa4533b7075c54a0fdb5f0a25bc4f84 Mon Sep 17 00:00:00 2001 From: Dane Harnett <2585257+dane-harnett@users.noreply.github.com> Date: Tue, 10 Jan 2023 03:12:09 +1000 Subject: [PATCH] fix: use nvim-treesitter define_modules to attach/detach to buffers (#42) --- lua/nvim-biscuits/init.lua | 57 ++++++++++++++++++++++++-------------- plugin/nvim-biscuits.vim | 4 --- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/lua/nvim-biscuits/init.lua b/lua/nvim-biscuits/init.lua index a857aad..f2ddf66 100644 --- a/lua/nvim-biscuits/init.lua +++ b/lua/nvim-biscuits/init.lua @@ -140,34 +140,15 @@ nvim_biscuits.decorate_nodes = function(bufnr, lang) end end -nvim_biscuits.setup = function(user_config) - if user_config == nil then - user_config = {} - end - - final_config = utils.merge_tables(final_config, user_config) - - if user_config.default_config then - final_config = utils.merge_tables(final_config, - user_config.default_config) - end - - utils.clear_log() -end - local attached_buffers = {} -nvim_biscuits.BufferAttach = function(bufnr) - bufnr = bufnr or vim.api.nvim_get_current_buf() +nvim_biscuits.BufferAttach = function(bufnr, lang) if attached_buffers[bufnr] then return end attached_buffers[bufnr] = true - local lang = ts_parsers.get_buf_lang(bufnr):gsub("-", "") - local toggle_keybind = config.get_language_config(final_config, lang, "toggle_keybind") - if toggle_keybind ~= nil and - not config.get_language_config(final_config, lang, "disabled") then + if toggle_keybind ~= nil then vim.api.nvim_set_keymap("n", toggle_keybind, "lua require('nvim-biscuits').toggle_biscuits()", { noremap = false, desc = "toggle biscuits" }) @@ -214,6 +195,40 @@ nvim_biscuits.BufferAttach = function(bufnr) end end +nvim_biscuits.setup = function(user_config) + if user_config == nil then + user_config = {} + end + + final_config = utils.merge_tables(final_config, user_config) + + if user_config.default_config then + final_config = utils.merge_tables(final_config, + user_config.default_config) + end + + -- This uses the official nvim-treesitter api to attach/detach to buffers + -- see: https://github.com/nvim-treesitter/nvim-treesitter#adding-modules + -- the attach/detach functions will not run if the is_supported function + -- returns false. + require'nvim-treesitter'.define_modules { + nvim_biscuits = { + enable = true, + attach = function(bufnr, lang) + nvim_biscuits.BufferAttach(bufnr, lang) + end, + detach = function(bufnr) + attached_buffers[bufnr] = nil + end, + is_supported = function(lang) + return not config.get_language_config(final_config, lang, "disabled") + end + } + } + + utils.clear_log() +end + nvim_biscuits.toggle_biscuits = function() nvim_biscuits.should_render_biscuits = not nvim_biscuits.should_render_biscuits diff --git a/plugin/nvim-biscuits.vim b/plugin/nvim-biscuits.vim index 30e12c0..092fcbb 100644 --- a/plugin/nvim-biscuits.vim +++ b/plugin/nvim-biscuits.vim @@ -2,7 +2,3 @@ highlight default BiscuitColor ctermfg=gray guifg=gray -augroup NVIM_BISCUITS - autocmd! - autocmd BufEnter * :lua require('nvim-biscuits').BufferAttach() -augroup END