From 727dfdb1913b88b485e0456a5162baabddcec26b Mon Sep 17 00:00:00 2001 From: Shoshin Nikita Date: Tue, 1 Feb 2022 17:22:11 +0000 Subject: [PATCH] internal/lsp/source/completion: add conventional acronyms for type names This change adds a list of conventional acronyms that are used for function completion. For example, "err" for "error" and "tx" for "sql.Tx" or "sqlx.Tx". Fixes golang/go#48260 Change-Id: Iff951ee58c0c95389d474cc45dfd84b483ff71e3 GitHub-Last-Rev: 05ccd5a2fe95e2e0504d28230308c4888477a977 GitHub-Pull-Request: golang/tools#363 Reviewed-on: https://go-review.googlesource.com/c/tools/+/381969 Reviewed-by: Robert Findley Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot Trust: Hyang-Ah Hana Kim --- internal/lsp/source/completion/literal.go | 13 +++++++++++++ .../lsp/testdata/snippets/literal_snippets.go.in | 10 ++++++++-- internal/lsp/testdata/summary.txt.golden | 2 +- internal/lsp/testdata/summary_go1.18.txt.golden | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/internal/lsp/source/completion/literal.go b/internal/lsp/source/completion/literal.go index 0fc7a81a13..5025f1f749 100644 --- a/internal/lsp/source/completion/literal.go +++ b/internal/lsp/source/completion/literal.go @@ -294,6 +294,15 @@ func (c *completer) functionLiteral(ctx context.Context, sig *types.Signature, m }) } +// conventionalAcronyms contains conventional acronyms for type names +// in lower case. For example, "ctx" for "context" and "err" for "error". +var conventionalAcronyms = map[string]string{ + "context": "ctx", + "error": "err", + "tx": "tx", + "responsewriter": "w", +} + // abbreviateTypeName abbreviates type names into acronyms. For // example, "fooBar" is abbreviated "fb". Care is taken to ignore // non-identifier runes. For example, "[]int" becomes "i", and @@ -320,6 +329,10 @@ func abbreviateTypeName(s string) string { return !unicode.IsLetter(r) }) + if acr, ok := conventionalAcronyms[strings.ToLower(s)]; ok { + return acr + } + for i, r := range s { // Stop if we encounter a non-identifier rune. if !unicode.IsLetter(r) && !unicode.IsNumber(r) { diff --git a/internal/lsp/testdata/snippets/literal_snippets.go.in b/internal/lsp/testdata/snippets/literal_snippets.go.in index e1585dd81b..4a2a01dfa1 100644 --- a/internal/lsp/testdata/snippets/literal_snippets.go.in +++ b/internal/lsp/testdata/snippets/literal_snippets.go.in @@ -2,6 +2,7 @@ package snippets import ( "bytes" + "context" "go/ast" "net/http" "sort" @@ -137,14 +138,14 @@ func _() { sort.Slice(nil, fun) //@complete(")", litFunc),snippet(")", litFunc, "func(i, j int) bool {$0\\}", "func(i, j int) bool {$0\\}") - http.HandleFunc("", f) //@snippet(")", litFunc, "func(rw http.ResponseWriter, r *http.Request) {$0\\}", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}") + http.HandleFunc("", f) //@snippet(")", litFunc, "func(w http.ResponseWriter, r *http.Request) {$0\\}", "func(${1:w} http.ResponseWriter, ${2:r} *http.Request) {$0\\}") // no literal "func" completions http.Handle("", fun) //@complete(")") http.HandlerFunc() //@item(handlerFunc, "http.HandlerFunc()", "", "var") http.Handle("", h) //@snippet(")", handlerFunc, "http.HandlerFunc($0)", "http.HandlerFunc($0)") - http.Handle("", http.HandlerFunc()) //@snippet("))", litFunc, "func(rw http.ResponseWriter, r *http.Request) {$0\\}", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}") + http.Handle("", http.HandlerFunc()) //@snippet("))", litFunc, "func(w http.ResponseWriter, r *http.Request) {$0\\}", "func(${1:w} http.ResponseWriter, ${2:r} *http.Request) {$0\\}") var namedReturn func(s string) (b bool) namedReturn = f //@snippet(" //", litFunc, "func(s string) (b bool) {$0\\}", "func(s string) (b bool) {$0\\}") @@ -167,6 +168,11 @@ func _() { builtinTypes = f //@snippet(" //", litFunc, "func(i1 []int, b [two]bool, m map[string]string, s struct{ i int \\}, i2 interface{ foo() \\}, c <-chan int) {$0\\}", "func(${1:i1} []int, ${2:b} [two]bool, ${3:m} map[string]string, ${4:s} struct{ i int \\}, ${5:i2} interface{ foo() \\}, ${6:c} <-chan int) {$0\\}") var _ func(ast.Node) = f //@snippet(" //", litFunc, "func(n ast.Node) {$0\\}", "func(${1:n} ast.Node) {$0\\}") + var _ func(error) = f //@snippet(" //", litFunc, "func(err error) {$0\\}", "func(${1:err} error) {$0\\}") + var _ func(context.Context) = f //@snippet(" //", litFunc, "func(ctx context.Context) {$0\\}", "func(${1:ctx} context.Context) {$0\\}") + + type context struct {} + var _ func(context) = f //@snippet(" //", litFunc, "func(ctx context) {$0\\}", "func(${1:ctx} context) {$0\\}") } func _() { diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index fa1bddfe55..bfe40b3a6d 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -2,7 +2,7 @@ CallHierarchyCount = 2 CodeLensCount = 5 CompletionsCount = 265 -CompletionSnippetCount = 103 +CompletionSnippetCount = 106 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8 diff --git a/internal/lsp/testdata/summary_go1.18.txt.golden b/internal/lsp/testdata/summary_go1.18.txt.golden index a7afe2df66..02c674252c 100644 --- a/internal/lsp/testdata/summary_go1.18.txt.golden +++ b/internal/lsp/testdata/summary_go1.18.txt.golden @@ -2,7 +2,7 @@ CallHierarchyCount = 2 CodeLensCount = 5 CompletionsCount = 266 -CompletionSnippetCount = 104 +CompletionSnippetCount = 107 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8