3.4.0 • Published 3 months ago

coc-snippets v3.4.0

Weekly downloads
2,405
License
MIT
Repository
github
Last release
3 months ago

coc-snippets

Snippets solution for coc.nvim

2019-03-23 00_09_39

Snippet preview requires neovim 0.4 or latest vim8

It's capable of:

  • Load UltiSnips snippets.
  • Load snipmate snippets.
  • Load VSCode snippets from coc.nvim extensions.
  • Load VSCode snippets from custom directories.
  • Load VSCode snippets from ${workspaceFolder}/.vscode.
  • Load UltiSnips snippets from configured folder.
  • Load massCode snippets from running massCode application (disabled by default).
  • Create massCode snippets through the snippets.editMassCodeSnippets command.
  • Provide snippets as completion items.
  • Provide expand and expandOrJump keymaps for snippet.
  • Provide snippets list for edit snippet.
  • Provide snippets.editSnippets command for edit user snippets of current filetype.

Note: some features of ultisnips and snipmate format snippets not supported, checkout Ultisnips features.

Why?

  • Use same keys for jump placeholder.
  • Nested snippet support.
  • Always async, never slows you down.
  • Preview snippet context in float window/popup.
  • Improved match for complete items with TextEdit support.
  • Edit snippets of current buffer by :CocList snippets.

Python support

Ultisnips provider needs pythonx support on (neo)vim, to check the feature exists, try:

:echo has('pythonx')

On neovim, run command:

:checkhealth

If it is not installed, run:

pip install pynvim

and make sure you have Python 3 provider for neovim installed.

On vim8, run command:

:pyx print(1)

in your vim, if it throws, it means your vim is not compiled with python support or the python dynamic lib required by vim is missing(or broken).

Note: some python code may require python feature that not supported by the python interpreter used by vim, error will throw on that case.

~Error will be shown when pythonx with (neo)vim can't work, fix pythonx support or disable ultisnips support by add "snippets.ultisnips.enable": false in your configuration file.~

Install

In your vim/neovim, run command:

:CocInstall coc-snippets

Examples

" Use <C-l> for trigger snippet expand.
imap <C-l> <Plug>(coc-snippets-expand)

" Use <C-j> for select text for visual placeholder of snippet.
vmap <C-j> <Plug>(coc-snippets-select)

" Use <C-j> for jump to next placeholder, it's default of coc.nvim
let g:coc_snippet_next = '<c-j>'

" Use <C-k> for jump to previous placeholder, it's default of coc.nvim
let g:coc_snippet_prev = '<c-k>'

" Use <C-j> for both expand and jump (make expand higher priority.)
imap <C-j> <Plug>(coc-snippets-expand-jump)

" Use <leader>x for convert visual selected code to snippet
xmap <leader>x  <Plug>(coc-convert-snippet)

Make <tab> used for trigger completion, completion confirm, snippet expand and jump like VSCode.

Note from coc.nvim 0.0.82, functions starts with coc#pum should be used for custom completion of coc.nvim.

inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#_select_confirm() :
      \ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
      \ CheckBackspace() ? "\<TAB>" :
      \ coc#refresh()

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

let g:coc_snippet_next = '<tab>'

Ultisnips features

