From 54ba3b51322c1e9e8c0448a70345624f75800cca Mon Sep 17 00:00:00 2001 From: Chris Griffing Date: Sat, 20 Apr 2024 15:47:41 -0700 Subject: [PATCH] fix: allow BufAttach to be called without a bufnr and lang and allow detection via vim and and treesitter --- README.md | 41 ++++++++++++++++++++++++++++++++++++++ lua/nvim-biscuits/init.lua | 9 +++++++++ 2 files changed, 50 insertions(+) diff --git a/README.md b/README.md index 73d392f..01c236c 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,47 @@ require('nvim-biscuits').setup({ EOF ``` +## Configuration (lazy.nvim support) + +If you are using [lazy.nvim](https://github.com/folke/lazy.nvim), you will have to wire up nvim-biscuits is a specific way. + +Since you are using lazy.nvim, it is recommended to not use the internal nvim-biscuits [visibilty toggle](https://github.com/code-biscuits/nvim-biscuits?tab=readme-ov-file#configuration-keybinding-to-toggle-visibility). + +When setting your `keys` value, you just need to call `BufAttach` inside the callback function: + +```lua +keys = { + { + "bb", + function() + require("nvim-biscuits").BufferAttach() + end, + mode = "n", + desc = "Enable Biscuits", + }, +}, +``` + +If you want to lazy load nvim-biscuits but also use the internal visibilty toggle, there are a couple ways of setting things up. If you are letting `show_on_start` default to `false`, you will need to manually toggle the biscuits in the callback function. + +```lua +keys = { + { + "bb", + function() + local nvim_biscuits = + require("nvim-biscuits") + nvim_biscuits.BufferAttach() + nvim_biscuits.toggle_biscuits() + end, + mode = "n", + desc = "Enable Biscuits", + }, +}, +``` + +A cleaner way of doing it is to set `show_on_start` to true for nvim-biscuits. Then, you do not need to call `toggle_biscuits` in your lazy.nvim callback. + ## Supported Languages We currently support all the languages supported in tree-sitter. Not all languages have specialized support, though most will probably need some. diff --git a/lua/nvim-biscuits/init.lua b/lua/nvim-biscuits/init.lua index 7b0839c..e63ccfa 100644 --- a/lua/nvim-biscuits/init.lua +++ b/lua/nvim-biscuits/init.lua @@ -177,6 +177,15 @@ end local attached_buffers = {} nvim_biscuits.BufferAttach = function(bufnr, lang) + + if bufnr == nil then + bufnr = vim.api.nvim_get_current_buf() + end + + if lang == nil then + lang = ts_parsers.get_buf_lang(bufnr):gsub("-", "") + end + if attached_buffers[bufnr] then return end attached_buffers[bufnr] = true