Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go.json: review and update snippets #160

Open
stamblerre opened this issue Jun 3, 2020 · 1 comment
Open

go.json: review and update snippets #160

stamblerre opened this issue Jun 3, 2020 · 1 comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@stamblerre
Copy link
Contributor

See microsoft/vscode-go#2380 for the original discussion.

@stamblerre stamblerre changed the title snippets: review and update snippets go.json: review and update snippets Jun 3, 2020
@stamblerre stamblerre added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Jun 3, 2020
@hyangah
Copy link
Contributor

hyangah commented Sep 14, 2020

Facts to consider (from microsoft/vscode-go#2380 (comment))

  • Due to the current limitation in VS Code's snippets, it's hard to selectively disable snippets included in an extension.
  • The snippets may interfere with the language servers' auto-completion features. Be conservative in accepting any new snippet contribution.
  • There is clear overlap with some autocompletion features language servers can provide. So, if gopls can provide a better option, do not add the snippet.
  • Encourage to use personal snippets https://code.visualstudio.com/docs/editor/userdefinedsnippets#_create-your-own-snippets instead of adding to the extension's snippet unless the snippet promotes commonly used idiomatic pattern.

Existing snippets

Μaybe

  • single import: im -> import "package"
  • multiple imports: ims -> import ( "package" )
  • single constant: co -> const name = value
  • multiple constants: cos -> const ( name = value )
  • type interface declaration: tyi -> type name interface { }
  • type struct declaration: tys -> type name struct { }
  • package main and main function: pkgm -> package main\n\nfunc main() { }
  • function declaration: func -> func $1($2) $3 { $0 }
  • variable declaration: var -> var $1 $2
  • switch statement: switch -> switch $1 { case $2: }
  • select statement: sel -> select { case $1: }
  • case clause: cs -> case $1:
  • for statement: for -> for i := 0; i < count; i++ { }
  • for range statement: forr -> for _, var := range var { }
  • channel declaration: ch -> chan type
  • map declaration: map -> map[type]type
  • empty interface: in -> interface{}
  • if statement: if -> if condition { }
  • else branch: el -> else { }
  • if else statement: ie -> if condition { } else { }
  • if err != nil: iferr -> if err != nil { return nil, err }
  • defer statement: df -> defer $1($0)
  • goroutine anonymous function: go -> go func() { }( ) (Snippets: GoRoutine Function removal microsoft/vscode-go#2717 proposes to remove)
  • goroutine function: gf -> go $1($0) (Snippets: GoRoutine Function removal microsoft/vscode-go#2717 proposes to remove)

Can gopls's completion do a reasonable job if not better?

  • fmt.Println: fp -> fmt.Println("$1")
  • fmt.Printf: ff -> fmt.Printf("$1", $2)
  • log.Println: lp -> log.Println("$1")
  • log.Printf: lf -> log.Printf("$1", $2)
  • log variable content: lv -> log.Printf("$1: %#+v\n", $1)
  • t.Logf: tl -> t.Log("$1")
  • t.Logf: tlf -> t.Logf("$1", $2)
  • t.Logf variable content: tlv -> t.Logf("$1: %#+v\n", $1)
  • make(...): make -> make($1, $2)
  • new(...): new -> new($1)
  • panic(...): pn -> panic("")
  • method declaration: meth ->func (${1:receiver} ${2:type}) ${3:method}($4) $5 {\n\t$0\n}

HTTP specific shortcut keys, maybe reject?

  • http ResponseWriter *Request: wr -> w http.ResponseWriter, r *http.Request,
  • http.HandleFunc: hf -> http.HandlerFunc("/", handler)
  • http handler declaration: hand -> func (w http.ResponseWriter, r *http.Request) { }
  • http.Redirect: rd -> http.Redirect(w, r, "/", https.StatusFound)
  • http.Error: herr -> http.Error(w, err.Error(), http.StatusInternalServerError)
  • http.ListenAndServe: las ->http.ListenAdnServe(":8080", nil)
  • http.Serve: sv -> http.Serve(":8080", nil)

Test specific templates, maybe?

  • test function: tf -> func Test$1(t *testing.T) {}"
  • benchmark function: bf -> func Benchmark$1(b *testing.B) { for i := 0; i < b.N; i++ { } }
  • example function: ef -> func Example$1() {\n\t$2\n\t//Output:\n\t$3\n}
  • table driven test: tdt -> func Test$1(t *testing.T) {\n\ttestCases := []struct {\n\t\tdesc\tstring\n\t\t$2\n\t}{\n\t\t{\n\t\t\tdesc: \"$3\",\n\t\t\t$4\n\t\t},\n\t}\n\tfor _, tC := range testCases {\n\t\tt.Run(tC.desc, func(t *testing.T) {\n\t\t\t$0\n\t\t})\n\t}\n}
  • init function: finit -> func init() { }
  • main function: fmain -> func main() { }

To delete

  • hello world web app: helloweb ->package main\n\nimport (\n\t\"fmt\"\n\t\"net/http\"\n\t\"time\"\n)\n\nfunc greet(w http.ResponseWriter, r *http.Request) {\n\tfmt.Fprintf(w, \"Hello World! %s\", time.Now())\n}\n\nfunc main() {\n\thttp.HandleFunc(\"/\", greet)\n\thttp.ListenAndServe(\":8080\", nil)\n}
  • sort implementation: sort -> type ${1:SortBy} []${2:Type}\n\nfunc (a $1) Len() int { return len(a) }\nfunc (a $1) Swap(i, j int) { a[i], a[j] = a[j], a[i] }\nfunc (a $1) Less(i, j int) bool { ${3:return a[i] < a[j]} }
    : recent versions of Go have better APIs than this (sort.Sort, ...)

New things to consider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

3 participants