machakann / vim-sandwich

Set of operators and textobjects to search/select/edit sandwiched texts.
1.44k stars 37 forks source link

"Unanticipated error" when using operator-delete #114

Closed obcat closed 3 years ago

obcat commented 3 years ago

Explain the issue

Using <Plug>(operator-sandwich-delete) might give an Unanticipated error.

To Reproduce

  1. Write this in /tmp/vimrc:

    call delete('/tmp/vim-sandwich', 'rf')
    call system('git clone https://github.com/machakann/vim-sandwich.git /tmp/vim-sandwich')
    set runtimepath^=/tmp/vim-sandwich
  2. Run vim -Nu /tmp/vimrc.

  3. Source this script:

    let lines =<< trim END
      (foo)
    END
    call writefile(lines, '/tmp/t.txt')
    edit /tmp/t.txt
    execute "normal \<Plug>(operator-sandwich-delete)iW"

    Unanticipated error is given:

    operator-sandwich: Unanticipated error. [/private/tmp/s.vim[6]..function operator#sandwich#delete[1]..<SNR>18_do[9]..14[6]..20[7]..27[21]..46[10]..47[13]..<SNR>25_check_textobj_diff, line 68] Vim(normal):E471: Argument required: normal! 

    The buffer contains this text:

    (foo)

Expected behavior

No error is given. The buffer contains this text:

foo

Environment

obcat commented 3 years ago

I'm not sure it's correct, but this patch seems to fix the issue:

diff --git a/autoload/operator/sandwich/stuff.vim b/autoload/operator/sandwich/stuff.vim
index 6ee0fe5..2c7e36b 100644
--- a/autoload/operator/sandwich/stuff.vim
+++ b/autoload/operator/sandwich/stuff.vim
@@ -339,9 +339,11 @@ function! s:check_textobj_diff(head, tail, candidate, opt_noremap) abort  "{{{
     endif
   endfor

-  " restore visualmode
-  execute 'normal! ' . visualmode
-  execute 'normal! ' . "\<Esc>"
+  if visualmode !=# ''
+    " restore visualmode
+    execute 'normal! ' . visualmode
+    execute 'normal! ' . "\<Esc>"
+  endif
   " restore marks
   call setpos("'<", visual_head)
   call setpos("'>", visual_tail)
obcat commented 3 years ago

Ah! The patch might be wrong. Maybe we should restore the visualmode even when it was empty. I'll submit a revised version tomorrow (I'm already in the ohuton now)

obcat commented 3 years ago

Good morning. Here is a revised version:

diff --git a/autoload/operator/sandwich/stuff.vim b/autoload/operator/sandwich/stuff.vim
index 6ee0fe5..552b40d 100644
--- a/autoload/operator/sandwich/stuff.vim
+++ b/autoload/operator/sandwich/stuff.vim
@@ -340,8 +340,12 @@ function! s:check_textobj_diff(head, tail, candidate, opt_noremap) abort  "{{{
   endfor

   " restore visualmode
-  execute 'normal! ' . visualmode
-  execute 'normal! ' . "\<Esc>"
+  if visualmode ==# ''
+    call visualmode(1)
+  else
+    execute 'normal! ' . visualmode
+    execute 'normal! ' . "\<Esc>"
+  endif
   " restore marks
   call setpos("'<", visual_head)
   call setpos("'>", visual_tail)

Still, it may not correct. Just for your information...

machakann commented 3 years ago

Thanks!

obcat commented 3 years ago

Thanks too!