Skip to content

Commit

Permalink
refactor: namespaces for extmarks and class name annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Nov 20, 2024
1 parent 267345e commit 0a2cd27
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
4 changes: 2 additions & 2 deletions lua/rip-substitute/config.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}
--------------------------------------------------------------------------------

---@class ripSubstituteConfig
---@class RipSubstitute.Config
local defaultConfig = {
popupWin = {
title = " rip-substitute",
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lua/rip-substitute/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 5 additions & 3 deletions lua/rip-substitute/popup-win.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
})
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 8 additions & 10 deletions lua/rip-substitute/rg-operations.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -160,15 +161,15 @@ function M.incrementalPreviewAndMatchCount()
if toReplace == "" then
vim.api.nvim_buf_add_highlight(
state.targetBuf,
state.incPreviewNs,
ns,
hlGroup,
match.lnum,
match.col,
matchEndCol
)
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,
Expand All @@ -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

Expand Down
4 changes: 1 addition & 3 deletions lua/rip-substitute/run-parameters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 },
Expand Down
24 changes: 12 additions & 12 deletions lua/rip-substitute/state.lua
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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

0 comments on commit 0a2cd27

Please sign in to comment.