Skip to content

Commit f1473c6

Browse files
committed
feat: default buffer opts
1 parent 4f981b6 commit f1473c6

File tree

6 files changed

+94
-43
lines changed

6 files changed

+94
-43
lines changed

doc/search-and-replace.txt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,34 @@ Default values:
1111
SearchAndReplace.options = {
1212
-- Prints useful logs about what event are triggered, and reasons actions are executed.
1313
debug = false,
14+
-- When true, the opened replace prompt will be in 'normal' mode instead of 'insert' mode.
15+
default_replace_prompt_to_normal_mode = false,
16+
-- When true, the value being replaced is set as the default prompt buffer value.
17+
default_replace_prompt_to_selection = false,
1418
-- Creates mappings for you to easily interact with the exposed commands.
1519
---@type table
1620
mappings = {
1721
-- When `true`, creates all the mappings that are not set to `false`.
1822
---@type boolean
1923
enabled = false,
20-
-- Sets a global mapping to Neovim, which will trigger the "by pattern" replace function.
24+
-- Sets a global mapping to Neovim, which will trigger the "by reference" replace function.
2125
-- When `false`, the mapping is not created.
2226
---@type string
23-
search_and_replace_by_pattern = "<Leader>srp",
24-
-- Sets a global mapping to Neovim, which will trigger the "by reference" replace function.
27+
search_and_replace_by_pattern = "<Leader>srr",
28+
-- Sets a global mapping to Neovim, which will trigger the "by pattern" replace function
29+
-- for the word under the cursor or the current visual selection.
30+
-- When `false`, the mapping is not created.
31+
---@type string
32+
replace_by_pattern = "<Leader>rp",
33+
-- Sets a global mapping to Neovim, which will trigger the "by reference" replace function
34+
-- for the word under the cursor or the current visual selection.
2535
-- When `false`, the mapping is not created.
2636
---@type string
27-
search_and_replace_by_reference = "<Leader>srr",
37+
replace_by_reference = "<Leader>rr",
2838
-- Sets a global mapping to Neovim, which will trigger the "undo" function.
2939
-- When `false`, the mapping is not created.
3040
---@type string
31-
undo = "<Leader>sru",
41+
replace_undo = "<Leader>ru",
3242
},
3343
}
3444

lua/search-and-replace/config.lua

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,34 @@ local SearchAndReplace = {}
88
SearchAndReplace.options = {
99
-- Prints useful logs about what event are triggered, and reasons actions are executed.
1010
debug = false,
11+
-- When true, the opened replace prompt will be in 'normal' mode instead of 'insert' mode.
12+
default_replace_prompt_to_normal_mode = false,
13+
-- When true, the value being replaced is set as the default prompt buffer value.
14+
default_replace_prompt_to_selection = false,
1115
-- Creates mappings for you to easily interact with the exposed commands.
1216
---@type table
1317
mappings = {
1418
-- When `true`, creates all the mappings that are not set to `false`.
1519
---@type boolean
1620
enabled = false,
17-
-- Sets a global mapping to Neovim, which will trigger the "by pattern" replace function.
21+
-- Sets a global mapping to Neovim, which will trigger the "by reference" replace function.
1822
-- When `false`, the mapping is not created.
1923
---@type string
20-
search_and_replace_by_pattern = "<Leader>srp",
21-
-- Sets a global mapping to Neovim, which will trigger the "by reference" replace function.
24+
search_and_replace_by_pattern = "<Leader>srr",
25+
-- Sets a global mapping to Neovim, which will trigger the "by pattern" replace function
26+
-- for the word under the cursor or the current visual selection.
27+
-- When `false`, the mapping is not created.
28+
---@type string
29+
replace_by_pattern = "<Leader>rp",
30+
-- Sets a global mapping to Neovim, which will trigger the "by reference" replace function
31+
-- for the word under the cursor or the current visual selection.
2232
-- When `false`, the mapping is not created.
2333
---@type string
24-
search_and_replace_by_reference = "<Leader>srr",
34+
replace_by_reference = "<Leader>rr",
2535
-- Sets a global mapping to Neovim, which will trigger the "undo" function.
2636
-- When `false`, the mapping is not created.
2737
---@type string
28-
undo = "<Leader>sru",
38+
replace_undo = "<Leader>ru",
2939
},
3040
}
3141

@@ -83,9 +93,10 @@ function SearchAndReplace.setup(options)
8393
SearchAndReplace.options = SearchAndReplace.defaults(options or {})
8494

