Skip to content

Commit

Permalink
feat: Support left_var - closes #82
Browse files Browse the repository at this point in the history
This allows languages like Python, where the `left` string may vary for
regular and 'variable' lines, to have different debugprint config.
  • Loading branch information
andrewferrier committed Mar 18, 2024
1 parent f71d7e1 commit 3e8e393
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ In either case, the format is the same. For example, if adding via `setup()`:
```lua
local my_fileformat = {
left = 'print "',
left_var = 'print "', -- `left_var` is optional, for 'variable' lines only; `left` will be used if it's not present
right = '"',
mid_var = "${",
right_var = '}"',
Expand All @@ -274,10 +275,10 @@ configuration.

The keys in the configuration are used like this:

| Debug line type | Default keys | How debug line is constructed |
| ------------------- | ----------------------- | --------------------------------------------------------------------------------------------------------------------------- |
| Plain debug line | `g?p`/`g?P` | `my_fileformat.left .. "auto-gen DEBUG string" .. my_fileformat.right` |
| Variable debug line | `g?v`/`g?V`/`g?o`/`g?O` | `my_fileformat.left .. "auto-gen DEBUG string, variable=" .. my_file_format.mid_var .. variable .. my_fileformat.right_var` |
| Debug line type | Default keys | How debug line is constructed |
| ------------------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Plain debug line | `g?p`/`g?P` | `my_fileformat.left .. "auto-gen DEBUG string" .. my_fileformat.right` |
| Variable debug line | `g?v`/`g?V`/`g?o`/`g?O` | `my_fileformat.left_var (or my_fileformat.left) .. "auto-gen DEBUG string, variable=" .. my_file_format.mid_var .. variable .. my_fileformat.right_var` |

If it helps to understand these, you can look at the built-in configurations in
[filetypes.lua](lua/debugprint/filetypes.lua).
Expand Down
3 changes: 2 additions & 1 deletion lua/debugprint/filetypes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ return {
},
-- Don't print to stderr by default, because it requires 'import sys'
["python"] = {
left = 'print(f"',
left = 'print("',
left_var = 'print(f"',
right = '")',
mid_var = "{",
right_var = '}")',
Expand Down
10 changes: 9 additions & 1 deletion lua/debugprint/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ local addline = function(opts)
local line_to_insert_linenr

if opts.variable_name then
line_to_insert_content = fileconfig.left
local left

if fileconfig["left_var"] ~= nil then
left = fileconfig["left_var"]
else
left = fileconfig["left"]
end

line_to_insert_content = left
.. debuginfo(opts)
.. fileconfig.mid_var
.. opts.variable_name
Expand Down
25 changes: 21 additions & 4 deletions tests/debugprint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1391,7 +1391,7 @@ describe("check python indenting", function()

check_lines({
"x = 1",
'print(f"DEBUGPRINT[1]: ' .. filename .. ':1 (after x = 1)")',
'print("DEBUGPRINT[1]: ' .. filename .. ':1 (after x = 1)")',
"y = 2",
})
end)
Expand All @@ -1406,7 +1406,7 @@ describe("check python indenting", function()

check_lines({
"def xyz():",
' print(f"DEBUGPRINT[1]: '
' print("DEBUGPRINT[1]: '
.. filename
.. ':1 (after def xyz():)")',
" pass",
Expand All @@ -1418,14 +1418,31 @@ describe("check python indenting", function()
"def xyz():",
" x = 1",
" y = 2",
}, "python", 2, 0)
}, "python", 2, 5)

feedkeys("g?p")

check_lines({
"def xyz():",
" x = 1",
' print(f"DEBUGPRINT[1]: ' .. filename .. ':2 (after x = 1)")',
' print("DEBUGPRINT[1]: ' .. filename .. ':2 (after x = 1)")',
" y = 2",
})
end)

it("variable", function()
local filename = init_file({
"def xyz():",
" x = 1",
" y = 2",
}, "python", 2, 4)

feedkeys("g?v<CR>")

check_lines({
"def xyz():",
" x = 1",
' print(f"DEBUGPRINT[1]: ' .. filename .. ':2: x={x}")',
" y = 2",
})
end)
Expand Down

0 comments on commit 3e8e393

Please sign in to comment.