Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add preview for marks #43

Merged
merged 10 commits into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,13 @@ hi default link ClapMatches Search

" By default ClapQuery will use the bold fg of Normal and the same bg of ClapInput

hi ClapSelected cterm=bold,underline gui=bold,underline ctermfg=80 guifg=#5fd7d7
hi ClapCurrentSelection cterm=bold gui=bold ctermfg=224 guifg=#ffd7d7
hi ClapDefaultPreview ctermbg=237 guibg=#3E4452
hi ClapDefaultSelected cterm=bold,underline gui=bold,underline ctermfg=80 guifg=#5fd7d7
hi ClapDefaultCurrentSelection cterm=bold gui=bold ctermfg=224 guifg=#ffd7d7

hi default link ClapPreview ClapDefaultPreview
hi default link ClapSelected ClapDefaultSelected
hi default link ClapCurrentSelection ClapDefaultCurrentSelection
```

If you want a different highlight for the matches found, try:
Expand Down Expand Up @@ -267,14 +272,14 @@ Field | Type | Required | Has default implementation
`on_move` | funcref | optional | No
`on_enter` | funcref | optional | No
`converter` | funcref | optional | No
`jobstop` | funcref | **mandatory** | **Yes** if you use `clap#dispatcher#jobstart(cmd)`
`jobstop` | funcref | **mandatory** | **Yes** if you use `clap#dispatcher#job_start(cmd)`
`support_open_action` | Bool | optional | **Yes** if the `sink` is `e`/`edit`/`edit!`

- `on_typed`: reference to function to spawn an async job.
- `converter`: reference to function to convert the raw output of job to another form, e.g., prepend an icon to the grep result, see [clap/provider/grep.vim](autoload/clap/provider/grep.vim).
- `jobstop`: Reference to function to stop the current job of an async provider. By default you could utilize `clap#dispatcher#jobstart(cmd)` to start a new job, and then the job stop part will be handled by vim-clap as well, otherwise you'll have to take care of the `jobstart` and `jobstop` on your own.
- `jobstop`: Reference to function to stop the current job of an async provider. By default you could utilize `clap#dispatcher#job_start(cmd)` to start a new job, and then the job stop part will be handled by vim-clap as well, otherwise you'll have to take care of the `jobstart` and `jobstop` on your own.

You must provide `sink`, `on_typed` option. It's a bit of complex to write an asynchornous provider, you'll need to prepare the command for spawning the job and overal workflow, although you could use `clap#dispatcher#jobstart(cmd)` to let vim-clap deal with the job control and display update. Take [clap/provider/grep.vim](autoload/clap/provider/grep.vim) for a reference.
You must provide `sink`, `on_typed` option. It's a bit of complex to write an asynchornous provider, you'll need to prepare the command for spawning the job and overal workflow, although you could use `clap#dispatcher#job_start(cmd)` to let vim-clap deal with the job control and display update. Take [clap/provider/grep.vim](autoload/clap/provider/grep.vim) for a reference.

### Register provider

Expand Down
18 changes: 13 additions & 5 deletions autoload/clap/api.vim
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function! s:init_display() abort
" An empty buffer consists of one empty line. If you append, this line is still there.
" https://github.com/vim/vim/issues/5016
" Thus this is unavoidable.
if empty(getbufline(self.bufnr, '$')[0])
if empty(get(getbufline(self.bufnr, '$'), 0, ''))
silent call deletebufline(self.bufnr, '$')
endif
endfunction
Expand Down Expand Up @@ -477,6 +477,15 @@ function! s:init_provider() abort
return provider
endfunction

function! s:inject_base_api(dict) abort
let dict = a:dict
let dict.goto_win = function('s:_goto_win')
let dict.get_lines = function('s:_get_lines')
let dict.getbufvar = function('s:_getbufvar')
let dict.setbufvar = function('s:_setbufvar')
let dict.setbufvar_batch = function('s:_setbufvar_batch')
endfunction

function! clap#api#bake() abort
let g:clap = {}
let g:clap.is_busy = 0
Expand All @@ -485,10 +494,7 @@ function! clap#api#bake() abort
let g:clap.spinner = {}

let g:clap.start = {}
let g:clap.start.goto_win = function('s:_goto_win')
let g:clap.start.get_lines = function('s:_get_lines')
let g:clap.start.getbufvar = function('s:_getbufvar')
let g:clap.start.setbufvar = function('s:_setbufvar')
call s:inject_base_api(g:clap.start)

