Skip to content

Commit 80061d8

Browse files
committed
config optimizations
1 parent 4e68d36 commit 80061d8

File tree

6 files changed

+132
-164
lines changed

6 files changed

+132
-164
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ lint:
66
stylua --check lua/
77
luacheck lua/ --globals vim
88

9-
test: prepare lint
9+
test: prepare
1010
nvim --headless --noplugin -u tests/minimal_init.vim -c "PlenaryBustedDirectory tests { minimal_init = './tests/minimal_init.vim' }"
1111

1212
prepare:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ require('search-replace').setup({
7171
dashboard = {
7272
enable = true,
7373
symbols = {
74-
active = '*', -- Symbol for active flag
75-
inactive = 'o', -- Symbol for inactive flag
74+
active = '', -- Active flag indicator
75+
inactive = '', -- Inactive flag indicator
7676
},
7777
highlights = {
7878
title = 'Title',

lua/search-replace/config.lua

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
--- Centralized configuration for search-replace.nvim
2+
--- All default values and type definitions live here
3+
4+
---@class SearchReplaceKeymapConfig
5+
---@field enable boolean Whether to enable keymaps
6+
---@field populate? string Normal/Visual mode keymap to populate search line
7+
---@field toggle_g? string Keymap to toggle global flag
8+
---@field toggle_c? string Keymap to toggle confirm flag
9+
---@field toggle_i? string Keymap to toggle case-insensitive flag
10+
---@field toggle_replace? string Keymap to toggle replace term
11+
---@field toggle_range? string Keymap to cycle range
12+
---@field toggle_separator? string Keymap to cycle separator
13+
---@field toggle_magic? string Keymap to cycle magic mode
14+
---@field toggle_dashboard? string Keymap to toggle dashboard
15+
16+
---@class SearchReplaceDashboardSymbols
17+
---@field active string Symbol for active flag indicator
18+
---@field inactive string Symbol for inactive flag indicator
19+
20+
---@class SearchReplaceDashboardHighlights
21+
---@field title string Highlight group for title
22+
---@field key string Highlight group for key
23+
---@field arrow string Highlight group for arrow
24+
---@field active_desc string Highlight group for active description
25+
---@field inactive_desc string Highlight group for inactive description
26+
---@field active_indicator string Highlight group for active indicator
27+
---@field inactive_indicator string Highlight group for inactive indicator
28+
---@field status_label string Highlight group for status label
29+
---@field status_value string Highlight group for status value
30+
31+
---@class SearchReplaceDashboardConfig
32+
---@field enable boolean Whether to enable the dashboard
33+
---@field symbols SearchReplaceDashboardSymbols Visual symbols configuration
34+
---@field highlights SearchReplaceDashboardHighlights Highlight groups configuration
35+
36+
---@class SearchReplaceConfig
37+
---@field keymaps SearchReplaceKeymapConfig Keymaps configuration
38+
---@field dashboard SearchReplaceDashboardConfig Dashboard configuration
39+
---@field separators string[] Available separator characters
40+
---@field magic_modes string[] Available magic modes
41+
---@field flags string[] Available flags
42+
---@field default_range string Default range for substitute command
43+
---@field default_flags string Default flags for substitute command
44+
---@field default_magic string Default magic mode
45+
46+
---@class ConfigModule
47+
---@field defaults SearchReplaceConfig The default configuration
48+
---@field current SearchReplaceConfig The current merged configuration
49+
---@field setup fun(opts?: table) Merge user options with defaults
50+
---@field get fun(): SearchReplaceConfig Get the current configuration
51+
local M = {}
52+
53+
---@type SearchReplaceConfig
54+
M.defaults = {
55+
-- Keymaps configuration
56+
keymaps = {
57+
enable = true,
58+
populate = '<leader>r', -- Normal/Visual mode
59+
toggle_g = '<M-g>', -- Toggle global flag
60+
toggle_c = '<M-c>', -- Toggle confirm flag
61+
toggle_i = '<M-i>', -- Toggle case-insensitive
62+
toggle_replace = '<M-d>', -- Toggle replace term
63+
toggle_range = '<M-5>', -- Cycle range
64+
toggle_separator = '<M-/>', -- Cycle separator
65+
toggle_magic = '<M-m>', -- Cycle magic mode
66+
toggle_dashboard = '<M-h>', -- Toggle dashboard
67+
},
68+
-- Dashboard configuration
69+
dashboard = {
70+
enable = true,
71+
symbols = {
72+
active = '', -- Active flag indicator
73+
inactive = '', -- Inactive flag indicator
74+
},
75+
highlights = {
76+
title = 'Title',
77+
key = 'Special',
78+
arrow = 'Comment',
79+
active_desc = 'String',
80+
inactive_desc = 'Comment',
81+
active_indicator = 'DiagnosticOk',
82+
inactive_indicator = 'Comment',
83+
status_label = 'Comment',
84+
status_value = 'Constant',
85+
},
86+
},
87+
-- Core configuration
88+
separators = { '/', '?', '#', ':', '@' },
89+
magic_modes = { '\\v', '\\m', '\\M', '\\V', '' },
90+
flags = { 'g', 'c', 'i' },
91+
default_range = '.,$s',
92+
default_flags = 'gc',
93+
default_magic = '\\V',
94+
}
95+
96+
---@type SearchReplaceConfig
97+
M.current = vim.deepcopy(M.defaults)
98+
99+
---Setup configuration by merging user options with defaults
100+
---@param opts? table User configuration options
101+
function M.setup(opts)
102+
opts = opts or {}
103+
M.current = vim.tbl_deep_extend('force', vim.deepcopy(M.defaults), opts)
104+
end
105+
106+
---Get the current configuration
107+
---@return SearchReplaceConfig
108+
function M.get()
109+
return M.current
110+
end
111+
112+
return M

lua/search-replace/core.lua

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
---@field toggle_all_file fun(): string Toggle range (all file)
1212
---@field toggle_separator fun(): string Toggle separator
1313
---@field toggle_magic fun(): string Toggle magic mode
14-
---@field get_config fun(): CoreConfig Get the current configuration
1514
local M = {}
1615

1716
local utils = require('search-replace.utils')
@@ -24,18 +23,9 @@ local utils = require('search-replace.utils')
2423
---@field default_flags string Default flags for substitute command
2524
---@field default_magic string Default magic mode
2625

26+
-- Config is set by setup() from init.lua, which uses centralized config.lua defaults
2727
---@type CoreConfig
28-
local default_config = {
29-
separators = { '/', '?', '#', ':', '@' },
30-
magic_modes = { '\\v', '\\m', '\\M', '\\V', '' },
31-
flags = { 'g', 'c', 'i' },
32-
default_range = '.,$s',
33-
default_flags = 'gc',
34-
default_magic = '\\V',
35-
}
36-
37-
---@type CoreConfig
38-
local config = vim.deepcopy(default_config)
28+
local config
3929

4030
---@class SarState
4131
---@field active boolean Whether search-replace mode is active
@@ -325,13 +315,11 @@ function M.toggle_magic()
325315
end
326316

327317
---Setup the core module with configuration
328-
---@param opts? CoreConfig Configuration options
318+
---@param opts CoreConfig Configuration options (required, provided by init.lua)
329319
---@return nil
330320
function M.setup(opts)
331-
opts = opts or {}
332-
333-
-- Merge configuration
334-
config = vim.tbl_deep_extend('force', default_config, opts)
321+
-- Config is provided by init.lua from centralized config.lua
322+
config = opts
335323

336324
-- Autocommand to deactivate search-replace mode when leaving cmdline
337325
vim.api.nvim_create_autocmd('CmdlineLeave', {
@@ -342,10 +330,4 @@ function M.setup(opts)
342330
})
343331
end
344332

345-
---Get the current configuration
346-
---@return CoreConfig config The current configuration
347-
function M.get_config()
348-
return config
349-
end
350-
351333
return M

lua/search-replace/dashboard.lua

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,9 @@ end
6464
---@field highlights DashboardHighlights Highlight groups configuration
6565
---@field keymaps KeymapInfo[] Keymap information
6666

67+
-- Config is set by setup() from init.lua, which uses centralized config.lua defaults
6768
---@type DashboardSetupConfig
68-
local default_config = {
69-
symbols = {
70-
active = '*',
71-
inactive = 'o',
72-
},
73-
highlights = {
74-
title = 'Title',
75-
key = 'Special',
76-
arrow = 'Comment',
77-
active_desc = 'String',
78-
inactive_desc = 'Comment',
79-
active_indicator = 'DiagnosticOk',
80-
inactive_indicator = 'Comment',
81-
status_label = 'Comment',
82-
status_value = 'Constant',
83-
},
84-
keymaps = {
85-
{ key = '<M-g>', flag = 'g', desc = "Toggle 'g' flag (global)" },
86-
{ key = '<M-c>', flag = 'c', desc = "Toggle 'c' flag (confirm)" },
87-
{ key = '<M-i>', flag = 'i', desc = "Toggle 'i' flag (case-insensitive)" },
88-
{ key = '<M-d>', flag = nil, desc = 'Toggle replace term' },
89-
{ key = '<M-5>', flag = nil, desc = 'Cycle range' },
90-
{ key = '<M-/>', flag = nil, desc = 'Cycle separator' },
91-
{ key = '<M-m>', flag = nil, desc = 'Cycle magic mode' },
92-
{ key = '<M-h>', flag = nil, desc = 'Toggle dashboard' },
93-
},
94-
}
95-
96-
---@type DashboardSetupConfig
97-
local config = vim.deepcopy(default_config)
69+
local config
9870

9971
---@class ParsedCommand
10072
---@field range string The range part (e.g., ".,$s", "%s")
@@ -249,20 +221,13 @@ local function format_search_replace_lines(parsed)
249221
}
250222
end
251223

252-
---Get keymap information with state checking
253-
---@return KeymapInfo[] keymaps The keymap information
254-
local function get_keymaps_info()
255-
return config.keymaps
256-
end
257-
258224
---Format the keymap lines with state indicators
259225
---@param parsed ParsedCommand The parsed command
260226
---@return string[] keymap_lines The formatted keymap lines
261227
local function format_keymap_lines(parsed)
262228
local lines = {}
263-
local keymaps = get_keymaps_info()
264229

265-
for _, km in ipairs(keymaps) do
230+
for _, km in ipairs(config.keymaps) do
266231
local indicator = ' '
267232
if km.flag then
268233
-- Check if this flag is active
@@ -443,8 +408,7 @@ local function apply_highlights(buf_id, lines, parsed)
443408
-- Line 12: Empty (separator)
444409

445410
-- Lines 13+: Keymap lines
446-
local keymaps = get_keymaps_info()
447-
for i, km in ipairs(keymaps) do
411+
for i, km in ipairs(config.keymaps) do
448412
local line_idx = 13 + i - 1
449413
local line = lines[line_idx + 1]
450414

@@ -640,13 +604,11 @@ function M.toggle_dashboard()
640604
end
641605

642606
---Setup autocmds for the dashboard
643-
---@param opts? DashboardSetupConfig Configuration options
607+
---@param opts DashboardSetupConfig Configuration options (required, provided by init.lua)
644608
---@return nil
645609
function M.setup(opts)
646-
opts = opts or {}
647-
648-
-- Merge configuration
649-
config = vim.tbl_deep_extend('force', default_config, opts)
610+
-- Config is provided by init.lua from centralized config.lua
611+
config = opts
650612

651613
local augroup = vim.api.nvim_create_augroup('SearchReplaceDashboard', { clear = true })
652614

lua/search-replace/init.lua

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -19,94 +19,7 @@
1919
local M = {}
2020

2121
local core = require('search-replace.core')
22-
23-
---@class SearchReplaceKeymapConfig
24-
---@field enable boolean Whether to enable keymaps
25-
---@field populate? string Normal/Visual mode keymap to populate search line
26-
---@field toggle_g? string Keymap to toggle global flag
27-
---@field toggle_c? string Keymap to toggle confirm flag
28-
---@field toggle_i? string Keymap to toggle case-insensitive flag
29-
---@field toggle_replace? string Keymap to toggle replace term
30-
---@field toggle_range? string Keymap to cycle range
31-
---@field toggle_separator? string Keymap to cycle separator
32-
---@field toggle_magic? string Keymap to cycle magic mode
33-
---@field toggle_dashboard? string Keymap to toggle dashboard
34-
35-
---@class SearchReplaceDashboardSymbols
36-
---@field active string Symbol for active flag indicator
37-
---@field inactive string Symbol for inactive flag indicator
38-
39-
---@class SearchReplaceDashboardHighlights
40-
---@field title string Highlight group for title
41-
---@field key string Highlight group for key
42-
---@field arrow string Highlight group for arrow
43-
---@field active_desc string Highlight group for active description
44-
---@field inactive_desc string Highlight group for inactive description
45-
---@field active_indicator string Highlight group for active indicator
46-
---@field inactive_indicator string Highlight group for inactive indicator
47-
---@field status_label string Highlight group for status label
48-
---@field status_value string Highlight group for status value
49-
50-
---@class SearchReplaceDashboardConfig
51-
---@field enable boolean Whether to enable the dashboard
52-
---@field symbols SearchReplaceDashboardSymbols Visual symbols configuration
53-
---@field highlights SearchReplaceDashboardHighlights Highlight groups configuration
54-
55-
---@class SearchReplaceConfig
56-
---@field keymaps SearchReplaceKeymapConfig Keymaps configuration
57-
---@field dashboard SearchReplaceDashboardConfig Dashboard configuration
58-
---@field separators string[] Available separator characters
59-
---@field magic_modes string[] Available magic modes
60-
---@field flags string[] Available flags
61-
---@field default_range string Default range for substitute command
62-
---@field default_flags string Default flags for substitute command
63-
---@field default_magic string Default magic mode
64-
65-
---@type SearchReplaceConfig
66-
local default_config = {
67-
-- Keymaps configuration
68-
keymaps = {
69-
enable = true,
70-
populate = '<leader>r', -- Normal/Visual mode
71-
toggle_g = '<M-g>', -- Toggle global flag
72-
toggle_c = '<M-c>', -- Toggle confirm flag
73-
toggle_i = '<M-i>', -- Toggle case-insensitive
74-
toggle_replace = '<M-d>', -- Toggle replace term
75-
toggle_range = '<M-5>', -- Cycle range
76-
toggle_separator = '<M-/>', -- Cycle separator
77-
toggle_magic = '<M-m>', -- Cycle magic mode
78-
toggle_dashboard = '<M-h>', -- Toggle dashboard
79-
},
80-
-- Dashboard configuration
81-
dashboard = {
82-
enable = true,
83-
symbols = {
84-
active = '*',
85-
inactive = 'o',
86-
},
87-
highlights = {
88-
title = 'Title',
89-
key = 'Special',
90-
arrow = 'Comment',
91-
active_desc = 'String',
92-
inactive_desc = 'Comment',
93-
active_indicator = 'DiagnosticOk',
94-
inactive_indicator = 'Comment',
95-
status_label = 'Comment',
96-
status_value = 'Constant',
97-
},
98-
},
99-
-- Core configuration
100-
separators = { '/', '?', '#', ':', '@' },
101-
magic_modes = { '\\v', '\\m', '\\M', '\\V', '' },
102-
flags = { 'g', 'c', 'i' },
103-
default_range = '.,$s',
104-
default_flags = 'gc',
105-
default_magic = '\\V',
106-
}
107-
108-
---@type SearchReplaceConfig
109-
local config = vim.deepcopy(default_config)
22+
local config_module = require('search-replace.config')
11023

11124
---Setup keymaps for search-replace functionality
11225
---@param keymap_config SearchReplaceKeymapConfig
@@ -193,10 +106,9 @@ end
193106
--- Setup the plugin with user configuration
194107
---@param opts? table User configuration options
195108
function M.setup(opts)
196-
opts = opts or {}
197-
198-
-- Merge configuration
199-
config = vim.tbl_deep_extend('force', default_config, opts)
109+
-- Merge user options with defaults in centralized config
110+
config_module.setup(opts)
111+
local config = config_module.get()
200112

201113
-- Setup core module
202114
core.setup({
@@ -251,7 +163,7 @@ M.is_active = core.is_active
251163
---Get the current configuration
252164
---@return SearchReplaceConfig config The current configuration
253165
function M.get_config()
254-
return config
166+
return config_module.get()
255167
end
256168

257169
return M

0 commit comments

Comments
 (0)