My dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Readme.md 6.7 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # coc-snippets
  2. Snippets solution for [coc.nvim](https://github.com/neoclide/coc.nvim)
  3. ![2019-03-23 00_09_39](https://user-images.githubusercontent.com/251450/54837017-62891300-4d00-11e9-9e53-49742a1a33f2.gif)
  4. _Snippet preview requires [neovim 0.4 or latest vim8](https://github.com/neoclide/coc.nvim/wiki/F.A.Q#how-to-make-preview-window-shown-aside-with-pum)_
  5. It's capable of:
  6. - Load UltiSnips snippets.
  7. - Load snipmate snippets.
  8. - Load VSCode snippets from coc extensions.
  9. - Load VSCode snippets from custom directories.
  10. - Load UltiSnips snippets from configured folder.
  11. - Provide snippets as completion items.
  12. - Provide expand and expandOrJump keymaps for snippet.
  13. - Provide snippets list for edit snippet.
  14. - Provide `snippets.editSnippets` command for edit user snippets of current filetype.
  15. **Note:** some features of ultisnips and snipmate format snippets not supported, checkout [faq](#faq).
  16. ## Why?
  17. - Use same keys for jump placeholder.
  18. - Nested snippet support.
  19. - Always async, never slows you down.
  20. - Improved match for complete items with TextEdit support.
  21. - Edit snippets of current buffer by `:CocList snippets`, sorted by mru.
  22. ## Install
  23. In your vim/neovim, run command:
  24. ```
  25. :CocInstall coc-snippets
  26. ```
  27. ## Examples
  28. ```vim
  29. " Use <C-l> for trigger snippet expand.
  30. imap <C-l> <Plug>(coc-snippets-expand)
  31. " Use <C-j> for select text for visual placeholder of snippet.
  32. vmap <C-j> <Plug>(coc-snippets-select)
  33. " Use <C-j> for jump to next placeholder, it's default of coc.nvim
  34. let g:coc_snippet_next = '<c-j>'
  35. " Use <C-k> for jump to previous placeholder, it's default of coc.nvim
  36. let g:coc_snippet_prev = '<c-k>'
  37. " Use <C-j> for both expand and jump (make expand higher priority.)
  38. imap <C-j> <Plug>(coc-snippets-expand-jump)
  39. ```
  40. Make `<tab>` used for trigger completion, completion confirm, snippet expand and jump like VSCode.
  41. ```vim
  42. inoremap <silent><expr> <TAB>
  43. \ pumvisible() ? coc#_select_confirm() :
  44. \ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
  45. \ <SID>check_back_space() ? "\<TAB>" :
  46. \ coc#refresh()
  47. function! s:check_back_space() abort
  48. let col = col('.') - 1
  49. return !col || getline('.')[col - 1] =~# '\s'
  50. endfunction
  51. let g:coc_snippet_next = '<tab>'
  52. ```
  53. **Note:** `coc#_select_confirm()` helps select first complete item when there's
  54. no complete item selected, neovim 0.4 or latest vim8 required for this function
  55. work as expected.
  56. ## Ultisnips features
  57. Some ultisnips features are **not** supported:
  58. - [x] Position check of trigger option, including `b`, `w` and `i`.
  59. - [x] Execute vim, python and shell code in snippet.
  60. - [x] `extends`, `priority` and `clearsnippets` command in snippet file.
  61. - [x] Visual placeholder.
  62. - [x] Placeholder and variable transform.
  63. - [x] Expression snippet.
  64. - [x] Automatic trigger snippet.
  65. - [x] Context snippets.
  66. - [x] Support loading snipmate snippets.
  67. - [ ] Execute shell code with custom shabang (will not support).
  68. - [ ] Automatic reformat snippet after change of placeholder (can't support).
  69. - [ ] Format related snippet options, including `t`, `s` and `m` (can't support).
  70. - [ ] Snippet actions (can't support).
  71. **Note**: python regex in snippet are converted to javascript regex, however,
  72. some regex patterns can't be supported by javascript, including
  73. `\u` `(?s)` `\Z` `(?(id/name)yes-pattern|no-pattern)`.
  74. ## Options
  75. - `snippets.priority`: priority of snippets source, default `90`.
  76. - `snippets.editSnippetsCommand`: Open command used for snippets.editSnippets command, use coc.preferences.jumpCommand by default.
  77. - `snippets.trace`: Trace level of snippets channel.
  78. - `snippets.enableStatusItem`: Enable status item in `g:coc_status` used for statusline.
  79. - `snippets.extends`: extends filetype's snippets with other filetypes, example:
  80. ```json
  81. {
  82. "cpp": ["c"],
  83. "javascriptreact": ["javascript"],
  84. "typescript": ["javascript"]
  85. }
  86. ```
  87. - `snippets.userSnippetsDirectory`, Directory that contains custom user ultisnips snippets, use ultisnips in extension root by default.
  88. - `snippets.shortcut`, shortcut in completion menu, default `S`.
  89. - `snippets.autoTrigger`: enable auto trigger for auto trigger ultisnips snippets, default `true`.
  90. - `snippets.triggerCharacters`: trigger characters for completion, default `[]`.
  91. - `snippets.loadFromExtensions`: load snippets from coc.nvim extensions, default: `true`.
  92. - `snippets.convertToSnippetsAction`: Add convert to snippet to code action list.
  93. - `snippets.textmateSnippetsRoots`: absolute directories that contains textmate/VSCode snippets to load.
  94. - `snippets.ultisnips.enable`: enable load UltiSnips snippets, default `true`.
  95. - `snippets.ultisnips.usePythonx`: use `pythonx` for eval python code when possible, default `true`.
  96. - `snippets.ultisnips.pythonVersion`: when `usePythonx` is false, python version to use for
  97. python code, default to `3`.
  98. - `snippets.ultisnips.directories`: directories that searched for snippet files,
  99. could be subfolder in every \$runtimepath or absolute paths, default: `["UltiSnips"]`
  100. - `snippets.snipmate.enable`: enable load snipmate snippets, default `true`.
  101. - `snippets.snippets.author`: author name used for `g:snips_author`
  102. ## Commands
  103. - Use `:CocList snippets` to open snippets list.
  104. - Use `:CocCommand snippets.editSnippets` to edit user snippet of current filetype.
  105. - Use `:CocCommand snippets.openSnippetFiles` to open snippet files of current filetype.
  106. ## F.A.Q
  107. **Q:** How to check if a snippet successfully loaded?
  108. **A:** Use command `:CocCommand workspace.showOutput snippets`
  109. **Q:** Some ultisnips snippet not works as expected.
  110. **A:** Reformat after change of placeholder feature can't be supported for now,
  111. and some regex pattern can't be converted to javascript regex pattern, so the
  112. snippet can be failed to load.
  113. **Q:** Where to get snippets?
  114. **A:** One solution is install [honza/vim-snippets](https://github.com/honza/vim-snippets) which is widely used.
  115. **Q:** Do I need to install [Ultisnips](https://github.com/SirVer/ultisnips).
  116. **A:** No! This extension is designed to work with or without Ultisnips, you can
  117. still install Ultisnips, but this extension would not run any code or read
  118. configuration from it.
  119. **Q:** How to check jumpable or expandable at current position.
  120. **A:** Use functions provided by coc.nvim: `coc#expandable()` `coc#jumpable()` and `coc#expandableOrJumpable()`.
  121. **Q:** It doesn't load snippets from [vim-go](https://github.com/fatih/vim-go).
  122. **A:** It uses `g:UltiSnipsSnippetDirectories` which is not supported, you can
  123. add settings:
  124. ```
  125. snippets.ultisnips.directories: [
  126. "UltiSnips",
  127. "gosnippets/UltiSnips"
  128. ],
  129. ```
  130. to load it.
  131. **Q:** How could I add custom UltiSnips snippets.
  132. **A:** You can create snippet files in folder: `$VIMCONFIG/coc/ultisnips`, use
  133. command `:CocCommand snippets.editSnippets` to open user snippet of current
  134. filetype.
  135. ## License
  136. MIT