Coder Social home page Coder Social logo

asyncomplete.vim's Introduction

asyncomplete.vim

Provide async autocompletion for vim8 and neovim with timers. This repository is fork of https://github.com/roxma/nvim-complete-manager in pure vim script with python dependency removed.

Installing

Plug 'prabirshrestha/asyncomplete.vim'

Tab completion

inoremap <expr> <Tab> pumvisible() ? "\<C-n>" : "\<Tab>"
inoremap <expr> <S-Tab> pumvisible() ? "\<C-p>" : "\<S-Tab>"
inoremap <expr> <cr> pumvisible() ? "\<C-y>\<cr>" : "\<cr>"

Force refresh completion

imap <c-space> <Plug>(asyncomplete_force_refresh)

Auto popup

By default asyncomplete will automatically show the autocomplete popup menu as you start typing. If you would like to disable the default behvior set g:asyncomplete_auto_popup to 0.

let g:asyncomplete_auto_popup = 0

You can use the above <Plug>(asyncomplete_force_refresh) to show the popup or can you tab to show the autocomplete.

let g:asyncomplete_auto_popup = 0

function! s:check_back_space() abort
    let col = col('.') - 1
    return !col || getline('.')[col - 1]  =~ '\s'
endfunction

inoremap <silent><expr> <TAB>
  \ pumvisible() ? "\<C-n>" :
  \ <SID>check_back_space() ? "\<TAB>" :
  \ asyncomplete#force_refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"

Preview Window

To disable preview window:

set completeop-=preview

To enable preview window:

set completeopt+=preview

To auto close preview window when completion is done.

autocmd! CompleteDone * if pumvisible() == 0 | pclose | endif

Sources

asyncomplete.vim deliberately does not contain any sources. Please use one of the following sources or create your own.

Example

function! s:js_completor(opt, ctx) abort
    let l:col = a:ctx['col']
    let l:typed = a:ctx['typed']

    let l:kw = matchstr(l:typed, '\v\S+$')
    let l:kwlen = len(l:kw)

    let l:startcol = l:col - l:kwlen

    let l:matches = [
        \ "do", "if", "in", "for", "let", "new", "try", "var", "case", "else", "enum", "eval", "null", "this", "true",
        \ "void", "with", "await", "break", "catch", "class", "const", "false", "super", "throw", "while", "yield",
        \ "delete", "export", "import", "public", "return", "static", "switch", "typeof", "default", "extends",
        \ "finally", "package", "private", "continue", "debugger", "function", "arguments", "interface", "protected",
        \ "implements", "instanceof"
        \ ]

    call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches)
endfunction

call asyncomplete#register_source({
    \ 'name': 'javascript',
    \ 'whitelist': ['javascript'],
    \ 'completor': function('s:js_completor'),
    \ })

The above sample shows synchronous completion. If you would like to make it async just call asyncomplete#complete whenever you have the results ready.

call timer_start(2000, {timer-> asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches)})

If you are returning incomplete results and would like to trigger completion on the next keypress pass 1 as the fifth parameter to asyncomplete#complete which signifies the result is incomplete.

call asyncomplete#complete(a:opt['name'], a:ctx, l:startcol, l:matches, 1)

As a source author you do not have to worry about synchronization issues in case the server returns the async completion after the user has typed more characters. asyncomplete.vim uses partial caching as well as ignores if the context changes when calling asyncomplete#complete. This is one of the core reason why the original context must be passed when calling asyncomplete#complete.

Credits

All the credit goes to the following projects

asyncomplete.vim's People

Contributors

andreypopp avatar prabirshrestha avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.