fix(tables): auto-realign on edit + stop dragging trailing | onto new row
Two bugs in the insert-mode table editing path: 1. <Tab> past the last cell (and <CR> on a table row) used <CR>+typed keystrokes to create a new row, which split the current line at the cursor — pulling the trailing | down onto the next line and mangling the formatting. Both paths now go through helpers that append() a fresh row beside the current one without touching it. 2. The table never realigned to new content. Ported the LSP-side render_aligned_table algorithm to Lua + VimL so smart_tab, smart_shift_tab, and smart_return tighten column widths locally on every navigation. No vim-lsp / nuwiki server roundtrip required. Neovim side schedules the work via vim.schedule (textlock-safe); plain Vim hands off via <Cmd>:call …<CR> to keep insert mode and avoid the cmdline-mode flash. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+223
-49
@@ -582,28 +582,192 @@ function M.list_toggle_or_add_checkbox()
|
||||
vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line })
|
||||
end
|
||||
|
||||
-- ===== Pure-Lua table alignment =====
|
||||
--
|
||||
-- The LSP exposes `nuwiki.table.align` (see `M.table_align()` below),
|
||||
-- but that path is async — by the time the workspace edit lands the
|
||||
-- cursor has already been positioned by our keystrokes, which leaves
|
||||
-- the cursor visually mid-cell after columns expand. For insert-mode
|
||||
-- editing we replicate the alignment logic locally so we can realign
|
||||
-- *before* placing the cursor, all in a single `vim.schedule` tick.
|
||||
-- The algorithm matches `crates/nuwiki-lsp/src/commands.rs::ops::
|
||||
-- render_aligned_table`.
|
||||
|
||||
local function is_table_row(line)
|
||||
return line ~= nil and line:match('^%s*|.*|%s*$') ~= nil
|
||||
end
|
||||
|
||||
local function table_cell_starts(line)
|
||||
-- Returns 1-based byte positions of each `|` separator. Cell N spans
|
||||
-- (starts[N], starts[N+1]); placing the cursor at 0-based byte column
|
||||
-- `starts[i]` lands one position past the i-th `|` (the first space
|
||||
-- of the cell that the `|` opens).
|
||||
local out = {}
|
||||
local pos = 0
|
||||
while true do
|
||||
local bar = line:find('|', pos + 1, true)
|
||||
if not bar then break end
|
||||
table.insert(out, bar)
|
||||
pos = bar
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function find_table_range(row)
|
||||
-- 1-based inclusive line numbers spanning the table that contains `row`.
|
||||
local total = vim.api.nvim_buf_line_count(0)
|
||||
local s = row
|
||||
while s > 1 and is_table_row(vim.fn.getline(s - 1)) do
|
||||
s = s - 1
|
||||
end
|
||||
local e = row
|
||||
while e < total and is_table_row(vim.fn.getline(e + 1)) do
|
||||
e = e + 1
|
||||
end
|
||||
return s, e
|
||||
end
|
||||
|
||||
local function parse_table_row(line)
|
||||
local indent = line:match('^(%s*)') or ''
|
||||
local body = line:sub(#indent + 1)
|
||||
local segments = {}
|
||||
local pos = 1
|
||||
while true do
|
||||
local bar = body:find('|', pos, true)
|
||||
if not bar then
|
||||
table.insert(segments, body:sub(pos))
|
||||
break
|
||||
end
|
||||
table.insert(segments, body:sub(pos, bar - 1))
|
||||
pos = bar + 1
|
||||
end
|
||||
-- Drop the empty piece before the opening `|` and the trailing piece
|
||||
-- after the closing `|`.
|
||||
if #segments > 0 and segments[1] == '' then table.remove(segments, 1) end
|
||||
if #segments > 0 and segments[#segments]:match('^%s*$') then
|
||||
table.remove(segments, #segments)
|
||||
end
|
||||
for i, c in ipairs(segments) do
|
||||
segments[i] = c:match('^%s*(.-)%s*$') or ''
|
||||
end
|
||||
return indent, segments
|
||||
end
|
||||
|
||||
local function is_separator_cell(s)
|
||||
return s ~= '' and s:match('^[%-:%s]+$') ~= nil
|
||||
end
|
||||
|
||||
local function is_separator_row(cells)
|
||||
if #cells == 0 then return false end
|
||||
for _, c in ipairs(cells) do
|
||||
if not is_separator_cell(c) then return false end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local function content_width(s)
|
||||
-- Match the LSP-side `chars().count()` so user-triggered `gqq` and
|
||||
-- the auto-realign here agree on column widths.
|
||||
local ok, w = pcall(vim.fn.strchars, s, 1)
|
||||
if ok and type(w) == 'number' then return w end
|
||||
return #s
|
||||
end
|
||||
|
||||
local function compute_table_widths(rows)
|
||||
local widths = {}
|
||||
local max_cols = 0
|
||||
for _, r in ipairs(rows) do
|
||||
if #r.cells > max_cols then max_cols = #r.cells end
|
||||
end
|
||||
for i = 1, max_cols do widths[i] = 1 end
|
||||
for _, r in ipairs(rows) do
|
||||
if not is_separator_row(r.cells) then
|
||||
for i, c in ipairs(r.cells) do
|
||||
local w = content_width(c)
|
||||
if w > widths[i] then widths[i] = w end
|
||||
end
|
||||
end
|
||||
end
|
||||
return widths
|
||||
end
|
||||
|
||||
local function render_table_row(indent, cells, widths, all_sep)
|
||||
local out = { indent, '|' }
|
||||
for i = 1, #widths do
|
||||
local w = widths[i]
|
||||
if all_sep then
|
||||
table.insert(out, string.rep('-', w + 2))
|
||||
else
|
||||
local c = cells[i] or ''
|
||||
local pad = w - content_width(c)
|
||||
if pad < 0 then pad = 0 end
|
||||
table.insert(out, ' ' .. c .. string.rep(' ', pad) .. ' ')
|
||||
end
|
||||
table.insert(out, '|')
|
||||
end
|
||||
return table.concat(out)
|
||||
end
|
||||
|
||||
local function align_table_at(row)
|
||||
local s, e = find_table_range(row)
|
||||
local lines = vim.api.nvim_buf_get_lines(0, s - 1, e, false)
|
||||
local rows = {}
|
||||
for _, l in ipairs(lines) do
|
||||
local indent, cells = parse_table_row(l)
|
||||
table.insert(rows, { indent = indent, cells = cells })
|
||||
end
|
||||
local widths = compute_table_widths(rows)
|
||||
if #widths == 0 then return end
|
||||
local new_lines = {}
|
||||
for _, r in ipairs(rows) do
|
||||
table.insert(new_lines, render_table_row(r.indent, r.cells, widths, is_separator_row(r.cells)))
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(0, s - 1, e, false, new_lines)
|
||||
end
|
||||
|
||||
local function ensure_insert_mode()
|
||||
if vim.api.nvim_get_mode().mode ~= 'i' then
|
||||
vim.cmd('startinsert')
|
||||
end
|
||||
end
|
||||
|
||||
local function schedule_insert_table_row_below(row)
|
||||
-- After `align_table_at`, the row count of the table containing
|
||||
-- `row` is fixed and `getline(row)` reflects the aligned content.
|
||||
vim.schedule(function()
|
||||
align_table_at(row)
|
||||
local new_line = vim.fn.getline(row)
|
||||
local starts = table_cell_starts(new_line)
|
||||
if #starts < 2 then return end
|
||||
local indent = new_line:match('^(%s*)') or ''
|
||||
local cell_count = #starts - 1
|
||||
local new_row = indent .. '|' .. string.rep(' |', cell_count)
|
||||
vim.api.nvim_buf_set_lines(0, row, row, false, { new_row })
|
||||
vim.api.nvim_win_set_cursor(0, { row + 1, #indent + 1 })
|
||||
ensure_insert_mode()
|
||||
end)
|
||||
end
|
||||
|
||||
--- Smart `<CR>` for insert mode — vimwiki parity for `:VimwikiReturn`.
|
||||
--- Used as an `<expr>` keymap so the function is side-effect-free on
|
||||
--- the buffer (textlock is active). The returned string is fed into
|
||||
--- Vim's input stream — we encode everything (including the table-row
|
||||
--- insertion) as keystrokes so the user's typed keys after `<CR>` land
|
||||
--- at the right cursor position.
|
||||
--- the buffer (textlock is active). Non-table branches encode their
|
||||
--- behaviour as returned keystrokes; the table-row branch schedules a
|
||||
--- post-textlock callback that aligns the table, inserts a fresh row,
|
||||
--- and places the cursor inside the new row's first cell.
|
||||
function M.smart_return()
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local function tc(s) return vim.api.nvim_replace_termcodes(s, true, false, true) end
|
||||
local cr = tc('<CR>')
|
||||
local esc = tc('<Esc>')
|
||||
|
||||
-- Table row → newline + empty row + cursor inside first cell.
|
||||
if line:match('^%s*|.*|%s*$') then
|
||||
local cell_count = 0
|
||||
for _ in line:gmatch('|') do cell_count = cell_count + 1 end
|
||||
cell_count = math.max(cell_count - 1, 1)
|
||||
local new_row = '|' .. string.rep(' |', cell_count)
|
||||
-- After `<CR>` + new_row, cursor sits at end of new row. Bounce to
|
||||
-- normal, jump to col 0, step right past the leading `|`, re-enter
|
||||
-- insert: `<Esc>0li` puts the cursor immediately after the `|`.
|
||||
return cr .. new_row .. esc .. '0li'
|
||||
-- Table row → align existing rows, then add a fresh empty row below.
|
||||
-- Doing this via `<CR>` + typed row keystrokes would split the
|
||||
-- current line at the cursor and drag the trailing `|` onto the new
|
||||
-- line — so we punt to `vim.schedule` instead.
|
||||
if is_table_row(line) then
|
||||
local cur = vim.api.nvim_win_get_cursor(0)
|
||||
schedule_insert_table_row_below(cur[1])
|
||||
return ''
|
||||
end
|
||||
|
||||
-- List item with marker?
|
||||
@@ -629,66 +793,76 @@ end
|
||||
|
||||
--- Insert-mode `<Tab>` / `<S-Tab>` table navigation — vimwiki parity.
|
||||
--- Used as `<expr>` keymaps so we can fall back to a literal `<Tab>` /
|
||||
--- `<S-Tab>` when the cursor isn't on a table row. Inside a row:
|
||||
--- `<Tab>` jumps to the next cell, creating a fresh row below the
|
||||
--- current one if we were in the final cell. `<S-Tab>` jumps to the
|
||||
--- previous cell, no-op past the first.
|
||||
local function table_cell_starts(line)
|
||||
-- Returns 0-based byte offsets right after each `|` separator.
|
||||
local out = {}
|
||||
local pos = 0
|
||||
while true do
|
||||
local bar = line:find('|', pos + 1, true)
|
||||
if not bar then break end
|
||||
table.insert(out, bar) -- 1-based: position immediately AFTER the |
|
||||
pos = bar
|
||||
end
|
||||
return out
|
||||
end
|
||||
--- `<S-Tab>` when the cursor isn't on a table row. Inside a row both
|
||||
--- variants align the table first (so columns stay tight as cells
|
||||
--- grow), then jump to the adjacent cell; `<Tab>` past the final cell
|
||||
--- adds a fresh row below, `<S-Tab>` past the first cell is a no-op.
|
||||
|
||||
function M.smart_tab()
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local function tc(s) return vim.api.nvim_replace_termcodes(s, true, false, true) end
|
||||
local tab = tc('<Tab>')
|
||||
if not line:match('^%s*|.*|%s*$') then
|
||||
if not is_table_row(line) then
|
||||
return tab
|
||||
end
|
||||
local cell_starts = table_cell_starts(line)
|
||||
if #cell_starts < 2 then return tab end
|
||||
local _, col = unpack(vim.api.nvim_win_get_cursor(0)) -- 0-based byte col
|
||||
-- Find the next cell start strictly after the cursor's column.
|
||||
for _, start in ipairs(cell_starts) do
|
||||
if start > col + 1 and start < #line then
|
||||
-- `<Esc>` + jump to that col (`start + 1` 1-based → +1 for space
|
||||
-- inside the cell), then `i` to re-enter insert.
|
||||
local target = start + 1
|
||||
return tc('<Esc>') .. tostring(target) .. '|i'
|
||||
end
|
||||
local cur = vim.api.nvim_win_get_cursor(0)
|
||||
local row, col = cur[1], cur[2]
|
||||
local current_idx = 1
|
||||
for i, start in ipairs(cell_starts) do
|
||||
if start <= col + 1 then current_idx = i end
|
||||
end
|
||||
-- Past last cell → insert a new row below, cursor in first cell.
|
||||
local cell_count = #cell_starts - 1
|
||||
local new_row = '|' .. string.rep(' |', cell_count)
|
||||
return tc('<CR>') .. new_row .. tc('<Esc>') .. '0li'
|
||||
local target_idx = current_idx + 1
|
||||
|
||||
vim.schedule(function()
|
||||
align_table_at(row)
|
||||
local new_line = vim.fn.getline(row)
|
||||
local starts = table_cell_starts(new_line)
|
||||
if target_idx < #starts then
|
||||
-- Land at the first space of the target cell (byte position
|
||||
-- equal to the 1-based pipe position).
|
||||
vim.api.nvim_win_set_cursor(0, { row, starts[target_idx] })
|
||||
ensure_insert_mode()
|
||||
else
|
||||
local indent = new_line:match('^(%s*)') or ''
|
||||
local cell_count = math.max(#starts - 1, 1)
|
||||
local new_row = indent .. '|' .. string.rep(' |', cell_count)
|
||||
vim.api.nvim_buf_set_lines(0, row, row, false, { new_row })
|
||||
vim.api.nvim_win_set_cursor(0, { row + 1, #indent + 1 })
|
||||
ensure_insert_mode()
|
||||
end
|
||||
end)
|
||||
return ''
|
||||
end
|
||||
|
||||
function M.smart_shift_tab()
|
||||
local line = vim.api.nvim_get_current_line()
|
||||
local function tc(s) return vim.api.nvim_replace_termcodes(s, true, false, true) end
|
||||
local stab = tc('<S-Tab>')
|
||||
if not line:match('^%s*|.*|%s*$') then
|
||||
if not is_table_row(line) then
|
||||
return stab
|
||||
end
|
||||
local cell_starts = table_cell_starts(line)
|
||||
if #cell_starts < 2 then return stab end
|
||||
local _, col = unpack(vim.api.nvim_win_get_cursor(0))
|
||||
-- Find the cell the cursor is in, then go to the *previous* cell.
|
||||
local cur = vim.api.nvim_win_get_cursor(0)
|
||||
local row, col = cur[1], cur[2]
|
||||
local current_idx
|
||||
for i, start in ipairs(cell_starts) do
|
||||
if start <= col + 1 then current_idx = i end
|
||||
end
|
||||
if not current_idx or current_idx <= 1 then return stab end
|
||||
local target = cell_starts[current_idx - 1]
|
||||
return tc('<Esc>') .. tostring(target + 1) .. '|i'
|
||||
local target_idx = current_idx - 1
|
||||
|
||||
vim.schedule(function()
|
||||
align_table_at(row)
|
||||
local new_line = vim.fn.getline(row)
|
||||
local starts = table_cell_starts(new_line)
|
||||
if target_idx < 1 or target_idx >= #starts then return end
|
||||
vim.api.nvim_win_set_cursor(0, { row, starts[target_idx] })
|
||||
ensure_insert_mode()
|
||||
end)
|
||||
return ''
|
||||
end
|
||||
|
||||
-- §13.1 Cluster B — table rewriters.
|
||||
|
||||
Reference in New Issue
Block a user