|
| 1 | +local M = {} |
| 2 | + |
| 3 | +M.default_opts = { |
| 4 | + split_line_attributes = false, |
| 5 | +} |
| 6 | + |
| 7 | +local cli = require("dioxus.cli") |
| 8 | + |
| 9 | +M.setup = function(opts) |
| 10 | + M.opts = vim.tbl_deep_extend("force", {}, M.default_opts, opts or {}) |
| 11 | +end |
| 12 | + |
| 13 | +M.format_buffer = function() |
| 14 | + local bufnr = vim.api.nvim_get_current_buf() |
| 15 | + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) |
| 16 | + local content = table.concat(lines, "\n") |
| 17 | + |
| 18 | + local temp_file = vim.fn.tempname() .. ".rs" |
| 19 | + local file = io.open(temp_file, "w") |
| 20 | + if not file then |
| 21 | + vim.notify("Failed to create temporary file", vim.log.levels.ERROR) |
| 22 | + return |
| 23 | + end |
| 24 | + file:write(content) |
| 25 | + file:close() |
| 26 | + |
| 27 | + local command = "fmt --file " .. vim.fn.shellescape(temp_file) |
| 28 | + |
| 29 | + -- additonal args |
| 30 | + if M.opts and M.opts.split_line_attributes then |
| 31 | + command = command .. " --split-line-attributes" |
| 32 | + end |
| 33 | + |
| 34 | + local output = cli.execute(command) |
| 35 | + |
| 36 | + if not output then |
| 37 | + return |
| 38 | + end |
| 39 | + |
| 40 | + file = io.open(temp_file, "r") |
| 41 | + if not file then |
| 42 | + vim.notify("Failed to read formatted file", vim.log.levels.ERROR) |
| 43 | + os.remove(temp_file) |
| 44 | + return |
| 45 | + end |
| 46 | + |
| 47 | + local formatted_content = file:read("*all") |
| 48 | + file:close() |
| 49 | + os.remove(temp_file) |
| 50 | + |
| 51 | + if formatted_content == content then |
| 52 | + return |
| 53 | + end |
| 54 | + |
| 55 | + local formatted_lines = vim.split(formatted_content, "\n", { trimempty = true }) |
| 56 | + |
| 57 | + local cursor_pos = vim.api.nvim_win_get_cursor(0) |
| 58 | + |
| 59 | + vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, formatted_lines) |
| 60 | + |
| 61 | + vim.api.nvim_win_set_cursor(0, cursor_pos) |
| 62 | +end |
| 63 | + |
| 64 | +M.format_selection = function() |
| 65 | + local start_buf, start_row, start_col, _ = unpack(vim.fn.getpos("'<")) |
| 66 | + local _, end_row, end_col, _ = unpack(vim.fn.getpos("'>")) |
| 67 | + |
| 68 | + local lines = vim.fn.getline(start_row, end_row) |
| 69 | + |
| 70 | + local content = type(lines) == "table" and table.concat(lines, "\n") or tostring(lines) |
| 71 | + |
| 72 | + local command = "fmt --raw " .. vim.fn.shellescape(content) |
| 73 | + |
| 74 | + -- additonal args |
| 75 | + if M.opts and M.opts.split_line_attributes then |
| 76 | + command = command .. " --split-line-attributes" |
| 77 | + end |
| 78 | + |
| 79 | + local output = cli.execute(command) |
| 80 | + if not output then |
| 81 | + return |
| 82 | + end |
| 83 | + |
| 84 | + local end_line_text = vim.api.nvim_buf_get_lines(start_buf, end_row - 1, end_row, false)[1] or "" |
| 85 | + local actual_end_col = math.min(end_col, #end_line_text) |
| 86 | + |
| 87 | + local output_lines = vim.split(output, "\n", { trimempty = true }) |
| 88 | + |
| 89 | + if #output_lines == 0 then |
| 90 | + output_lines = { "" } |
| 91 | + end |
| 92 | + |
| 93 | + vim.api.nvim_buf_set_text(start_buf, start_row - 1, start_col - 1, end_row - 1, actual_end_col, output_lines) |
| 94 | +end |
| 95 | + |
| 96 | +return M |
0 commit comments