Skip to content

Commit 1cf33fb

Browse files
committed
feat(surround)!: stop creating update_n_lines mapping
Details: - It occupies `sn` "mapping real estate" (for which there are future plans). - It is rarely needed during runtime. But if it is, it is straightforward to create manually with present `update_n_lines()` function.
1 parent ceb7211 commit 1cf33fb

File tree

5 files changed

+49
-73
lines changed

5 files changed

+49
-73
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ There are following change types:
133133

134134
## mini.surround
135135

136+
### Evolve
137+
138+
- 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()`.
139+
136140
### Refine
137141

138142
- 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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Features:
1616
- Replace surrounding with `sr`.
1717
- Find surrounding with `sf` or `sF` (move cursor right or left).
1818
- Highlight surrounding with `sh`.
19-
- Change number of neighbor lines with `sn` (see |MiniSurround-algorithm|).
2019

2120
- Surrounding is identified by a single character as both "input" (in
2221
`delete` and `replace` start, `find`, and `highlight`) and "output" (in
@@ -507,7 +506,6 @@ Default values:
507506
find_left = 'sF', -- Find surrounding (to the left)
508507
highlight = 'sh', -- Highlight surrounding
509508
replace = 'sr', -- Replace surrounding
510-
update_n_lines = 'sn', -- Update `n_lines`
511509

512510
suffix_last = 'l', -- Suffix to search with "prev" method
513511
suffix_next = 'n', -- Suffix to search with "next" method
@@ -547,7 +545,6 @@ behavior closest to 'tpope/vim-surround' (but not identical), use this setup: >l
547545
find_left = '',
548546
highlight = '',
549547
replace = 'cs',
550-
update_n_lines = '',
551548

552549
-- Add this only if you don't want to use extended mappings
553550
suffix_last = '',
@@ -746,11 +743,12 @@ No need to use it directly, everything is setup in |MiniSurround.setup|.
746743
------------------------------------------------------------------------------
747744
*MiniSurround.update_n_lines()*
748745
`MiniSurround.update_n_lines`()
749-
Update `MiniSurround.config.n_lines`
746+
Update `MiniSurround.config.n_lines` from user input
750747

751-
Convenient wrapper for updating `MiniSurround.config.n_lines` in case the
752-
default one is not appropriate.
748+
Mapping example: >lua
753749

750+
vim.keymap.set('n', 'sn', '<Cmd>lua MiniSurround.update_n_lines()<CR>')
751+
<
754752
------------------------------------------------------------------------------
755753
*MiniSurround.user_input()*
756754
`MiniSurround.user_input`({prompt}, {text})

lua/mini/surround.lua

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
--- - Replace surrounding with `sr`.
1717
--- - Find surrounding with `sf` or `sF` (move cursor right or left).
1818
--- - Highlight surrounding with `sh`.
19-
--- - Change number of neighbor lines with `sn` (see |MiniSurround-algorithm|).
2019
---
2120
--- - Surrounding is identified by a single character as both "input" (in
2221
--- `delete` and `replace` start, `find`, and `highlight`) and "output" (in
@@ -514,7 +513,6 @@ end
514513
--- find_left = '',
515514
--- highlight = '',
516515
--- replace = 'cs',
517-
--- update_n_lines = '',
518516
---
519517
--- -- Add this only if you don't want to use extended mappings
520518
--- suffix_last = '',
@@ -687,7 +685,6 @@ MiniSurround.config = {
687685
find_left = 'sF', -- Find surrounding (to the left)
688686
highlight = 'sh', -- Highlight surrounding
689687
replace = 'sr', -- Replace surrounding
690-
update_n_lines = 'sn', -- Update `n_lines`
691688

692689
suffix_last = 'l', -- Suffix to search with "prev" method
693690
suffix_next = 'n', -- Suffix to search with "next" method
@@ -894,16 +891,15 @@ MiniSurround.highlight = function()
894891
end, config.highlight_duration)
895892
end
896893

897-
--- Update `MiniSurround.config.n_lines`
894+
--- Update `MiniSurround.config.n_lines` from user input
898895
---
899-
--- Convenient wrapper for updating `MiniSurround.config.n_lines` in case the
900-
--- default one is not appropriate.
896+
--- Mapping example: >lua
897+
---
898+
--- vim.keymap.set('n', 'sn', '<Cmd>lua MiniSurround.update_n_lines()<CR>')
899+
--- <
901900
MiniSurround.update_n_lines = function()
902-
if H.is_disabled() then return '<Esc>' end
903-
904901
local n_lines = MiniSurround.user_input('New number of neighbor lines', MiniSurround.config.n_lines)
905-
n_lines = math.floor(tonumber(n_lines) or MiniSurround.config.n_lines)
906-
MiniSurround.config.n_lines = n_lines
902+
MiniSurround.config.n_lines = math.floor(tonumber(n_lines) or MiniSurround.config.n_lines)
907903
end
908904

909905
--- Ask user for input
@@ -1173,7 +1169,6 @@ H.setup_config = function(config)
11731169
H.check_type('mappings.find_left', config.mappings.find_left, 'string')
11741170
H.check_type('mappings.highlight', config.mappings.highlight, 'string')
11751171
H.check_type('mappings.replace', config.mappings.replace, 'string')
1176-
H.check_type('mappings.update_n_lines', config.mappings.update_n_lines, 'string')
11771172

11781173
H.check_type('mappings.suffix_last', config.mappings.suffix_last, 'string')
11791174
H.check_type('mappings.suffix_next', config.mappings.suffix_next, 'string')
@@ -1199,7 +1194,6 @@ H.apply_config = function(config)
11991194
map(m.find_left, H.make_action('find', 'left'), 'Find left surrounding')
12001195
map(m.highlight, H.make_action('highlight'), 'Highlight surrounding')
12011196

1202-
H.map('n', m.update_n_lines, MiniSurround.update_n_lines, { desc = 'Update `MiniSurround.config.n_lines`' })
12031197
H.map('x', m.add, [[:<C-u>lua MiniSurround.add('visual')<CR>]], { desc = 'Add surrounding to selection' })
12041198

12051199
-- Make extended mappings

readmes/mini-surround.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ https://github.com/user-attachments/assets/e91b6e16-7a9c-44aa-afb4-7e07efc3e811
3636
- Replace surrounding with `sr`.
3737
- Find surrounding with `sf` or `sF` (move cursor right or left).
3838
- Highlight surrounding with `sh`.
39-
- Change number of neighbor lines with `sn` (see `:h MiniSurround-algorithm`).
4039
- Surrounding is identified by a single character as both "input" (in `delete` and `replace` start, `find`, and `highlight`) and "output" (in `add` and `replace` end):
4140
- 'f' - function call (string of alphanumeric symbols or '_' or '.' followed by balanced '()'). In "input" finds function call, in "output" prompts user to enter function name.
4241
- 't' - tag. In "input" finds tag with same identifier, in "output" prompts user to enter tag name.
@@ -166,7 +165,6 @@ Here are code snippets for some common installation methods (use only one):
166165
find_left = 'sF', -- Find surrounding (to the left)
167166
highlight = 'sh', -- Highlight surrounding
168167
replace = 'sr', -- Replace surrounding
169-
update_n_lines = 'sn', -- Update `n_lines`
170168

171169
suffix_last = 'l', -- Suffix to search with "prev" method
172170
suffix_next = 'n', -- Suffix to search with "next" method

tests/test_surround.lua

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ T['setup()']['creates `config` field'] = function()
122122
expect_config('mappings.find_left', 'sF')
123123
expect_config('mappings.highlight', 'sh')
124124
expect_config('mappings.replace', 'sr')
125-
expect_config('mappings.update_n_lines', 'sn')
126125
expect_config('mappings.suffix_last', 'l')
127126
expect_config('mappings.suffix_next', 'n')
128127
expect_config('respect_selection_type', false)
@@ -153,7 +152,6 @@ T['setup()']['validates `config` argument'] = function()
153152
expect_config_error({ mappings = { find_left = 1 } }, 'mappings.find_left', 'string')
154153
expect_config_error({ mappings = { highlight = 1 } }, 'mappings.highlight', 'string')
155154
expect_config_error({ mappings = { replace = 1 } }, 'mappings.replace', 'string')
156-
expect_config_error({ mappings = { update_n_lines = 1 } }, 'mappings.update_n_lines', 'string')
157155
expect_config_error({ mappings = { suffix_last = 1 } }, 'mappings.suffix_last', 'string')
158156
expect_config_error({ mappings = { suffix_next = 1 } }, 'mappings.suffix_next', 'string')
159157
expect_config_error({ n_lines = 'a' }, 'n_lines', 'number')
@@ -198,6 +196,41 @@ T['setup()']['properly handles `config.mappings`'] = function()
198196
eq(has_map('srn', 'next'), true)
199197
end
200198

199+
T['update_n_lines()'] = new_set({
200+
hooks = {
201+
pre_case = function() child.lua('vim.keymap.set("n", "sn", "<Cmd>lua MiniSurround.update_n_lines()<CR>")') end,
202+
},
203+
})
204+
205+
T['update_n_lines()']['works'] = function()
206+
local cur_n_lines = child.lua_get('MiniSurround.config.n_lines')
207+
208+
-- Should ask for input, display prompt text and current value of `n_lines`
209+
type_keys('sn')
210+
eq(child.fn.mode(), 'c')
211+
eq(child.fn.getcmdline(), tostring(cur_n_lines))
212+
213+
type_keys('0', '<CR>')
214+
eq(child.lua_get('MiniSurround.config.n_lines'), 10 * cur_n_lines)
215+
end
216+
217+
T['update_n_lines()']['allows cancelling with `<Esc> and <C-c>`'] = function()
218+
local validate_cancel = function(key)
219+
child.ensure_normal_mode()
220+
local cur_n_lines = child.lua_get('MiniSurround.config.n_lines')
221+
222+
type_keys('sn')
223+
eq(child.fn.mode(), 'c')
224+
225+
type_keys(key)
226+
eq(child.fn.mode(), 'n')
227+
eq(child.lua_get('MiniSurround.config.n_lines'), cur_n_lines)
228+
end
229+
230+
validate_cancel('<Esc>')
231+
validate_cancel('<C-c>')
232+
end
233+
201234
T['gen_spec'] = new_set()
202235

203236
T['gen_spec']['input'] = new_set()
@@ -1740,57 +1773,6 @@ T['Highlight surrounding']['respects `vim.b.minisurround_config`'] = function()
17401773
child.expect_screenshot({ ignore_attr = { 5 } })
17411774
end
17421775

1743-
T['Update number of lines'] = new_set()
1744-
1745-
T['Update number of lines']['works'] = function()
1746-
local cur_n_lines = child.lua_get('MiniSurround.config.n_lines')
1747-
1748-
-- Should ask for input, display prompt text and current value of `n_lines`
1749-
type_keys('sn')
1750-
eq(child.fn.mode(), 'c')
1751-
eq(child.fn.getcmdline(), tostring(cur_n_lines))
1752-
1753-
type_keys('0', '<CR>')
1754-
eq(child.lua_get('MiniSurround.config.n_lines'), 10 * cur_n_lines)
1755-
end
1756-
1757-
T['Update number of lines']['allows cancelling with `<Esc> and <C-c>`'] = function()
1758-
local validate_cancel = function(key)
1759-
child.ensure_normal_mode()
1760-
local cur_n_lines = child.lua_get('MiniSurround.config.n_lines')
1761-
1762-
type_keys('sn')
1763-
eq(child.fn.mode(), 'c')
1764-
1765-
type_keys(key)
1766-
eq(child.fn.mode(), 'n')
1767-
eq(child.lua_get('MiniSurround.config.n_lines'), cur_n_lines)
1768-
end
1769-
1770-
validate_cancel('<Esc>')
1771-
validate_cancel('<C-c>')
1772-
end
1773-
1774-
T['Update number of lines']['works with different mapping'] = function()
1775-
reload_module({ mappings = { update_n_lines = 'SN' } })
1776-
1777-
local cur_n_lines = child.lua_get('MiniSurround.config.n_lines')
1778-
type_keys('SN', '0', '<CR>')
1779-
child.api.nvim_del_keymap('n', 'SN')
1780-
eq(child.lua_get('MiniSurround.config.n_lines'), 10 * cur_n_lines)
1781-
end
1782-
1783-
T['Update number of lines']['respects `vim.{g,b}.minisurround_disable`'] = new_set({
1784-
parametrize = { { 'g' }, { 'b' } },
1785-
}, {
1786-
test = function(var_type)
1787-
child[var_type].minisurround_disable = true
1788-
local cur_n_lines = child.lua_get('MiniSurround.config.n_lines')
1789-
type_keys('sn', '0', '<CR>')
1790-
eq(child.lua_get('MiniSurround.config.n_lines'), cur_n_lines)
1791-
end,
1792-
})
1793-
17941776
T['Search method'] = new_set()
17951777

17961778
T['Search method']['works with "cover_or_prev"'] = function()

0 commit comments

Comments
 (0)