diff --git a/lua/rip-substitute/config.lua b/lua/rip-substitute/config.lua index d8a104e..9b5e750 100644 --- a/lua/rip-substitute/config.lua +++ b/lua/rip-substitute/config.lua @@ -1,7 +1,7 @@ local M = {} -------------------------------------------------------------------------------- ----@class ripSubstituteConfig +---@class RipSubstitute.Config local defaultConfig = { popupWin = { title = " rip-substitute", @@ -60,7 +60,7 @@ local defaultConfig = { M.config = defaultConfig ----@param userConfig? ripSubstituteConfig +---@param userConfig? RipSubstitute.Config function M.setup(userConfig) M.config = vim.tbl_deep_extend("force", M.config, userConfig or {}) local notify = require("rip-substitute.utils").notify diff --git a/lua/rip-substitute/init.lua b/lua/rip-substitute/init.lua index 2ccc508..400c7c7 100644 --- a/lua/rip-substitute/init.lua +++ b/lua/rip-substitute/init.lua @@ -15,7 +15,7 @@ local M = {} -- on initialization instead of lazy-loading them when needed. -------------------------------------------------------------------------------- ----@param userConfig? ripSubstituteConfig +---@param userConfig? RipSubstitute.Config function M.setup(userConfig) require("rip-substitute.config").setup(userConfig) end function M.rememberCursorWord() diff --git a/lua/rip-substitute/popup-win.lua b/lua/rip-substitute/popup-win.lua index 5ebf55a..dfdc0d8 100644 --- a/lua/rip-substitute/popup-win.lua +++ b/lua/rip-substitute/popup-win.lua @@ -17,14 +17,15 @@ local function setPopupLabelsIfEnoughSpace(popupWidth) if hide then return end local state = require("rip-substitute.state").state - vim.api.nvim_buf_clear_namespace(state.popupBufNr, state.labelNs, 0, -1) + local ns = vim.api.nvim_create_namespace("rip-substitute.labels") + vim.api.nvim_buf_clear_namespace(state.popupBufNr, ns, 0, -1) local popupLines = getPopupLines() local labels = { " Search", "Replace" } for i = 1, 2 do local contentOverlapsLabel = #popupLines[i] >= (popupWidth - #labels[i]) if not contentOverlapsLabel then - vim.api.nvim_buf_set_extmark(state.popupBufNr, state.labelNs, i - 1, 0, { + vim.api.nvim_buf_set_extmark(state.popupBufNr, ns, i - 1, 0, { virt_text = { { labels[i], "DiagnosticVirtualTextInfo" } }, virt_text_pos = "right_align", }) @@ -65,7 +66,8 @@ local function closePopupWin() if vim.api.nvim_buf_is_valid(state.popupBufNr) then vim.api.nvim_buf_delete(state.popupBufNr, { force = true }) end - vim.api.nvim_buf_clear_namespace(0, state.incPreviewNs, 0, -1) + local ns = vim.api.nvim_create_namespace("rip-substitute.incPreview") + vim.api.nvim_buf_clear_namespace(0, ns, 0, -1) end local function confirmSubstitution() diff --git a/lua/rip-substitute/rg-operations.lua b/lua/rip-substitute/rg-operations.lua index 95483ef..1445eff 100644 --- a/lua/rip-substitute/rg-operations.lua +++ b/lua/rip-substitute/rg-operations.lua @@ -105,8 +105,9 @@ end function M.incrementalPreviewAndMatchCount() local viewStartLnum, viewEndLnum = u.getViewport() local state = require("rip-substitute.state").state + local ns = vim.api.nvim_create_namespace("rip-substitute.incPreview") state.matchCount = 0 - vim.api.nvim_buf_clear_namespace(state.targetBuf, state.incPreviewNs, 0, -1) + vim.api.nvim_buf_clear_namespace(state.targetBuf, ns, 0, -1) local hlGroup = require("rip-substitute.config").config.incrementalPreview.matchHlGroup local toSearch, toReplace = M.getSearchAndReplaceValuesFromPopup() @@ -160,7 +161,7 @@ function M.incrementalPreviewAndMatchCount() if toReplace == "" then vim.api.nvim_buf_add_highlight( state.targetBuf, - state.incPreviewNs, + ns, hlGroup, match.lnum, match.col, @@ -168,7 +169,7 @@ function M.incrementalPreviewAndMatchCount() ) else -- INFO requires `conceallevel` >= 2 - vim.api.nvim_buf_set_extmark(state.targetBuf, state.incPreviewNs, match.lnum, match.col, { + vim.api.nvim_buf_set_extmark(state.targetBuf, ns, match.lnum, match.col, { conceal = "", end_col = matchEndCol, end_row = match.lnum, @@ -193,13 +194,10 @@ function M.incrementalPreviewAndMatchCount() vim.iter(replacements):slice(viewStartIdx, viewEndIdx):map(parseRgResult):each(function(repl) local matchEndCol = table.remove(matchEndcolsInViewport, 1) local virtText = { repl.text, hlGroup } - vim.api.nvim_buf_set_extmark( - state.targetBuf, - state.incPreviewNs, - repl.lnum, - matchEndCol, - { virt_text = { virtText }, virt_text_pos = "inline" } - ) + vim.api.nvim_buf_set_extmark(state.targetBuf, ns, repl.lnum, matchEndCol, { + virt_text = { virtText }, + virt_text_pos = "inline", + }) end) end diff --git a/lua/rip-substitute/run-parameters.lua b/lua/rip-substitute/run-parameters.lua index 1dc82df..b026527 100644 --- a/lua/rip-substitute/run-parameters.lua +++ b/lua/rip-substitute/run-parameters.lua @@ -29,7 +29,7 @@ function M.setParameters(exCmdArgs) end -- RANGE - ---@type CmdRange|false + ---@type RipSubstitute.CmdRange|false local range = false if mode == "V" then vim.cmd.normal { "V", bang = true } -- leave visual mode, so marks are set @@ -47,8 +47,6 @@ function M.setParameters(exCmdArgs) stateModule.update { targetBuf = bufnr, targetWin = vim.api.nvim_get_current_win(), - labelNs = vim.api.nvim_create_namespace("rip-substitute-labels"), - incPreviewNs = vim.api.nvim_create_namespace("rip-substitute-incpreview"), range = range, rememberedPrefill = nil, -- reset for subsequent runs prefill = { searchPrefill, replacePrefill }, diff --git a/lua/rip-substitute/state.lua b/lua/rip-substitute/state.lua index b834dce..d2b4eda 100644 --- a/lua/rip-substitute/state.lua +++ b/lua/rip-substitute/state.lua @@ -1,25 +1,25 @@ local M = {} ----@class (exact) CmdRange +---@class (exact) RipSubstitute.CmdRange ---@field start number ---@field end_ number ----@class (exact) RipSubstituteState ----@field targetBuf number ----@field targetWin number ----@field range CmdRange|false ----@field labelNs number ----@field incPreviewNs number +---@class (exact) RipSubstitute.State +---@field popupHistory? string[][] +---@field matchCount? number +---@field useFixedStrings? boolean +---@field useIgnoreCase? boolean +---@field targetBuf? number +---@field targetWin? number +---@field range? RipSubstitute.CmdRange|false ---@field popupBufNr? number ---@field popupWinNr? number ----@field popupHistory? string[][] ---@field popupPresentContent? string[] ---@field historyPosition? number ----@field matchCount? number ---@field prefill? string[] ---@field rememberedPrefill? string ----@field useFixedStrings? boolean ----@field useIgnoreCase? boolean + +---@type RipSubstitute.State M.state = { popupHistory = {}, matchCount = 0, @@ -30,7 +30,7 @@ M.state = { ---@type string M.targetBufCache = "" ----@param newState RipSubstituteState +---@param newState RipSubstitute.State function M.update(newState) M.state = vim.tbl_deep_extend("force", M.state, newState) end return M