From 2a25afdce755cf5748a36963f1ed0d2d7286178f Mon Sep 17 00:00:00 2001 From: Nate Bosch Date: Sun, 30 Jul 2017 17:19:36 -0700 Subject: [PATCH] Add support for automatic per-buffer mappings Closes #25 If `g:lsc_auto_map` is set, for each buffer that opens with a tracked filetype set up `` maps. Can either configure defaults by setting to `v:true` or only the specified keys by setting to a dict. Update README for latest configuration options and mark for tagging the next version. --- CHANGELOG.md | 4 ++- README.md | 56 +++++++++++++++++++++++++++++------------ autoload/lsc/config.vim | 31 +++++++++++++++++++++++ autoload/lsc/file.vim | 1 + 4 files changed, 75 insertions(+), 17 deletions(-) create mode 100644 autoload/lsc/config.vim diff --git a/CHANGELOG.md b/CHANGELOG.md index a219ec0a..ed64f034 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -# 0.2.3-dev +# 0.2.3 - `redraw` after jumping to definition in another file. - Allow configuring trace level with `g:lsc_trace_level`. May be one of 'off', @@ -8,6 +8,8 @@ - Add `LSClientDisable`, `LSClientEnable` to disable or re-enable the client for the current filetype during a session. - Add `LSClientShowHover` to display hover information in a preview window. +- Add support for automatically mapping keys only in buffers for tracked + filetypes. # 0.2.2 diff --git a/README.md b/README.md index 26167ad1..e6733ca1 100644 --- a/README.md +++ b/README.md @@ -20,23 +20,40 @@ Install with your method of choice. If you don't have a preference check out ## Configuration Map a filetype to the command that starts the language server for that filetype -in your `vimrc`. I also recommend mapping shortcuts to the go to definition and -find references commands. +in your `vimrc`. ```viml let g:lsc_server_commands = {'dart': 'dart_language_server'} - -nnoremap gd :LSClientGoToDefinition -nnoremap gr :LSClientFindReferences ``` To disable autocomplete in favor of manual completion also add ```viml let g:lsc_enable_autocomplete = v:false -set completefunc=lsc#complete#complete ``` +Most interactive features are triggered by commands, you can map keys to these +commands, or use `g:lsc_auto_map` to have them automatically mapped for the +buffers which have a language server enabled. You can use the default mappings +by setting it to `v:true`, or specify your own mappings in a dict. The +`'Complete'` key will set a completion function only if +`g:lsc_enable_autocomplete` is false. + +```viml +let g:lsc_auto_map = v:true " Use defaults +" ... or set only the keys you want mapped, defaults are: +let g:lsc_auto_map = { + \ 'GoToDefinition': '', + \ 'FindReferences': 'gr', + \ 'ShowHover': 'K', + \ 'Completion': 'completefunc', + \} +``` + +During the initialization call LSP supports a `trace` argument which configures +logging on the server. Set this with `g:lsc_trace_level`. Valid values are +`'off'`, `'messages'`, or `'verbose'`. Defaults to `'off'`. + ## Features The protocol does not require that every language server supports every feature @@ -47,6 +64,12 @@ For requests that trigger an action the response might be silently ignored if it can no longer be used - you can abort most operations that are too slow by moving the cursor. +The client can be temporarily disabled for a session with `LSClientDisable` and +re-enabled with `LSClientEnable`. At any time the server can be exited and +restarted with `LSClientRestartServer` - this sends a request for the server to +exit rather than kill it's process so a completely unresponsive server should be +killed manually instead. + ### Diagnostics Errors, warnings, and hints reported by the server are highlighted in the buffer. @@ -59,9 +82,10 @@ buffer open. ### Autocomplete -When more than 3 word characters or a '.' are typed a request for autocomplete -suggestions is sent to the server. If the server responds before the cursor -moves again the options will be provided using vim's built in completion. +When more than 3 word characters or a trigger character are typed a request for +autocomplete suggestions is sent to the server. If the server responds before +the cursor moves again the options will be provided using vim's built in +completion. Note: By default `completeopt` includes `preview` and completion items include documentation in the preview window. Close the window after completion with @@ -78,16 +102,16 @@ requested before the server responds with suggestions. ### Jump to definition -While the cursor is on any identifier call `LSClientGoToDefinition` to jump to -the location of the definition. If the cursor moves before the server responds -the response will be ignored. +While the cursor is on any identifier call `LSClientGoToDefinition` (`` if +using the default mappings) to jump to the location of the definition. If the +cursor moves before the server responds the response will be ignored. ### Find references -While the cursor is on any identifier call `LSClientFindReferences` to populate -the quickfix list with usage locations. +While the cursor is on any identifier call `LSClientFindReferences` (`gr` if +using the default mappings) to populate the quickfix list with usage locations. ### Hover -While the cursor is on any identifier call `LSClientShowHover` to request hover -text and show it in a preview window. +While the cursor is on any identifier call `LSClientShowHover` (`K` if using the +default mappings) to request hover text and show it in a preview window. diff --git a/autoload/lsc/config.vim b/autoload/lsc/config.vim new file mode 100644 index 00000000..621b327f --- /dev/null +++ b/autoload/lsc/config.vim @@ -0,0 +1,31 @@ +let s:default_maps = { + \ 'GoToDefinition': '', + \ 'FindReferences': 'gr', + \ 'ShowHover': 'K', + \ 'Completion': 'completefunc', + \} + +function! lsc#config#mapKeys() abort + if !exists('g:lsc_auto_map') + \ || (type(g:lsc_auto_map) == v:t_bool && !g:lsc_auto_map) + \ || (type(g:lsc_auto_map) == v:t_number && !g:lsc_auto_map) + return + endif + let maps = g:lsc_auto_map + if type(maps) == v:t_bool || type(maps) == v:t_number + let maps = s:default_maps + endif + if type(maps) != v:t_dict + call lsc#util#error('g:lsc_auto_map must be a bool or dict') + return + endif + + for command in ['GoToDefinition', 'FindReferences', 'ShowHover'] + if has_key(maps, command) + execute 'nnoremap '.maps[command].' :LSClient'.command.'' + endif + endfor + if !g:lsc_enable_autocomplete && has_key(maps, 'Completion') + execute 'setlocal '.maps['Completion'].'=lsc#complete#complete' + endif +endfunction diff --git a/autoload/lsc/file.vim b/autoload/lsc/file.vim index d407564e..d01fdaf8 100644 --- a/autoload/lsc/file.vim +++ b/autoload/lsc/file.vim @@ -16,6 +16,7 @@ endfunction " the 'didOpen' message. function! lsc#file#onOpen() abort call lsc#server#start(&filetype) + call lsc#config#mapKeys() call s:DidOpen(expand('%:p')) endfunction