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

feature: add togglability and documentation #18

Merged
merged 3 commits into from
Jul 21, 2021
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
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,41 @@ require('nvim-biscuits').setup({
EOF
```

## Configuration (Keybinding to toggle visibility)

You can show or hide the biscuits by placing a `toggle_keybind` at the root of your config or inside a language config.

```lua
lua <<EOF
require('nvim-biscuits').setup({
toggle_keybind = "<leader>cb"
})
EOF
```

OR

```lua
lua <<EOF
require('nvim-biscuits').setup({
language_config = {
rust = {
toggle_keybind = "<leader>cb"
}
}
})
EOF
```

If you prefer to bind manually, the function is exposed as:

```lua
require('nvim-biscuits').toggle_biscuits()
```

## Configuration (Cursor Line Only)

You can configure the biscuits to only show on the line that has your cursor. This can be useful if you find that default config makes the text too cluttered.
You can configure the biscuits to only show on the line that has your cursor. This can be useful if you find that default config makes the text too cluttered

```lua
lua <<EOF
Expand Down
25 changes: 25 additions & 0 deletions lua/nvim-biscuits/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ local ts_parsers = require('nvim-treesitter.parsers')
local ts_utils = require('nvim-treesitter.ts_utils')
local nvim_biscuits = {}

local should_render_biscuits = true

local make_biscuit_hl_group_name =
function(lang) return 'BiscuitColor' .. lang end

Expand All @@ -30,6 +32,12 @@ nvim_biscuits.decorate_nodes = function(bufnr, lang)
local biscuit_highlight_group_name = make_biscuit_hl_group_name(lang)
local biscuit_highlight_group = vim.api.nvim_create_namespace(
biscuit_highlight_group_name)

if not should_render_biscuits then
vim.api.nvim_buf_clear_namespace(bufnr, biscuit_highlight_group, 0, -1)
return
end

local root = parser:parse()[1]:root()

local nodes = ts_utils.get_named_children(root)
Expand Down Expand Up @@ -153,6 +161,16 @@ nvim_biscuits.BufferAttach = function(bufnr)
attached_buffers[bufnr] = true

local lang = ts_parsers.get_buf_lang(bufnr)

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
vim.api.nvim_set_keymap("n", toggle_keybind,
":lua require('nvim-biscuits').toggle_biscuits()<CR>",
{noremap = false})
end

local on_lines = function() nvim_biscuits.decorate_nodes(bufnr, lang) end

vim.cmd("highlight default link " .. make_biscuit_hl_group_name(lang) ..
Expand Down Expand Up @@ -186,4 +204,11 @@ nvim_biscuits.BufferAttach = function(bufnr)
end
end

nvim_biscuits.toggle_biscuits = function()
should_render_biscuits = not should_render_biscuits
local bufnr = vim.api.nvim_get_current_buf()
local lang = ts_parsers.get_buf_lang(bufnr)
nvim_biscuits.decorate_nodes(bufnr, lang)
end

return nvim_biscuits