let g:clap.input = s:init_input()
let g:clap.display = s:init_display()
Expand All @@ -507,6 +513,8 @@ function! clap#api#bake() abort
let g:clap.open_win = function('clap#popup#open')
let g:clap.close_win = function('clap#popup#close')
endif

call s:inject_base_api(g:clap.preview)
endfunction

let &cpo = s:save_cpo
Expand Down
44 changes: 36 additions & 8 deletions autoload/clap/dispatcher.vim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
let s:save_cpo = &cpo
set cpo&vim

let s:job_timer = -1
let s:dispatcher_delay = 300

if has('nvim')

function! s:apply_append_or_cache(raw_output) abort
Expand Down Expand Up @@ -130,7 +133,7 @@ else
endfunction

function! s:err_cb(channel, message) abort
call g:clap.abort(['channel: '.a:channel, 'message: '.a:message, 'cmd: '.s:executed_cmd])
" call g:clap.abort(['channel: '.a:channel, 'message: '.a:message, 'cmd: '.s:executed_cmd])
endfunction

function! s:close_cb(_channel) abort
Expand Down Expand Up @@ -181,26 +184,51 @@ function! s:has_no_matches() abort
endif
endfunction

" Start a job given the command.
function! clap#dispatcher#jobstart(cmd) abort
function! s:apply_job_start(_timer) abort
call clap#util#run_from_project_root(function('s:job_start'), s:cmd)

let s:executed_cmd = strftime("%Y-%m-%d %H:%M:%S").' '.s:cmd
endfunction

function! s:prepare_job_start(cmd) abort
call s:jobstop()

let s:cache_size = 0
let s:loaded_size = 0
let g:clap.display.cache = []
let s:preload_is_complete = v:false

let s:cmd = a:cmd

let s:vim_output = []

if has_key(g:clap.provider._(), 'converter')
let s:has_converter = v:true
let s:Converter = g:clap.provider._().converter
else
let s:has_converter = v:false
endif

let s:vim_output = []
let g:clap.display.cache = []
endfunction

call s:jobstop()
function! s:job_strart_with_delay() abort
if s:job_timer != -1
call timer_stop(s:job_timer)
endif

let s:job_timer = timer_start(s:dispatcher_delay, function('s:apply_job_start'))
endfunction

call clap#util#run_from_project_root(function('s:job_start'), a:cmd)
" Start a job immediately given the command.
function! clap#dispatcher#job_start(cmd) abort
call s:prepare_job_start(a:cmd)
call s:apply_job_start('')
endfunction

let s:executed_cmd = strftime("%Y-%m-%d %H:%M:%S").' '.a:cmd
" Start a job with a delay given the command.
function! clap#dispatcher#job_start_with_delay(cmd) abort
call s:prepare_job_start(a:cmd)
call s:job_strart_with_delay()
endfunction

function! clap#dispatcher#jobstop() abort
Expand Down
15 changes: 14 additions & 1 deletion autoload/clap/floating_win.vim
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ function! g:clap#floating_win#display.compact_if_undersize() abort
let opts.height = s:display_opts.height
endif
call nvim_win_set_config(s:display_winid, opts)
call s:try_adjust_preview()
endfunction

function! g:clap#floating_win#spinner.open() abort
Expand Down Expand Up @@ -133,6 +134,15 @@ function! g:clap#floating_win#input.open() abort
let g:clap.input.winid = s:input_winid
endfunction

function! s:try_adjust_preview() abort
if exists('s:preview_winid')
let preview_opts = nvim_win_get_config(s:preview_winid)
let opts = nvim_win_get_config(s:display_winid)
let preview_opts.row = opts.row + opts.height
call nvim_win_set_config(s:preview_winid, preview_opts)
endif
endfunction

function! clap#floating_win#preview.show(lines) abort
if !exists('s:preview_winid')
let opts = nvim_win_get_config(s:display_winid)
Expand All @@ -147,6 +157,9 @@ function! clap#floating_win#preview.show(lines) abort
call setbufvar(s:preview_bufnr, '&number', 0)
call setbufvar(s:preview_bufnr, '&cursorline', 0)
call setbufvar(s:preview_bufnr, '&signcolumn', 'no')

