vim-surround
vim-surround copied to clipboard
empty text objects broken
Please figure out how to handle empty text objects.
Related issues:
All empty text objects that I have noticed that don't work:
-
tags:
<div></div>csttIMGreturn
-
quotes:
""cs"'
-
brackets/braces:
[]cs])
Every day I want to or accidentally attempt and, of course, fail to change an empty text object, but because this plugin doesn't account for this, it merely ends up frustrating me.
I am not sure if this issue, "inner tag" pending operator broken, is related, or if it is an entirely different issue all together. This issue is difficult to pinpoint, but if someone/somepeople could figure it out I would forever be in his/her/their debt.
Cheers,
The core of this is that empty text objects in Vim behave weird. If you press di] on an empty pair of brackets, for example, it doesn't update the register to an empty string. This could be fixed with some special casing, or with my long term fantasy of a rewrite to not directly depend on text objects.
It is technically possible to distinguish between an empty-region text object and a failed/invalid text object, if you create your own 'delete' operator...
function! s:delete_or_empty(...)
if !a:0
let l:func = matchstr(expand('<sfile>'), '<SNR>\w\+$')
let &opfunc = l:func
return "\<SNR>".matchstr(l:func, '<SNR>\zs\d\+_').'(oper)'
endif
if line("'[") == line("']") && col("'[") > col("']")
let s:empty_region = 1
return
endif
let l:v = { 'char': 'v', 'line': 'V', 'block': "\<c-v>"}[a:1]
execute 'normal! `['.l:v.'`]d'
endfunction
nnoremap <sid>(oper) g@
nmap <expr> <sid>(delete-or-empty) <sid>delete_or_empty()
function! Example()
" ...
let s:empty_region = 0
execute "normal \<SNR>".s:SID()."_(delete-or-empty)i["
if s:empty_region
echo 'empty'
endif
" ...
endfunction
function! s:SID()
return matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$')
endfunction
Though, empty regions would work only for vim built-in text objects.
Well, I suppose you both would know the issue better than I would. I see what you're saying tpope--even though I don't quite understand why vim does it that way.
That is an interesting idea andymass--although, I must admit that is is a bit beyond me tonight, as I have had a long day (s/day/week/)--but is it not possible to check the surroundings with getline('.') maybe in conjunction with some other builtin functions ... if the current text object is empty, then update the '< and '> registers manually or do whatever needs to be done to get the plugin to recognize that the text object is empty and manually update whatever it needs to update before continuing with the vim-surround magic?
I will certainly look more into it later. I must eat and rest. Thanks for your 2¢, both of you.
I'm not sure if you want to close the issue since this objective seems tricky due to vim's implementation of these text objects, but I will leave it open unless it is undesirable. Close it if you wish, but it has certainly been daily struggle for me ... albeit a minor inconvenience :smile:
@dylnmc I also met such problem. And I just delete some lines of codes to make it work. More info please refer to PR #270.