maralla / completor.vim

Async completion framework made ease.
MIT License
1.27k stars 63 forks source link

Ultisnips - wrong <tab> mapping at the beginning, incomplete selection of placeholder #235

Closed rkueke closed 2 years ago

rkueke commented 5 years ago

Dear all, Completor works pretty fine for c/c++ completion and placeholder selection (Vim8, clang). My completor setup follows your web site proposal incl. function definition Tab_Or_Complete().

In case I only add the plugin Ultisnips to my .vimrc without any further Ultisnips configuration the completion for c/c++ does not work anymore.

CASE A - wrong mapping at the beginning Manually starting of completion with on a well known pattern in the buffer shows me a . No wonder: :imap results in i * =UltiSnips#ExpandSnippet()

After sourcing .vimrc everything is fine again (workaround) :imap results in i * Tab_Or_Complete()

CASE B - incomplete selection of snippet placeholder Manually starting completion with on a snippet identifier (e.g. 'for') the snippet is expanded correctly: for ( i = 0; i < count; ++i )
{
} But the placeholder 'count' is not completely selected (only 'coun'). The last letter of the placeholder is missing. I remember a similar and solved problem appeared with c function argument placeholders.

rkueke commented 5 years ago

CASE A: Better integration of manual triggering the auto completion ( ultisnips, other completors) is recommend by habamax (see https://github.com/maralla/completor.vim/issues/236). This works fine for Vim8, ultisnips and clang as well.

CASE B: Incomplete selection of snippet placeholder is NOT caused by completor. In my environment an incremental formatting call of clang-format is the reason for this behavior.

I am currently looking how to guard the manual triggering of autocompletion against inadvertent formatting of clang-format.

rkueke commented 5 years ago

FINAL SUMMARY Issue Using completor, ultisnips and the incremental formatting of clang-format.vim for c/c++:

Solution Taking habamax recommendation for TAB based autocompletion (see https://github.com/maralla/completor.vim/issues/236) and an additional guarding of the snippet expansion against incremental formatting solves the problem:

let g:UltiSnipsExpandTrigger = '<tab>'
let g:UltiSnipsJumpForwardTrigger = '<tab>'

" Completor and ultisnips to reuse TAB key
fun! Tab_Or_Complete() "{{{
    call UltiSnips#ExpandSnippet()
    if g:ulti_expand_res == 0
        if pumvisible()
            return "\<C-n>"
        else
            call UltiSnips#JumpForwards()
            if g:ulti_jump_forwards_res == 0
                " If completor is not open and we are in the middle of typing a word then
                " `tab` opens completor menu.
                let inp_str = strpart( getline('.'), col('.')-3, 2 )
                if col('.')>1 && (inp_str =~ '^\w$' || inp_str =~ '\%(->\)\|\%(.\w\)\|\%(\w\.\)\|\%(./\)')
                    return "\<C-R>=completor#do('complete')\<CR>"
                else
                    return "\<TAB>"
                endif
            endif
        endif
    endif
    return ""
endf "}}}

au InsertEnter * exec "inoremap <silent> " . g:UltiSnipsExpandTrigger . " <C-R>=Tab_Or_Complete()<cr>"

augroup UltiSnipsExpansionGuard
    " signal running snippet expansion via flag b:UltiSnipsGuard = 1
    autocmd!
    autocmd BufRead,BufNewFile * let b:UltiSnipsGuard = 0
    autocmd! User UltiSnipsEnterFirstSnippet
    autocmd User UltiSnipsEnterFirstSnippet let b:UltiSnipsGuard = 1
    autocmd! User UltiSnipsExitLastSnippet
    autocmd User UltiSnipsExitLastSnippet let b:UltiSnipsGuard = 0
augroup end

augroup FormatCode
    " incremental formatting after completed snippet leaving insert mode,
    " total file formatting after read file and before write file
    autocmd! 
    autocmd BufRead,BufNewFile *.c,*.h,*.cpp,*.hpp 
            \ let b:InsertionStartline = 1 |
            \ let b:InsertionEndline = line('$')
    autocmd InsertEnter * let b:InsertionStartline = line('.')
    autocmd InsertLeave * let b:InsertionEndline = line('.')
    autocmd InsertLeave *.c,*.h,*.cpp,*.hpp 
            \ exec b:InsertionStartline . ',' . b:InsertionEndline . 'FormatCode'
    autocmd BufRead,BufWrite *.c,*.h,*.cpp,*.hpp FormatCode
augroup end

func! FormatCode() range
    if !b:UltiSnipsGuard
        exec a:firstline . ',' . a:lastline . 'ClangFormat'
    endif
endfunc
command! -range=% FormatCode <line1>,<line2>call FormatCode()

A further replacement of clang-format.vim by e.g. plugin vim-autoformat can easily extend the incremental formatting to other programming languages.

No open actions for completor.vim - we can close this issue.