let g:clap#floating_win#preview.winid = s:preview_winid
let g:clap#floating_win#preview.bufnr = s:preview_bufnr
endif
call clap#util#nvim_buf_set_lines(s:preview_bufnr, a:lines)
endfunction
Expand Down Expand Up @@ -177,7 +190,7 @@ function! clap#floating_win#open() abort

augroup ClapEnsureAllClosed
autocmd!
autocmd BufEnter,WinEnter,WinLeave * call s:ensure_closed()
" autocmd BufEnter,WinEnter,WinLeave * call s:ensure_closed()
augroup END

" This augroup should be retained after closing vim-clap for the benefit
Expand Down
12 changes: 10 additions & 2 deletions autoload/clap/handler.vim
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ function! s:navigate(direction) abort
call clap#sign#toggle_cursorline()
endfunction

function! s:on_move_safe() abort
" try
call g:clap.provider.on_move()
" catch
" call g:clap.preview.show([v:exception])
" endtry
endfunction

if has('nvim')
function! clap#handler#navigate_result(direction) abort
call g:clap.display.goto_win()
Expand All @@ -82,7 +90,7 @@ if has('nvim')

call g:clap.input.goto_win()

call g:clap.provider.on_move()
call s:on_move_safe()

" Must return '' explicitly
return ''
Expand All @@ -97,7 +105,7 @@ if has('nvim')
else
function! clap#handler#navigate_result(direction) abort
call s:navigate(a:direction)
call g:clap.provider.on_move()
call s:on_move_safe()
endfunction

function! clap#handler#internal_navigate(direction) abort
Expand Down
5 changes: 3 additions & 2 deletions autoload/clap/impl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ endfunction

function! s:apply_source_async() abort
let cmd = g:clap.provider.source_async_or_default()
call clap#dispatcher#jobstart(cmd)
call clap#dispatcher#job_start(cmd)
endfunction

function! s:on_typed_async_impl() abort
Expand Down Expand Up @@ -113,7 +113,8 @@ function! clap#impl#on_typed() abort
call s:on_typed_async_impl()
else
" Choose the suitable way according to the source size.
if len(g:clap.provider.get_source()) > s:async_threshold
if type(g:clap.provider._().sink) == v:t_string
\ || len(g:clap.provider.get_source()) > s:async_threshold
call s:on_typed_async_impl()
else
call s:on_typed_sync_impl()
Expand Down
6 changes: 4 additions & 2 deletions autoload/clap/init.vim
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ function! s:init_hi_groups() abort
call s:hi_display_invisible()
autocmd ColorScheme * call s:hi_display_invisible()

hi ClapDefaultPreview ctermbg=237 guibg=#3E4452

if !hlexists('ClapPreview')
execute 'hi default link ClapPreview' s:preview_defaualt_hi_group
let s:preview_group = s:preview_defaualt_hi_group
hi default link ClapPreview ClapDefaultPreview
let s:preview_group = 'ClapDefaultPreview'
else
let s:preview_group = 'ClapPreview'
endif
Expand Down
50 changes: 36 additions & 14 deletions autoload/clap/popup.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ function! s:reconfigure_display_opts() abort
let s:display_opts = s:prepare_display_opts()
endfunction

function! s:execute_in_display() abort
let w:clap_no_matches_id = matchadd("ClapNoMatchesFound", g:__clap_no_matches_pattern)
setlocal signcolumn=yes
endfunction

function! s:create_display() abort
if !exists('s:display_winid') || empty(popup_getpos(s:display_winid))
let col = &signcolumn ==# 'yes' ? 2 : 1
Expand All @@ -61,8 +66,7 @@ function! s:create_display() abort

let g:clap#popup#display.width = &columns * 2 / 3

call win_execute(s:display_winid, 'let w:clap_no_matches_id = matchadd("ClapNoMatchesFound", g:__clap_no_matches_pattern)')
call win_execute(s:display_winid, 'setlocal signcolumn=yes')
call win_execute(s:display_winid, 'call s:execute_in_display()')
call popup_hide(s:display_winid)

let g:clap.display.winid = s:display_winid
Expand All @@ -84,6 +88,17 @@ function! g:clap#popup#display.compact_if_undersize() abort
let pos.maxheight = s:display_opts.height
endif
call popup_move(s:display_winid, pos)

call s:try_adjust_preview()
endfunction

