Skip to content

Commit 2987d31

Browse files
Add nvim-cmp support
1 parent 031b604 commit 2987d31

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

README.md

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Provides async clojure completion for:
66
* [ncm2][]
77
* [asyncomplete.vim][]
88
* [coc.nvim][]
9+
* [nvim-cmp][]
910

1011
Trying to use Fireplace's omnicompletion with auto-complete is painfully
1112
slow at times, making typing blocked. Using this module will be faster as
@@ -60,6 +61,19 @@ au User asyncomplete_setup call asyncomplete#register_source({
6061
Plug 'clojure-vim/async-clj-omni'
6162
```
6263

64+
### nvim-cmp
65+
66+
1. Follow the install instructions for [nvim-cmp][].
67+
2. Add `{ name = 'async_clj_omni' }`, a complete example:
68+
```lua
69+
cmp.setup({
70+
sources = {
71+
{ name = 'async_clj_omni' },
72+
}
73+
})
74+
```
75+
3. Add `call async_clj_omni#cmp#register()` somewhere in your init.vim.
76+
6377
## Developing
6478

6579
### Deoplete
@@ -104,3 +118,4 @@ NVIM_PYTHON_LOG_FILE=logfile NVIM_PYTHON_LOG_LEVEL=DEBUG nvim
104118
[ncm2]: https://github.com/ncm2/ncm2
105119
[coc.nvim]: https://github.com/neoclide/coc.nvim
106120
[asyncomplete.vim]: https://github.com/prabirshrestha/asyncomplete.vim
121+
[nvim-cmp]: https://github.com/hrsh7th/nvim-cmp

autoload/async_clj_omni/cmp.vim

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
let s:source = {}
2+
3+
function! s:source.new() abort
4+
return deepcopy(s:source)
5+
endfunction
6+
7+
function! s:source.is_available()
8+
if fireplace#op_available('complete')
9+
return v:true
10+
else
11+
return v:false
12+
endif
13+
endfunction
14+
15+
function! s:source.get_keyword_pattern(params)
16+
" Minimum 2 letters because completion on "y" doesn't resolve any
17+
" namespaces, but "ya" will resolve on "yada".
18+
return '\k\k\+'
19+
endfunction
20+
21+
function! s:source.get_trigger_characters(params)
22+
return ['/', '.', ':']
23+
endfunction
24+
25+
" unfortunately f is for both function & static method. to workaround, we'll
26+
" need to go to a lower level than fireplace#omnicomplete, which would lose us
27+
" the context of the completion from surrounding lines.
28+
let s:lsp_kinds = luaeval("
29+
\ (function()
30+
\ local cmp = require'cmp'
31+
\ return {f = cmp.lsp.CompletionItemKind.Function,
32+
\ m = cmp.lsp.CompletionItemKind.Function,
33+
\ v = cmp.lsp.CompletionItemKind.Variable,
34+
\ s = cmp.lsp.CompletionItemKind.Keyword,
35+
\ c = cmp.lsp.CompletionItemKind.Class,
36+
\ k = cmp.lsp.CompletionItemKind.Keyword,
37+
\ l = cmp.lsp.CompletionItemKind.Variable,
38+
\ n = cmp.lsp.CompletionItemKind.Module,
39+
\ i = cmp.lsp.CompletionItemKind.Field,
40+
\ r = cmp.lsp.CompletionItemKind.File,}
41+
\ end)()")
42+
43+
function! s:coerce_to_lsp(vc)
44+
return {'label': a:vc.word,
45+
\ 'labelDetails': {
46+
\ 'detail': a:vc.menu,
47+
\ },
48+
\ 'documentation': a:vc.info,
49+
\ 'kind': get(s:lsp_kinds, a:vc.kind, 1)
50+
\ }
51+
endf
52+
53+
function! s:source.complete(params, callback) abort
54+
let l:kw = a:params.context.cursor_before_line[(a:params.offset-1):]
55+
56+
call fireplace#omnicomplete({candidates ->
57+
\ a:callback(map(candidates,
58+
\ {_, val -> s:coerce_to_lsp(val)}))
59+
\ },
60+
\ l:kw)
61+
endfunction
62+
63+
function async_clj_omni#cmp#register()
64+
call cmp#register_source('async_clj_omni', s:source.new())
65+
endf

plugin/cmp_fireplace.vim

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
" cmp isn't guaranteed to be loaded before this file runs, which is why we
2+
" leave the option to manually register.
3+
if exists('*cmp#register_source')
4+
call async_clj_omni#cmp#register()
5+
endif

0 commit comments

Comments
 (0)