Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

nvim-completion-manager - A completion framework for neovim

$
0
0

:heart: for my favorite editor

A Completion Framework for Neovim

This is my experimental completion framework for neovim, which offers great flexibility for writing your own completion plugin, including async support. For more information, please read the Whysection

Current Completion Sources Keyword from current buffer Ultisnips hint File path completion python code completion javascript code completion Golang code completion php code completion Language specific completion for markdown Requirements Neovim python3 support. :help provider-python . For lazy linux users, I recommend this plugin python-support.nvim . (Note: Self promotion) For python code completion , you need to install jedi library. For python code completion in markdown file, you need to install mistune For Javascript code completion , you need to install nodejs and npm on your system. For Golang code completion , you need to install gocode . Installation and Configuration

Assumming you're using vim-plug

" `npm install` For javascript code completion support Plug 'roxma/nvim-completion-manager', {'do': 'npm install'} " PHP code completion is moved to a standalone plugin Plug 'roxma/nvim-cm-php-language-server', {'do': 'composer install && composer run-script parse-stubs'}

If you are using python-support.nvim , add the following code into your vimrc, to satisfy requirement 1 and requirement 2.

Plug 'roxma/python-support.nvim' " for python completions let g:python_support_python3_requirements = add(get(g:,'python_support_python3_requirements',[]),'jedi') " enable python completions on markdown file let g:python_support_python3_requirements = add(get(g:,'python_support_python3_requirements',[]),'mistune')

Add this to supress the annoying completion messages:

" don't give |ins-completion-menu| messages. For example, " '-- XXX completion (YYY)', 'match 1 of 2', 'The only match', set shortmess+=c

Notethat there's no guarantee that this plugin will be compatible with other completion plugin in the same buffer. Use let g:cm_enable_for_all=0 and call cm#enable_for_buffer() to use this plugin for specific buffer.

Tab Completion inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>" inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>" How to extend this framework? For really simple, light weight completion candidate calculation, refer to autoload/cm/sources/ultisnips.vim For really async completion source, refer to the file path completion example: autoload/cm/sources/cm_filepath.py Why?

I'm writing this for fun, feeding my own need, and it's working pleasingly for me now. And It seems there's lots of differences between deoplete, YCM, and nvim-completion-manager, by design.

I havn't read the source of YCM yet. So here I'm describing the main design of NCM (from now on, I'm using NCM as short for nvim-completion-manager) and some of the differences between deoplete and this plugin.

Async architecture

Each completion source should be a standalone process, the manager notifies the completion source for any text changing, even when popup menu is visible. The completion source notifies the manager if there's any complete matches available. After some basic priority sorting between completion sources, and some simple filtering, the completion popup menu will be trigger with the complete() function by the completion manager.

As shown intentionally in the python jedi completion demo, If some of the completion source is calculating matches for a long long time, the popup menu will still be shown quickly if other completion sources works properly. And if the user havn't changed anything, the popup menu will be updated after the slow completion source finish the work.

As the time as of this plugin being created, the completion sources of deoplete are gathered with gather_candidates() of the Source object, inside a for loop, in deoplete's process. A slow completion source may defer the display of popup menu. Of course It will not block the ui.

Scoping

I write markdown file with code blocks quite often, so I've also implementedlanguage specific completion for markdown file. This is a framework feature, which is called scoping. It should work for any markdown code block whose language completion source is avaible to NCM. I've also added support for javascript completion in script tag of html files.

Experimental hacks

Note that there's some hacks done in NCM. It uses a per 30ms timer to detect changes even popup menu is visible, instead of using the TextChangedI event, which only triggers when no popup menu is visible. This is important for implementing the async architecture. I'm hoping one day neovim will offer better option rather than a timer or the limited TextChangedI .

Also note that the calling context of nvim's complete() function by NCM does not meet the requirement in the documentation :help complete() , which says:

You need to use a mapping with CTRL-R = |i_CTRL-R|. It does not work after CTRL-O or with an expression mapping.

I work on remote VM quite often. I tend to avoid the CTRL-R = mapping, because this triggers text updated on neovim's command line and it's potentially slowing down the ui. Luckily it seems it's working by calling this function directly. This is why I claimed it's experimental . I'm hoping one day I can confirm that the calling context is legal.

Deoplete and YCM are mature, legit, they have tons of features I'm not offering currently, which should be considered a main difference too.

FAQ Vim 8 support?

Sorry, no plan for that. #1

Related Projects

asyncomplete.vim

Demo Keyword from current buffer
nvim-completion-manager - A completion framework for neovim
Ultisnips hint
nvim-completion-manager - A completion framework for neovim
File path completion
nvim-completion-manager - A completion framework for neovim
Python code completion
nvim-completion-manager - A completion framework for neovim
Language specific completion for markdown

I've also added python completion for markdown file , just for fun. Note that this is a framework feature, which is called scoping , It should work for any markdown code block whose language completion source is added to NCM.


nvim-completion-manager - A completion framework for neovim
Javascript code completion
nvim-completion-manager - A completion framework for neovim
Golang code completion
nvim-completion-manager - A completion framework for neovim

Viewing all articles
Browse latest Browse all 9596

Trending Articles