Skip to content

Commit

Permalink
Support minisnip (#1589)
Browse files Browse the repository at this point in the history
Supprt minisnip

Needs joereynolds/vim-minisnip#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.
  • Loading branch information
arp242 authored Dec 7, 2017
1 parent 6366c6e commit 2fe16b9
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
18 changes: 13 additions & 5 deletions doc/vim-go.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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'*

Expand Down
49 changes: 34 additions & 15 deletions ftplugin/go/snippets.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
3 changes: 3 additions & 0 deletions gosnippets/minisnip/_go_eq
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if !reflect.DeepEqual({{+got+}}, {{+want+}}) {
t.Errorf("\ngot: %#v\nwant: %#v\n", {{+~\~2+}}, {{+~\~2+}})
}
3 changes: 3 additions & 0 deletions gosnippets/minisnip/_go_err
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if err != nil {
return {{+err+}}
}
4 changes: 4 additions & 0 deletions gosnippets/minisnip/_go_errt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if err != nil {
t.Fatal(err)
}
{{++}}
3 changes: 3 additions & 0 deletions gosnippets/minisnip/_go_errw
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
if err != nil {
return errors.Wrap(err, "{{++}}")
}
3 changes: 3 additions & 0 deletions gosnippets/minisnip/_go_f
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// {{++}}
func {{+~\~1+}}() {
}
1 change: 1 addition & 0 deletions gosnippets/minisnip/_go_ff
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fmt.Printf("%#v\n", {{++}})
3 changes: 3 additions & 0 deletions gosnippets/minisnip/_go_fori
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for i := 0; i < {{++}}; i++ {
{{++}}
}
2 changes: 2 additions & 0 deletions gosnippets/minisnip/_go_pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package {{+~expand('%:p:h:t')+}} {{++}}
package {{+~\~2+}}
2 changes: 2 additions & 0 deletions gosnippets/minisnip/_go_sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fmt.Sprintf("{{++}}", {{++}})

0 comments on commit 2fe16b9

Please sign in to comment.