1
0
Fork 0
mirror of synced 2024-11-05 00:48:57 -05:00
ultimate-vim/sources_non_forked/vim-go/gosnippets/UltiSnips/go.snippets

402 lines
6.6 KiB
Text
Raw Normal View History

2014-10-31 17:30:24 -04:00
# Snippets for Go
priority -10
2015-01-18 07:58:28 -05:00
# shorthand variable declaration
snippet : "v := value"
${1} := ${0}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# anonymous function
snippet anon "fn := func() { ... }"
${1:fn} := func() {
2015-02-04 05:43:54 -05:00
${2:${VISUAL}}
2015-01-18 07:58:28 -05:00
}
2015-02-04 05:43:54 -05:00
${0}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# append
snippet ap "append(slice, value)"
append(${1:slice}, ${0:value})
endsnippet
2015-02-24 05:45:22 -05:00
# append assignment
snippet ap= "a = append(a, value)"
${1:slice} = append($1, ${0:value})
endsnippet
2015-01-18 07:58:28 -05:00
# break
snippet br "break"
break
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# channel
snippet ch "chan Type"
chan ${0:int}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# case
snippet case "case ...:"
case ${1:value}:
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2015-01-18 07:58:28 -05:00
endsnippet
# constant
snippet con "const XXX Type = ..."
const ${1:NAME} ${2:Type} = ${0:0}
endsnippet
# constants
snippet cons "const ( ... )"
2014-10-31 17:30:24 -04:00
const (
2015-01-18 07:58:28 -05:00
${1:NAME} ${2:Type} = ${3:value}
${0}
2014-10-31 17:30:24 -04:00
)
endsnippet
2015-01-18 07:58:28 -05:00
# constants with iota
snippet iota "const ( ... = iota )"
const (
${1:NAME} ${2:Type} = iota
${0}
)
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# continue
snippet cn "continue"
continue
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# default case
snippet default "default: ..."
default:
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# defer
snippet df "defer someFunction()"
defer ${1:func}(${2})
${0}
endsnippet
snippet def "defer func() { ... }"
defer func() {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2015-01-18 07:58:28 -05:00
}()
endsnippet
# defer recover
snippet defr
defer func() {
if err := recover(); err != nil {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2015-01-18 07:58:28 -05:00
}
}()
endsnippet
# gpl
snippet gpl
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) ${1:Author}, `strftime("%Y")`
*/
${0}
endsnippet
# import
snippet import "import ( ... )"
import (
"${1:package}"
)
endsnippet
# full interface snippet
snippet interface "interface I { ... }"
type ${1:Interface} interface {
${2:/* TODO: add methods */}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# if condition
snippet if "if ... { ... }"
if ${1:condition} {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# else snippet
snippet else
else {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# error snippet
snippet errn "Error return " !b
if err != nil {
return err
2014-10-31 17:30:24 -04:00
}
2015-01-18 07:58:28 -05:00
${0}
2014-10-31 17:30:24 -04:00
endsnippet
2015-02-24 05:45:22 -05:00
# error snippet
snippet errt "Error test fatal " !b
if err != nil {
t.Fatal(err)
}
${0}
endsnippet
2015-01-18 07:58:28 -05:00
snippet errn, "Error return with two return values" !b
if err != nil {
return ${1:nil}, err
2014-10-31 17:30:24 -04:00
}
2015-01-18 07:58:28 -05:00
${0}
2014-10-31 17:30:24 -04:00
endsnippet
2015-03-14 16:02:10 -04:00
snippet errh "Error handle and return" !b
if err != nil {
${1}
return
}
${0}
endsnippet
2015-01-18 07:58:28 -05:00
snippet json "\`json:key\`"
\`json:"${1:keyName}"\`
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# fallthrough
snippet ft "fallthrough"
fallthrough
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# for loop
snippet for "for ... { ... }"
for ${1} {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# for integer loop
snippet fori "for 0..N-1 { ... }"
for ${1:i} := 0; $1 < ${2:N}; $1++ {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# for range loop
snippet forr "for k, v := range items { ... }"
for ${2:k}, ${3:v} := range ${1} {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# function
snippet func "func Function(...) [error] { ... }"
func ${1:name}(${2:params})${3/(.+)/ /}`!p opening_par(snip, 3)`$3`!p closing_par(snip, 3)` {
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# Fmt Printf debug
snippet ff "fmt.Printf(...)"
2015-02-04 05:43:54 -05:00
fmt.Printf("${1:${VISUAL}} = %+v\n", $1)
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# Fmt Println debug
snippet fn "fmt.Println(...)"
2015-03-14 16:02:10 -04:00
fmt.Println("${1:${VISUAL}}")
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# log printf
snippet lf "log.Printf(...)"
2015-03-14 16:02:10 -04:00
log.Printf("${1:${VISUAL}} = %+v\n", $1)
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# log println
snippet ln "log.Println(...)"
2015-03-14 16:02:10 -04:00
log.Println("${1:${VISUAL}}")
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# make
snippet make "make(Type, size)"
make(${1:[]string}, ${2:0})${0}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# map
snippet map "map[Type]Type"
map[${1:string}]${0:int}
endsnippet
# main()
snippet main "func main() { ... }"
func main() {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# method
snippet meth "func (self Type) Method(...) [error] { ... }"
func (${1:receiver} ${2:type}) ${3:name}(${4:params})${5/(.+)/ /}`!p opening_par(snip, 5)`$5`!p closing_par(snip, 5)` {
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# ok
snippet ok "if !ok { ... }"
if !ok {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-01-18 07:58:28 -05:00
# package
snippet package "package ..."
// Package $1 provides ${2:...}
package ${1:main}
2014-10-31 17:30:24 -04:00
${0}
endsnippet
2015-01-18 07:58:28 -05:00
# panic
snippet pn "panic()"
panic("${0:msg}")
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# return
snippet rt "return"
2015-03-14 16:02:10 -04:00
return ${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# select
snippet select "select { case a := <-chan: ... }"
select {
case ${1:v1} := <-${2:chan1}
${0}
}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# struct
snippet st "type T struct { ... }"
type ${1:Type} struct {
${0}
}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# switch
snippet switch "switch x { ... }"
switch ${1:var} {
case ${2:value1}:
${0}
}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# sprintf
snippet sp "fmt.Sprintf(...)"
fmt.Sprintf("%${1:s}", ${2:var})
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# goroutine named function
snippet go "go someFunc(...)"
go ${1:funcName}(${0})
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# goroutine anonymous function
snippet gof "go func() { ... }()"
go func() {
2015-02-04 05:43:54 -05:00
${1:${VISUAL}}
2015-01-18 07:58:28 -05:00
}()
2015-02-04 05:43:54 -05:00
${0}
2014-10-31 17:30:24 -04:00
endsnippet
2015-01-18 07:58:28 -05:00
# test function
snippet test "func TestXYZ(t *testing.T) { ... }"
2014-10-31 17:30:24 -04:00
func Test${1:Function}(t *testing.T) {
2015-03-14 16:02:10 -04:00
${0:${VISUAL}}
2014-10-31 17:30:24 -04:00
}
endsnippet
2015-02-24 05:45:22 -05:00
# quick test server
snippet tsrv "httptest.NewServer"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, ${1:`response`})
}))
defer ts.Close()
${0:someUrl} = ts.URL
endsnippet
# test error handling
snippet ter "if err != nil { t.Errorf(...) }"
if err != nil {
t.Errorf("${0:message}")
}
endsnippet
# test fatal error
snippet terf "if err != nil { t.Fatalf(...) }"
if err != nil {
t.Fatalf("${0:message}")
}
endsnippet
2015-01-18 07:58:28 -05:00
# variable declaration
snippet var "var x Type [= ...]"
2015-03-14 16:02:10 -04:00
var ${1:x} ${2:Type}${3: = ${0:value}}
2015-01-18 07:58:28 -05:00
endsnippet
# variables declaration
snippet vars "var ( ... )"
var (
2015-03-14 16:02:10 -04:00
${1:x} ${2:Type}${3: = ${0:value}}
2015-01-18 07:58:28 -05:00
)
endsnippet
2015-03-14 16:02:10 -04:00
# equals fails the test if exp is not equal to act.
snippet eq "equals: test two identifiers with DeepEqual"
if !reflect.DeepEqual(${1:expected}, ${2:actual}) {
_, file, line, _ := runtime.Caller(0)
fmt.Printf("%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, $1, $2)
t.FailNow()
}
endsnippet
2015-01-18 07:58:28 -05:00
global !p
import re
# Automatically wrap return types with parentheses
def return_values(s):
# remove everything wrapped in parentheses
s = re.sub("\(.*?\)|\([^)]*$", "", s)
return len(s.split(","))
def opening_par(snip, pos):
if return_values(t[pos]) > 1 and not t[pos].startswith("("):
snip.rv = "("
else:
snip.rv = ""
def closing_par(snip, pos):
if return_values(t[pos]) > 1:
snip.rv = ")"
else:
snip.rv = ""
endglobal
2014-10-31 17:30:24 -04:00
# vim:ft=snippets: