• Stars
    star
    924
  • Rank 47,920 (Top 1.0 %)
  • Language
    TypeScript
  • Created over 5 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Snippets solution for coc.nvim

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.
  • Execute shell code with custom shabang (will not support).
  • Option m, empty line in snippet not have indentation with coc.nvim.
  • 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.

Options

  • snippets.priority: priority of snippets source, default 90.

  • snippets.editSnippetsCommand: Open command used for snippets.editSnippets command, use coc.preferences.jumpCommand by default.

  • snippets.trace: Trace level of snippets channel.

  • snippets.excludePatterns: List of filepath patterns to exclude, $HOME and other environment variables are expanded.

  • snippets.extends: extends filetype's snippets with other filetypes, example:

    {
      "cpp": ["c"],
      "javascriptreact": ["javascript"],
      "typescript": ["javascript"]
    }
  • snippets.userSnippetsDirectory, Directory that contains custom user ultisnips snippets, use ultisnips in extension root by default.

  • snippets.shortcut, shortcut in completion menu, default S.

  • snippets.autoTrigger: enable auto trigger for auto trigger ultisnips snippets, default true.

  • snippets.triggerCharacters: trigger characters for completion, default [].

  • snippets.loadFromExtensions: load snippets from coc.nvim extensions, default: true.

  • snippets.loadVSCodeProjectSnippets: Load code snippets in folder ${workspaceFolder}/.vscode, default: true.

  • snippets.textmateSnippetsRoots: absolute directories that contains textmate/VSCode snippets to load, default: [].

  • snippets.ultisnips.enable: enable load UltiSnips snippets, default true.

  • snippets.ultisnips.trace: Trace verbose snippet information, default false.

  • snippets.ultisnips.directories: directories that searched for snippet files, could be subfolder in every $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: enable load snipmate snippets, default true.

  • snippets.snipmate.author: author name used for g:snips_author

  • snippets.snipmate.trace: Trace verbose snippet information, default false.

F.A.Q

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:

Patreon donate button PayPal donate button

License

MIT

More Repositories

1

coc.nvim

Nodejs extension host for vim & neovim, load extensions like VSCode and host language servers.
TypeScript
22,790
star
2

coc-tsserver

Tsserver extension for coc.nvim that provide rich features like VSCode for javascript & typescript
TypeScript
1,000
star
3

coc-python

Python extension for coc.nvim, fork of vscode-python
TypeScript
576
star
4

coc-prettier

Prettier extension for coc.nvim.
TypeScript
512
star
5

coc-tabnine

Tabnine integration of coc.nvim
TypeScript
470
star
6

coc-java

Java extension for coc.nvim
TypeScript
407
star
7

coc-git

Git integration of coc.nvim
TypeScript
401
star
8

coc-rls

Rust language server support for coc.nvim
TypeScript
378
star
9

coc-eslint

Eslint extension for coc.nvim
TypeScript
378
star
10

vim-jsx-improve

Syntax and indent plugin for React jsx.
Vim Script
285
star
11

coc-pairs

Basic auto pairs extension of coc.nvim
TypeScript
283
star
12

coc-lists

Common lists for coc.nvim
TypeScript
259
star
13

coc-json

Json language extension for coc.nvim
TypeScript
229
star
14

coc-yaml

Yaml language server extension for coc.nvim
TypeScript
226
star
15

coc-vetur

Vue language server extension for coc.nvim
TypeScript
217
star
16

coc-highlight

Document highlight and document colors LSP support for coc.nvim
TypeScript
210
star
17

coc-sources

Additional common sources of coc.nvim
JavaScript
200
star
18

coc-html

Html language server extension for coc.nvim.
TypeScript
196
star
19

coc-css

Css language server extension for coc.nvim
TypeScript
189
star
20

coc-solargraph

Solargraph extension for coc.nvim
TypeScript
188
star
21

coc-yank

Yank highlight and persist yank history support for vim
TypeScript
188
star
22

coc-emmet

Emmet extension for coc.nvim
TypeScript
169
star
23

coc-vimtex

vimtex integration for coc.nvim
JavaScript
121
star
24

denite-git

Manger list of git objects with interface of denite.nvim
Python
73
star
25

coc-smartf

Make jump to character easier.
TypeScript
73
star
26

vim-node-rpc

Make vim behavior like neovim rpc server by use node server in the middle.
TypeScript
61
star
27

jsonc.vim

jsonc syntax support for vim
Vim Script
60
star
28

denite-extra

extra useful sources for denite.nvim
Python
55
star
29

coc-jest

Jest extension for coc.nvim
TypeScript
51
star
30

coc-tslint-plugin

coc.nvim extension that provides TSLint support using the typescript-tslint-plugin
TypeScript
49
star
31

coc-r-lsp

R LSP Client for coc.nvim
TypeScript
48
star
32

coc-stylelint

Stylelint language server extension for coc.nvim
JavaScript
42
star
33

vim-easygit

A git wrapper focus on simplity and usability
Vim Script
38
star
34

coc-imselect

Input method enhance for iTerm2 on mac.
TypeScript
37
star
35

coc-typos

Typos integration with coc.nvim
TypeScript
32
star
36

neovim

Node client API for both vim8 and neovim.
TypeScript
28
star
37

npm.nvim

Npm plugin to make vim user works with npm easier
Python
26
star
38

redismru.vim

MRU plugin build for speed with async IO operation
Vim Script
25
star
39

coc-neco

viml completion source for coc.nvim
Vim Script
23
star
40

coc-denite

Denite support of coc.nvim
Python
22
star
41

todoapp.vim

Todo management in vim using unite and sqlite
Vim Script
20
star
42

neoclide

Web enhanced IDE forked from Nyaovim
JavaScript
18
star
43

coc-tslint

Tslint language server extension of coc.nvim
TypeScript
17
star
44

rename.nvim

Rename plugin for neovim
TypeScript
16
star
45

plug.nvim

None block plugin manager for neovim
TypeScript
14
star
46

coc-repl

Read-Eval-Print-Loop (REPL) for coc.nvim
JavaScript
12
star
47

macnote.vim

Light weight note manager on mac using vim
Vim Script
11
star
48

macdown.vim

Live preview markdown in Chrome using applescript
JavaScript
11
star
49

wasm-fuzzy

Fuzzy match at native speed
C
10
star
50

mycomment.vim

one mapping for comment: <leader>c
Vim Script
10
star
51

coc-extension-codemod

Fix the code of coc.nvim extension.
JavaScript
10
star
52

coc-jedi

Deprecated, use https://github.com/neoclide/coc-python instead.
Python
10
star
53

coc-pyls

Deprecated, use https://github.com/neoclide/coc-python instead.
TypeScript
7
star
54

keep.nvim

Recover removed files made ease for neovim
JavaScript
5
star
55

neoclide-client

Embedding neovim as UI component with unified data flow
JavaScript
3
star
56

tern-neovim

Neovim tern plugin use remote plugin feature
Python
3
star
57

coc-wxml

wxml extension for coc.nvim
TypeScript
3
star
58

neovim-component

Editor component used for neoclide
JavaScript
2
star
59

electron-discuss

Discuss issues with electron
1
star