Skip to content

Commit 79f9a8d

Browse files
committed
(mini.ai) Allow empty regions to work with 'mini.operators'.
Details: - Resolves #441.
1 parent 5dff4f0 commit 79f9a8d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lua/mini/ai.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -987,8 +987,14 @@ MiniAi.select_textobject = function(ai_type, id, opts)
987987
local cache_eventignore = vim.o.eventignore
988988

989989
pcall(function()
990-
-- Do nothing in Operator-pending mode for empty region (except `c` or `d`)
991-
if tobj_is_empty and opts.operator_pending and not (vim.v.operator == 'c' or vim.v.operator == 'd') then
990+
-- Do nothing in Operator-pending mode for empty region (except `c`, `d`,
991+
-- or "replace" from 'mini.operators'). These are hand picked because they
992+
-- completely remove selected text, which is necessary for currently only
993+
-- possible empty region selection implementation.
994+
local is_empty_opending = tobj_is_empty and opts.operator_pending
995+
local is_minioperators_replace = vim.v.operator == 'g@' and vim.o.operatorfunc:find('MiniOperators%.replace') ~= nil
996+
local is_allowed_empty_opending = vim.v.operator == 'c' or vim.v.operator == 'd' or is_minioperators_replace
997+
if is_empty_opending and not is_allowed_empty_opending then
992998
H.message('Textobject region is empty. Nothing is done.')
993999
return
9941000
end
@@ -1009,7 +1015,7 @@ MiniAi.select_textobject = function(ai_type, id, opts)
10091015
vim.cmd('normal! ' .. vis_mode)
10101016
set_cursor(tobj.from)
10111017

1012-
if tobj_is_empty and opts.operator_pending then
1018+
if is_empty_opending then
10131019
-- Add single space (without triggering events) and visually select it.
10141020
-- Seems like the only way to make `ci)` and `di)` move inside empty
10151021
-- brackets. Original idea is from 'wellle/targets.vim'.

tests/test_ai.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,13 +2042,31 @@ T['Builtin']['Bracket']['is balanced'] = function(key)
20422042
end
20432043

20442044
T['Builtin']['Bracket']['works with empty region'] = function(key)
2045+
-- Emulate "replace" from 'mini.operators'
2046+
child.lua([[
2047+
_G.MiniOperators = {}
2048+
_G.MiniOperators.replace = function(mode)
2049+
if mode == nil then
2050+
vim.o.operatorfunc = 'v:lua.MiniOperators.replace'
2051+
return 'g@'
2052+
end
2053+
vim.fn.setreg('a', 'xxx')
2054+
vim.cmd('normal! `[v`]"aP`[')
2055+
end
2056+
2057+
vim.keymap.set('n', 'gr', _G.MiniOperators.replace, { expr = true })
2058+
]])
2059+
2060+
-- Validate
20452061
local left, right, _ = unpack(brackets[key])
20462062

20472063
local line = 'a' .. left .. right
20482064
for i = 0, 1 do
20492065
validate_tobj1d(line, i, { 'i', key }, { 3, 3 })
20502066
validate_edit1d(line, i, line, 2, { 'ci', key })
20512067
validate_edit1d(line, i, line, 2, { 'di', key })
2068+
2069+
validate_edit1d(line, i, 'a' .. left .. 'xxx' .. right, 2, { 'gri', key })
20522070
end
20532071
end
20542072

0 commit comments

Comments
 (0)