From: martin f. krafft Date: Tue, 8 Apr 2025 15:44:57 +0000 (+0200) Subject: Squashed '.vim/bundle/asyncomplete-lsp/' content from commit cc5247bc X-Git-Url: https://git.madduck.net/etc/vim.git/commitdiff_plain/d49e95aa7ba744f0a7f544aca43afdb6aab41f24?ds=sidebyside Squashed '.vim/bundle/asyncomplete-lsp/' content from commit cc5247bc git-subtree-dir: .vim/bundle/asyncomplete-lsp git-subtree-split: cc5247bc268fb2c79d8b127bd772514554efb3ee --- d49e95aa7ba744f0a7f544aca43afdb6aab41f24 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..176a458f --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..6e92f57d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..320f535c --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Prabir Shrestha + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 00000000..9e27bf50 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +LSP source for asyncomplete.vim vim-lsp +======================================= + +Provide [Language Server Protocol](https://github.com/Microsoft/language-server-protocol) autocompletion source for [asyncomplete.vim](https://github.com/prabirshrestha/asyncomplete.vim) and [vim-lsp](https://github.com/prabirshrestha/vim-lsp). + +## Installing +Refer to asyncomplete.vim docs on Language Server Protocol at https://github.com/prabirshrestha/asyncomplete.vim#language-server-protocol-lsp. + + +## License + +MIT diff --git a/plugin/asyncomplete-lsp.vim b/plugin/asyncomplete-lsp.vim new file mode 100644 index 00000000..bc42d3d7 --- /dev/null +++ b/plugin/asyncomplete-lsp.vim @@ -0,0 +1,103 @@ +if exists('g:asyncomplete_lsp_loaded') + finish +endif +let g:asyncomplete_lsp_loaded = 1 + +let s:servers = {} " { server_name: 1 } + +augroup asyncomplete_lsp + au! + au User lsp_server_init call s:server_initialized() + au User lsp_server_exit call s:server_exited() +augroup END + +function! s:server_initialized() abort + let l:server_names = lsp#get_server_names() + for l:server_name in l:server_names + if has_key(s:servers, l:server_name) + continue + endif + let l:init_capabilities = lsp#get_server_capabilities(l:server_name) + if !has_key(l:init_capabilities, 'completionProvider') + continue + endif + + let l:server = lsp#get_server_info(l:server_name) + let l:name = s:generate_asyncomplete_name(l:server_name) + let l:source_opt = { + \ 'name': l:name, + \ 'completor': function('s:completor', [l:server]), + \ } + if type(l:init_capabilities['completionProvider']) == type({}) && has_key(l:init_capabilities['completionProvider'], 'triggerCharacters') + let l:source_opt['triggers'] = { '*': l:init_capabilities['completionProvider']['triggerCharacters'] } + endif + if has_key(l:server, 'allowlist') + let l:source_opt['allowlist'] = l:server['allowlist'] + elseif has_key(l:server, 'whitelist') + let l:source_opt['allowlist'] = l:server['whitelist'] + endif + if has_key(l:server, 'blocklist') + let l:source_opt['blocklist'] = l:server['blocklist'] + elseif has_key(l:server, 'blacklist') + let l:source_opt['blocklist'] = l:server['blacklist'] + endif + if has_key(l:server, 'priority') + let l:source_opt['priority'] = l:server['priority'] + endif + call asyncomplete#register_source(l:source_opt) + let s:servers[l:server_name] = 1 + endfor +endfunction + +function! s:server_exited() abort + let l:server_names = lsp#get_server_names() + for l:server_name in l:server_names + if !has_key(s:servers, l:server_name) + continue + endif + let l:name = s:generate_asyncomplete_name(l:server_name) + if s:servers[l:server_name] + call asyncomplete#unregister_source(l:name) + endif + unlet s:servers[l:server_name] + endfor +endfunction + +function! s:generate_asyncomplete_name(server_name) abort + return 'asyncomplete_lsp_' . a:server_name +endfunction + +function! s:completor(server, opt, ctx) abort + let l:position = lsp#get_position() + call lsp#send_request(a:server['name'], { + \ 'method': 'textDocument/completion', + \ 'params': { + \ 'textDocument': lsp#get_text_document_identifier(), + \ 'position': l:position, + \ }, + \ 'on_notification': function('s:handle_completion', [a:server, l:position, a:opt, a:ctx]), + \ }) +endfunction + +function! s:handle_completion(server, position, opt, ctx, data) abort + if lsp#client#is_error(a:data) || !has_key(a:data, 'response') || !has_key(a:data['response'], 'result') + return + endif + + let l:options = { + \ 'server': a:server, + \ 'position': a:position, + \ 'response': a:data['response'], + \ } + + let l:completion_result = lsp#omni#get_vim_completion_items(l:options) + + let l:col = a:ctx['col'] + let l:typed = a:ctx['typed'] + let l:kw = matchstr(l:typed, get(b:, 'asyncomplete_refresh_pattern', '\k\+$')) + let l:kwlen = len(l:kw) + let l:startcol = l:col - l:kwlen + let l:startcol = min([l:startcol, get(l:completion_result, 'startcol', l:startcol)]) + + call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:completion_result['items'], l:completion_result['incomplete']) +endfunction