Skip to content

Commit 3ae60ba

Browse files
author
Andrew Ferrier
committed
fix: Indent line correctly - closes #6
1 parent 2044dc9 commit 3ae60ba

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

lua/debugprint/init.lua

+11-4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ local get_fix = function(filetype)
5353
end
5454
end
5555

56+
local indent_line = function(current_line)
57+
local pos = vim.api.nvim_win_get_cursor(0)
58+
-- There's probably a better way to do this indent, but I don't know what it is
59+
vim.cmd(current_line + 1 .. "normal! ==")
60+
vim.api.nvim_win_set_cursor(0, pos)
61+
end
62+
5663
M.debugprint = function(o)
5764
local funcopts = vim.tbl_deep_extend(
5865
"force",
@@ -66,7 +73,6 @@ M.debugprint = function(o)
6673

6774
local current_line = vim.api.nvim_win_get_cursor(0)[1]
6875
local filetype = vim.api.nvim_get_option_value("filetype", {})
69-
local indent = string.rep(" ", vim.fn.indent(current_line))
7076
local fixes = get_fix(filetype)
7177

7278
if fixes == nil then
@@ -79,14 +85,13 @@ M.debugprint = function(o)
7985
if funcopts.variable then
8086
local variable_name = vim.fn.input("Variable name: ")
8187

82-
line_to_insert = indent
83-
.. fixes.left
88+
line_to_insert = fixes.left
8489
.. debuginfo(variable_name)
8590
.. fixes.mid_var
8691
.. variable_name
8792
.. fixes.right_var
8893
else
89-
line_to_insert = indent .. fixes.left .. debuginfo() .. fixes.right
94+
line_to_insert = fixes.left .. debuginfo() .. fixes.right
9095
end
9196

9297
if funcopts.above then
@@ -102,6 +107,8 @@ M.debugprint = function(o)
102107
true,
103108
{ line_to_insert }
104109
)
110+
111+
indent_line(line_to_insert_on)
105112
end
106113

107114
M.setup = function(o)

tests/debugprint.lua

+79
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,82 @@ describe("can do various file types", function()
206206
})
207207
end)
208208
end)
209+
210+
describe("can do indenting correctly", function()
211+
before_each(function()
212+
debugprint.setup()
213+
end)
214+
215+
it("lua - inside function", function()
216+
set_lines({
217+
"function()",
218+
"end",
219+
})
220+
221+
local filename = write_file("lua")
222+
vim.api.nvim_set_option_value("shiftwidth", 4, {})
223+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
224+
feedkeys("dqp")
225+
226+
check_lines({
227+
"function()",
228+
" print('DEBUG: " .. filename .. ":1 [1]')",
229+
"end",
230+
})
231+
end)
232+
233+
it("lua - inside function from below", function()
234+
set_lines({
235+
"function()",
236+
"end",
237+
})
238+
239+
local filename = write_file("lua")
240+
vim.api.nvim_set_option_value("shiftwidth", 4, {})
241+
vim.api.nvim_win_set_cursor(0, { 2, 0 })
242+
feedkeys("dqP")
243+
244+
check_lines({
245+
"function()",
246+
" print('DEBUG: " .. filename .. ":2 [1]')",
247+
"end",
248+
})
249+
end)
250+
251+
it("lua - above function", function()
252+
set_lines({
253+
"function()",
254+
"end",
255+
})
256+
257+
local filename = write_file("lua")
258+
vim.api.nvim_set_option_value("shiftwidth", 4, {})
259+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
260+
feedkeys("dqP")
261+
262+
check_lines({
263+
"print('DEBUG: " .. filename .. ":1 [1]')",
264+
"function()",
265+
"end",
266+
})
267+
end)
268+
269+
it("lua - inside function using tabs", function()
270+
set_lines({
271+
"function()",
272+
"end",
273+
})
274+
275+
local filename = write_file("lua")
276+
vim.api.nvim_set_option_value("expandtab", false, {})
277+
vim.api.nvim_set_option_value("shiftwidth", 8, {})
278+
vim.api.nvim_win_set_cursor(0, { 1, 0 })
279+
feedkeys("dqp")
280+
281+
check_lines({
282+
"function()",
283+
"\tprint('DEBUG: " .. filename .. ":1 [1]')",
284+
"end",
285+
})
286+
end)
287+
end)

0 commit comments

Comments
 (0)