Skip to content

Commit bbf80d3

Browse files
committed
feat(format_editor): add support for alignments
1 parent 3b5f196 commit bbf80d3

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

lua/care/extras/format_editor.lua

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ local function get_config_function()
2121
return new_contents
2222
end
2323

24-
function FormatEditor.draw(entries, ns, buf, format_entry)
24+
function FormatEditor.draw(entries, ns, buf, format_entry, alignments)
2525
local function get_texts(aligned_sec)
2626
local texts = {}
2727
for _, aligned_chunks in ipairs(aligned_sec) do
@@ -78,7 +78,6 @@ function FormatEditor.draw(entries, ns, buf, format_entry)
7878
return aligned_table
7979
end
8080

81-
local alignments = require("care.config").options.ui.menu.alignments or {}
8281
local width, _ = get_width()
8382
local aligned_table = get_align_tables()
8483
local column = 0
@@ -127,7 +126,7 @@ function FormatEditor.draw(entries, ns, buf, format_entry)
127126
end
128127
end
129128

130-
local function open_test_buf(contents)
129+
local function open_test_buf(contents, alignments)
131130
local ns = vim.api.nvim_create_namespace("care-format-editor")
132131
local buf = vim.api.nvim_create_buf(false, true)
133132
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { string.rep(" ", 100), string.rep(" ", 100) })
@@ -146,9 +145,8 @@ local function open_test_buf(contents)
146145
table.insert(new_contents, 1, entry)
147146
table.insert(new_contents, 1, data)
148147
table.insert(new_contents, "return format_entry(entry, data)")
149-
print(table.concat(new_contents, "\n"))
150148
return loadstring(table.concat(new_contents, "\n"))()
151-
end)
149+
end, alignments)
152150
vim.api.nvim_open_win(buf, true, {
153151
relative = "editor",
154152
style = "minimal",
@@ -163,6 +161,10 @@ end
163161
function FormatEditor.start()
164162
local buf = vim.api.nvim_create_buf(false, true)
165163
local contents = get_config_function()
164+
table.insert(
165+
contents,
166+
'local alignments = { "' .. table.concat(require("care.config").options.ui.menu.alignments, '", "') .. '" }'
167+
)
166168
vim.api.nvim_buf_set_lines(buf, 0, -1, false, contents)
167169
vim.bo[buf].ft = "lua"
168170
vim.api.nvim_open_win(buf, true, {
@@ -175,7 +177,25 @@ function FormatEditor.start()
175177
border = "rounded",
176178
})
177179
vim.keymap.set("n", "<cr>", function()
178-
open_test_buf(vim.api.nvim_buf_get_lines(buf, 0, -1, false))
180+
open_test_buf(
181+
vim.api.nvim_buf_get_lines(buf, 0, -2, false),
182+
vim.iter(
183+
vim.split(
184+
vim.api.nvim_buf_get_lines(0, -2, -1, false)[1]:gsub("local alignments.*=.*{.-(.*).-}", "%1"),
185+
","
186+
)
187+
)
188+
:map(function(alignment)
189+
alignment = vim.trim(alignment)
190+
alignment = alignment:gsub([=[['"](.*)[%'"]]=], "%1")
191+
if not vim.tbl_contains({ "left", "right", "center" }, alignment) then
192+
vim.notify("Ignoring unknow aligment: " .. alignment)
193+
alignment = "left"
194+
end
195+
return vim.trim(alignment)
196+
end)
197+
:totable()
198+
)
179199
end, { buffer = buf })
180200
end
181201
FormatEditor.start()

0 commit comments

Comments
 (0)