From 2fe16b99fce404a918417510b77b9318f821d09a Mon Sep 17 00:00:00 2001 From: Martin Tournoij Date: Thu, 7 Dec 2017 18:34:32 +0000 Subject: [PATCH] Support minisnip (#1589) Supprt minisnip Needs https://github.com/joereynolds/vim-minisnip/pull/5 I've also changed the default for `g:go_snippet_engine` to `automatic`, which is a new value that will use the first snippet engine that's loaded. I think that's more user-friendly than expecting the user to set it. I also changed the detection method to use the variables, instead of scanning `runtimepath`. This is more efficient. --- CHANGELOG.md | 6 +++++ doc/vim-go.txt | 18 +++++++++---- ftplugin/go/snippets.vim | 49 +++++++++++++++++++++++++----------- gosnippets/minisnip/_go_eq | 3 +++ gosnippets/minisnip/_go_err | 3 +++ gosnippets/minisnip/_go_errt | 4 +++ gosnippets/minisnip/_go_errw | 3 +++ gosnippets/minisnip/_go_f | 3 +++ gosnippets/minisnip/_go_ff | 1 + gosnippets/minisnip/_go_fori | 3 +++ gosnippets/minisnip/_go_pkg | 2 ++ gosnippets/minisnip/_go_sp | 2 ++ 12 files changed, 77 insertions(+), 20 deletions(-) create mode 100644 gosnippets/minisnip/_go_eq create mode 100644 gosnippets/minisnip/_go_err create mode 100644 gosnippets/minisnip/_go_errt create mode 100644 gosnippets/minisnip/_go_errw create mode 100644 gosnippets/minisnip/_go_f create mode 100644 gosnippets/minisnip/_go_ff create mode 100644 gosnippets/minisnip/_go_fori create mode 100644 gosnippets/minisnip/_go_pkg create mode 100644 gosnippets/minisnip/_go_sp diff --git a/CHANGELOG.md b/CHANGELOG.md index 147d84c797..b141cca7bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,12 @@ IMPROVEMENTS: [[GH-1582]](https://github.com/fatih/vim-go/pull/1582). * `:GoDescribe` doesn't require a scope anymore [[GH-1596]](https://github.com/fatih/vim-go/pull/1596). +* Add some standard snippets for + [vim-minisnip](https://github.com/joereynolds/vim-minisnip) + [[GH-1589]](https://github.com/fatih/vim-go/pull/1589). +* `g:go_snippet_engine` now defaults to `automatic` to use the first installed + snippet engine it can find. + [[GH-1589]](https://github.com/fatih/vim-go/pull/1589). ## 1.15 - (October 3, 2017) diff --git a/doc/vim-go.txt b/doc/vim-go.txt index 60218be02e..b5479cce8a 100644 --- a/doc/vim-go.txt +++ b/doc/vim-go.txt @@ -57,7 +57,8 @@ tools developed by the Go community to provide a seamless Vim experience. * Advanced source analysis tools utilizing `guru`, such as |:GoImplements|, |:GoCallees|, and |:GoReferrers|. * Precise type-safe renaming of identifiers with |:GoRename|. - * Integrated and improved snippets, supporting `ultisnips` or `neosnippet`. + * Integrated and improved snippets, supporting `ultisnips`, `neosnippet`, + and `vim-minisnip`. * Share your current code to play.golang.org with |:GoPlay|. * On-the-fly information about the word under the cursor. Plug it into your custom Vim function. @@ -136,7 +137,8 @@ The following plugins are supported for use with vim-go: * Snippets: https://github.com/Shougo/neosnippet.vim or - https://github.com/SirVer/ultisnips + https://github.com/SirVer/ultisnips or + https://github.com/joereynolds/vim-minisnip * For a better documentation viewer check out: https://github.com/garyburd/go-explorer @@ -1278,10 +1280,16 @@ Use this option to change default path for vim-go tools when using < *'g:go_snippet_engine'* -Use this option to define the default snippet engine. By default "ultisnips" -is used. Use "neosnippet" for neosnippet.vim: > +Define the snippet engine to use. The default is to auto-detect one. Valid +values are: - let g:go_snippet_engine = "ultisnips" + automatic Automatically detect a snippet engine. + ultisnips https://github.com/SirVer/ultisnips + neosnippet https://github.com/Shougo/neosnippet.vim + minisnip https://github.com/joereynolds/vim-minisnip + Note: the original at KeyboardFire/vim-minisnip won't work. +> + let g:go_snippet_engine = "automatic" < *'g:go_get_update'* diff --git a/ftplugin/go/snippets.vim b/ftplugin/go/snippets.vim index 56c0811d32..33a3cd5a64 100644 --- a/ftplugin/go/snippets.vim +++ b/ftplugin/go/snippets.vim @@ -3,13 +3,8 @@ if exists("g:go_loaded_gosnippets") endif let g:go_loaded_gosnippets = 1 -" by default UltiSnips -if !exists("g:go_snippet_engine") - let g:go_snippet_engine = "ultisnips" -endif - -function! s:GoUltiSnips() - if globpath(&rtp, 'plugin/UltiSnips.vim') == "" +function! s:GoUltiSnips() abort + if get(g:, 'did_plugin_ultisnips') isnot 1 return endif @@ -20,29 +15,53 @@ function! s:GoUltiSnips() endif endfunction -function! s:GoNeosnippet() - if globpath(&rtp, 'plugin/neosnippet.vim') == "" +function! s:GoNeosnippet() abort + if get(g:, 'loaded_neosnippet') isnot 1 return endif let g:neosnippet#enable_snipmate_compatibility = 1 - let gosnippets_dir = globpath(&rtp, 'gosnippets/snippets') + let l:gosnippets_dir = globpath(&rtp, 'gosnippets/snippets') if type(g:neosnippet#snippets_directory) == type([]) - let g:neosnippet#snippets_directory += [gosnippets_dir] + let g:neosnippet#snippets_directory += [l:gosnippets_dir] elseif type(g:neosnippet#snippets_directory) == type("") if strlen(g:neosnippet#snippets_directory) > 0 - let g:neosnippet#snippets_directory = g:neosnippet#snippets_directory . "," . gosnippets_dir + let g:neosnippet#snippets_directory = g:neosnippet#snippets_directory . "," . l:gosnippets_dir else - let g:neosnippet#snippets_directory = gosnippets_dir + let g:neosnippet#snippets_directory = l:gosnippets_dir endif endif endfunction -if g:go_snippet_engine == "ultisnips" +function! s:GoMinisnip() abort + if get(g:, 'loaded_minisnip') isnot 1 + return + endif + + if exists('g:minisnip_dir') + let g:minisnip_dir .= ':' . globpath(&rtp, 'gosnippets/minisnip') + else + let g:minisnip_dir = globpath(&rtp, 'gosnippets/minisnip') + endif +endfunction + + +let s:engine = get(g:, 'go_snippet_engine', 'automatic') +if s:engine is? "automatic" + if get(g:, 'did_plugin_ultisnips') is 1 + call s:GoUltiSnips() + elseif get(g:, 'loaded_neosnippet') is 1 + call s:GoNeosnippet() + elseif get(g:, 'loaded_minisnip') is 1 + call s:GoMinisnip() + endif +elseif s:engine is? "ultisnips" call s:GoUltiSnips() -elseif g:go_snippet_engine == "neosnippet" +elseif s:engine is? "neosnippet" call s:GoNeosnippet() +elseif s:engine is? "minisnip" + call s:GoMinisnip() endif " vim: sw=2 ts=2 et diff --git a/gosnippets/minisnip/_go_eq b/gosnippets/minisnip/_go_eq new file mode 100644 index 0000000000..9e8fad09cb --- /dev/null +++ b/gosnippets/minisnip/_go_eq @@ -0,0 +1,3 @@ +if !reflect.DeepEqual({{+got+}}, {{+want+}}) { + t.Errorf("\ngot: %#v\nwant: %#v\n", {{+~\~2+}}, {{+~\~2+}}) +} diff --git a/gosnippets/minisnip/_go_err b/gosnippets/minisnip/_go_err new file mode 100644 index 0000000000..3a5a4930e1 --- /dev/null +++ b/gosnippets/minisnip/_go_err @@ -0,0 +1,3 @@ +if err != nil { + return {{+err+}} +} diff --git a/gosnippets/minisnip/_go_errt b/gosnippets/minisnip/_go_errt new file mode 100644 index 0000000000..c5cb6fc34c --- /dev/null +++ b/gosnippets/minisnip/_go_errt @@ -0,0 +1,4 @@ +if err != nil { + t.Fatal(err) +} +{{++}} diff --git a/gosnippets/minisnip/_go_errw b/gosnippets/minisnip/_go_errw new file mode 100644 index 0000000000..d1958d5bc8 --- /dev/null +++ b/gosnippets/minisnip/_go_errw @@ -0,0 +1,3 @@ +if err != nil { + return errors.Wrap(err, "{{++}}") +} diff --git a/gosnippets/minisnip/_go_f b/gosnippets/minisnip/_go_f new file mode 100644 index 0000000000..6e35d2498f --- /dev/null +++ b/gosnippets/minisnip/_go_f @@ -0,0 +1,3 @@ +// {{++}} +func {{+~\~1+}}() { +} diff --git a/gosnippets/minisnip/_go_ff b/gosnippets/minisnip/_go_ff new file mode 100644 index 0000000000..ed942c2268 --- /dev/null +++ b/gosnippets/minisnip/_go_ff @@ -0,0 +1 @@ +fmt.Printf("%#v\n", {{++}}) diff --git a/gosnippets/minisnip/_go_fori b/gosnippets/minisnip/_go_fori new file mode 100644 index 0000000000..8a52ec91bd --- /dev/null +++ b/gosnippets/minisnip/_go_fori @@ -0,0 +1,3 @@ +for i := 0; i < {{++}}; i++ { + {{++}} +} diff --git a/gosnippets/minisnip/_go_pkg b/gosnippets/minisnip/_go_pkg new file mode 100644 index 0000000000..2491d9a992 --- /dev/null +++ b/gosnippets/minisnip/_go_pkg @@ -0,0 +1,2 @@ +// Package {{+~expand('%:p:h:t')+}} {{++}} +package {{+~\~2+}} diff --git a/gosnippets/minisnip/_go_sp b/gosnippets/minisnip/_go_sp new file mode 100644 index 0000000000..b392681f11 --- /dev/null +++ b/gosnippets/minisnip/_go_sp @@ -0,0 +1,2 @@ +fmt.Sprintf("{{++}}", {{++}}) +