deoplete.nvimのソースをつくってみた

### Summary

下記のような感じでオートコンプリートしたい

`ap`と入力したら`apple`
`ba`と入力したら`banana`
`ch`と入力したら`cherry`

![image.png](https://qiita-image-store.s3.amazonaws.com/0/149269/e2827b6f-c443-2050-861c-125fbdfb18dc.png)

### 必要なもの

deoplete.nvim

```vim
# dein.toml の記入例
[[plugins]]
repo = 'Shougo/deoplete.nvim'
hook_add = 'let g:deoplete#enable_at_startup = 1'
```

###ソースをつくる
ソースの置き場所

```vim
$XDG_CONFIG_HOME/nvim/rplugin/python3/deoplete/sources
```

まずフォルダをつくる

```vim
cd $XDG_CONFIG_HOME/nvim
mkdir -p rplugin/python3/deoplete/sources
```

ソース（パイソンファイル）を作成

```vim
cd rplugin/python3/deoplete/sources
vim callmekohei.py
```

`callmekohei.py`の中身（さわるところは３ヶ所のみ！）

```python
from .base import Base

class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)
        self.name = 'callmekohei'           # なまえ
        self.mark = '[kohei]'               # mark のなまえ

    def gather_candidates(self, context):
        return ["apple","banana","cherry"]  # ポップアップのリスト

```

`init.vim`に記入
`filetype plugin indent on`の前に書く

```vim
set runtimepath+=$XDG_CONFIG_HOME/nvim/rplugin

filetype plugin indent on
syntax   enable
```

### やりたいこと２

`fruits.`と入力したら`apple, banana, cherry`

![image.png](https://qiita-image-store.s3.amazonaws.com/0/149269/9e5a323f-0a65-0d97-0c5f-4a65bbb0ba6e.png) 

### ソース

```python
import re
from .base import Base

class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)
        self.name = 'callmekohei'
        self.mark = '[kohei]'
        self.input_pattern = (r'^fruits\.')        # input_pattern で fruits. を指定する
        
    def get_complete_position(self, context):      # input_pattern を使うときは必須
        m = re.search(r'\w*$', context['input'])
        return m.start() if m else -1

    def gather_candidates(self, context):
        return ["apple","banana","cherry"]

```

### 最小のセッティング
```vim
set runtimepath+=~/.config/nvim/rplugin
set runtimepath+=~/.config/nvim/plugins/deoplete.nvim
let g:deoplete#enable_at_startup = 1
filetype plugin indent on
syntax enable

```

###やりたいこと３

`apple`を選択したら`apple`の説明を表示したい  

![image.png](https://qiita-image-store.s3.amazonaws.com/0/149269/6689602e-5071-548b-74c6-a35df6b604a1.png)

###ソース

```python
from .base import Base

class Source(Base):
    def __init__(self, vim):
        super().__init__(vim)
        self.name          = 'callmekohei'
        self.mark          = '[kohei]'

    def gather_candidates(self, context):
        return [
            {
                  'word' : "apple"
                , 'abbr' : "apple  : red, round and delicious!"
                , 'info' : "Apple is delicious"
                , 'kind' : "Food"
                , 'dup'  : 1
            }
        ]
```

### 参照

ヘルプ `:help deoplete`

```vim
CREATE SOURCE					*deoplete-create-source*

To create source, you should read default sources implementation in
rplugin/python3/deoplete/source/*.py.

The files are automatically loaded and deoplete creates new Source class
object.
Source class must extend Base class in ".base".

Note: The sources must be created by Python3 language.

Note: If you call Vim functions in your source, it is not asynchronous.

```

[次世代補完フレームワークdeoplete.nvimと、そのソースの内部を詳しく](http://qiita.com/zchee/items/f3e13773ae39ae3560da)