Skip to content

Commit ad2a3c1

Browse files
committed
feat: support default indent
1 parent 3290753 commit ad2a3c1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lua/format/init.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ function M.format(bang, user_input, start_line, end_line, opts)
8585
end
8686
end
8787

88+
local indent = 0
89+
90+
if start_line > 1 then
91+
indent = vim.fn.matchstr(vim.fn.getline(start_line - 1), '^\\s*')
92+
end
93+
8894
task.run({
8995
bufnr = vim.fn.bufnr(),
9096
stdin = vim.api.nvim_buf_get_lines(0, start_line - 1, end_line, false),
@@ -94,6 +100,7 @@ function M.format(bang, user_input, start_line, end_line, opts)
94100
timeout = timeout,
95101
hooks = opts.hooks,
96102
lock_buf = opts.lock_buf,
103+
indent = indent,
97104
})
98105
end
99106

lua/format/task.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ local function on_exit(id, code, single)
4040
else
4141
formatted_context = stdout
4242
end
43+
if #current_task.indent > 0 then
44+
formatted_context = (function()
45+
return vim.tbl_map(function(t)
46+
return current_task.indent .. t
47+
end, formatted_context)
48+
end)()
49+
end
4350
if table.concat(formatted_context, '\n') == table.concat(current_task.stdin, '\n') then
4451
util.msg('no necessary changes')
4552
else

0 commit comments

Comments
 (0)