function! s:try_adjust_preview() abort
if exists('s:preview_winid') && !empty(popup_getpos(s:preview_winid))
let pos = popup_getpos(s:display_winid)
let preview_pos = popup_getpos(s:preview_winid)
let preview_pos.line = pos.line + pos.height
call popup_move(s:preview_winid, preview_pos)
endif
endfunction

function! s:create_preview() abort
Expand All @@ -92,20 +107,20 @@ function! s:create_preview() abort
let col = pos.col
let line = pos.line + pos.height
let minwidth = pos.width
" If the preview win has border, then minwidth - 2.
let s:preview_winid = popup_create([], #{
\ zindex: 100,
\ col: col,
\ line: line,
\ minwidth: minwidth - 2,
\ maxwidth: minwidth - 2,
\ minwidth: minwidth,
\ maxwidth: minwidth,
\ wrap: v:false,
\ scrollbar: 0,
\ border: [1, 1, 1, 1],
\ highlight: 'ClapPreview',
\ })
call popup_hide(s:preview_winid)
call win_execute(s:preview_winid, 'setlocal nonumber')
let g:clap#popup#preview.winid = s:preview_winid
let g:clap#popup#preview.bufnr = winbufnr(s:preview_winid)
endif
endfunction

Expand Down Expand Up @@ -156,6 +171,14 @@ function! s:adjust_spinner() abort
endif
endfunction

function! s:execute_in_input() abort
let s:save_completeopt = &completeopt
set completeopt=
setlocal nonumber
let w:clap_query_hi_id = matchaddpos("ClapQuery", [1])
let b:coc_suggest_disable = 1
endfunction

function! s:create_input() abort
if !exists('s:input_winid') || empty(popup_getpos(s:input_winid))
let pos = popup_getpos(s:display_winid)
Expand All @@ -169,11 +192,8 @@ function! s:create_input() abort
let pos.zindex = 100
let s:input_winid = popup_create([], pos)
call popup_hide(s:input_winid)
call win_execute(s:input_winid, 'setlocal nonumber')
call win_execute(s:input_winid, 'let w:clap_query_hi_id = matchaddpos("ClapQuery", [1])')
let s:save_completeopt = &completeopt
call win_execute(s:input_winid, 'set completeopt=')
call win_execute(s:input_winid, 'let b:coc_suggest_disable = 1')

call win_execute(s:input_winid, 'call s:execute_in_input()')
if s:exists_deoplete
call deoplete#custom#buffer_option('auto_complete', v:false)
endif
Expand Down Expand Up @@ -323,11 +343,13 @@ function! s:move_manager.ctrl_d(_winid) abort
call s:apply_on_typed()
endfunction

let s:move_manager["\<C-J>"] = { winid -> win_execute(winid, 'call clap#handler#navigate_result("down")') }
" noautocmd is neccessary in that too many plugins use redir, otherwise we'll
" see E930: Cannot use :redir inside execute().
let s:move_manager["\<C-J>"] = { winid -> win_execute(winid, 'noautocmd call clap#handler#navigate_result("down")') }
let s:move_manager["\<Down>"] = s:move_manager["\<C-J>"]
let s:move_manager["\<C-K>"] = { winid -> win_execute(winid, 'call clap#handler#navigate_result("up")') }
let s:move_manager["\<C-K>"] = { winid -> win_execute(winid, 'noautocmd call clap#handler#navigate_result("up")') }
let s:move_manager["\<Up>"] = s:move_manager["\<C-K>"]
let s:move_manager["\<Tab>"] = { winid -> win_execute(winid, 'call clap#handler#select_toggle()') }
let s:move_manager["\<Tab>"] = { winid -> win_execute(winid, 'noautocmd call clap#handler#select_toggle()') }
let s:move_manager["\<CR>"] = { _winid -> clap#handler#sink() }
let s:move_manager["\<Esc>"] = { _winid -> clap#handler#exit() }
let s:move_manager["\<C-A>"] = s:move_manager.ctrl_a
Expand Down
2 changes: 1 addition & 1 deletion autoload/clap/provider/grep.vim
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function! s:spawn(query) abort
" This should happen before the new job.
call g:clap.display.clear()

call clap#dispatcher#jobstart(s:cmd(query))
call clap#dispatcher#job_start(s:cmd(query))

" Consistent with --smart-case of rg
" Searches case insensitively if the pattern is all lowercase. Search case sensitively otherwise.
Expand Down
Loading