Skip to content

Commit

Permalink
Merge branch 'master' into peek-definition
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasfaingnaert committed Jul 18, 2019
2 parents 5074476 + 9507824 commit 7ed35dc
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 13 deletions.
6 changes: 5 additions & 1 deletion autoload/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,8 @@ endfunction
function! lsp#default_get_supported_capabilities(server_info) abort
return {
\ 'workspace': {
\ 'applyEdit': v:true
\ 'applyEdit': v:true,
\ 'configuration': v:true
\ }
\ }
endfunction
Expand Down Expand Up @@ -627,6 +628,9 @@ function! s:on_request(server_name, id, request) abort
if a:request['method'] ==# 'workspace/applyEdit'
call lsp#utils#workspace_edit#apply_workspace_edit(a:request['params']['edit'])
call s:send_response(a:server_name, { 'id': a:request['id'], 'result': { 'applied': v:true } })
elseif a:request['method'] ==# 'workspace/configuration'
let l:response_items = map(a:request['params']['items'], { key, val -> lsp#utils#workspace_config#get_value(a:server_name, val) })
call s:send_response(a:server_name, { 'id': a:request['id'], 'result': l:response_items })
else
" Error returned according to json-rpc specification.
call s:send_response(a:server_name, { 'id': a:request['id'], 'error': { 'code': -32601, 'message': 'Method not found' } })
Expand Down
8 changes: 8 additions & 0 deletions autoload/lsp/ui/vim.vim
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ function! s:handle_location(ctx, server, type, data) abort "ctx = {counter, list
if empty(a:ctx['list'])
call lsp#utils#error('No ' . a:type .' found')
else
if exists('*gettagstack') && exists('*settagstack')
let from = [bufnr('%'), line('.'), col('.'), 0]
let tagname = expand('<cword>')
let winid = win_getid()
call settagstack(winid, {'items': [{'from': from, 'tagname': tagname}]}, 'a')
call settagstack(winid, {'curidx': len(gettagstack(winid)['items']) + 1})
endif

let l:loc = a:ctx['list'][0]

if len(a:ctx['list']) == 1 && a:ctx['jump_if_one'] && !a:ctx['in_preview']
Expand Down
6 changes: 6 additions & 0 deletions autoload/lsp/ui/vim/diagnostics/textprop.vim
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ function! s:clear_highlights(server_name, path) abort
if !s:enabled | return | endif

let l:bufnr = bufnr(a:path)

if l:bufnr == -1
call lsp#log('Skipping clear_highlights for ' . a:path . ': buffer is not loaded')
return
endif

for l:severity in keys(s:severity_sign_names_mapping)
let l:prop_type = s:get_prop_type(a:server_name, l:severity)
call prop_remove({
Expand Down
2 changes: 1 addition & 1 deletion autoload/lsp/ui/vim/highlights.vim
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ endfunction

function! s:clear_all_highlights(namespace) abort
if a:namespace =~# '^' . s:ns_key
let l:ns = s:get_highlight_group(a:namespace)
let l:ns = nvim_create_namespace(a:namespace)
for l:bufnr in nvim_list_bufs()
call nvim_buf_clear_namespace(l:bufnr, l:ns, 0, -1)
endfor
Expand Down
21 changes: 17 additions & 4 deletions autoload/lsp/ui/vim/output.vim
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,16 @@ function! lsp#ui#vim#output#floatingpreview(data) abort
" Enable closing the preview with esc, but map only in the scratch buffer
nmap <buffer><silent> <esc> :pclose<cr>
else
let s:winid = popup_atcursor('...', {
\ 'moved': 'any',
\ 'border': [1, 1, 1, 1],
\})
let l:options = {
\ 'moved': 'any',
\ 'border': [1, 1, 1, 1],
\ }

if g:lsp_preview_max_width > 0
let l:options['maxwidth'] = g:lsp_preview_max_width
endif

let s:winid = popup_atcursor('...', l:options)
endif
return s:winid
endfunction
Expand All @@ -139,6 +145,13 @@ function! s:setcontent(lines, ft) abort
else
" nvim floating
call setline(1, a:lines)

" Set maximum width of floating window, if specified
if g:lsp_preview_max_width > 0
let &l:textwidth = g:lsp_preview_max_width
normal! gggqGgg
endif

setlocal readonly nomodifiable
let &l:filetype = a:ft . '.lsp-hover'
endif
Expand Down
2 changes: 1 addition & 1 deletion autoload/lsp/utils.vim
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function! lsp#utils#echo_with_truncation(msg) abort
endif

if &showcmd
let l:winwidth -= 11
let l:winwidth -= 12
endif

if l:winwidth > 5 && l:winwidth < strdisplaywidth(l:msg)
Expand Down
11 changes: 10 additions & 1 deletion autoload/lsp/utils/text_edit.vim
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,16 @@ function! s:generate_sub_cmd_replace(text_edit) abort

let l:sub_cmd = s:preprocess_cmd(a:text_edit['range'])
let l:sub_cmd .= s:generate_move_start_cmd(l:start_line, l:start_character) " move to the first position
let l:sub_cmd .= 'v'

" If start and end position are 0, we are selecting a range of lines.
" Thus, we can use linewise-visual mode, which avoids some inconsistencies
" when applying text edits.
if l:start_character == 0 && l:end_character == 0
let l:sub_cmd .= 'V'
else
let l:sub_cmd .= 'v'
endif

let l:sub_cmd .= s:generate_move_end_cmd(l:end_line, l:end_character) " move to the last position

if len(l:new_text) == 0
Expand Down
14 changes: 14 additions & 0 deletions autoload/lsp/utils/workspace_config.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function! lsp#utils#workspace_config#get_value(server_name, item) abort
try
let l:server_info = lsp#get_server_info(a:server_name)
let l:config = l:server_info['workspace_config']

for section in split(a:item['section'], '\.')
let l:config = l:config[l:section]
endfor

return l:config
catch
return v:null
endtry
endfunction
44 changes: 40 additions & 4 deletions doc/vim-lsp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CONTENTS *vim-lsp-contents*
g:lsp_get_vim_completion_item |g:lsp_get_vim_completion_item|
g:lsp_get_supported_capabilities |g:lsp_get_supported_capabilities|
g:lsp_peek_alignment |g:lsp_peek_alignment|
g:lsp_preview_max_width |g:lsp_preview_max_width|
Functions |vim-lsp-functions|
enable |vim-lsp-enable|
disable |vim-lsp-disable|
Expand Down Expand Up @@ -175,24 +176,50 @@ g:lsp_preview_float *g:lsp_preview_float*
are shown in a floating window as |preview-window| at the cursor position.
The |preview-window| is closed automatically on cursor moves, unless it is
focused. While focused it may be closed with <esc>.
After opening an autocmd User event lsp_float_opened is issued, as well as
and lsp_float_closed upon closing. This can be used to alter the preview
window (using lsp#ui#vim#output#getpreviewwinid() to get the window id), or
setup custom bindings while a preview is open.

This feature requires neovim 0.4.0 (current master) or
Vim8.1 with has('patch-8.1.1517').

Example:
>
" Opens preview windows as floating
let g:lsp_preview_float = 1
" Opens preview windows as normal windows
let g:lsp_preview_float = 0
<
After opening an autocmd User event lsp_float_opened is issued, as well as
and lsp_float_closed upon closing. This can be used to alter the preview
window (using lsp#ui#vim#output#getpreviewwinid() to get the window id),
setup custom bindings while a preview is open, or change the highlighting
of the window.
Example of custom keybindings:
>
" Close preview window with <esc>
autocmd User lsp_float_opened nmap <buffer> <silent> <esc>
\ <Plug>(lsp-preview-close)
autocmd User lsp_float_closed nunmap <buffer> <esc>
<
Example of customising the highlighting:
>
highlight PopupWindow ctermbg=lightblue guibg=lightblue
augroup lsp_float_colours
autocmd!
if !has('nvim')
autocmd User lsp_float_opened
\ call win_execute(lsp#ui#vim#output#getpreviewwinid(),
\ 'setlocal wincolor=PopupWindow')
else
autocmd User lsp_float_opened
\ call nvim_win_set_option(lsp#ui#vim#output#getpreviewwinid(),
\ 'winhighlight', 'Normal:PopupWindow')
endif
augroup end
<
g:lsp_preview_autoclose *g:lsp_preview_autoclose*
Type: |Number|
Expand Down Expand Up @@ -396,6 +423,15 @@ g:lsp_peek_alignment *g:lsp_peek_alignment*
`"bottom"`, which place the location of interest at the first, middle and
last lines of the preview/popup/floating window, respectively.

g:lsp_preview_max_width *g:lsp_preview_max_width*
Type: |Number|
Default: `-1`

If positive, determines the maximum width of the preview window in
characters. Lines longer than `g:lsp_preview_max_width` will be wrapped to
fit in the preview window. Use a value of `-1` to disable setting a
maximum width.

===============================================================================
FUNCTIONS *vim-lsp-functions*

Expand Down
3 changes: 2 additions & 1 deletion plugin/lsp.vim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ let g:lsp_preview_float = get(g:, 'lsp_preview_float', 1)
let g:lsp_preview_autoclose = get(g:, 'lsp_preview_autoclose', 1)
let g:lsp_preview_doubletap = get(g:, 'lsp_preview_doubletap', [function('lsp#ui#vim#output#focuspreview')])
let g:lsp_peek_alignment = get(g:, 'lsp_peek_alignment', 'center')
let g:lsp_preview_max_width = get(g:, 'lsp_preview_max_width', -1)

let g:lsp_get_vim_completion_item = get(g:, 'lsp_get_vim_completion_item', [function('lsp#omni#default_get_vim_completion_item')])
let g:lsp_get_supported_capabilities = get(g:, 'lsp_get_supported_capabilities', [function('lsp#default_get_supported_capabilities')])
Expand Down Expand Up @@ -84,7 +85,7 @@ nnoremap <plug>(lsp-type-definition) :<c-u>call lsp#ui#vim#type_definition(0)<cr
nnoremap <plug>(lsp-peek-type-definition) :<c-u>call lsp#ui#vim#type_definition(1)<cr>
nnoremap <plug>(lsp-workspace-symbol) :<c-u>call lsp#ui#vim#workspace_symbol()<cr>
nnoremap <plug>(lsp-document-format) :<c-u>call lsp#ui#vim#document_format()<cr>
vnoremap <plug>(lsp-document-format) :call lsp#ui#vim#document_range_format()<cr>
vnoremap <plug>(lsp-document-format) :<Home>silent <End>call lsp#ui#vim#document_range_format()<cr>
nnoremap <plug>(lsp-implementation) :<c-u>call lsp#ui#vim#implementation(0)<cr>
nnoremap <plug>(lsp-peek-implementation) :<c-u>call lsp#ui#vim#implementation(1)<cr>
nnoremap <plug>(lsp-status) :<c-u>echo lsp#get_server_status()<cr>
Expand Down
23 changes: 23 additions & 0 deletions test/lsp/utils/text_edit.vimspec
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,29 @@ Describe lsp#utils#text_edit
Assert Equals(l:buffer_text, ['fr', 'baz', ''])
End

It replaces entire buffer correctly
call s:set_text(['foo', 'bar', 'baz'])

call lsp#utils#text_edit#apply_text_edits(
\ expand('%'),
\ [{
\ 'range': {
\ 'start': {
\ 'line': 0,
\ 'character': 0
\ },
\ 'end': {
\ 'line': 3,
\ 'character': 0
\ }
\ },
\ 'newText': "x\ny\nz\n"
\ }])

let l:buffer_text = s:get_text()
Assert Equals(l:buffer_text, ['x', 'y', 'z', ''])
End

It preserves v:completed_item
" Add some text to buffer
call s:set_text(['foo', 'bar'])
Expand Down

0 comments on commit 7ed35dc

Please sign in to comment.