From a7949a96fefc009e51fd76fd51741446c0f9bdb7 Mon Sep 17 00:00:00 2001 From: rootiest Date: Wed, 28 Aug 2024 06:36:13 -0400 Subject: [PATCH] feat: add automatic indentation insertion based on previous line --- lua/utils/indentor.lua | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 lua/utils/indentor.lua diff --git a/lua/utils/indentor.lua b/lua/utils/indentor.lua new file mode 100644 index 0000000..47379a3 --- /dev/null +++ b/lua/utils/indentor.lua @@ -0,0 +1,60 @@ +local M = {} + +local function get_previous_line_indentation() + -- Get the current cursor position + local current_line, _ = unpack(vim.api.nvim_win_get_cursor(0)) + + -- Ensure we're not on the first line + if current_line == 1 then + return 0 + end + + -- Get the previous line's content + local previous_line = + vim.api.nvim_buf_get_lines(0, current_line - 2, current_line - 1, false)[1] + + -- Match the leading spaces (indentation) + local indentation = previous_line:match("^%s*") + + -- Return the number of spaces (or indentation level) + return #indentation +end + +function M.insert_previous_line_indentation() + -- Get the cursor column position + local _, col = unpack(vim.api.nvim_win_get_cursor(0)) + + -- If the cursor is at the start of the line (column 0) + if col == 0 then + -- Get the number of spaces from the previous line's indentation + local indentation_level = get_previous_line_indentation() + + -- Insert the same number of spaces at the start of the current line + vim.api.nvim_put({ string.rep(" ", indentation_level) }, "c", true, true) + else + -- Insert a tab (or spaces if 'expandtab' is set) + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("", true, false, true), + "n", + true + ) + end +end + +-- Create a keymap to trigger the function using native mappings +-- vim.api.nvim_set_keymap( +-- { "i", "n" }, +-- "", +-- "lua require('indentor').insert_previous_line_indentation()", +-- { noremap = true, silent = true } +-- ) + +-- Create a keymap to trigger the function using Rootiest utils +require("data").func.add_keymap( + "", + M.insert_previous_line_indentation, + "Indent Line", + { "i", "n" } +) + +return M