context.vim icon indicating copy to clipboard operation
context.vim copied to clipboard

Plugin breaks window stack?

Open mizlan opened this issue 4 years ago • 7 comments
trafficstars

I ran into this while using this plugin with https://github.com/pechorin/any-jump.vim but I am able to reproduce it without it.

To reproduce:

  1. Open a buffer and scroll to a location where the context window shows up.
  2. Open a new window, for example :vs test.
  3. Attempt to go back to the previous window with :wincmd p

Bug: For me, it will not change windows the first time that command is run. This is already a bug. However, when :wincmd p is ran again, it will go to the context window instead of the buffer window.

Tested on this init.vim:

call plug#begin()

Plug 'https://github.com/wellle/context.vim'

call plug#end()

mizlan avatar Apr 02 '21 18:04 mizlan

Doesn't seem like there's a fix for this from what I can tell. Workarounds are already used to navigate between buffers for redraws. Is there a way to move windows without affecting the stack, or call commands to modify those windows without navigating to them?

mizlan avatar Apr 16 '21 02:04 mizlan

Thanks! I think we could do something similar to here to restore the previous window: https://github.com/wellle/visual-split.vim/blob/master/plugin/visual-split.vim

wellle avatar Apr 19 '21 18:04 wellle

This also breaks behavior of Ctrl+P (:CtrlPMRUFiles), Nerd Tree and similar when multiple windows are open. Eg.

  1. open two windows and let context window appear.
  2. go to second opened window.
  3. call for :CtrlPMRUFiles and choose some file to open

expected: have selected file open in second window

actual: selected file is open in first window

This won't trigger if context window is not opened or context plugin is disabled

renkam avatar Aug 09 '21 14:08 renkam

Thanks for bringing this back to my attention, I forgot about this.

It seems like what wellle linked was in specific:

https://github.com/wellle/visual-split.vim/blob/423a25911b3e8da04a28d29f205e1059a06e6afa/plugin/visual-split.vim#L147-L152

function! s:reset_previous_window(previous_winnr)
    " Switch to remembered window and back again to reset previous window to
    " what it was before we overwrote it.
    execute a:previous_winnr.'wincmd w'
    wincmd p
endfunction

I can integrate this into the context.vim codebase since a quick grep suggests this function isn't being used, and create a PR sometime.

mizlan avatar Aug 09 '21 19:08 mizlan

It would be awesome. Please do this, I can test the fix and report.

renkam avatar Aug 10 '21 08:08 renkam

This bug only happens on Neovim for me. Running Neovim 0.5 and Vim 8.2

mizlan avatar Aug 22 '21 16:08 mizlan

I can't get it to work... I keep getting this error

Error detected while processing function context#update:
line   90:
E121: Undefined variable: w:context
E15: Invalid expression: w:context.needs_layout
diff --git a/autoload/context/popup/nvim.vim b/autoload/context/popup/nvim.vim
index af8cf95..e7329c3 100644
--- a/autoload/context/popup/nvim.vim
+++ b/autoload/context/popup/nvim.vim
@@ -50,9 +50,27 @@ function! context#popup#nvim#redraw(winid, popup, lines) abort
     " NOTE: because of some neovim limitation we have to temporarily switch to
     " the popup window so we can clear the highlighting
     " https://github.com/neovim/neovim/issues/10822
+
+    " As a result we also need to reset the previous window. Code adapted from
+    " visual-split.vim
+    let win_count = winnr('$')
+    if win_count > 2
+      wincmd p
+      let previous_winnr = winnr()
+      wincmd p
+    endif
+
     execute 'noautocmd' bufwinnr(buf) . 'wincmd w'
     call clearmatches()
+
+    " Reset previous window
     wincmd p
+
+    if exists('previous_winnr') && previous_winnr != winnr()
+      wincmd p
+      execute previous_winnr.'wincmd w'
+      wincmd p
+    endif
 endfunction
 
 function! context#popup#nvim#close(popup) abort

I've tried different variations of the above. I think it has something to do with autocmds running.

mizlan avatar Aug 22 '21 16:08 mizlan