Some ultisnips features are not supported:

  • Position check of trigger option, including b, w and i.
  • Execute vim, python and shell code in snippet.
  • extends, priority and clearsnippets command in snippet file.
  • Visual placeholder.
  • Placeholder and variable transform.
  • Expression snippet.
  • Automatic trigger snippet.
  • Context snippets.
  • Support loading snipmate snippets.
  • Replacement String, (:h UltiSnips-replacement-string), requires latest coc.nvim.
  • Update python code block after change of placeholder.
  • snip.expand_anon() should work.
  • Execute shell code with custom shabang (will not support).
  • Option m, trim all whitespaces from right side of snippet lines.
  • Reformat snippet options, including t, s.
  • All snippet actions including pre_expand, post_expand and jump (can't support).

Note: python regex in snippet are converted to javascript regex, however, some regex patterns can't be supported by javascript, including (?x) (?s) \Z (?(id/name)yes-pattern|no-pattern).

Functions

  • coc#expandable() return 1 when can do snippet expand.
  • coc#jumpable() return 1 when snippet activated and can jump to next placeholder.
  • coc#expandableOrJumpable() return 1 when can do snippet expand or can jump to next placeholder.

Key-mappings

Note you can't use noremap with <Plug> key-mappings.

  • <Plug>(coc-convert-snippet) Create new snippet with current selected text, visual mode only.
  • <Plug>(coc-snippets-expand) Expand snippet with current inserted text, insert mode only.
  • <Plug>(coc-snippets-expand-jump) Expand snippet or jump to next placeholder of current snippet when possible, insert mode only.
  • <Plug>(coc-snippets-select) Remove selected text and save to g:coc_selected_text which will replace $VISUAL on next snippet expand.

Commands

  • Use :CocList snippets to open snippets list used by current buffer.
  • Use :CocCommand snippets.openSnippetFiles to choose and open a snippet file that used by current document.
  • Use :CocCommand snippets.editSnippets to edit user's ultisnips snippets of current document filetype.
  • Use :CocCommand snippets.openOutput to open output channel of snippets.
  • Use :CocCommand snippets.addFiletypes to add additional filetypes of current buffer.

Options

  • snippets.priority: Completion source priority of snippets. Default: 90
  • snippets.editSnippetsCommand: Open command used for snippets.editSnippets command, use coc.preferences.jumpCommand by default. Default: ""
  • snippets.trace: Trace level of snippets channel, used for textmate snippets only. Default: "error" Valid options: "error","verbose"
  • snippets.excludePatterns: List of minimatch patterns for filepath to exclude, support expand homedir and environment variables. Default: []
  • snippets.loadFromExtensions: Enable load snippets from extensions. Default: true
  • snippets.textmateSnippetsRoots: List of directories that contains textmate/VSCode snippets to load. Default: []
  • snippets.loadVSCodeProjectSnippets: Load code snippets in folder ${workspaceFolder}/.vscode Default: true
  • snippets.extends: Configure filetypes to inherit with, ex: {"cpp": "c", "javascriptreact": "javascript"} Default: {}
  • snippets.userSnippetsDirectory: Directory that contains custom user ultisnips snippets, use ultisnips in extension root of coc.nvim by default. Default: ""
  • snippets.shortcut: Shortcut in completion menu. Default: "S"
  • snippets.triggerCharacters: Trigger characters for trigger snippets completion. Default: []
  • snippets.disableSyntaxes: Disable snippets completion when syntax name matches one of disabled syntaxes. Default: []
  • snippets.execContext: Execute a snippet's context (if it exists) to check if the snippet should be shown in completion menu Default: false
  • snippets.autoTrigger: Enable trigger auto trigger snippet after type character. Default: true
  • snippets.ultisnips.enable: Enable load snippets from ultisnips folders. Default: true
  • snippets.ultisnips.pythonPrompt: Show prompt for user when python not supported on vim. Default: true
  • snippets.ultisnips.trace: Trace verbose snippet information. Default: false
  • snippets.ultisnips.directories: Directories that searched for ultisnips snippet files, could be directory as subfolder in $runtimepath or absolute paths. Default: ["UltiSnips"]
  • snippets.massCode.enable: Enable load snippets from MassCode. Default: false
  • snippets.massCode.host: Http host of MassCode. Default: "localhost"
  • snippets.massCode.port: Http port of MassCode. Default: 3033
  • snippets.massCode.trace: Trace verbose snippet information. Default: false
  • snippets.snipmate.enable: Load snipmate snippets from snippets directory in runtimepath. Default: true
  • snippets.snipmate.trace: Trace verbose snippet information. Default: false
  • snippets.snipmate.author: Author name used for g:snips_author Default: ""

F.A.Q

Q: How to add snippet filetypes on buffer create?

A: Use autocmd like: autocmd BufRead *.md let b:coc_snippets_filetypes = ['tex']

Q: How to trigger snippets completion when type special characters?

A: Use configuration like: "snippets.triggerCharacters": ["'"].

Q: How to check if a snippet successfully loaded?

A: Use command :CocCommand workspace.showOutput snippets

Q: Some ultisnips snippet not works as expected.

A: Reformat after change of placeholder feature can't be supported for now, and some regex pattern can't be converted to javascript regex pattern, so the snippet can be failed to load.

Q: Where to get snippets?

A: One solution is install honza/vim-snippets which is widely used. To create snippets yourself:

  • For Ultisnips, create ${filetype}.snippets in "snippets.ultisnips.directories"
  • For snipmate snippets, create ${filetype}.snippets in snippets folder under your vim's runtimepath.
  • For VSCode snippets, create ${filetype}.json in your "snippets.textmateSnippetsRoots".

Q: Get error message ModuleNotFoundError: No module named 'vimsnippets'

A: Make sure honza/vim-snippets in your vim's &runtimepath.

Q: Do I need to install Ultisnips.

A: No! This extension is designed to work with or without Ultisnips, you can still install Ultisnips, but this extension would not run any code or read configuration from it.

Q: How to check jumpable or expandable at current position.

A: Use functions provided by coc.nvim: coc#expandable() coc#jumpable() and coc#expandableOrJumpable().

Q: It doesn't load snippets from vim-go.

A: It uses g:UltiSnipsSnippetDirectories which is not supported, you can add settings:

snippets.ultisnips.directories: [
  "UltiSnips",
  "gosnippets/UltiSnips"
],

to load it.

Q: How could I add custom UltiSnips snippets.

A: You can create snippet files in folder: $VIMCONFIG/coc/ultisnips, use command :CocCommand snippets.editSnippets to open user snippet of current filetype.

Supporting

If you like this extension, consider supporting me on Patreon or PayPal:

License

MIT

3.2.0

3 months ago

3.4.0

3 months ago

3.3.1

3 months ago

3.3.0

3 months ago

3.3.2

3 months ago

3.1.7

2 years ago

3.1.10

2 years ago

3.1.9

2 years ago

3.1.8

2 years ago

3.1.6

3 years ago

3.1.3

3 years ago

3.1.5

3 years ago

3.1.4

3 years ago

3.1.2

3 years ago

3.1.1

3 years ago

3.1.0

3 years ago

3.0.12

3 years ago

3.0.13

3 years ago

3.0.10

3 years ago

3.0.11

3 years ago

3.0.14

3 years ago

3.0.9

3 years ago

2.5.9

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.0

3 years ago

2.5.10

3 years ago

2.5.6

3 years ago

2.5.5

3 years ago

2.5.8

3 years ago

2.5.7

3 years ago

2.4.10

3 years ago

2.4.9

3 years ago

2.5.0

3 years ago

2.5.2

3 years ago

2.5.1

3 years ago

2.5.4

3 years ago

2.5.3

3 years ago

2.4.7

3 years ago

2.4.8

3 years ago

2.4.6

4 years ago

2.4.5

4 years ago

2.4.4

4 years ago

2.4.3

4 years ago

2.4.2

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.1

5 years ago

2.2.10

5 years ago

2.2.9

5 years ago

2.2.8

5 years ago

2.2.7

5 years ago

2.2.6

5 years ago

2.2.5

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.28

5 years ago

2.1.27

5 years ago

2.1.26

5 years ago

2.1.25

5 years ago

2.1.24

5 years ago

2.1.23

5 years ago

2.1.22

5 years ago

2.1.21

5 years ago

2.1.20

5 years ago

2.1.19

5 years ago

2.1.18

5 years ago

2.1.17

5 years ago

2.1.16

5 years ago

2.1.15

5 years ago

2.1.14

6 years ago

2.1.13

6 years ago

2.1.12

6 years ago

2.1.11

6 years ago

2.1.10

6 years ago

2.1.9

6 years ago

2.1.8

6 years ago

2.1.7

6 years ago

2.1.6

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.16

6 years ago

2.0.15

6 years ago

2.0.14

6 years ago

2.0.13

6 years ago

2.0.12

6 years ago

2.0.11

6 years ago

2.0.10

6 years ago

2.0.9

6 years ago

2.0.8

6 years ago

2.0.7

6 years ago

2.0.6

6 years ago

2.0.5

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago