Shougo / denite.nvim

:dragon: Dark powered asynchronous unite all interfaces for Neovim/Vim8

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unexpected -immediately-1 behaviour

IhsanGunay opened this issue · comments

Warning: I will close the issue without the minimal init.vim and the reproduction instructions.

Problems summary

I wrote a denite source to use it for command completion but -immediately-1 option doesn't just call the default action if there is only a single candidate. It echoes the candidate but leaves me at the normal mode.

Expected

Just call the default action or at least don't suppress it. I want to put the cursor back to the original position if there is no completion like the default vim behavior.

relevant mapping:

  cnoremap <silent> <Tab> 
  \ <C-C>:Denite command/complete -immediately-1<CR>

ccomplete.py:

from pynvim import Nvim
import re

from denite.base.source import Base
from denite.kind.command import Kind as Command
from denite.util import UserContext, Candidates


class Source(Base):

    def __init__(self, vim: Nvim) -> None:
        super().__init__(vim)
        self.name = 'command/complete'
        self.kind = Kind(vim)

    def gather_candidates(self, context: UserContext) -> Candidates:
        cinput = self.vim.call('histget', 'cmd', -1)

        if ' ' not in cinput:
            candidates = [{
                'word': x,
            } for x in self.vim.call('getcompletion', cinput, 'command')]

        else:
            prefix = re.sub(r'\w*$', '', cinput)
            candidates = [{
                'word': prefix + x,
            } for x in self.vim.call(
                'getcompletion', cinput, 'cmdline')]

        ccount = len(candidates)

        if ccount == 0:
            candidates.append({'word': cinput})
            self.kind.default_action = 'abort'
        elif ccount == 1:
            self.kind.default_action = 'complete'
        else:
            self.kind.default_action = 'execute'

        return candidates


class Kind(Command):

    def __init__(self, vim: Nvim) -> None:
        super().__init__(vim)
        self.name = 'command/complete'

    def action_abort(self, context: UserContext) -> None:
        target = context['targets'][0]
        self.vim.call('feedkeys', f':{target["word"]}')

    def action_complete(self, context: UserContext) -> None:
        target = context['targets'][0]
        self.vim.call('feedkeys', f':{target["word"]} ')

    def action_execute(self, context: UserContext) -> None:
        target = context['targets'][0]
        self.vim.command(f'{target["word"]}')

I have tested it. The default action is executed.

feedkeys() hack is not magic solution. I cannot fix it.

but leaves me at the normal mode.

You have leaved at the normal mode by <C-c>.

Oh, I get the reason. It is bug.

Thanks Shougo! Everything works perfectly.
If anyone finds this issue in the future, you can use denite for comandline completion by pasting the file above to ~/.config/nvim/rplugin/python3/denite/source/ccomplete.py.