Skip to content

Commit fbcab1c

Browse files
committed
Refactor
1 parent fcb838d commit fbcab1c

File tree

6 files changed

+610
-544
lines changed

6 files changed

+610
-544
lines changed

lua/crazywall/commands/crazywall.lua

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
local PluginContext = require("lua.crazywall.context")
2+
local Config = require("core.config")
3+
local Context = require("core.context")
4+
local streams = require("core.streams")
5+
local plugin_utils = require("lua.crazywall.utils")
6+
local fold = require("core.fold")
7+
8+
---@param plugin_state PluginState
9+
return function(plugin_state)
10+
vim.api.nvim_create_user_command("Crazywall", function(opts)
11+
local plugin_ctx, err = PluginContext:new(
12+
opts.fargs[1] or "both",
13+
opts.fargs[2] or "warn",
14+
opts.fargs[3] or vim.fn.expand("%"),
15+
opts.fargs[4] or vim.fn.expand("%")
16+
)
17+
18+
if not plugin_ctx then
19+
vim.api.nvim_err_writeln(assert(err))
20+
return
21+
end
22+
23+
local buf_id = plugin_utils.find_buffer(plugin_ctx.src_path_str)
24+
if
25+
not plugin_utils.handle_unsaved(
26+
buf_id,
27+
plugin_ctx.on_unsaved == "write"
28+
)
29+
then
30+
return
31+
end
32+
33+
if plugin_state:get_current_config() == nil then
34+
return vim.api.nvim_err_writeln(
35+
"crazywall: Could not find config "
36+
.. plugin_state:get_current_config()
37+
.. "."
38+
)
39+
end
40+
41+
local config
42+
config, err = Config:new(assert(plugin_state:get_current_config()))
43+
if not config then
44+
return plugin_utils.display_err(err)
45+
end
46+
47+
local ctx
48+
ctx, err = Context:new(
49+
config,
50+
plugin_ctx.src_path_str,
51+
plugin_ctx.dest_path_str,
52+
vim.fn.readfile(plugin_ctx.src_path_str),
53+
nil,
54+
false,
55+
false,
56+
(
57+
plugin_ctx.output_style == "planonly"
58+
or plugin_ctx.output_style == "both"
59+
)
60+
and streams.STDOUT
61+
or streams.NONE,
62+
(
63+
plugin_ctx.output_style == "textonly"
64+
or plugin_ctx.output_style == "both"
65+
)
66+
and streams.STDOUT
67+
or streams.NONE,
68+
false,
69+
false
70+
)
71+
72+
if not ctx then
73+
return plugin_utils.display_err(err)
74+
end
75+
76+
local plan, root
77+
plan, root, err = plugin_utils.do_fold(buf_id, ctx, true)
78+
if not plan or not root then
79+
return plugin_utils.display_err(err)
80+
end
81+
82+
local lines =
83+
plugin_utils.get_plan_and_text_lines(ctx, plugin_ctx, plan)
84+
85+
local float_buf_id
86+
float_buf_id, err = plugin_utils.display_floating_window(lines)
87+
if not float_buf_id then
88+
return nil, err
89+
end
90+
vim.cmd("setlocal nowrap")
91+
print(":w -> CONFIRM :q -> EXIT")
92+
93+
vim.api.nvim_create_autocmd("BufWritePost", {
94+
buffer = float_buf_id,
95+
callback = function()
96+
if not vim.api.nvim_buf_is_valid(float_buf_id) then
97+
return
98+
end
99+
vim.api.nvim_buf_call(float_buf_id, function()
100+
vim.cmd("write!")
101+
end)
102+
_, err = fold.execute(root, ctx)
103+
if err then
104+
return plugin_utils.display_err(err)
105+
end
106+
local plan_path = plugin_utils.get_log_path()
107+
err = fold.write(plan_path, tostring(plan))
108+
if err then
109+
return plugin_utils.display_err(err)
110+
end
111+
vim.cmd("bwipeout! " .. float_buf_id)
112+
if buf_id then
113+
vim.api.nvim_buf_call(buf_id, function()
114+
vim.cmd("edit")
115+
end)
116+
end
117+
end,
118+
})
119+
end, {
120+
nargs = "*",
121+
complete = function(_, line)
122+
local args = vim.split(line, " ")
123+
if #args == 2 then
124+
return PluginContext["output_options"]
125+
end
126+
if #args == 3 then
127+
return PluginContext["on_unsaved_options"]
128+
end
129+
if #args == 4 then
130+
return { vim.fn.expand("%") }
131+
end
132+
if #args == 5 then
133+
return { vim.fn.expand("%") }
134+
end
135+
return {}
136+
end,
137+
desc = "Applies crazywall to a file.",
138+
})
139+
end
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
local PluginContext = require("crazywall.context")
2+
local Config = require("core.config")
3+
local Context = require("core.context")
4+
local streams = require("core.streams")
5+
local plugin_utils = require("lua.crazywall.utils")
6+
7+
---@param plugin_state PluginState
8+
return function(plugin_state)
9+
vim.api.nvim_create_user_command("CrazywallDry", function(opts)
10+
local plugin_ctx, err = PluginContext:new(
11+
opts.fargs[1] or "both",
12+
opts.fargs[2] or "warn",
13+
opts.fargs[3] or vim.fn.expand("%"),
14+
opts.fargs[4] or vim.fn.expand("%")
15+
)
16+
17+
if not plugin_ctx then
18+
vim.api.nvim_err_writeln(assert(err))
19+
return
20+
end
21+
22+
local buf_id = plugin_utils.find_buffer(plugin_ctx.src_path_str)
23+
if
24+
not plugin_utils.handle_unsaved(
25+
buf_id,
26+
plugin_ctx.on_unsaved == "write"
27+
)
28+
then
29+
return
30+
end
31+
32+
if not plugin_state:get_current_config() then
33+
return vim.api.nvim_err_writeln(
34+
"crazywall: Could not find config "
35+
.. plugin_state.current_config_name
36+
.. "."
37+
)
38+
end
39+
40+
local config
41+
config, err = Config:new(assert(plugin_state:get_current_config()))
42+
if not config then
43+
return plugin_utils.display_err(err)
44+
end
45+
46+
local ctx
47+
ctx, err = Context:new(
48+
config,
49+
plugin_ctx.src_path_str,
50+
plugin_ctx.dest_path_str,
51+
vim.fn.readfile(plugin_ctx.src_path_str),
52+
nil,
53+
true,
54+
false,
55+
(
56+
plugin_ctx.output_style == "planonly"
57+
or plugin_ctx.output_style == "both"
58+
)
59+
and streams.STDOUT
60+
or streams.NONE,
61+
(
62+
plugin_ctx.output_style == "textonly"
63+
or plugin_ctx.output_style == "both"
64+
)
65+
and streams.STDOUT
66+
or streams.NONE,
67+
true,
68+
false
69+
)
70+
71+
if not ctx then
72+
return plugin_utils.display_err(err)
73+
end
74+
75+
local plan
76+
plan, _, err = plugin_utils.do_fold(buf_id, ctx, true)
77+
if not plan then
78+
return plugin_utils.display_err(err)
79+
end
80+
81+
local lines =
82+
plugin_utils.get_plan_and_text_lines(ctx, plugin_ctx, plan)
83+
84+
_, err = plugin_utils.display_floating_window(lines)
85+
if err then
86+
return nil, err
87+
end
88+
89+
vim.cmd("setlocal nowrap")
90+
end, {
91+
nargs = "*",
92+
complete = function(_, line)
93+
local args = vim.split(line, " ")
94+
if #args == 2 then
95+
return PluginContext["output_options"]
96+
end
97+
if #args == 3 then
98+
return PluginContext["on_unsaved_options"]
99+
end
100+
if #args == 4 then
101+
return { vim.fn.expand("%") }
102+
end
103+
if #args == 5 then
104+
return { vim.fn.expand("%") }
105+
end
106+
return {}
107+
end,
108+
desc = "Applies crazywall to a file in dry-run mode.",
109+
})
110+
end
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
local PluginContext = require("crazywall.context")
2+
local plugin_utils = require("lua.crazywall.utils")
3+
local Config = require("core.config")
4+
local Context = require("core.context")
5+
local streams = require("core.streams")
6+
7+
---@param plugin_state PluginState
8+
return function(plugin_state)
9+
vim.api.nvim_create_user_command("CrazywallQuick", function(opts)
10+
local plugin_ctx, err = PluginContext:new(
11+
"both",
12+
opts.fargs[1] or "warn",
13+
opts.fargs[2] or vim.fn.expand("%"),
14+
opts.fargs[3] or vim.fn.expand("%")
15+
)
16+
17+
if not plugin_ctx then
18+
vim.api.nvim_err_writeln(assert(err))
19+
return
20+
end
21+
22+
local buf_id = plugin_utils.find_buffer(plugin_ctx.src_path_str)
23+
if
24+
not plugin_utils.handle_unsaved(
25+
buf_id,
26+
plugin_ctx.on_unsaved == "write"
27+
)
28+
then
29+
return
30+
end
31+
32+
if plugin_state:get_current_config() == nil then
33+
return vim.api.nvim_err_writeln(
34+
"crazywall: Could not find config "
35+
.. plugin_state:get_current_config()
36+
.. "."
37+
)
38+
end
39+
40+
local config
41+
config, err = Config:new(assert(plugin_state:get_current_config()))
42+
if not config then
43+
return plugin_utils.display_err(err)
44+
end
45+
46+
local ctx
47+
ctx, err = Context:new(
48+
config,
49+
plugin_ctx.src_path_str,
50+
plugin_ctx.dest_path_str,
51+
vim.fn.readfile(plugin_ctx.src_path_str),
52+
nil,
53+
false,
54+
true,
55+
streams.NONE,
56+
streams.NONE,
57+
false,
58+
false
59+
)
60+
61+
if not ctx then
62+
return plugin_utils.display_err(err)
63+
end
64+
65+
local plan
66+
plan, _, err = plugin_utils.do_fold(buf_id, ctx, false)
67+
if not plan then
68+
return plugin_utils.display_err(err)
69+
end
70+
71+
local plan_path = plugin_utils.get_log_path()
72+
err = plugin_utils.write(plan_path, tostring(plan))
73+
if err then
74+
return plugin_utils.display_err(err)
75+
end
76+
end, {
77+
nargs = "*",
78+
complete = function(_, line)
79+
local args = vim.split(line, " ")
80+
if #args == 2 then
81+
return PluginContext["on_unsaved_options"]
82+
end
83+
if #args == 3 then
84+
return { vim.fn.expand("%") }
85+
end
86+
if #args == 4 then
87+
return { vim.fn.expand("%") }
88+
end
89+
return {}
90+
end,
91+
desc = "Applies crazywall to a file, skipping the confirmation window.",
92+
})
93+
end

0 commit comments

Comments
 (0)