From 006a4d62bb44e26aad567ca6ea9a2129084c1112 Mon Sep 17 00:00:00 2001 From: Kyle Chui Date: Sat, 2 May 2026 19:28:17 -0400 Subject: [PATCH] refactor: Remove top-level modules. Moving all of the module loads into their respective functions. --- lua/nvim-surround/buffer.lua | 3 +-- lua/nvim-surround/motions.lua | 5 ++--- lua/nvim-surround/patterns.lua | 5 +++-- lua/nvim-surround/utils.lua | 8 ++++---- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lua/nvim-surround/buffer.lua b/lua/nvim-surround/buffer.lua index 9cec5cb..df6db30 100644 --- a/lua/nvim-surround/buffer.lua +++ b/lua/nvim-surround/buffer.lua @@ -1,5 +1,3 @@ -local config = require("nvim-surround.config") - local M = {} M.namespace = { @@ -34,6 +32,7 @@ end -- Move the cursor to a location in the buffer, depending on the `move_cursor` setting. ---@param pos { first_pos: position, sticky_pos: position, old_pos: position } Various positions in the buffer. M.restore_curpos = function(pos) + local config = require("nvim-surround.config") if config.get_opts().move_cursor == "begin" then M.set_curpos(pos.first_pos) elseif config.get_opts().move_cursor == "sticky" then diff --git a/lua/nvim-surround/motions.lua b/lua/nvim-surround/motions.lua index 06e7877..afd2076 100644 --- a/lua/nvim-surround/motions.lua +++ b/lua/nvim-surround/motions.lua @@ -1,6 +1,3 @@ -local buffer = require("nvim-surround.buffer") -local config = require("nvim-surround.config") - local M = {} -- Determines whether the input character is a quote character. @@ -16,6 +13,8 @@ end ---@return selection|nil @The selection that represents the text-object. ---@nodiscard M.get_selection = function(motion) + local buffer = require("nvim-surround.buffer") + local config = require("nvim-surround.config") local char = config.get_alias(motion:sub(2, 2)) local curpos = buffer.get_curpos() diff --git a/lua/nvim-surround/patterns.lua b/lua/nvim-surround/patterns.lua index f8a6809..71d3577 100644 --- a/lua/nvim-surround/patterns.lua +++ b/lua/nvim-surround/patterns.lua @@ -1,5 +1,3 @@ -local buffer = require("nvim-surround.buffer") - local M = {} -- Gets the EOL character for the current buffer, based on the file format. @@ -45,6 +43,7 @@ end ---@return selection @The adjusted selection, handling multi-byte characters. ---@nodiscard M.adjust_selection = function(selection) + local buffer = require("nvim-surround.buffer") selection.first_pos = buffer.get_first_byte(selection.first_pos) selection.last_pos = buffer.get_last_byte(selection.last_pos) return selection @@ -55,6 +54,7 @@ end ---@return selection|nil @The closest selection matching the pattern, if any. ---@nodiscard M.get_selection = function(find) + local buffer = require("nvim-surround.buffer") -- Get the current cursor position, buffer contents local curpos = buffer.get_curpos() local buffer_text = table.concat(buffer.get_lines(1, -1), end_of_line()) @@ -130,6 +130,7 @@ end ---@return selections|nil @The selections for the left and right delimiters. ---@nodiscard M.get_selections = function(selection, pattern) + local buffer = require("nvim-surround.buffer") local offset = M.pos_to_index(selection.first_pos) local str = table.concat(buffer.get_text(selection), end_of_line()) -- Get the surrounding pair, and the start/end indices diff --git a/lua/nvim-surround/utils.lua b/lua/nvim-surround/utils.lua index 8c9e63c..13d686e 100644 --- a/lua/nvim-surround/utils.lua +++ b/lua/nvim-surround/utils.lua @@ -1,7 +1,3 @@ -local buffer = require("nvim-surround.buffer") -local config = require("nvim-surround.config") -local functional = require("nvim-surround.functional") - local M = {} -- Do nothing. @@ -42,12 +38,15 @@ end ---@return selections|nil @A table containing the start and end positions of the delimiters. ---@nodiscard M.get_nearest_selections = function(char, action) + local config = require("nvim-surround.config") + local functional = require("nvim-surround.functional") char = config.get_alias(char) local chars = functional.to_list(config.get_opts().aliases[char] or char) if not chars then return nil end + local buffer = require("nvim-surround.buffer") local curpos = buffer.get_curpos() local winview = vim.fn.winsaveview() local selections_list = {} @@ -79,6 +78,7 @@ end ---@return selections|nil @The best selections from the list. ---@nodiscard M.filter_selections_list = function(selections_list) + local buffer = require("nvim-surround.buffer") local curpos = buffer.get_curpos() local best_selections for _, cur_selections in ipairs(selections_list) do