Skip to content

Commit

Permalink
feat: Override display_* on a per-filetype basis - closes #135
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewferrier committed Nov 3, 2024
1 parent 95d2a3e commit 30e3fb3
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ the NeoVim generation. It:

* Includes reference information in each 'print line' such as file names, line
numbers, a counter, and snippets of other lines to make it easier to
cross-reference them in output (each of these can be [optionally disabled](#other-options)).
cross-reference them in output (each of these can be optionally
disabled [globally](#other-options) or on a per-filetype basis).

* Can output the value of variables (or in some cases, expressions).

Expand Down
14 changes: 14 additions & 0 deletions SHOWCASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ return {
}
```

## Setting `display_*` options on per-filetype basis

The three `display_*` options [supported on a global basis](README.md#other-options) by `debugprint` can also be overridden on a per-filetype basis so you can show and hide differently for different filetypes. Filetypes without these set (which is the default for all filetypes) will continue to use the values set globally. Pass them into the `setup()` method or the `add_custom_filetypes()` method like this:

```lua
require('debugprint').setup({ filetypes = { ["filetype"] = { display_counter = false }}})
```

or

```lua
require('debugprint').add_custom_filetypes({ ["filetype"] = { display_counter = false }, … })
```

## Use lazy-loading with lazy.nvim

`debugprint` can be configured, when using [lazy.nvim](https://github.com/folke/lazy.nvim) as a plugin manager, to lazy load itself. Use configuration that looks like this:
Expand Down
59 changes: 45 additions & 14 deletions lua/debugprint/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,56 +14,87 @@ local default_display_counter = function()
return "[" .. tostring(default_counter) .. "]"
end

---@param display_counter? boolean|function
---@return string
local get_debugline_tag_and_counter = function()
local get_debugline_tag_and_counter = function(display_counter)
local tag_and_counter = ""

if global_opts.print_tag then
tag_and_counter = global_opts.print_tag
end

if global_opts.display_counter == true then
if display_counter == true then
tag_and_counter = tag_and_counter .. default_display_counter()
elseif type(global_opts.display_counter) == "function" then
tag_and_counter = tag_and_counter
.. tostring(global_opts.display_counter())
elseif type(display_counter) == "function" then
tag_and_counter = tag_and_counter .. tostring(display_counter())
end

return tag_and_counter
end

---@param fileconfig DebugprintFileTypeConfig
---@return function|boolean?, boolean?, boolean?
local get_display_options = function(fileconfig)
local display_counter
if fileconfig.display_counter ~= nil then
display_counter = fileconfig.display_counter
else
display_counter = global_opts.display_counter
end

local display_location
if fileconfig.display_location ~= nil then
display_location = fileconfig.display_location
else
display_location = global_opts.display_location
end

local display_snippet
if fileconfig.display_snippet ~= nil then
display_snippet = fileconfig.display_snippet
else
display_snippet = global_opts.display_snippet
end

return display_counter, display_location, display_snippet
end

---@param opts DebugprintFunctionOptionsInternal
---@param fileconfig DebugprintFileTypeConfig
---@return string
local get_debugline_textcontent = function(opts)
local get_debugline_textcontent = function(opts, fileconfig)
local current_line_nr = vim.api.nvim_win_get_cursor(0)[1]

local line_components = {}
local force_snippet_for_plain = false

local display_counter, display_location, display_snippet =
get_display_options(fileconfig)

if
not global_opts.display_location
and not global_opts.display_snippet
and not global_opts.display_counter
not display_location
and not display_snippet
and not display_counter
and global_opts.print_tag == ""
then
force_snippet_for_plain = true
end

local tag_and_counter = get_debugline_tag_and_counter()
local tag_and_counter = get_debugline_tag_and_counter(display_counter)

if tag_and_counter ~= "" then
table.insert(line_components, tag_and_counter .. ":")
end

if global_opts.display_location then
if display_location then
table.insert(
line_components,
vim.fn.expand("%:t") .. ":" .. current_line_nr
)
end

if
(global_opts.display_snippet or force_snippet_for_plain)
(display_snippet or force_snippet_for_plain)
and opts.variable_name == nil
then
local snippet = utils.get_snippet(current_line_nr, opts.above)
Expand Down Expand Up @@ -115,14 +146,14 @@ local construct_debugprint_line = function(opts, fileconfig)
end

line_to_insert = left
.. get_debugline_textcontent(opts)
.. get_debugline_textcontent(opts, fileconfig)
.. fileconfig.mid_var
.. opts.variable_name
.. fileconfig.right_var
else
opts.variable_name = nil
line_to_insert = fileconfig.left
.. get_debugline_textcontent(opts)
.. get_debugline_textcontent(opts, fileconfig)
.. fileconfig.right
end

Expand Down
3 changes: 3 additions & 0 deletions lua/debugprint/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
---@field mid_var string
---@field right_var string
---@field find_treesitter_variable? function
---@field display_counter? boolean|function
---@field display_location? boolean
---@field display_snippet? boolean

---@class DebugprintGlobalOptions
---@field keymaps? DebugprintKeymapOptions
Expand Down
88 changes: 88 additions & 0 deletions tests/debugprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2324,3 +2324,91 @@ describe("variations of display_* options", function()
})
end)
end)

describe("allow display_* to be set in filetypes", function()
after_each(teardown)

it("display_counter", function()
debugprint.setup({ filetypes = { bash = { display_counter = false } } })

local lua_filename = init_file({
"foo",
"bar",
}, "lua", 1, 0)

feedkeys("g?p")

check_lines({
"foo",
"print('DEBUGPRINT[1]: " .. lua_filename .. ":1 (after foo)')",
"bar",
})

local sh_filename = init_file({
"XYZ=123",
}, "bash", 1, 1)

feedkeys("g?v")

check_lines({
"XYZ=123",
'>&2 echo "DEBUGPRINT: ' .. sh_filename .. ':1: XYZ=${XYZ}"',
})
end)

it("display_location", function()
debugprint.setup({ filetypes = { lua = { display_location = false } } })

local lua_filename = init_file({
"foo",
"bar",
}, "lua", 1, 0)

feedkeys("g?p")

check_lines({
"foo",
"print('DEBUGPRINT[1]: (after foo)')",
"bar",
})

local sh_filename = init_file({
"XYZ=123",
}, "bash", 1, 1)

feedkeys("g?v")

check_lines({
"XYZ=123",
'>&2 echo "DEBUGPRINT[2]: ' .. sh_filename .. ':1: XYZ=${XYZ}"',
})
end)

it("display_snippet", function()
debugprint.setup({ filetypes = { lua = { display_snippet = false } } })

local lua_filename = init_file({
"foo",
"bar",
}, "lua", 1, 0)

feedkeys("g?p")

check_lines({
"foo",
"print('DEBUGPRINT[1]: " .. lua_filename .. ":1')",
"bar",
})

local sh_filename = init_file({
"XYZ=123",
}, "bash", 1, 1)

feedkeys("g?p")

check_lines({
"XYZ=123",
'>&2 echo "DEBUGPRINT[2]: ' .. sh_filename .. ':1 (after XYZ=123)"',
})
end)
end)

0 comments on commit 30e3fb3

Please sign in to comment.