Bring in more snippets

pull/165/head
ray-x 2 years ago
parent 876bf4998e
commit c0617af8d6

@ -29,6 +29,8 @@ The plugin covers most features required for a gopher.
- GoCheat get go cheatsheet from [cheat.sh](https://cheat.sh/).
- Smart build tag detection when debug/run tests (e.g. `//go:build integration`)
- Generate mocks with mockgen
- luasnip: you may have friendly-snippets installed, you still need to try pure lua snippets with go.nvim out,
checkout [LuaSnip Tutorial](https://www.youtube.com/watch?v=ub0REXjhpmk) and [TJ's Introduction to LuaSnip](https://www.youtube.com/watch?v=Dn800rlPIho)
## Installation
@ -607,7 +609,8 @@ require('go').setup({
-- float term recommand if you use richgo/ginkgo with terminal color
test_efm = false, -- errorfomat for quickfix, default mix mode, set to true will be efm only
luasnip = false, -- enable included luasnip
luasnip = false, -- enable included luasnip. you can also disable while add lua/snips folder to luasnip load
-- Do not enable this if you already added the path, that will duplicate the entries
})
```

@ -6,6 +6,7 @@ local rep = require("luasnip.extras").rep
local ai = require("luasnip.nodes.absolute_indexer")
local partial = require("luasnip.extras").partial
local snips = require("go.snips")
local log = require("go.utils").log
local in_test_fn = {
show_condition = snips.in_test_function,
condition = snips.in_test_function,
@ -25,6 +26,7 @@ local not_in_fn = {
show_condition = snips.in_func,
condition = snips.in_func,
}
-- stylua: ignore start
local snippets = {
-- Main
@ -36,7 +38,7 @@ local snippets = {
-- If call error
ls.s(
{ trig = "ifcall", name = "IF CALL", dscr = "Call a function and check the error" },
{ trig = "ifc", name = "if call", dscr = "Call a function and check the error" },
fmt(
[[
{val}, {err1} := {func}({args})
@ -47,7 +49,7 @@ local snippets = {
]], {
val = ls.i(1, { "val" }),
err1 = ls.i(2, { "err" }),
func = ls.i(3, { "Func" }),
func = ls.i(3, { "func" }),
args = ls.i(4),
err2 = rep(2),
err3 = ls.d(5, snips.make_return_nodes, { 2 }),
@ -56,6 +58,26 @@ local snippets = {
snips.in_fn
),
-- if err:=call(); err != nil { return err }
ls.s(
{ trig = "ifce", name = "if call err inline", dscr = "Call a function and check the error inline" },
fmt(
[[
if {err1} := {func}({args}); {err2} != nil {{
return {err3}
}}
{finally}
]], {
err1 = ls.i(1, { "err" }),
func = ls.i(2, { "func" }),
args = ls.i(3, { "args" }),
err2 = rep(1),
err3 = ls.d(4, snips.make_return_nodes, { 1 }),
finally = ls.i(0),
}),
snips.in_fn
),
-- Function
ls.s(
{ trig = "fn", name = "Function", dscr = "Create a function or a method" },
@ -100,12 +122,101 @@ local snippets = {
in_fn
),
-- errors.Wrap
ls.s(
{ trig = "erw", dscr = "errors.Wrap" },
fmt([[errors.Wrap({}, "{}")]], {
ls.i(1, "err"),
ls.i(2, "failed to"),
})
),
-- errors.Wrapf
ls.s(
{ trig = "erwf", dscr = "errors.Wrapf" },
fmt([[errors.Wrapf({}, "{}", {})]], {
ls.i(1, "err"),
ls.i(2, "failed %v"),
ls.i(3, "args"),
})
),
-- for select
ls.s(
{ trig = "fsel", dscr = "for select" },
fmt([[
for {{
select {{
case {} <- {}:
{}
default:
{}
}}
}}
]], {
ls.i(1, {ls.i(1, "ch"), ls.i(2, "ch := ")}),
ls.i(2, "ch"),
ls.i(3, "break"),
ls.i(0, ""),
})
),
-- type switch
ls.s(
{ trig = "tysw", dscr = "type switch" },
fmt([[
switch {} := {}.(type) {{
case {}:
{}
default:
{}
}}
]], {
ls.i(1, "v"),
ls.i(2, "i"),
ls.i(3, "int"),
ls.i(4, 'fmt.Println("int")'),
ls.i(0, ""),
})
),
-- fmt.Sprintf
ls.s(
{ trig = "spn", dscr = "fmt.Sprintf" },
fmt([[fmt.Sprintf("{}%{}", {})]], {
ls.i(1, "msg "),
ls.i(2, "+v"),
ls.i(2, "arg"),
})
),
-- build tags
ls.s(
{ trig = "//tag", dscr = "// +build tags" },
fmt([[// +build:{}{}]], {
ls.i(1, "integration"),
ls.i(2, ",unit"),
})
),
-- Nolint
ls.s(
{ trig = "nolint", dscr = "ignore linter" },
fmt([[// nolint:{} // {}]], {
ls.i(1, "names"),
ls.i(2, "explaination"),
{ trig = "nolt", dscr = "ignore linter" },
fmt([[// nolint:{}{}]], {
ls.i(1, "funlen"),
ls.i(2, ",cyclop"),
})
),
-- defer recover
ls.s(
{ trig = "dfr", dscr = "defer recover" },
fmt(
[[
defer func() {{
if err := recover(); err != nil {{
{}
}}
}}()]], {
ls.i(1, ""),
})
),
@ -174,6 +285,15 @@ local snippets = {
in_test_fn
),
-- Assert equal
ls.s(
{ trig = "aeq", name = "Assert Equal", dscr = "Add assert.Equal" },
ls.c(1, {
ls.sn(nil, fmt('assert.Equal(t, {}, {})', { ls.i(1, "got"), ls.i(2, "want") })),
ls.sn(nil, fmt('assert.Equalf(t, {}, {}, "{}", {})', { ls.i(1, "got"), ls.i(2, "want"), ls.i(3, "got %v not equal to want"), ls.i(4, "got") })),
}),
in_test_fn
),
-- Subtests
ls.s(
{ trig = "Test", name = "Test/Subtest", dscr = "Create subtests and their function stubs" },
@ -193,9 +313,9 @@ local snippets = {
partial(vim.fn.expand, "%:t:r"),
})
),
-- }}}
}
log('create snippet')
ls.add_snippets("go", snippets)
-- stylua: ignore end

@ -1,14 +0,0 @@
let s:cpo_save = &cpo
set cpo&vim
" CompilerSet errorformat =%-G#\ %.%# " Ignore lines beginning with '#' ('# command-line-arguments' line sometimes appears?)
" CompilerSet errorformat+=%-G%.%#panic:\ %m " Ignore lines containing 'panic: message'
" CompilerSet errorformat+=%Ecan\'t\ load\ package:\ %m " Start of multiline error string is 'can\'t load package'
" CompilerSet errorformat+=%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:%c:\ %m " Start of multiline unspecified string is 'filename:linenumber:columnnumber:'
" CompilerSet errorformat+=%A%\\%%(%[%^:]%\\+:\ %\\)%\\?%f:%l:\ %m " Start of multiline unspecified string is 'filename:linenumber:'
" CompilerSet errorformat+=%C%*\\s%m " Continuation of multiline error message is indented
" CompilerSet errorformat+=%-G%.%# " All lines not matching any of the above patterns are ignored
"
let &cpo = s:cpo_save
unlet s:cpo_save
Loading…
Cancel
Save