Skip to content

Commit c93893c

Browse files
committed
Fix "plan-only"/"text-only" bug with :CrazywallDry
1 parent dc7a9e6 commit c93893c

File tree

1 file changed

+40
-12
lines changed

1 file changed

+40
-12
lines changed

lua/crazywall/init.lua

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ local display_floating_window = function(lines)
8080
local editor_height = vim.api.nvim_get_option("lines")
8181
local editor_width = vim.api.nvim_get_option("columns")
8282
local win_height = math.min(math.floor(editor_height * 0.75), #lines)
83+
win_height = math.max(win_height, 1)
8384
local longest_line_length = (function()
8485
local res = 0
8586
for _, line in ipairs(lines) do
@@ -89,9 +90,9 @@ local display_floating_window = function(lines)
8990
end)()
9091
local win_width =
9192
math.min(math.floor(editor_width * 0.75), longest_line_length)
93+
win_width = math.max(win_width, 1)
9294
local row = math.floor((editor_height - win_height) / 2)
9395
local col = math.floor((editor_width - win_width) / 2)
94-
9596
local win = vim.api.nvim_open_win(buf_id, true, {
9697
relative = "editor",
9798
width = win_width,
@@ -105,9 +106,22 @@ local display_floating_window = function(lines)
105106
return nil, "Failed to open floating window."
106107
end
107108
vim.api.nvim_buf_set_option(buf_id, "modifiable", false)
109+
vim.api.nvim_buf_set_option(buf_id, "modified", false)
110+
111+
vim.api.nvim_create_augroup(
112+
"CrazywallCloseBufferOnWindowClose",
113+
{ clear = true }
114+
)
108115

109-
local default_bg = vim.api.nvim_get_hl(0, { name= 'Normal'}).background
110-
vim.api.nvim_set_hl(0, 'NormalFloat', { bg = default_bg })
116+
vim.api.nvim_create_autocmd("WinLeave", {
117+
group = "CrazywallCloseBufferOnWindowClose",
118+
pattern = "*",
119+
callback = function()
120+
if vim.fn.bufnr("%") == buf_id then
121+
vim.cmd('b#|bwipeout! ' .. buf_id)
122+
end
123+
end,
124+
})
111125

112126
return buf_id
113127
end
@@ -266,7 +280,7 @@ end, {
266280
end
267281
return {}
268282
end,
269-
desc = "Folds a file with crazywall, skipping the confirmation window.",
283+
desc = "Applies crazywall to a file, skipping the confirmation window.",
270284
})
271285

272286
vim.api.nvim_create_user_command("CrazywallDry", function(opts)
@@ -308,10 +322,10 @@ vim.api.nvim_create_user_command("CrazywallDry", function(opts)
308322
nil,
309323
true,
310324
false,
311-
(output_style == "plan-only" or output_style == "both")
325+
(output_style == "planonly" or output_style == "both")
312326
and streams.STDOUT
313327
or streams.NONE,
314-
(output_style == "text-only" or output_style == "both")
328+
(output_style == "textonly" or output_style == "both")
315329
and streams.STDOUT
316330
or streams.NONE,
317331
true,
@@ -328,13 +342,29 @@ vim.api.nvim_create_user_command("CrazywallDry", function(opts)
328342
return display_err(err)
329343
end
330344

331-
_, err =
332-
display_floating_window(utils.str.split_lines_to_list(tostring(plan)))
345+
local text = {}
346+
if output_style == "planonly" or output_style == "both" then
347+
table.insert(text, "Plan (dry-run):")
348+
for line in utils.str.split_lines(tostring(plan)) do
349+
table.insert(text, line)
350+
end
351+
end
352+
if output_style == "textonly" or output_style == "both" then
353+
if #text ~= 0 then
354+
table.insert(text, "")
355+
end
356+
table.insert(text, "Text (dry-run):")
357+
for line in utils.str.split_lines(utils.str.join_lines(ctx.lines)) do
358+
table.insert(text, line)
359+
end
360+
end
361+
362+
_, err = display_floating_window(text)
333363
if err then
334364
return nil, err
335365
end
336366

337-
vim.cmd('setlocal nowrap')
367+
vim.cmd("setlocal nowrap")
338368
end, {
339369
nargs = "*",
340370
complete = function(_, line)
@@ -353,7 +383,7 @@ end, {
353383
end
354384
return {}
355385
end,
356-
desc = "Folds a file with crazywall, skipping the confirmation window.",
386+
desc = "Applies crazywall to a file, skipping the confirmation window.",
357387
})
358388

359389
M.setup = function(opts)
@@ -370,6 +400,4 @@ M.setup = function(opts)
370400
current_config_name = opts.default_config_name or current_config_name
371401
end
372402

373-
M.foo = 42
374-
375403
return M

0 commit comments

Comments
 (0)