Skip to content

Commit

Permalink
feat: add vim command to toggle enabled state (#54)
Browse files Browse the repository at this point in the history
Adds the command `:NvimContextVtToggle` to toggle the enabled state for
showing virtual text.

closes #53
  • Loading branch information
andersevenrud authored Feb 10, 2022
1 parent 07da9b4 commit a713c07
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ require('nvim_context_vt').setup({
})
```

## Commands

* `:NvimContextVtToggle` - Enable/disable context virtual text

## Debug

If you don't see the expected context vitual text, run `:NvimContextVtDebug` to print out the
Expand Down
10 changes: 8 additions & 2 deletions doc/nvim_context_vt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ CONTENTS *nvim_context_vt-content
1. nvim_context_vt...............................................|nvim_context_vt|
1.1. How to install..................................|nvim_context_vt-install|
1.2. Advanced usage.................................|nvim_context_vt-advanced|
1.3. Debug.............................................|nvim_context_vt-debug|
1.4. License.........................................|nvim_context_vt-license|
1.3. Commands.......................................|nvim_context_vt-commands|
1.4. Debug.............................................|nvim_context_vt-debug|
1.5. License.........................................|nvim_context_vt-license|

--------------------------------------------------------------------------------
HOW TO INSTALL *nvim_context_vt-install*
Expand Down Expand Up @@ -87,6 +88,11 @@ To customize the behavior use the setup function:
})
<

--------------------------------------------------------------------------------
COMMANDS *nvim_context_vt-commands*

* `:NvimContextVtToggle` - Enable/disable context virtual text

--------------------------------------------------------------------------------
DEBUG *nvim_context_vt-debug*

Expand Down
13 changes: 12 additions & 1 deletion lua/nvim_context_vt/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ local utils = require('nvim_context_vt.utils')
local M = {}
local ns = vim.api.nvim_create_namespace('context_vt')
local opts = config.default_opts
local enabled = true

function M.setup(user_opts)
opts = vim.tbl_extend('force', config.default_opts, user_opts or {})
Expand All @@ -29,9 +30,19 @@ function M.show_debug()
end
end

function M.toggle_context()
if enabled then
enabled = false
vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
else
enabled = true
M.show_context()
end
end

function M.show_context()
local ft = parsers.get_buf_lang()
if vim.tbl_contains(opts.disable_ft, ft) then
if not enabled or vim.tbl_contains(opts.disable_ft, ft) then
return
end

Expand Down
1 change: 1 addition & 0 deletions plugin/nvim_context_vt.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
highlight default link ContextVt Comment

command NvimContextVtDebug lua require'nvim_context_vt'.show_debug()
command NvimContextVtToggle lua require'nvim_context_vt'.toggle_context()

autocmd CursorMoved * lua require 'nvim_context_vt'.show_context()
autocmd CursorMovedI * lua require 'nvim_context_vt'.show_context()

0 comments on commit a713c07

Please sign in to comment.