Youcompleteme: Auto close preview window after insertion not closing sometimes

Created on 21 Aug 2013  路  13Comments  路  Source: ycm-core/YouCompleteMe

Hi, the option "g:ycm_autoclose_preview_window_after_insertion" isn't working as it should. It definitely works, but on a certain case, it won't close the preview window.

If you auto complete a function and then go back to normal mode, it'll close.
If you auto complete a function, then type some random stuff, it'll close.
However, if you auto complete a function, then type another variable (which will autocomplete), and complete the function arguments, it won't close.

I'm testing this on Vim 7.4, Arch Linux 64 bit. And testing this with C++. If I type: printf -> CTRL + SPACE -> TAB -> ("testing"); -> ESCAPE, it won't close the preview window. Even though "testing" isn't a variable, it will show up as a completion (but this isn't the issue). If you test it out with other functions, it will be the same thing.

Is there any workaround or is it possible to fix this? It is a bit of a nuisance to see it not closing sometimes, but if I have to live with this, I guess it's all right. (Although, any kind of perfection wouldn't hurt :) ) Thanks.

Most helpful comment

@jrmrjnck, you can add to your ~/.vimrc file the following option in order to auto close the preview on option select:
let g:ycm_autoclose_preview_window_after_completion=1

All 13 comments

Please provide a complete, reproducible test case. Closed until then.

I'm seeing the same issue. Here's a reproducible test case (python):

With preview in completeopt and g:ycm_autoclose_preview_window_after_insertion set to 1.

import os
file_path = os.path  # cursor at the end of this line
  1. With your cursor at the end of os.path type .. Completion menu will appear.
  2. Type j followed by TAB. Will complete to join, and the preview window will open.
  3. At this point try two things:

    1. Press ESCAPE. The preview window will close (expected behavior).

    2. Type (, then ESCAPE. The preview window will not close (not expected).

I tested this with a minimal vimrc only loading YCM.

(I just deleted my previous comment, thought there was a dupplicate. I'll just make another)
I got the same issue. I took a look at the thing and I think I found the cause.

In the function that's supposed to close the preview window, there's a condition at line 511. The condition is that if the last query for completion returned results, then the preview window is closed. Else, we do nothing.

function! s:ClosePreviewWindowIfNeeded()
  let current_buffer_name = bufname('')

  " We don't want to try to close the preview window in special buffers like
  " "[Command Line]"; if we do, Vim goes bonkers. Special buffers always start
  " with '['.
  if current_buffer_name[ 0 ] == '['
    return
  endif

  if s:searched_and_results_found
    " This command does the actual closing of the preview window. If no preview
    " window is shown, nothing happens.
    pclose
  endif
endfunction

If I type (, then another query for completion is made and there's no result, since nothing matches "myFunctionName(". The completion popup closes when I type (, which goes along with the "no results from completion query" case. Since the last query for completion returned no results, s:searched_and_results_found is set to 0 and that means the preview window won't be closed when I leave the Insert mode. That explains the bug.

I don't have any good fix in mind for this.
Removing the whole s:searched_and_results_found condition at line 511 would fix it, but there's probably a good reason for this condition. Maybe someone who knows this thing better than me could tell.

Hope this'll help.

(English isn't my native language. I hope the reading wasn't too painfull.)

I've also found a "cheap" workaround if you're still interested, @reportados123.
=> Type CTRL + SPACEbefore leaving "Insert Mode".

Reason / Explanation (details in previous comment) :
This will open the completion popup, which means it'll do a completion query which will return results. s:searched_and_results_found will be different from 0.
As long as the completion popup is there, the preview window should close when you leave "Insert Mode".

Note to self: take a closer look at this when you find the time.

+1 I'm experiencing the same thing, as @vphilippon says, for me it's after typing a (. I suppose the reason for not removing the s:searched_and_results_found is that the preview window would then autoclose every time insert mode is exited, which may not always be the desired effect.

+1 experiencing the same issue.
Type the following into a python file to reproduce:

import os
os.WCONTINUED;

In other words, if you type something after the completion is done (in this case the ';'), the window is left open.

.vimrc settings:

let g:ycm_add_preview_to_completeopt = 1
let g:ycm_autoclose_preview_window_after_insertion = 1 "Not working ??

+1 experiencing the same issue.
Type the following:

import sys
sys.std

press Tab to select stdin, then press ESC to exit insert mode, the windows is closed.

But if type the following:

sys.stdin = blabla

after I select stdin, the windows is left open.

And most time I will type something after selecting the completion(type the parameters according to the preview window for example), so the preview window is basically always open for me. I really hope you can fix this, but thanks for your amazing YCM no matter what.

Allright, I went a little deeper into this to find a fix.

After doing a bit of history research, I came to the conclusion that s:searched_and_results_found was actually used to prevent an infinite loop.
Although, the de8f45c2021814529928c5025ce08ffb6b47cb6f commit changed this. The only purpose of the s:searched_and_results_found variable now was to avoid calling pclose if there was no result.

Now, this condition is only causing trouble. Calling pclose whether there are results or not is not a problem.

So in the end, I've found no reason anymore for this condition, unless @Valloric says otherwise, so we can simply remove it, and the s:searched_and_results_found variable too.

PR is comming, and I've signed the CLA.
Cheers!

I am still seeing this problem - in fact, once the preview window opens for any reason it never closes until I manually :q it. I have tried disabling all other plugins to make sure it wasn't a bad interaction. Any idea if there's a regression or if it's a problem on my end?

@jrmrjnck I've updated to the last YCM version, and it still works on my end (Used examples above), so I assume there's no regression.
Make sure you have let g:ycm_autoclose_preview_window_after_insertion = 1 in your .vimrc (I'm telling, just in case).

@jrmrjnck, you can add to your ~/.vimrc file the following option in order to auto close the preview on option select:
let g:ycm_autoclose_preview_window_after_completion=1

This should be default too.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tgzhou98 picture tgzhou98  路  3Comments

Bercio picture Bercio  路  5Comments

ghost picture ghost  路  4Comments

d0u9 picture d0u9  路  4Comments

blackode picture blackode  路  3Comments