diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c639333..4d87d1d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -39,4 +39,8 @@ Use the command `:NvimContextVtDebug` to show the context for the current line t * `line_targets`: valid node targets used for line targets and not inline * `line_ft`: filetypes that should use `line_targets` +Note that the node type names printed by `:NvimContextVtDebug` are also what the `disable_targets` and +`disable_targets_ft` setup options take, so removing a target from `targets` is only appropriate when it +never made sense for anyone, not as a matter of personal preference. + Commit the changes with the message `feat: support X` or `feat: improve Y`, etc. into your fork and open a pull-request. diff --git a/README.md b/README.md index 16586e9..b0c9be7 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,15 @@ require('nvim_context_vt').setup({ -- Default: {} disable_virtual_lines_ft = { 'yaml' }, + -- Never show virtual text for these node types + -- Default: {} + disable_targets = { 'call' }, + + -- Same as above but only for spesific filetypes + -- Adds to the list above instead of replacing it + -- Default: {} + disable_targets_ft = { python = { 'if_statement' } }, + -- How many lines required after starting position to show virtual text -- Default: 1 (equals two lines total) min_rows = 1, @@ -68,16 +77,15 @@ require('nvim_context_vt').setup({ -- Custom node validator callback -- Default: nil custom_validator = function(node, ft, opts) - -- Internally a node is matched against min_rows and configured targets + -- Internally a node is matched against min_rows, the configured targets + -- and the disabled targets local default_validator = require('nvim_context_vt.utils').default_validator - if default_validator(node, ft) then - -- Custom behaviour after using the internal validator - if node:type() == 'function' then - return false - end + if not default_validator(node, ft, opts) then + return false end - return true + -- Custom behaviour after using the internal validator + return node:type() ~= 'function' end, -- Custom node virtual text resolver callback diff --git a/doc/nvim_context_vt.txt b/doc/nvim_context_vt.txt index aa2f653..7d1db39 100644 --- a/doc/nvim_context_vt.txt +++ b/doc/nvim_context_vt.txt @@ -52,6 +52,15 @@ To customize the behavior use the setup function: -- Default: {} disable_virtual_lines_ft = { 'yaml' }, + -- Never show virtual text for these node types + -- Default: {} + disable_targets = { 'call' }, + + -- Same as above but only for spesific filetypes + -- Adds to the list above instead of replacing it + -- Default: {} + disable_targets_ft = { python = { 'if_statement' } }, + -- How many lines required after starting position to show virtual text -- Default: 1 (equals two lines total) min_rows = 1, @@ -76,15 +85,14 @@ To customize the behavior use the setup function: -- Custom node validator callback -- Default: nil custom_validator = function(node, ft, opts) - -- Internally a node is matched against min_rows and configured targets + -- Internally a node is matched against min_rows, the configured + -- targets and the disabled targets local default_validator = require('nvim_context_vt.utils').default_validator - if default_validator(node, ft) then - -- Custom behaviour after using the internal validator - if node:type() == 'function' then - return false - end + if not default_validator(node, ft, opts) then + return false end - return false + -- Custom behaviour after using the internal validator + return node:type() ~= 'function' end, -- Custom node virtual text resolver callback diff --git a/lua/nvim_context_vt/config.lua b/lua/nvim_context_vt/config.lua index 5effe36..50064bb 100644 --- a/lua/nvim_context_vt/config.lua +++ b/lua/nvim_context_vt/config.lua @@ -11,6 +11,8 @@ M.default_opts = { disable_ft = { 'markdown' }, disable_virtual_lines = false, disable_virtual_lines_ft = {}, + disable_targets = {}, + disable_targets_ft = {}, prefix = '-->', priority = 1000, } diff --git a/lua/nvim_context_vt/utils.lua b/lua/nvim_context_vt/utils.lua index 4d2b117..44b9c48 100644 --- a/lua/nvim_context_vt/utils.lua +++ b/lua/nvim_context_vt/utils.lua @@ -33,9 +33,20 @@ end M.default_validator = function(node, ft, opts) ---@type integer local min_rows = opts.min_rows_ft[ft] or opts.min_rows - if vim.tbl_contains(config.targets, node:type()) then + local node_type = node:type() + + if vim.tbl_contains(opts.disable_targets, node_type) then + return false + end + + if vim.tbl_contains(opts.disable_targets_ft[ft] or {}, node_type) then + return false + end + + if vim.tbl_contains(config.targets, node_type) then return node:end_() > node:start() + min_rows end + return false end diff --git a/tests/context_spec.lua b/tests/context_spec.lua index 70fb90f..6c51492 100644 --- a/tests/context_spec.lua +++ b/tests/context_spec.lua @@ -99,6 +99,78 @@ t.test('min_rows_ft overrides min_rows for one filetype', function() t.eq({ '--> if (argc > 1) {', '--> int main(int argc, char **argv)' }, other) end) +t.test('disable_targets removes only the node types it names', function() + local inline = t.context({ + lines = LUA, + filetype = 'lua', + cursor = CURSOR, + opts = { disable_targets = { 'if_statement' } }, + }) + + t.eq({ '--> for _, item in ipairs(items) do', '--> function M.run(items)' }, inline) +end) + +t.test('disable_targets_ft only applies to its own language', function() + local opts = { disable_targets_ft = { lua = { 'if_statement' } } } + local inline = t.context({ lines = LUA, filetype = 'lua', cursor = CURSOR, opts = opts }) + + t.eq({ '--> for _, item in ipairs(items) do', '--> function M.run(items)' }, inline) + + local other = t.context({ lines = C, filetype = 'c', cursor = { 4, 8 }, opts = opts }) + + t.eq({ '--> if (argc > 1) {', '--> int main(int argc, char **argv)' }, other) +end) + +t.test('disable_targets_ft adds to disable_targets instead of replacing it', function() + local inline = t.context({ + lines = LUA, + filetype = 'lua', + cursor = CURSOR, + opts = { + disable_targets = { 'for_statement' }, + disable_targets_ft = { lua = { 'if_statement' } }, + }, + }) + + t.eq({ '--> function M.run(items)' }, inline) +end) + +t.test('a custom_validator takes over disable_targets as well', function() + local opts = { + disable_targets = { 'if_statement' }, + custom_validator = function(node) + return node:type() == 'if_statement' or node:type() == 'for_statement' + end, + } + + -- Replacing the validator replaces everything it does, the same way it + -- already replaces min_rows and the configured targets + local inline = t.context({ lines = LUA, filetype = 'lua', cursor = CURSOR, opts = opts }) + + t.eq({ '--> for _, item in ipairs(items) do', '--> if item.done then' }, inline) + + -- Calling the default validator from a custom one brings it back + opts.custom_validator = function(node, ft, node_opts) + return utils.default_validator(node, ft, node_opts) + end + + local composed = t.context({ lines = LUA, filetype = 'lua', cursor = CURSOR, opts = opts }) + + t.eq({ '--> for _, item in ipairs(items) do', '--> function M.run(items)' }, composed) +end) + +t.test('disable_targets does not hide anything from the debug output', function() + -- The debug command is how the node type names are found in the first + -- place, so filtering it would make the option impossible to configure + nvim_context_vt.setup({ disable_targets = { 'if_statement' } }) + t.buffer(LUA, 'lua') + vim.api.nvim_win_set_cursor(0, CURSOR) + + local output = vim.api.nvim_exec2('NvimContextVtDebug', { output = true }).output + + t.eq(true, output:find('if_statement --> if item.done then', 1, true) ~= nil) +end) + t.test('renders nothing for a disabled filetype', function() local lines = { '# Title', '', 'Some paragraph', 'across two lines', '' } local inline = t.context({ lines = lines, filetype = 'markdown', cursor = { 4, 0 } })