-
Notifications
You must be signed in to change notification settings - Fork 309
Add float window for hover info #347
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
Changes from all commits
eab83e4
944b56d
efd1be4
b35ed42
4a32d43
5f803e4
96a9c3d
c554bfb
9daf69f
f5e3605
f7af2c6
066cbb3
f0d9738
2e59e3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
"""""""""""""""""""""""""""""""""""""""""" | ||
" LICENSE: | ||
" Author: | ||
" Version: | ||
" CreateTime: 2019-03-16 16:36:16 | ||
" LastUpdate: 2019-03-16 16:36:16 | ||
" Desc: float win | ||
"""""""""""""""""""""""""""""""""""""""""" | ||
|
||
if exists("s:is_load") | ||
finish | ||
endif | ||
let s:is_load = 1 | ||
|
||
let s:float_win = 0 | ||
let s:curbuf = 0 | ||
let s:data_buf = [] | ||
|
||
let s:float_width = 0 " float window height | ||
let s:float_height = 0 | ||
|
||
function! s:reset() | ||
let s:data_buf = [] | ||
call nvim_buf_set_option(s:curbuf, 'modifiable', v:true) | ||
endfunction | ||
|
||
function! s:set_buf_option() | ||
call nvim_buf_set_option(s:curbuf, 'modifiable', v:false) | ||
endfunction | ||
|
||
function! s:set_win_option() | ||
call nvim_win_set_option(s:float_win, 'number', v:false) | ||
call nvim_win_set_option(s:float_win, 'relativenumber', v:false) | ||
endfunction | ||
|
||
function! s:float_win_position() abort | ||
let l:win_height = winheight('.') | ||
let l:win_width = winwidth('.') | ||
let l:max_text_width = l:win_width | ||
if l:max_text_width > 12 | ||
let l:max_text_width = l:max_text_width - 3 | ||
endif | ||
|
||
let l:max_width = 0 | ||
let l:line_count = 0 | ||
|
||
for l:line in s:data_buf | ||
let l:line_count = l:line_count + 1 | ||
let l:len = strwidth(line) | ||
if l:len < l:max_text_width | ||
if l:len > l:max_width | ||
let l:max_width = l:len | ||
endif | ||
else | ||
let l:max_width = l:max_text_width | ||
let l:calc_count = l:len / l:max_width | ||
let l:line_count = l:line_count + l:calc_count | ||
endif | ||
endfor | ||
|
||
let l:cline = winline() | ||
let l:ccol = wincol() | ||
|
||
if l:ccol + l:max_width + 1 <= l:win_width | ||
let l:ccol = l:ccol - 1 | ||
else | ||
let l:ccol = l:win_width - l:max_width | ||
endif | ||
|
||
if l:cline + l:line_count > l:win_height | ||
if l:cline > l:win_height / 2 | ||
let l:line_count = min([l:line_count, l:cline - 1]) | ||
let l:cline = l:cline - l:line_count - 1 | ||
else | ||
let l:line_count = l:win_height - l:cline | ||
endif | ||
endif | ||
|
||
return {'col': l:ccol, 'row': l:cline, 'height': l:line_count, 'width': l:max_width} | ||
|
||
endfunction | ||
|
||
function! s:remove_spec_char(data) abort | ||
return split(a:data, '\%x00') | ||
endfunction | ||
|
||
function! lsp#ui#vim#float#float_open(data) | ||
if s:curbuf ==# 0 | ||
let s:curbuf = nvim_create_buf(v:false, v:true) | ||
endif | ||
call s:reset() | ||
call s:convert_to_data_buf(a:data) | ||
call nvim_buf_set_lines(s:curbuf, 0, -1, v:true, s:data_buf) | ||
call s:set_buf_option() | ||
call s:open_float_win() | ||
endfunction | ||
|
||
function! s:convert_to_data_buf(data) | ||
if type(a:data) == type([]) | ||
for l:entry in a:data | ||
call s:convert_to_data_buf(entry) | ||
endfor | ||
|
||
return | ||
elseif type(a:data) == type('') | ||
call extend(s:data_buf, s:remove_spec_char(a:data)) | ||
|
||
return | ||
elseif type(a:data) == type({}) && has_key(a:data, 'language') | ||
call add(s:data_buf, '```'.a:data.language) | ||
call extend(s:data_buf, s:remove_spec_char(a:data.value)) | ||
call add(s:data_buf, '```') | ||
|
||
return | ||
elseif type(a:data) == type({}) && has_key(a:data, 'kind') | ||
call extend(s:data_buf, s:remove_spec_char(a:data.value)) | ||
|
||
return | ||
endif | ||
endfunction | ||
|
||
function! s:open_float_win() | ||
let l:opts = s:float_win_position() | ||
call extend(l:opts, {'relative': 'win', 'anchor': 'NW'}) | ||
let s:float_win = nvim_open_win(s:curbuf, v:true, l:opts) | ||
map <silent> <esc> :call <SID>float_close()<cr> | ||
call s:set_win_option() | ||
endfunction | ||
|
||
function! s:float_close() | ||
call nvim_win_close(s:float_win, 1) | ||
unmap <esc> | ||
endfunction |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,18 @@ function! s:handle_hover(server, data) abort | |
endif | ||
|
||
if !empty(a:data['response']['result']) && !empty(a:data['response']['result']['contents']) | ||
call lsp#ui#vim#output#preview(a:data['response']['result']['contents']) | ||
return | ||
for ui in g:lsp_hover_ui | ||
if ui == "float" | ||
if exists("*nvim_open_win") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we cache the |
||
call lsp#ui#vim#float#float_open(a:data['response']['result']['contents']) | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like we need to use |
||
endif | ||
elseif ui == "preview" | ||
call lsp#ui#vim#output#preview(a:data['response']['result']['contents']) | ||
return | ||
endif | ||
endfor | ||
call lsp#utils#error('Hover ui is not found') | ||
else | ||
call lsp#utils#error('No hover information found') | ||
endif | ||
|
Uh oh!
There was an error while loading. Please reload this page.