An open API service indexing awesome lists of open source software.

https://github.com/leafoftree/vim-imagine

A simple context-based vim completion plugin.
https://github.com/leafoftree/vim-imagine

complete completion plugin vim

Last synced: 9 months ago
JSON representation

A simple context-based vim completion plugin.

Awesome Lists containing this project

README

          

# vim-imagine

A simple context-based vim completion plugin. It wil try to return the most possible word based on current context. Choosing a word from the menu should be avoided.

**Supports**

- Literial tab.
- [Custom snippets](#custom_snippets).
- [Extensible fuzzy match](#fuzzy_match).
- [emmet-vim][0].
- [supertab][1].

## Usage

In INSERT mode, press tab to complete the word. Press c-u to undo the completion.

## Install

- Use [VundleVim](https://github.com/VundleVim/Vundle.vim):

Plugin 'leafOfTree/vim-imagine'

- Or manually, clone this plugin, drop it in custom `path/to/this_plugin`, and add it to `rtp` in vimrc

set rpt+=path/to/this_plugin

## How it works

It tries these methods in order to find out the completion word.

- Literial tab.
- [Custom snippets](#custom_snippets).
- [Extensible fuzzy match](#fuzzy_match).
- [emmet-vim][0].
- [supertab][1].

### Literial tab

It will return literial tab if there are blanks or the start of line before the cursor.

### Custom snippets

It will return the snippet if the characters before the cursor match the key in the dictionary defined by [snippets file](#snippets).

### Extensible fuzzy search

It returns the first match that fuzzy methods find in current file. The order can be changed by [g:vim_imagine_fuzzy_chain](#fuzzy_chain) except the `favoured` method.

The longest is used if there are more than one matched words.

The fuuzy search will try lines near current line first, then all the lines. See [fuzzy_near](#fuzzy_near).

#### Builtin methods

- favoured(Always try it at first)

If [g:vim_imagine_fuzzy_favoured_words](#fuzzy_favoured_words) is `['myLongName', 'anotherLongName']`,

mln -> myLongName

- hyphen

adg -> abc-def-ghi

- capital

adg -> abcDefGhi

- dot

adg -> abc.def.ghi

**Note**: context words won't be split by `.` in this method

- underscore

adg -> abc_def_ghi

- include

xyz -> 000x111y222z

#### Custom methods

All methods defined in [g:vim_imagine_fuzzy_custom_methods](#fuzzy_custom_methods) are used in fuzzy completion. See the [example](#custom_methods_eample).

### Emmet

It will return [emmet-vim][0]'s result if it's installed when there is only one character before the cursor or [b:vim_imagine_use_emmet](#use_emmet) is 1.

### Supertab

It will return [supertab][1]'s result if it's installed. It's worth nothing that tab can't be used to move in popup menu, but up, down, enter still work.

## Configuration

### Variables

#### g:vim_imagine_snippets_path

- description: the path of the snippets file under the `runtimepath`. See [the example snippets](/setting/example_snippets.vim)
- type: `string`.
- default: `'plugin/imagine_snippets.vim'`.

It's recommended to put it under `.vim` or `vimfiles`. It's a normal vimscript file. The only requirement is that these dictionary variables
`g:vim_imagine_dict`,
`g:vim_imagine_dict_1`,
`g:vim_imagine_dict_2`
are defined as expected. If the path is not readable, [the example snippets](/setting/example_snippets.vim) will be used.

#### Snippets file format
For dictionary variable
`g:vim_imagine_dict`,
`g:vim_imagine_dict_1`,
`g:vim_imagine_dict_2`, the key is the chars before the cursor, and the value is the snippet string.

Special characters like `\r`, `\t`, `\e` are supported in double quoted string(see `:h string`). Normal mode commands can be executed after `\e`.

`|` can be used to mark cursor location.

#### g:vim_imagine_fuzzy_chain

- description: the order of methods that fuzzy search uses.
- type: `list`.
- default:

```vim
let g:vim_imagine_fuzzy_chain = [
\'capital',
\'dot',
\'hyphen',
\'underscore',
\'include',
\]
```

#### g:vim_imagine_fuzzy_near

- description: the top and bottom offset to current line which defines the lines to search first. Both of offsets are positive value.
- type: `list`.
- default:

```vim
let g:vim_imagine_fuzzy_near = [5, 0]
```

#### g:vim_imagine_fuzzy_near_chain

- description: the order of methods that fuzzy near search uses.
- type: `list`.
- default:

```vim
let g:vim_imagine_fuzzy_chain = [
\'capital',
\'dot',
\'hyphen',
\'underscore',
\'include',
\]
```

#### g:vim_imagine_fuzzy_custom_methods

- description: defines custom methods that fuzzy search uses.
- type: `dictionary`.

- members: `chars => regexp`. The `chars` is the characters before the cursor and the `regexp` is used to match words in current file. The member name can be used in `g:vim_imagine_fuzzy_chain`.

- members end with `_splits`: `splits => modified splits`. Modify `splits` which determines how words is generated from current file. Default is a comma-separated string(comma and space will always be added).

(,),{,},<,>,[,],=,:,'',",;,/,\,+,!,#,*,`,|,.

- default: `{}`
- example:

```vim
let g:vim_imagine_fuzzy_custom_methods = {}

function! g:vim_imagine_fuzzy_custom_methods.same_first(chars)
let case_flag = ''
if a:chars =~ '\v\C[A-Z]'
let case_flag = '\C'
endif

let regexp = join(split(a:chars, '\zs'), '.*')
let regexp = escape(regexp, '()@$')
let regexp = '\v'.case_flag.'^(\@|\$)?'.regexp.'.*>'
return regexp
endfunction

let g:vim_imagine_fuzzy_chain = [
\'capital',
\'hyphen',
\'dot',
\'underscore',
\'same_first',
\'include',
\]
```

#### g:vim_imagine_fuzzy_favoured_words

- description: The words to check firstly when fuzzy search starts. The list can be changed while editing files by leader a in NORMAL mode.
- type: `list`.
- default: `[]`

#### b:vim_imagine_use_emmet

- description: Enable emmet method at first. It can be togggled by c-f.
- type: `int` (0 or 1)
- default: `0`
- example:

```vim
autocmd FileType html,pug,xml,css,less let b:vim_imagine_use_emmet = 1
```

### Mappings

#### INSERT mode

- tab: complete the chars before the cursor.

- c-u: undo the completion.

```vim
let g:vim_imagine_mapping_undo_completion = ''
```

- c-f: toggle emmet.

```vim
let g:vim_imagine_mapping_toggle_emmet = ''
```

#### NORMAL mode

- leader a: add the word under the cursor to favoured words .

```vim
let g:vim_imagine_mapping_add_favoured_word = 'a'
```

[0]: https://github.com/mattn/emmet-vim
[1]: https://github.com/ervandew/supertab