phase 2: remove CodeWriter
Some checks failed
ci / ci (push) Failing after 3m7s

This commit is contained in:
2026-04-04 17:05:56 -06:00
parent b00d8d2fa7
commit f12928749b
4 changed files with 55 additions and 148 deletions

View File

@@ -1,4 +1,3 @@
local CodeWriter = require 'u.codewriter'
local Range = require 'u.range'
local vim_repeat = require 'u.repeat'
@@ -8,8 +7,27 @@ local M = {}
--- @param left string
--- @param right string
local function split(bracket_range, left, right)
local code = CodeWriter.from_pos(bracket_range.start)
code:write_raw(left)
local bufnr = bracket_range.start.bufnr
local first_line = Range.from_line(bufnr, bracket_range.start.lnum):text()
local ws = first_line:match '^%s*'
local expandtab = vim.bo[bufnr].expandtab
local shiftwidth = vim.bo[bufnr].shiftwidth
local indent_str, base_indent
if expandtab then
indent_str = string.rep(' ', shiftwidth)
base_indent = math.floor(#ws / shiftwidth)
else
indent_str = '\t'
base_indent = #ws
end
local lines = {}
local function write(line, indent_offset)
table.insert(lines, indent_str:rep(base_indent + (indent_offset or 0)) .. line)
end
table.insert(lines, left)
local curr = bracket_range.start:next()
if curr == nil then return end
@@ -27,7 +45,7 @@ local function split(bracket_range, left, right)
if vim.tbl_contains({ ',', ';' }, curr:char()) then
-- accumulate item:
local item = vim.trim(Range.new(last_start, curr):text())
if item ~= '' then code:indent():write(item) end
if item ~= '' then write(item, 1) end
local next_last_start = curr:next()
if next_last_start == nil then break end
@@ -45,11 +63,11 @@ local function split(bracket_range, left, right)
local pos_before_right = bracket_range.stop:must_next(-1)
if last_start < pos_before_right then
local item = vim.trim(Range.new(last_start, pos_before_right):text())
if item ~= '' then code:indent():write(item) end
if item ~= '' then write(item, 1) end
end
code:write(right)
bracket_range:replace(code.lines)
write(right)
bracket_range:replace(lines)
end
--- @param bracket_range u.Range