Skip to content
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
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
22 changes: 15 additions & 7 deletions doc/nvim_context_vt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lua/nvim_context_vt/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
13 changes: 12 additions & 1 deletion lua/nvim_context_vt/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
72 changes: 72 additions & 0 deletions tests/context_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 } })
Expand Down
Loading