8595
register_mappings(SearchAndReplace.options.mappings, {
86-
search_and_replace_by_pattern = ":SearchAndReplaceByPattern<CR>",
8796
search_and_replace_by_reference = ":SearchAndReplaceByReference<CR>",
88-
undo = ":SearchAndReplaceUndo<CR>",
97+
replace_by_pattern = ":ReplaceByPattern<CR>",
98+
replace_by_reference = ":ReplaceByReference<CR>",
99+
replace_undo = ":ReplaceUndo<CR>",
89100
})
90101

91102
return SearchAndReplace.options

lua/search-and-replace/init.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ local config = require("search-and-replace.config")
33

44
local SearchAndReplace = {}
55

6+
function SearchAndReplace.search_and_replace_by_pattern()
7+
main.search_and_replace_by_pattern("search and replace by pattern")
8+
end
9+
610
function SearchAndReplace.replace_by_pattern()
711
main.replace_by_pattern("by pattern")
812
end
@@ -11,8 +15,8 @@ function SearchAndReplace.replace_by_references()
1115
main.replace_by_references("by reference")
1216
end
1317

14-
function SearchAndReplace.undo()
15-
main.undo("undo")
18+
function SearchAndReplace.replace_undo()
19+
main.undo("replace undo")
1620
end
1721

1822
function SearchAndReplace.setup(opts)

lua/search-and-replace/main.lua

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,28 @@ local state = require("search-and-replace.state")
44
-- o methods
55
local main = {}
66

7-
--- Replace the word under cursor in the current project.
7+
--- Opens the prompt for a vimgrep term to apply in the current project, then an other
8+
--- prompt to replace by pattern.
89
---
9-
--- @param scope string: o identifier for logging purposes.
10+
--- @param scope string: identifier for logging purposes.
11+
---@private
12+
function main.search_and_replace_by_pattern(scope)
13+
state.init(state)
14+
15+
state.create_buffer(state, scope, function(selection, replace)
16+
selection = selection or ""
17+
vim.cmd("vimgrep /" .. selection .. "/g **")
18+
if state.backup_qflist(state, scope) then
19+
api.replace(selection, replace)
20+
end
21+
end)
22+
23+
state.create_window(state, scope)
24+
end
25+
26+
--- Replaces the word under cursor or current visual selection in the current project.
27+
---
28+
--- @param scope string: identifier for logging purposes.
1029
---@private
1130
function main.replace_by_pattern(scope)
1231
state.init(state)
@@ -22,9 +41,9 @@ function main.replace_by_pattern(scope)
2241
state.create_window(state, scope)
2342
end
2443

25-
--- Replace the word under cursor by references using vim.lsp.buf.references().
44+
--- Replaces the word under cursor or current visual selection using vim.lsp.buf.references().
2645
---
27-
--- @param scope string: o identifier for logging purposes.
46+
--- @param scope string: identifier for logging purposes.
2847
---@private
2948
function main.replace_by_references(scope)
3049
state.init(state)
@@ -46,11 +65,11 @@ function main.replace_by_references(scope)
4665
state.create_window(state, scope)
4766
end
4867

49-
--- Undoes the last `replace_*` operation by restoring the saved backup.
68+
--- Restores the backup files of the last `replace_*` operation.
5069
---
51-
--- @param scope string: o identifier for logging purposes.
70+
--- @param scope string: identifier for logging purposes.
5271
---@private
53-
function main.undo(scope)
72+
function main.replace_undo(scope)
5473
state.restore_backup(state, scope)
5574
end
5675

lua/search-and-replace/state.lua

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,15 @@ function state:backup_qflist(scope)
7070
vim.cmd("silent! wundo " .. vim.fn.fnameescape(tmpfile))
7171

7272
self.backup[name] = { bufnr = bufnr, tmp = tmpfile, seq = seq }
73-
log.debug(scope, "saved undo for %s -> %s (seq=%d)", name, tmpfile, seq)
73+
-- keep for debug but way too spammy
74+
-- log.debug(scope, "saved undo for '%s' -> '%s' with seq '%d'", name, tmpfile, seq)
7475

7576
total = total + 1
7677
end
7778
end
7879
end
7980

80-
log.debug(scope, "stored %d items in backup", total)
81+
log.debug(scope, "stored '%d' items in backup", total)
8182

8283
return true
8384
end
@@ -92,10 +93,10 @@ function state:restore_backup(scope)
9293
return
9394
end
9495

95-
log.debug(scope, "restoring %d items", vim.fn.len(self.backup))
96+
log.debug(scope, "restoring '%d' items", vim.fn.len(self.backup))
9697

