@@ -8,6 +8,21 @@ local SearchAndReplace = {}
88SearchAndReplace .options = {
99 -- Prints useful logs about what event are triggered, and reasons actions are executed.
1010 debug = false ,
11+ -- Creates mappings for you to easily interact with the exposed commands.
12+ --- @type table
13+ mappings = {
14+ -- When `true`, creates all the mappings that are not set to `false`.
15+ --- @type boolean
16+ enabled = false ,
17+ -- Sets a global mapping to Neovim, which will trigger the "in project" replace function.
18+ -- When `false`, the mapping is not created.
19+ --- @type string
20+ search_and_replace_in_project = " <Leader>srp" ,
21+ -- Sets a global mapping to Neovim, which will trigger the "by reference" replace function.
22+ -- When `false`, the mapping is not created.
23+ --- @type string
24+ search_and_replace_by_reference = " <Leader>srr" ,
25+ },
1126}
1227
1328--- @private
@@ -31,6 +46,42 @@ function SearchAndReplace.defaults(options)
3146 return SearchAndReplace .options
3247end
3348
49+ --- Registers the plugin mappings if the option is enabled.
50+ ---
51+ --- @param options table The mappins provided by the user.
52+ --- @param mappings table A key value map of the mapping name and its command.
53+ ---
54+ --- @private
55+ local function register_mappings (options , mappings )
56+ -- all of the mappings are disabled
57+ if not options .enabled then
58+ return
59+ end
60+
61+ for name , command in pairs (mappings ) do
62+ -- this specific mapping is disabled
63+ if not options [name ] then
64+ return
65+ end
66+
67+ if (name == " widthUp" or name == " widthDown" ) and type (options [name ]) ~= " string" then
68+ assert (
69+ type (options [name ]) == " table"
70+ and options [name ][" mapping" ] ~= nil
71+ and options [name ][" value" ] ~= nil ,
72+ string.format (
73+ " `%s` must be a string or a table with the following properties {mapping: 'your_mapping', value: 5}" ,
74+ name
75+ )
76+ )
77+ vim .api .nvim_set_keymap (" n" , options [name ].mapping , command , { silent = true })
78+ else
79+ assert (type (options [name ]) == " string" , string.format (" `%s` must be a string" , name ))
80+ vim .api .nvim_set_keymap (" n" , options [name ], command , { silent = true })
81+ end
82+ end
83+ end
84+
3485--- Define your search-and-replace setup.
3586---
3687--- @param options table Module config table. See | SearchAndReplace.options | .
3990function SearchAndReplace .setup (options )
4091 SearchAndReplace .options = SearchAndReplace .defaults (options or {})
4192
93+ register_mappings (SearchAndReplace .options .mappings , {
94+ search_and_replace_in_project = " :SearchAndReplaceInProject<CR>" ,
95+ search_and_replace_by_reference = " :SearchAndReplaceByReference<CR>" ,
96+ })
97+
4298 return SearchAndReplace .options
4399end
44100
0 commit comments