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

Improve handling of key mapping #325

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions autoload/lsc/config.vim
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function! lsc#config#mapKeys() abort
return
endif

let b:lsc_save = {}
let b:lsc_maps = []
for command in [
\ 'GoToDefinition',
\ 'GoToDefinitionSplit',
Expand All @@ -69,24 +71,50 @@ function! lsc#config#mapKeys() abort
continue
endif
for m in type(lhs) == type([]) ? lhs : [lhs]
call add(b:lsc_maps, m)
execute 'nnoremap <buffer>'.m.' :LSClient'.command.'<CR>'
endfor
endfor
if has_key(l:maps, 'Completion') &&
\ type(l:maps['Completion']) == type('') &&
\ len(l:maps['Completion']) > 0
let b:lsc_save[l:maps['Completion']] = getbufvar('', l:maps['Completion'])
execute 'setlocal '.l:maps['Completion'].'=lsc#complete#complete'
endif
if has_key(l:maps, 'ShowHover')
let l:show_hover = l:maps['ShowHover']
if type(l:show_hover) == type(v:true) || type(l:show_hover) == type(0)
if l:show_hover
let b:lsc_save.keywordprg = &l:keywordprg
setlocal keywordprg=:LSClientShowHover
endif
endif
endif
endfunction

function! lsc#config#unmapKeys() abort
if exists('b:lsc_save')
for opt in keys(b:lsc_save)
execute 'setlocal '.opt.'='.b:lsc_save[opt]
endfor
unlet b:lsc_save
endif

if exists('b:lsc_maps')
for m in b:lsc_maps
silent! execute 'nunmap <buffer>'.m
endfor
unlet b:lsc_maps
endif
endfunction

" Unmap buffer-local keys if the associated language server is not running
function! lsc#config#checkKeys() abort
if lsc#server#status(&filetype) !=# 'running'
call lsc#config#unmapKeys()
endif
endfunction

" Wraps [Callback] with a function that will first translate a result through a
" user provided translation.
function! lsc#config#responseHook(server, method, Callback) abort
Expand Down
1 change: 0 additions & 1 deletion autoload/lsc/file.vim
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ endfunction
" Run language servers for this filetype if they aren't already running and
" flush file changes.
function! lsc#file#onOpen() abort
call lsc#config#mapKeys()
if &modifiable && expand('%') !~# '\vfugitive:///'
call lsc#server#start(&filetype)
call s:FlushChanges(lsc#file#fullPath(), &filetype)
Expand Down
7 changes: 7 additions & 0 deletions autoload/lsc/server.vim
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ function! s:Kill(server, status, OnExit) abort
call a:server._channel.notify('exit', v:null)
endif
if a:OnExit != v:null | call a:OnExit() | endif

" Unmap keys if the language server is used in the current buffer
if index(a:server.filetypes, &filetype) >= 0
call lsc#config#unmapKeys()
endif
endfunction
return a:server.request('shutdown', v:null, funcref('Exit'))
endfunction
Expand Down Expand Up @@ -119,6 +124,7 @@ endfunction
function! s:Start(server) abort
if has_key(a:server, '_channel')
" Server is already running
call lsc#config#mapKeys()
return
endif
let l:command = a:server.config.command
Expand All @@ -145,6 +151,7 @@ function! s:Start(server) abort
for filetype in a:server.filetypes
call lsc#file#trackAll(filetype)
endfor
call lsc#config#mapKeys()
endfunction
if exists('g:lsc_trace_level') &&
\ index(['off', 'messages', 'verbose'], g:lsc_trace_level) >= 0
Expand Down
2 changes: 1 addition & 1 deletion plugin/lsc.vim
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function! LSCEnsureCurrentWindowState() abort
call lsc#diagnostics#updateLocationList(lsc#file#fullPath())
call lsc#highlights#update()
call lsc#cursor#onWinEnter()
call lsc#config#checkKeys()
endfunction

" Run `function` if LSC is enabled for the current filetype.
Expand All @@ -159,7 +160,6 @@ endfunction

function! s:OnOpen() abort
if !has_key(g:lsc_servers_by_filetype, &filetype) | return | endif
call lsc#config#mapKeys()
if !lsc#server#filetypeActive(&filetype) | return | endif
call lsc#file#onOpen()
endfunction
Expand Down