From a713c0736bb115f978d3c9a89a4e354e7ef36683 Mon Sep 17 00:00:00 2001 From: Anders Evenrud Date: Thu, 10 Feb 2022 19:05:19 +0100 Subject: [PATCH] feat: add vim command to toggle enabled state (#54) Adds the command `:NvimContextVtToggle` to toggle the enabled state for showing virtual text. closes #53 --- README.md | 4 ++++ doc/nvim_context_vt.txt | 10 ++++++++-- lua/nvim_context_vt/init.lua | 13 ++++++++++++- plugin/nvim_context_vt.vim | 1 + 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7fd9517..83ae7c5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/doc/nvim_context_vt.txt b/doc/nvim_context_vt.txt index 259366e..d922f4b 100644 --- a/doc/nvim_context_vt.txt +++ b/doc/nvim_context_vt.txt @@ -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* @@ -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* diff --git a/lua/nvim_context_vt/init.lua b/lua/nvim_context_vt/init.lua index 3e43a13..6eb8dac 100644 --- a/lua/nvim_context_vt/init.lua +++ b/lua/nvim_context_vt/init.lua @@ -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 {}) @@ -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 diff --git a/plugin/nvim_context_vt.vim b/plugin/nvim_context_vt.vim index 6eeda34..9fa1618 100644 --- a/plugin/nvim_context_vt.vim +++ b/plugin/nvim_context_vt.vim @@ -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()