From 30e3fb35a91f2f82a4d4eb9a8bbbd04a7ac8ffd3 Mon Sep 17 00:00:00 2001 From: Andrew Ferrier <107015+andrewferrier@users.noreply.github.com> Date: Sun, 3 Nov 2024 12:25:44 +0000 Subject: [PATCH] feat: Override display_* on a per-filetype basis - closes #135 --- README.md | 3 +- SHOWCASE.md | 14 +++++++ lua/debugprint/init.lua | 59 ++++++++++++++++++++------- lua/debugprint/types.lua | 3 ++ tests/debugprint.lua | 88 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 152 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6d470fe..67d354a 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/SHOWCASE.md b/SHOWCASE.md index 15c79fa..ad3664c 100644 --- a/SHOWCASE.md +++ b/SHOWCASE.md @@ -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: diff --git a/lua/debugprint/init.lua b/lua/debugprint/init.lua index b8737a4..404ed9b 100644 --- a/lua/debugprint/init.lua +++ b/lua/debugprint/init.lua @@ -14,48 +14,79 @@ 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 @@ -63,7 +94,7 @@ local get_debugline_textcontent = function(opts) 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) @@ -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 diff --git a/lua/debugprint/types.lua b/lua/debugprint/types.lua index 03d12d2..dd73d97 100644 --- a/lua/debugprint/types.lua +++ b/lua/debugprint/types.lua @@ -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 diff --git a/tests/debugprint.lua b/tests/debugprint.lua index e069ffb..f65ed0d 100644 --- a/tests/debugprint.lua +++ b/tests/debugprint.lua @@ -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)