diff --git a/autoload/lsc/config.vim b/autoload/lsc/config.vim index 316ecd21..b6f1038d 100644 --- a/autoload/lsc/config.vim +++ b/autoload/lsc/config.vim @@ -51,6 +51,8 @@ function! lsc#config#mapKeys() abort return endif + let b:lsc_save = {} + let b:lsc_maps = [] for command in [ \ 'GoToDefinition', \ 'GoToDefinitionSplit', @@ -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 '.m.' :LSClient'.command.'' 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 '.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 diff --git a/autoload/lsc/file.vim b/autoload/lsc/file.vim index 2f295cb1..c6eed2e5 100644 --- a/autoload/lsc/file.vim +++ b/autoload/lsc/file.vim @@ -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) diff --git a/autoload/lsc/server.vim b/autoload/lsc/server.vim index 93a1fd9d..d7f30353 100644 --- a/autoload/lsc/server.vim +++ b/autoload/lsc/server.vim @@ -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 @@ -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 @@ -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 diff --git a/plugin/lsc.vim b/plugin/lsc.vim index 33948742..1e4b8497 100644 --- a/plugin/lsc.vim +++ b/plugin/lsc.vim @@ -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. @@ -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