Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use nvim-treesitter define_modules to attach/detach to buffers #42

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 36 additions & 21 deletions lua/nvim-biscuits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -140,34 +140,15 @@ nvim_biscuits.decorate_nodes = function(bufnr, lang)
end
end

nvim_biscuits.setup = function(user_config)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this function below nvim_biscuits.BufferAttach definition so that we can use it inside the setup.

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()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The bufnr should now be passed in.

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("-", "")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lang should now be passed in.


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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now implemented in the is_supported function below

if toggle_keybind ~= nil then
vim.api.nvim_set_keymap("n", toggle_keybind,
"<Cmd>lua require('nvim-biscuits').toggle_biscuits()<CR>",
{ noremap = false, desc = "toggle biscuits" })
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions plugin/nvim-biscuits.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@

highlight default BiscuitColor ctermfg=gray guifg=gray

augroup NVIM_BISCUITS
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer need this.

autocmd!
autocmd BufEnter * :lua require('nvim-biscuits').BufferAttach()
augroup END