From ffe8e832c955f24c3563e08fdfc164b6e651a6fa Mon Sep 17 00:00:00 2001 From: r0nsha Date: Wed, 2 Jul 2025 14:24:31 +0300 Subject: [PATCH] refactor(nvim): apply stylua formatting --- lua/lib/config.lua | 30 +++---- lua/lib/input.lua | 199 ++++++++++++++++++++------------------------- lua/lib/utils.lua | 50 ++++++------ lua/multinput.lua | 10 +-- stylua.toml | 9 ++ 5 files changed, 142 insertions(+), 156 deletions(-) create mode 100644 stylua.toml diff --git a/lua/lib/config.lua b/lua/lib/config.lua index b5e9a6f..97c6dd1 100644 --- a/lua/lib/config.lua +++ b/lua/lib/config.lua @@ -16,20 +16,20 @@ ---@field max integer return { - ---@type multinput.Config - defaults = { - opts = { numbers = "never" }, - padding = 5, - width = { min = 20, max = 60 }, - height = { min = 1, max = 6 }, - win = { - title = "Input: ", - style = "minimal", - focusable = true, - relative = "cursor", - col = -1, - width = 1, - height = 1, - }, + ---@type multinput.Config + defaults = { + opts = { numbers = "never" }, + padding = 5, + width = { min = 20, max = 60 }, + height = { min = 1, max = 6 }, + win = { + title = "Input: ", + style = "minimal", + focusable = true, + relative = "cursor", + col = -1, + width = 1, + height = 1, }, + }, } diff --git a/lua/lib/input.lua b/lua/lib/input.lua index e1ad0f3..9f0111c 100644 --- a/lua/lib/input.lua +++ b/lua/lib/input.lua @@ -1,4 +1,4 @@ -local utils = require "lib.utils" +local utils = require("lib.utils") local defaults = require("lib.config").defaults ---@class multinput.Input @@ -14,147 +14,124 @@ local group = vim.api.nvim_create_augroup("multinput.nvim", { clear = true }) ---@param opts any ---@param on_confirm fun(input?: string) function Input:new(config, opts, on_confirm) - local i = {} - i.config = vim.tbl_deep_extend( - "force", - defaults, - config, - { win = { title = opts.prompt or "Input: " } } - ) - i.default = opts.default or "" - i.on_confirm = on_confirm or function() end - setmetatable(i, self) - self.__index = self - return i + local i = {} + i.config = vim.tbl_deep_extend("force", defaults, config, { win = { title = opts.prompt or "Input: " } }) + i.default = opts.default or "" + i.on_confirm = on_confirm or function() end + setmetatable(i, self) + self.__index = self + return i end ---@param default string function Input:open(default) - -- Position window relative to the cursor, such that it doesn't overlap with the cursor's line. - local curr_win = vim.api.nvim_get_current_win() - local cursor_row = vim.api.nvim_win_get_cursor(curr_win)[1] - local win_config = (cursor_row <= 3) and { anchor = "NW", row = 1 } - or { anchor = "SW", row = 0 } - self.config = vim.tbl_deep_extend("keep", self.config, { win = win_config }) - - -- Create buffer and floating window. - self.bufnr = vim.api.nvim_create_buf(false, true) - utils.set_options({ buftype = "prompt", bufhidden = "wipe" }, { buf = self.bufnr }) - vim.fn.prompt_setprompt(self.bufnr, "") - - self.winnr = vim.api.nvim_open_win(self.bufnr, true, self.config.win) - utils.set_options( - { wrap = true, linebreak = true, winhighlight = "Search:None" }, - { win = self.winnr } - ) - - -- Write default value and put cursor at the end - vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, true, { default }) - vim.cmd "startinsert" - vim.api.nvim_win_set_cursor( - self.winnr, - { 1, vim.str_utfindex(default, "utf-8") + 1 } - ) - - self:resize() - self:autocmds() - self:mappings() + -- Position window relative to the cursor, such that it doesn't overlap with the cursor's line. + local curr_win = vim.api.nvim_get_current_win() + local cursor_row = vim.api.nvim_win_get_cursor(curr_win)[1] + local win_config = (cursor_row <= 3) and { anchor = "NW", row = 1 } or { anchor = "SW", row = 0 } + self.config = vim.tbl_deep_extend("keep", self.config, { win = win_config }) + + -- Create buffer and floating window. + self.bufnr = vim.api.nvim_create_buf(false, true) + utils.set_options({ buftype = "prompt", bufhidden = "wipe" }, { buf = self.bufnr }) + vim.fn.prompt_setprompt(self.bufnr, "") + + self.winnr = vim.api.nvim_open_win(self.bufnr, true, self.config.win) + utils.set_options({ wrap = true, linebreak = true, winhighlight = "Search:None" }, { win = self.winnr }) + + -- Write default value and put cursor at the end + vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, true, { default }) + vim.cmd("startinsert") + vim.api.nvim_win_set_cursor(self.winnr, { 1, vim.str_utfindex(default, "utf-8") + 1 }) + + self:resize() + self:autocmds() + self:mappings() end ---@param result string? function Input:close(result) - vim.cmd "stopinsert" - vim.api.nvim_win_close(self.winnr, true) - self.on_confirm(result) + vim.cmd("stopinsert") + vim.api.nvim_win_close(self.winnr, true) + self.on_confirm(result) end ---@param height integer ---@return boolean function Input:set_numbers(height) - if - self.config.opts.numbers == "always" - or (self.config.opts.numbers == "multiline" and height > 1) - then - utils.set_option_if_globally_enabled("number", self.winnr) - utils.set_option_if_globally_enabled("relativenumber", self.winnr) - end - - return vim.api.nvim_get_option_value("number", { win = self.winnr }) - or vim.api.nvim_get_option_value("relativenumber", { win = self.winnr }) + if self.config.opts.numbers == "always" or (self.config.opts.numbers == "multiline" and height > 1) then + utils.set_option_if_globally_enabled("number", self.winnr) + utils.set_option_if_globally_enabled("relativenumber", self.winnr) + end + + return vim.api.nvim_get_option_value("number", { win = self.winnr }) + or vim.api.nvim_get_option_value("relativenumber", { win = self.winnr }) end ---@param width integer ---@param height integer function Input:set_size(width, height) - local h = utils.clamp(height, self.config.height.min, self.config.height.max) - vim.api.nvim_win_set_height(self.winnr, h) - - local w = utils.clamp( - width + self.config.padding, - self.config.width.min, - self.config.width.max - ) - - local has_numbers = self:set_numbers(h) - w = has_numbers and w + utils.get_linenr_width(self.winnr, self.bufnr) or w - w = w + 2 -- HACK: add padding to avoid glitches - vim.api.nvim_win_set_width(self.winnr, w) + local h = utils.clamp(height, self.config.height.min, self.config.height.max) + vim.api.nvim_win_set_height(self.winnr, h) + + local w = utils.clamp(width + self.config.padding, self.config.width.min, self.config.width.max) + + local has_numbers = self:set_numbers(h) + w = has_numbers and w + utils.get_linenr_width(self.winnr, self.bufnr) or w + w = w + 2 -- HACK: add padding to avoid glitches + vim.api.nvim_win_set_width(self.winnr, w) end function Input:resize() - local text = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) - local line = table.concat(text, "") + local text = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false) + local line = table.concat(text, "") - if line == "" then - self:set_size(0, 1) - return - end + if line == "" then + self:set_size(0, 1) + return + end - local lines = utils.split_wrapped_lines(line, self.config.width.max) + local lines = utils.split_wrapped_lines(line, self.config.width.max) - local lens = vim.tbl_map(function(l) - return vim.fn.strdisplaywidth(l) - end, lines) + local lens = vim.tbl_map(function(l) + return vim.fn.strdisplaywidth(l) + end, lines) - self:set_size(math.max(unpack(lens)), #lines) + self:set_size(math.max(unpack(lens)), #lines) end function Input:autocmds() - vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { - group = group, - buffer = self.bufnr, - callback = function() - self:resize() - end, - }) + vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, { + group = group, + buffer = self.bufnr, + callback = function() + self:resize() + end, + }) end function Input:mappings() - ---@param mode string|string[] - ---@param lhs string - ---@param rhs string|function - local function map(mode, lhs, rhs) - vim.keymap.set(mode, lhs, rhs, { buffer = self.bufnr }) - end - - map({ "n", "i", "v" }, "", function() - self:close(vim.api.nvim_buf_get_lines(self.bufnr, 0, 1, false)[1]) - end) - map({ "i" }, "", function() - vim.api.nvim_feedkeys( - vim.api.nvim_replace_termcodes("", true, false, true), - "n", - true - ) - end) - - map("n", "", function() - self:close() - end) - - map("n", "q", function() - self:close() - end) + ---@param mode string|string[] + ---@param lhs string + ---@param rhs string|function + local function map(mode, lhs, rhs) + vim.keymap.set(mode, lhs, rhs, { buffer = self.bufnr }) + end + + map({ "n", "i", "v" }, "", function() + self:close(vim.api.nvim_buf_get_lines(self.bufnr, 0, 1, false)[1]) + end) + map({ "i" }, "", function() + vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("", true, false, true), "n", true) + end) + + map("n", "", function() + self:close() + end) + + map("n", "q", function() + self:close() + end) end return Input diff --git a/lua/lib/utils.lua b/lua/lib/utils.lua index 2dab91d..b56bca0 100644 --- a/lua/lib/utils.lua +++ b/lua/lib/utils.lua @@ -3,17 +3,17 @@ local M = {} ---@param options table ---@param opts vim.api.keyset.option function M.set_options(options, opts) - for k, v in pairs(options) do - vim.api.nvim_set_option_value(k, v, opts) - end + for k, v in pairs(options) do + vim.api.nvim_set_option_value(k, v, opts) + end end ---@param option string ---@param winnr integer function M.set_option_if_globally_enabled(option, winnr) - if vim.api.nvim_get_option_value(option, { scope = "global" }) then - vim.api.nvim_set_option_value(option, true, { win = winnr }) - end + if vim.api.nvim_get_option_value(option, { scope = "global" }) then + vim.api.nvim_set_option_value(option, true, { win = winnr }) + end end ---@param value number @@ -21,41 +21,41 @@ end ---@param max number ---@return number function M.clamp(value, min, max) - return math.min(math.max(value, min), max) + return math.min(math.max(value, min), max) end ---@param text string ---@param width integer ---@return string[] function M.split_wrapped_lines(text, width) - if text == "" then - return {} - end + if text == "" then + return {} + end - ---@type string[] - local lines = {} - local textlen = vim.fn.strchars(text, true) + ---@type string[] + local lines = {} + local textlen = vim.fn.strchars(text, true) - local i = 0 - while i < textlen do - local len = i + width <= textlen and width or textlen - i - local new_line = vim.fn.strcharpart(text, i, len) + local i = 0 + while i < textlen do + local len = i + width <= textlen and width or textlen - i + local new_line = vim.fn.strcharpart(text, i, len) - table.insert(lines, new_line) - i = i + len - end + table.insert(lines, new_line) + i = i + len + end - return lines + return lines end ---@param winnr integer ---@param bufnr integer ---@return integer function M.get_linenr_width(winnr, bufnr) - local line_count = vim.api.nvim_buf_line_count(bufnr) - local line_digits = math.floor(math.log10(math.max(1, line_count))) + 1 - local numberwidth = vim.api.nvim_get_option_value("numberwidth", { win = winnr }) - return math.max(line_digits, numberwidth) + local line_count = vim.api.nvim_buf_line_count(bufnr) + local line_digits = math.floor(math.log10(math.max(1, line_count))) + 1 + local numberwidth = vim.api.nvim_get_option_value("numberwidth", { win = winnr }) + return math.max(line_digits, numberwidth) end return M diff --git a/lua/multinput.lua b/lua/multinput.lua index a901e03..d9ee596 100644 --- a/lua/multinput.lua +++ b/lua/multinput.lua @@ -1,13 +1,13 @@ -local Input = require "lib.input" +local Input = require("lib.input") local M = {} ---@param config? multinput.Config function M.setup(config) - vim.ui.input = function(opts, on_confirm) - local input = Input:new(config or {}, opts or {}, on_confirm) - input:open(opts.default or "") - end + vim.ui.input = function(opts, on_confirm) + local input = Input:new(config or {}, opts or {}, on_confirm) + input:open(opts.default or "") + end end return M diff --git a/stylua.toml b/stylua.toml new file mode 100644 index 0000000..478137f --- /dev/null +++ b/stylua.toml @@ -0,0 +1,9 @@ +column_width = 120 +indent_type = "Spaces" +indent_width = 2 +quote_style = "AutoPreferDouble" +call_parentheses = "Always" +collapse_simple_statement = "Never" + +[sort_requires] +enabled = true -- 2.51.2