Skip to content
Closed
Show file tree
Hide file tree
Changes from 9 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
134 changes: 134 additions & 0 deletions autoload/lsp/ui/vim/float.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
""""""""""""""""""""""""""""""""""""""""""
" 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 = []
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should use 4 spaces

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 substitute(a:data, '\%x00', " ", "g")
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
6 changes: 5 additions & 1 deletion autoload/lsp/ui/vim/hover.vim
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ 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'])
if g:lsp_hover_win == "preview" || !has("nvim-0.4.0")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of checking version can we check if the function exists?

call lsp#ui#vim#output#preview(a:data['response']['result']['contents'])
else
call lsp#ui#vim#float#float_open(a:data['response']['result']['contents'])
endif
return
else
call lsp#utils#error('No hover information found')
Expand Down
7 changes: 7 additions & 0 deletions doc/vim-lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CONTENTS *vim-lsp-contents*
g:lsp_insert_text_enabled |g:lsp_insert_text_enabled|
g:lsp_signs_enabled |g:lsp_signs_enabled|
g:lsp_use_event_queue |g:lsp_use_event_queue|
g:lsp_hover_win |g:lsp_hover_win|
Functions |vim-lsp-functions|
enable |vim-lsp-enable|
disable |vim-lsp-disable|
Expand Down Expand Up @@ -121,6 +122,12 @@ g:lsp_auto_enable *g:lsp_auto_enable*
let g:lsp_auto_enable = 1
let g:lsp_auto_enable = 0

g:lsp_hover_win *g:lsp_hover_win*
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we change this to g:lsp_hover_ui?

Type: String
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use array of string instead.

let g:lsp_hover_ui = ['float', 'preview']

This will allow us to have a consistent pattern when we support other ui.

Default: "float"

Indicates hover info show position.Opt: "preview" | "float"

g:lsp_preview_keep_focus *g:lsp_preview_keep_focus*
Type: |Number|
Default: `1`
Expand Down
1 change: 1 addition & 0 deletions plugin/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let g:lsp_next_sign_id = get(g:, 'lsp_next_sign_id', 6999)
let g:lsp_preview_keep_focus = get(g:, 'lsp_preview_keep_focus', 1)
let g:lsp_use_event_queue = get(g:, 'lsp_use_event_queue', has('nvim') || has('patch-8.1.0889'))
let g:lsp_insert_text_enabled= get(g:, 'lsp_insert_text_enabled', 1)
let g:lsp_hover_win = get(g:, "hover_win", "float")

if g:lsp_auto_enable
augroup lsp_auto_enable
Expand Down