junegunn / fzf.vim

fzf :heart: vim
MIT License
9.62k stars 584 forks source link

The preview window pops up and then crashes before you can see it clearly on Windows 10 #1541

Closed jiangjianshan closed 4 months ago

jiangjianshan commented 5 months ago

Hello,

I have update to the newest version of fzf and fzf.vim from github via vim-plug. Today when I want to search the word under cursor. I have found the preview window pops up and then crashes before you can see it clearly on Windows 10. Below is my configuration of _vimrc on Windows 10:

" -------------------------begin------------------------------------
" -------------------------------------------------------------------
" Plugin: fzf.vim
" Website: https://github.com/junegunn/fzf.vim
" Description: Things you can do with fzf and Vim.
" Keymap: TODO
" -------------------------------------------------------------------
" This is the default option:
"   - Preview window on the right with 50% width
"   - CTRL-/ will toggle preview window.
" - Note that this array is passed as arguments to fzf#vim#with_preview function.
" - To learn more about preview window options, see `--preview-window` section of `man fzf`.
let g:fzf_preview_window = ['right,50%', 'ctrl-/']

" [Buffers] Jump to the existing window if possible
let g:fzf_buffers_jump = 1

" [[B]Commits] Customize the options used by 'git log':
let g:fzf_commits_log_options = '--graph --color=always --format="%C(auto)%h%d %s %C(black)%C(bold)%cr"'

" [Tags] Command to generate tags file
let g:fzf_tags_command = 'ctags -R'

" [Commands] --expect expression for directly executing the command
let g:fzf_commands_expect = 'alt-enter,ctrl-x'

" An action can be a reference to a function that processes selected lines
function! s:build_quickfix_list(lines)
  call setqflist(map(copy(a:lines), '{ "filename": v:val }'))
  copen
  cc
endfunction

let g:fzf_action = {
  \ 'ctrl-q': function('s:build_quickfix_list'),
  \ 'ctrl-t': 'tab split',
  \ 'ctrl-x': 'split',
  \ 'ctrl-v': 'vsplit' }

" - Popup window (center of the current window)
let g:fzf_layout = { 'window': { 'width': 0.9, 'height': 0.8, 'relative': v:true } }

" Customize fzf colors to match your color scheme
let g:fzf_colors =
\ { 'fg':      ['fg', 'Normal'],
  \ 'bg':      ['bg', 'Normal'],
  \ 'hl':      ['fg', 'Comment'],
  \ 'fg+':     ['fg', 'CursorLine', 'CursorColumn', 'Normal'],
  \ 'bg+':     ['bg', 'CursorLine', 'CursorColumn'],
  \ 'hl+':     ['fg', 'Statement'],
  \ 'info':    ['fg', 'PreProc'],
  \ 'border':  ['fg', 'Ignore'],
  \ 'prompt':  ['fg', 'Conditional'],
  \ 'pointer': ['fg', 'Exception'],
  \ 'marker':  ['fg', 'Keyword'],
  \ 'spinner': ['fg', 'Label'],
  \ 'header':  ['fg', 'Comment'] }

" Enable per-command history
" - History files will be stored in the specified directory
" - When set, CTRL-N and CTRL-P will be bound to 'next-history' and
"   'previous-history' instead of 'down' and 'up'.
if has('win32')
  let g:fzf_history_dir = 'E:/SPB_Data/.local/share/fzf-history'
else
  let g:fzf_history_dir = '$HOME/.local/share/fzf-history'
endif

" Get Files
if has('win32')
  command! -bang -nargs=? -complete=dir Files
      \ call fzf#vim#files(<q-args>, {'options': ['--layout=reverse', '--info=inline', '--preview', '$VIM/vimfiles/plugged/fzf.vim/bin/preview.sh {}']}, <bang>0)
else
  command! -bang -nargs=? -complete=dir Files
      \ call fzf#vim#files(<q-args>, {'options': ['--layout=reverse', '--info=inline', '--preview', '~/.vim/plugged/fzf.vim/bin/preview.sh {}']}, <bang>0)
endif

" Git grep wrapper
command! -bang -nargs=* GGrep
  \ call fzf#vim#grep(
  \   'git grep --line-number -- '.shellescape(<q-args>), 0,
  \   fzf#vim#with_preview({'dir': systemlist('git rev-parse --show-toplevel')[0]}), <bang>0)

" Get text in files with Rg
command! -bang -nargs=* Rg
  \ call fzf#vim#grep(
  \   'rg --column --line-number --no-heading --color=always --smart-case -- '.shellescape(<q-args>), 1,
  \   fzf#vim#with_preview(), <bang>0)

" Ripgrep advanced
function! RipgrepFzf(query, fullscreen)
  let command_fmt = 'rg --column --line-number --no-heading --color=always --smart-case -- %s || true'
  let initial_command = printf(command_fmt, shellescape(a:query))
  let reload_command = printf(command_fmt, '{q}')
  let spec = {'options': ['--phony', '--query', a:query, '--bind', 'change:reload:'.reload_command]}
  call fzf#vim#grep(initial_command, 1, fzf#vim#with_preview(spec), a:fullscreen)
endfunction

command! -nargs=* -bang RG call RipgrepFzf(<q-args>, <bang>0)

