Deoplete.nvim: Insert <CR> if pum visible and no selection been made

Created on 1 Dec 2019  路  4Comments  路  Source: Shougo/deoplete.nvim

Requirement/Problem

When

  1. If no selection was made by user, close pum and insert enter for new line.
  2. If selection been made, close pum

Expected

Is there a more elegant way to verify the completion selected 'abbr'?

Please see my ugly attempt below.

Provide a minimal init.vim/vimrc with less than 50 lines (Required!)

" Your minimal init.vim/vimrc
set runtimepath+=~/path/to/deoplete.nvim/
let g:deoplete#enable_at_startup = 1

" --8<--- custom ----

set completeopt=menuone,noselect,noinsert

inoremap <silent><expr><CR> <SID>smart_carriage_return()

augroup user_plugin_deoplete
    autocmd!
    autocmd CompleteDone * call <SID>smart_complete_done()
augroup END

let g:completed_abbr = ''
function s:smart_complete_done()
    " Store the last completed item abbr
    let g:completed_abbr = get(v:completed_item, 'abbr', '')
    silent! pclose!
endfunction

function s:smart_carriage_return()
    " <CR>: If popup menu visible, close popup with selection. But insert CR
    "       if no selection has been made.
    if pumvisible()
        call deoplete#handler#_skip_next_completion()
        return "\<C-y>\<C-R>=" . '(g:completed_abbr ==# "" ? "\<CR>" : "")' . "\<CR>"
    endif
    return "\<CR>"
endfunction

How to reproduce the problem from neovim/Vim startup (Required!)

  1. Start insert mode
  2. Type something that trigger completion
  3. Do not select anything, just press
  4. I wish to insert new line _only_ if no selection been made by user

This might be related to #492

Most helpful comment

function s:smart_carriage_return()
   if !pumvisible() || get(complete_info(), 'selected', -1) < 0
      return "\<CR>"
   else
      return "\<C-y>"
   endif
endfunction

All 4 comments

function s:smart_carriage_return()
   if !pumvisible() || get(complete_info(), 'selected', -1) < 0
      return "\<CR>"
   else
      return "\<C-y>"
   endif
endfunction

Neither the suggested snippet nor the recommendation seem to do anything.

(the snippet in the official docs does work, but breaks lexima.vim - see https://github.com/Shougo/deoplete.nvim/issues/492#issuecomment-641660357)

(the snippet in the official docs does work, but breaks lexima.vim - see #492 (comment))

Yes. Because lexima.vim overwrite <CR> mapping.
It is conflicted.

So I don't use lexima.vim.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pappasam picture pappasam  路  4Comments

letharion picture letharion  路  5Comments

shibumi picture shibumi  路  4Comments

tcstory picture tcstory  路  5Comments

tomspeak picture tomspeak  路  5Comments