Skip to content

Commit fbb6ad0

Browse files
committed
feat(surround)!: map s to <Nop> if any of the mappings start with it
Details: - This prevents accidental trigger of built-in `s` if there is a long delay between pressing "s" and the next key. This was previously a documented improvement, but it makes sense to do automatically during `setup()` because it improves default behavior (without much harm for non-default one).
1 parent 9a2798a commit fbb6ad0

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ There are following change types:
155155

156156
- Stop creating `update_n_lines` mapping: it occupies "mapping real estate" while being rarely needed and straightforward to create manually using `MiniSurround.update_n_lines()`.
157157

158+
- Automatically map `s` key to `<Nop>` if any of created mappings start with it. This prevents accidental trigger of built-in `s` if there is a long delay between pressing "s" and the next key.
159+
158160
### Refine
159161

160162
- Update `gen_spec.inpuf.treesitter()` to have `use_nvim_treesitter = false` as default option value (instead of `true`). It used to implement more advanced behavior, but as built-in `vim.treesitter` is capable enough, there is no need in extra dependency. The option will be removed after the release.

doc/mini-surround.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,11 +541,10 @@ Defaults ~
541541
By default it uses "prefix style" left hand side starting with "s" (for
542542
"surround"): `sa` - "surround add", `sd` - "surround delete", etc.
543543

544-
Note: if 'timeoutlen' is low enough to cause occasional usage of |s| key
545-
(that deletes character under cursor), disable it with the following call: >lua
544+
Note: if any of the mappings start with "s" (as is by default), it is mapped
545+
to |<Nop>| to prevent accidental trigger of built-in |s| (can happen if there
546+
is a long enough delay between pressing "s" and the next key). Use `cl` instead.
546547

547-
vim.keymap.set({ 'n', 'x' }, 's', '<Nop>')
548-
<
549548
# Custom surroundings ~
550549

551550
User can define own surroundings by supplying `config.custom_surroundings`.

lua/mini/surround.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,10 @@ end
508508
--- By default it uses "prefix style" left hand side starting with "s" (for
509509
--- "surround"): `sa` - "surround add", `sd` - "surround delete", etc.
510510
---
511-
--- Note: if 'timeoutlen' is low enough to cause occasional usage of |s| key
512-
--- (that deletes character under cursor), disable it with the following call: >lua
511+
--- Note: if any of the mappings start with "s" (as is by default), it is mapped
512+
--- to |<Nop>| to prevent accidental trigger of built-in |s| (can happen if there
513+
--- is a long enough delay between pressing "s" and the next key). Use `cl` instead.
513514
---
514-
--- vim.keymap.set({ 'n', 'x' }, 's', '<Nop>')
515-
--- <
516515
--- # Custom surroundings ~
517516
---
518517
--- User can define own surroundings by supplying `config.custom_surroundings`.
@@ -2266,6 +2265,7 @@ H.map = function(mode, lhs, rhs, opts)
22662265
if lhs == '' then return end
22672266
opts = vim.tbl_deep_extend('force', { silent = true }, opts or {})
22682267
vim.keymap.set(mode, lhs, rhs, opts)
2268+
if lhs:sub(1, 1) == 's' then vim.keymap.set(mode, 's', '<Nop>') end
22692269
end
22702270

22712271
H.get_line_cols = function(line_num) return vim.fn.getline(line_num):len() end

tests/test_surround.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ T['setup()']['properly handles `config.mappings`'] = function()
171171
-- Regular mappings
172172
eq(has_map('sa', 'surround'), true)
173173

174+
-- Should map "s" to <Nop>, but only if needed
175+
eq(child.fn.maparg('s', 'n'), '<Nop>')
176+
eq(child.fn.maparg('s', 'x'), '<Nop>')
177+
174178
unload_module()
175179
child.api.nvim_del_keymap('n', 'sa')
176180

@@ -194,6 +198,32 @@ T['setup()']['properly handles `config.mappings`'] = function()
194198
eq(has_map('sdn', 'next'), false)
195199
eq(has_map('srl', 'previous'), false)
196200
eq(has_map('srn', 'next'), true)
201+
202+
-- Should precisely set 's' keymap
203+
unload_module()
204+
child.api.nvim_del_keymap('n', 'sa')
205+
child.api.nvim_del_keymap('n', 's')
206+
child.api.nvim_del_keymap('x', 's')
207+
208+
load_module({ mappings = { add = 'cs' } })
209+
eq(child.fn.maparg('s', 'n'), '<Nop>')
210+
eq(child.fn.maparg('s', 'x'), '')
211+
212+
child.api.nvim_del_keymap('n', 's')
213+
load_module({
214+
mappings = {
215+
add = 'ys',
216+
delete = 'ds',
217+
find = '',
218+
find_left = '',
219+
highlight = '',
220+
replace = 'cs',
221+
suffix_last = '',
222+
suffix_next = '',
223+
},
224+
})
225+
eq(child.fn.maparg('s', 'n'), '')
226+
eq(child.fn.maparg('s', 'x'), '')
197227
end
198228

199229
T['update_n_lines()'] = new_set({

0 commit comments

Comments
 (0)