nnoremap <silent> <leader>b :Buffers<CR>
nnoremap <silent> <leader>p :Files<CR>
nnoremap <silent> <leader>f :Rg <C-R><C-W><CR>
nnoremap <silent> <leader>l :Lines<CR>
nnoremap <silent> <leader>bl :BLines<CR>
nnoremap <silent> <leader>` :Marks<CR>
nnoremap <silent> <leader>w :Windows<CR>
nnoremap <silent> <Leader>hh :History<CR>
nnoremap <silent> <Leader>h :History:<CR>
nnoremap <silent> <Leader>h/ :History/<CR> 

" Mapping selecting mappings
nmap <leader>m <plug>(fzf-maps-n)
xmap <leader>m <plug>(fzf-maps-x)
omap <leader>m <plug>(fzf-maps-o)

" Insert mode completion
imap <c-x><c-k> <plug>(fzf-complete-word)
imap <c-x><c-f> <plug>(fzf-complete-path)
imap <c-x><c-l> <plug>(fzf-complete-line)

" Path completion with custom source command
inoremap <expr> <c-x><c-f> fzf#vim#complete#path('rg --files')

" Word completion with custom spec with popup layout option
inoremap <expr> <c-x><c-k> fzf#vim#complete#word({'window': { 'width': 0.2, 'height': 0.9, 'xoffset': 1 }})

" Global line completion (not just open buffers. ripgrep required.)
inoremap <expr> <c-x><c-l> fzf#vim#complete(fzf#wrap({
  \ 'prefix': '^.*$',
  \ 'source': 'rg -n ^ --color always',
  \ 'options': '--ansi --delimiter : --nth 3..',
  \ 'reducer': { lines -> join(split(lines[0], ':\zs')[2:], '') }}))

" Reducer
function! s:make_sentence(lines)
  return substitute(join(a:lines), '^.', '\=toupper(submatch(0))', '').'.'
endfunction

if has('win32')
  inoremap <expr> <c-x><c-s> fzf#vim#complete({
    \ 'source':  'cat E:/SPB_Data/share/dict/words',
    \ 'reducer': function('<sid>make_sentence'),
    \ 'options': '--multi --reverse --margin 15%,0',
    \ 'left':    20})
else
  inoremap <expr> <c-x><c-s> fzf#vim#complete({
    \ 'source':  'cat /usr/share/dict/words',
    \ 'reducer': function('<sid>make_sentence'),
    \ 'options': '--multi --reverse --margin 15%,0',
    \ 'left':    20})
endif

function! s:fzf_statusline()
  " Override statusline as you like
  highlight fzf1 ctermfg=161 ctermbg=251
  highlight fzf2 ctermfg=23 ctermbg=251
  highlight fzf3 ctermfg=237 ctermbg=251
  setlocal statusline=%#fzf1#\ >\ %#fzf2#fz%#fzf3#f
endfunction

autocmd! User FzfStatusLine call <SID>fzf_statusline()
" -------------------------end------------------------------------

When I try :Files command, I can see the preview windows. But if I try :Rg or :RG command, the issue will be happened. The above configuration of _vimrc work for a long time for the older version of fzf and fzf.vim. Maybe the newest version of fzf and fzf.vim have changed something and my configuration has to be changed?

junegunn commented 5 months ago

Please update tha main fzf repo to the latest and see if it helps. https://github.com/junegunn/fzf/commit/5669f48343cac868eb7432950db61c9aa2383ab6

jiangjianshan commented 4 months ago

@junegunn , I have just check the code content of fzf, and compared with the commit https://github.com/junegunn/fzf/commit/5669f48343cac868eb7432950db61c9aa2383ab6 .This commit has been applied already but no any effect on this issue. I have try :Files comanad in Vim, it can work correctly for preview window. but :GGrep and :Rg can't. image Maybe the issue is caused inside this fzf#vim#with_preview function. And if I remove below parts from _vimrc: image So that to make :Rg to use default definition command. And then try :Rg command, the issue is still there. Compare the implementation of :Files and :Rg in fzf.vim\autoload\fzf\vim.vim. Because both of :Files and :Rg using fzf#vim#with_preview function and only :Rg using fzf#vim#grep function. So it seems that the issue is caused by the function fzf#vim#grep. I'm not family with vim script. Just can analyze the issue hang at here.

junegunn commented 4 months ago

Did you update the fzf binary as well?

You can test with the one attached in https://github.com/junegunn/fzf/issues/3771#issuecomment-2097140852

jiangjianshan commented 4 months ago

@junegunn , this morning I have updated the fzf and fzf.vim to the newest version of commit and tried again. The issue is still there. Is there any way I can catch the trace or log so that I can send it here for analysis purpose.

junegunn commented 4 months ago

I'll test it on a Windows PC when I get a chance. By the way, the :RG command is now included in fzf.vim repo, so you don't need to define it on your configuration file.

https://github.com/junegunn/fzf.vim/blob/99093bd103b847dd3ed149b0c9e7146c8ed46643/plugin/fzf.vim#L64

Also, :Rg is in fzf.vim with preview support.

https://github.com/junegunn/fzf.vim/blob/99093bd103b847dd3ed149b0c9e7146c8ed46643/plugin/fzf.vim#L63

jiangjianshan commented 4 months ago

@junegunn , I have new discoveries this morning. I have forget to put the path of bash.exe into Windows's PATH before because my Windows 10 has been reinstalled several weeks ago. If I add ''C:\Program Files\Git\bin“ into PATH envrionment of Windows 10. When I use :Rg command on Vim, it will open the pop up window and show it for 1~2 seconds and then disappear. image This could be a usefull clues to analyze the issue. Something should be the arguments pass to the preview.sh is wrong via the default :Rg definition inside fzf.vim.

junegunn commented 4 months ago

Hi, 0.52.1 was just released. It fixes argument escaping on Windows. Can you update both fzf and fzf.vim and test if it helps, preferably with a minimal configuration like this?


call plug#begin()
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
call plug#end()
jiangjianshan commented 4 months ago

@junegunn , thanks for your job. Now the issues has been solved after I use the newest version of fzf and fzf.vim.