From 81614f36bf6796f44c3ef793fbacffe1d41f8d87 Mon Sep 17 00:00:00 2001 From: Quinn Date: Tue, 23 Dec 2025 09:56:18 +0100 Subject: [PATCH] fix: smart indentation was counting \t as a character. The code now counts the amount of characters up to a \t, which we know is already aligned. --- .config/nvim/lua/config/maps.lua | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.config/nvim/lua/config/maps.lua b/.config/nvim/lua/config/maps.lua index e58b707..43964c5 100644 --- a/.config/nvim/lua/config/maps.lua +++ b/.config/nvim/lua/config/maps.lua @@ -38,14 +38,20 @@ Map('n', 'gr', vim.lsp.buf.references, { desc = "Go to the reference" }) -- smart tabulation Map('i', '', function() local _, col = unpack(vim.api.nvim_win_get_cursor(0)) -- get cursor position - local isindent = vim.api.nvim_get_current_line():sub(1, col):match('^%s*$') + local ln = vim.api.nvim_get_current_line():sub(1, col) + local isindent = ln:match('^%s*$') local tabexpnd = vim.bo.expandtab local tabwidth = vim.bo.shiftwidth > 0 and vim.bo.shiftwidth or vim.bo.tabstop if not tabexpnd and isindent then return '\t' - else - local spaces = tabwidth - col % tabwidth + else -- Compute the amount of spaces needed for the line. + local i = col + while i >= 1 do + if ln:sub(i, i) == '\t' then break end + i = i - 1 + end + local spaces = tabwidth - (col - i) % tabwidth return string.rep(' ', spaces == 0 and tabwidth or spaces) end end, { expr = true, desc = "smart tabulation" })