9798
for name, data in pairs(self.backup) do
98-
log.debug(scope, "restoring %s", name)
99+
log.debug(scope, "restoring '%s'", name)
99100

100101
if vim.api.nvim_buf_is_valid(data.bufnr) then
101102
vim.api.nvim_set_current_buf(data.bufnr)
@@ -104,12 +105,12 @@ function state:restore_backup(scope)
104105
vim.cmd("update")
105106
log.debug(
106107
scope,
107-
"restored undo for %s (seq=%d)",
108+
"restored undo for '%s'",
108109
vim.api.nvim_buf_get_name(data.bufnr),
109110
data.seq
110111
)
111112
else
112-
log.debug(scope, "skipped %s (invalid buffer or missing file)", name)
113+
log.debug(scope, "skipped '%s' (invalid buffer or missing file)", name)
113114
end
114115
end
115116

@@ -138,7 +139,16 @@ function state:create_buffer(scope, cb)
138139
end
139140
end)
140141

141-
vim.api.nvim_command("startinsert!")
142+
if not _G.SearchAndReplace.config.default_replace_prompt_to_normal_mode then
143+
vim.api.nvim_command("startinsert!")
144+
end
145+
146+
if
147+
_G.SearchAndReplace.config.default_replace_prompt_to_selection
148+
and self.selection ~= ""
149+
then
150+
vim.api.nvim_buf_set_text(self.buffer, 0, 0, 0, 0, { self.selection })
151+
end
142152
end,
143153
group = self.augroup_name,
144154
desc = "Keeps track of the state after entering new windows",
@@ -149,15 +159,10 @@ function state:create_buffer(scope, cb)
149159

150160
cb(self.selection, replace)
151161

152-
log.debug(scope, "replace done")
162+
log.debug(scope, "replace with '%s' done", replace)
153163
end)
154164

155-
vim.keymap.set({ "i", "n" }, "<Esc>", function()
156-
log.debug(scope, "requested closing")
157-
self.cleanup(self, scope)
158-
end, { buffer = self.buffer })
159-
160-
log.debug(scope, "prompt buffer created")
165+
log.debug(scope, "prompt buffer created, searching for '%s'", self.selection)
161166

162167
self.save(self)
163168
end
@@ -170,19 +175,19 @@ function state:create_window(scope)
170175
self.window = vim.api.nvim_open_win(self.buffer, true, {
171176
style = "minimal",
172177
relative = "editor",
173-
width = vim.o.columns,
178+
width = math.floor(vim.o.columns / 3),
174179
height = 1,
175180
row = math.floor((vim.o.lines - 1) / 2),
176-
col = math.floor(vim.o.columns / 2),
181+
col = math.floor(vim.o.columns / 3),
177182
border = "rounded",
178-
title = string.format('Replace %s: "%s"', scope, self.selection),
183+
title = string.format("Replacing '%s'", self.selection),
179184
title_pos = "center",
180185
})
181186
vim.keymap.set("i", "<Esc>", function()
182187
vim.api.nvim_win_close(self.window, true)
183188
end, { buffer = self.buffer })
184189

185-
log.debug(scope, "window for buffer %d created", self.buffer)
190+
log.debug(scope, "window for buffer '%d' created", self.buffer)
186191

187192
self.save(self)
188193
end
@@ -207,7 +212,9 @@ function state:cleanup(scope)
207212

208213
self.reset(self)
209214

210-
vim.api.nvim_command("stopinsert")
215+
if not _G.SearchAndReplace.config.default_replace_prompt_to_normal_mode then
216+
vim.api.nvim_command("stopinsert")
217+
end
211218
end
212219

213220
return state

plugin/search-and-replace.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ end
55

66
_G.SearchAndReplaceLoaded = true
77

8-
vim.api.nvim_create_user_command("SearchAndReplaceByPattern", function()
8+
vim.api.nvim_create_user_command("ReplaceByPattern", function()
99
require("search-and-replace").replace_by_pattern()
1010
end, { range = true })
1111

12-
vim.api.nvim_create_user_command("SearchAndReplaceByReferences", function()
12+
vim.api.nvim_create_user_command("ReplaceByReferences", function()
1313
require("search-and-replace").replace_by_references()
1414
end, { range = true })
1515

16-
vim.api.nvim_create_user_command("SearchAndReplaceUndo", function()
16+
vim.api.nvim_create_user_command("ReplaceUndo", function()
1717
require("search-and-replace").undo()
1818
end, {})

0 commit comments

Comments
 (0)