Skip to content

Commit

Permalink
Add support for automatic per-buffer mappings
Browse files Browse the repository at this point in the history
Closes #25

If `g:lsc_auto_map` is set, for each buffer that opens with a tracked
filetype set up `<buffer>` 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.
  • Loading branch information
natebosch committed Jul 31, 2017
1 parent dcd9280 commit 2a25afd
Showing 4 changed files with 75 additions and 17 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<CR>
nnoremap gr :LSClientFindReferences<CR>
```

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': '<C-]>',
\ '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` (`<C-]>` 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.
31 changes: 31 additions & 0 deletions autoload/lsc/config.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let s:default_maps = {
\ 'GoToDefinition': '<C-]>',
\ '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 <buffer>'.maps[command].' :LSClient'.command.'<CR>'
endif
endfor
if !g:lsc_enable_autocomplete && has_key(maps, 'Completion')
execute 'setlocal '.maps['Completion'].'=lsc#complete#complete'
endif
endfunction
1 change: 1 addition & 0 deletions autoload/lsc/file.vim
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2a25afd

Please sign in to comment.