From 9c4a82ab3253256da11d25ad221b3c0376dc29f6 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Wed, 25 Sep 2019 00:46:57 -0400 Subject: [PATCH] internal/lsp: remove duplicated enums source.DiagnosticSeverity and source.CompletionItemKind are duplicated and not worth maintaining. Change-Id: I8d6c8621a227855309c0977da59d8c9fa53617bf Reviewed-on: https://go-review.googlesource.com/c/tools/+/197177 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/completion.go | 27 +-------- internal/lsp/diagnostics.go | 25 +++------ internal/lsp/protocol/enums.go | 4 +- internal/lsp/source/completion.go | 18 +----- internal/lsp/source/completion_format.go | 40 ++++--------- internal/lsp/source/completion_literal.go | 7 ++- internal/lsp/source/diagnostics.go | 8 +-- internal/lsp/source/enums.go | 68 ----------------------- internal/lsp/source/util.go | 8 +-- internal/lsp/testdata/bad/bad1.go | 4 +- internal/lsp/testdata/good/good1.go | 2 +- internal/lsp/tests/completion.go | 29 +--------- internal/lsp/tests/tests.go | 6 +- 13 files changed, 43 insertions(+), 203 deletions(-) delete mode 100644 internal/lsp/source/enums.go diff --git a/internal/lsp/completion.go b/internal/lsp/completion.go index c176be47ad..7b64a99392 100644 --- a/internal/lsp/completion.go +++ b/internal/lsp/completion.go @@ -82,7 +82,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol. item := protocol.CompletionItem{ Label: candidate.Label, Detail: candidate.Detail, - Kind: toProtocolCompletionItemKind(candidate.Kind), + Kind: candidate.Kind, TextEdit: &protocol.TextEdit{ NewText: insertText, Range: rng, @@ -110,28 +110,3 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol. } return items } - -func toProtocolCompletionItemKind(kind source.CompletionItemKind) protocol.CompletionItemKind { - switch kind { - case source.InterfaceCompletionItem: - return protocol.InterfaceCompletion - case source.StructCompletionItem: - return protocol.StructCompletion - case source.TypeCompletionItem: - return protocol.TypeParameterCompletion // ?? - case source.ConstantCompletionItem: - return protocol.ConstantCompletion - case source.FieldCompletionItem: - return protocol.FieldCompletion - case source.ParameterCompletionItem, source.VariableCompletionItem: - return protocol.VariableCompletion - case source.FunctionCompletionItem: - return protocol.FunctionCompletion - case source.MethodCompletionItem: - return protocol.MethodCompletion - case source.PackageCompletionItem: - return protocol.ModuleCompletion // ?? - default: - return protocol.TextCompletion - } -} diff --git a/internal/lsp/diagnostics.go b/internal/lsp/diagnostics.go index 7818027b15..4f137b6cce 100644 --- a/internal/lsp/diagnostics.go +++ b/internal/lsp/diagnostics.go @@ -84,24 +84,13 @@ func (s *Server) publishDiagnostics(ctx context.Context, uri span.URI, diagnosti func toProtocolDiagnostics(ctx context.Context, diagnostics []source.Diagnostic) []protocol.Diagnostic { reports := []protocol.Diagnostic{} for _, diag := range diagnostics { - reports = append(reports, toProtocolDiagnostic(ctx, diag)) + reports = append(reports, protocol.Diagnostic{ + Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline + Range: diag.Range, + Severity: diag.Severity, + Source: diag.Source, + Tags: diag.Tags, + }) } return reports } - -func toProtocolDiagnostic(ctx context.Context, diag source.Diagnostic) protocol.Diagnostic { - var severity protocol.DiagnosticSeverity - switch diag.Severity { - case source.SeverityError: - severity = protocol.SeverityError - case source.SeverityWarning: - severity = protocol.SeverityWarning - } - return protocol.Diagnostic{ - Message: strings.TrimSpace(diag.Message), // go list returns errors prefixed by newline - Range: diag.Range, - Severity: severity, - Source: diag.Source, - Tags: diag.Tags, - } -} diff --git a/internal/lsp/protocol/enums.go b/internal/lsp/protocol/enums.go index c2ee277a89..434808eeb1 100644 --- a/internal/lsp/protocol/enums.go +++ b/internal/lsp/protocol/enums.go @@ -61,7 +61,7 @@ func init() { namesCompletionItemKind[int(ConstructorCompletion)] = "constructor" namesCompletionItemKind[int(FieldCompletion)] = "field" namesCompletionItemKind[int(VariableCompletion)] = "var" - namesCompletionItemKind[int(ClassCompletion)] = "class" + namesCompletionItemKind[int(ClassCompletion)] = "type" namesCompletionItemKind[int(InterfaceCompletion)] = "interface" namesCompletionItemKind[int(ModuleCompletion)] = "package" namesCompletionItemKind[int(PropertyCompletion)] = "property" @@ -79,7 +79,7 @@ func init() { namesCompletionItemKind[int(StructCompletion)] = "struct" namesCompletionItemKind[int(EventCompletion)] = "event" namesCompletionItemKind[int(OperatorCompletion)] = "operator" - namesCompletionItemKind[int(TypeParameterCompletion)] = "type" + namesCompletionItemKind[int(TypeParameterCompletion)] = "typeParam" namesInsertTextFormat[int(PlainTextTextFormat)] = "PlainText" namesInsertTextFormat[int(SnippetTextFormat)] = "Snippet" diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index b71318270e..651c0b2b9f 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -36,7 +36,7 @@ type CompletionItem struct { // The insert text does not contain snippets. InsertText string - Kind CompletionItemKind + Kind protocol.CompletionItemKind // An optional array of additional TextEdits that are applied when // selecting this completion. @@ -85,22 +85,6 @@ func (i *CompletionItem) Snippet() string { return i.InsertText } -type CompletionItemKind int - -const ( - Unknown CompletionItemKind = iota - InterfaceCompletionItem - StructCompletionItem - TypeCompletionItem - ConstantCompletionItem - FieldCompletionItem - ParameterCompletionItem - VariableCompletionItem - FunctionCompletionItem - MethodCompletionItem - PackageCompletionItem -) - // Scoring constants are used for weighting the relevance of different candidates. const ( // stdScore is the base score for all completion items. diff --git a/internal/lsp/source/completion_format.go b/internal/lsp/source/completion_format.go index 98a42130e9..9ccb573322 100644 --- a/internal/lsp/source/completion_format.go +++ b/internal/lsp/source/completion_format.go @@ -34,7 +34,7 @@ func (c *completer) item(cand candidate) (CompletionItem, error) { label = cand.name detail = types.TypeString(obj.Type(), c.qf) insert = label - kind CompletionItemKind + kind = protocol.TextCompletion snip *snippet.Builder protocolEdits []protocol.TextEdit ) @@ -52,18 +52,16 @@ func (c *completer) item(cand candidate) (CompletionItem, error) { case *types.TypeName: detail, kind = formatType(obj.Type(), c.qf) case *types.Const: - kind = ConstantCompletionItem + kind = protocol.ConstantCompletion case *types.Var: if _, ok := obj.Type().(*types.Struct); ok { detail = "struct{...}" // for anonymous structs } if obj.IsField() { - kind = FieldCompletionItem + kind = protocol.FieldCompletion snip = c.structFieldSnippet(label, detail) - } else if c.isParameter(obj) { - kind = ParameterCompletionItem } else { - kind = VariableCompletionItem + kind = protocol.VariableCompletion } if sig, ok := obj.Type().Underlying().(*types.Signature); ok && cand.expandFuncCall { @@ -74,16 +72,16 @@ func (c *completer) item(cand candidate) (CompletionItem, error) { if !ok { break } - kind = FunctionCompletionItem + kind = protocol.FunctionCompletion if sig != nil && sig.Recv() != nil { - kind = MethodCompletionItem + kind = protocol.MethodCompletion } if cand.expandFuncCall { expandFuncCall(sig) } case *types.PkgName: - kind = PackageCompletionItem + kind = protocol.ModuleCompletion detail = fmt.Sprintf("%q", obj.Imported().Path()) } @@ -150,20 +148,6 @@ func (c *completer) item(cand candidate) (CompletionItem, error) { return item, nil } -// isParameter returns true if the given *types.Var is a parameter -// of the enclosingFunction. -func (c *completer) isParameter(v *types.Var) bool { - if c.enclosingFunction == nil { - return false - } - for i := 0; i < c.enclosingFunction.Params().Len(); i++ { - if c.enclosingFunction.Params().At(i) == v { - return true - } - } - return false -} - func (c *completer) formatBuiltin(cand candidate) CompletionItem { obj := cand.obj item := CompletionItem{ @@ -173,9 +157,9 @@ func (c *completer) formatBuiltin(cand candidate) CompletionItem { } switch obj.(type) { case *types.Const: - item.Kind = ConstantCompletionItem + item.Kind = protocol.ConstantCompletion case *types.Builtin: - item.Kind = FunctionCompletionItem + item.Kind = protocol.FunctionCompletion builtin := c.view.BuiltinPackage().Lookup(obj.Name()) if obj == nil { break @@ -191,12 +175,12 @@ func (c *completer) formatBuiltin(cand candidate) CompletionItem { item.snippet = c.functionCallSnippet(obj.Name(), params) case *types.TypeName: if types.IsInterface(obj.Type()) { - item.Kind = InterfaceCompletionItem + item.Kind = protocol.InterfaceCompletion } else { - item.Kind = TypeCompletionItem + item.Kind = protocol.ClassCompletion } case *types.Nil: - item.Kind = VariableCompletionItem + item.Kind = protocol.VariableCompletion } return item } diff --git a/internal/lsp/source/completion_literal.go b/internal/lsp/source/completion_literal.go index 9254605013..4769b0fe50 100644 --- a/internal/lsp/source/completion_literal.go +++ b/internal/lsp/source/completion_literal.go @@ -9,6 +9,7 @@ import ( "strings" "unicode" + "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/snippet" ) @@ -216,7 +217,7 @@ func (c *completer) functionLiteral(sig *types.Signature, matchScore float64) { c.items = append(c.items, CompletionItem{ Label: "func(...) {}", Score: matchScore * literalCandidateScore, - Kind: VariableCompletionItem, + Kind: protocol.VariableCompletion, snippet: snip, }) } @@ -263,7 +264,7 @@ func (c *completer) compositeLiteral(T types.Type, typeName string, matchScore f Label: nonSnippet, InsertText: nonSnippet, Score: matchScore * literalCandidateScore, - Kind: VariableCompletionItem, + Kind: protocol.VariableCompletion, snippet: snip, }) } @@ -316,7 +317,7 @@ func (c *completer) makeCall(typeName string, secondArg string, matchScore float Label: nonSnippet.String(), InsertText: nonSnippet.String(), Score: matchScore * literalCandidateScore, - Kind: FunctionCompletionItem, + Kind: protocol.FunctionCompletion, snippet: snip, }) } diff --git a/internal/lsp/source/diagnostics.go b/internal/lsp/source/diagnostics.go index 1278131082..f6a42b74a5 100644 --- a/internal/lsp/source/diagnostics.go +++ b/internal/lsp/source/diagnostics.go @@ -25,7 +25,7 @@ type Diagnostic struct { Range protocol.Range Message string Source string - Severity DiagnosticSeverity + Severity protocol.DiagnosticSeverity Tags []protocol.DiagnosticTag SuggestedFixes []SuggestedFix @@ -113,7 +113,7 @@ func diagnostics(ctx context.Context, view View, pkg Package, reports map[span.U URI: spn.URI(), Message: err.Msg, Source: "LSP", - Severity: SeverityError, + Severity: protocol.SeverityError, } set, ok := diagSets[diag.URI] if !ok { @@ -249,7 +249,7 @@ func toDiagnostic(ctx context.Context, view View, diag analysis.Diagnostic, cate Range: rng, Source: category, Message: diag.Message, - Severity: SeverityWarning, + Severity: protocol.SeverityWarning, SuggestedFixes: fixes, Tags: tags, }, nil @@ -302,7 +302,7 @@ func singleDiagnostic(uri span.URI, format string, a ...interface{}) map[span.UR URI: uri, Range: protocol.Range{}, Message: fmt.Sprintf(format, a...), - Severity: SeverityError, + Severity: protocol.SeverityError, }}, } } diff --git a/internal/lsp/source/enums.go b/internal/lsp/source/enums.go deleted file mode 100644 index d6b033a054..0000000000 --- a/internal/lsp/source/enums.go +++ /dev/null @@ -1,68 +0,0 @@ -// Copyright 2018 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package source - -import ( - "fmt" -) - -var ( - namesDiagnosticSeverity [int(SeverityError) + 1]string - namesCompletionItemKind [int(PackageCompletionItem) + 1]string -) - -func init() { - namesDiagnosticSeverity[SeverityWarning] = "Warning" - namesDiagnosticSeverity[SeverityError] = "Error" - - namesCompletionItemKind[Unknown] = "Unknown" - namesCompletionItemKind[InterfaceCompletionItem] = "interface" - namesCompletionItemKind[StructCompletionItem] = "struct" - namesCompletionItemKind[TypeCompletionItem] = "type" - namesCompletionItemKind[ConstantCompletionItem] = "const" - namesCompletionItemKind[FieldCompletionItem] = "field" - namesCompletionItemKind[ParameterCompletionItem] = "parameter" - namesCompletionItemKind[VariableCompletionItem] = "var" - namesCompletionItemKind[FunctionCompletionItem] = "func" - namesCompletionItemKind[MethodCompletionItem] = "method" - namesCompletionItemKind[PackageCompletionItem] = "package" -} - -func formatEnum(f fmt.State, c rune, i int, names []string, unknown string) { - s := "" - if i >= 0 && i < len(names) { - s = names[i] - } - if s != "" { - fmt.Fprint(f, s) - } else { - fmt.Fprintf(f, "%s(%d)", unknown, i) - } -} - -func parseEnum(s string, names []string) int { - for i, name := range names { - if s == name { - return i - } - } - return 0 -} - -func (e DiagnosticSeverity) Format(f fmt.State, c rune) { - formatEnum(f, c, int(e), namesDiagnosticSeverity[:], "DiagnosticSeverity") -} - -func ParseDiagnosticSeverity(s string) DiagnosticSeverity { - return DiagnosticSeverity(parseEnum(s, namesDiagnosticSeverity[:])) -} - -func (e CompletionItemKind) Format(f fmt.State, c rune) { - formatEnum(f, c, int(e), namesCompletionItemKind[:], "CompletionItemKind") -} - -func ParseCompletionItemKind(s string) CompletionItemKind { - return CompletionItemKind(parseEnum(s, namesCompletionItemKind[:])) -} diff --git a/internal/lsp/source/util.go b/internal/lsp/source/util.go index 586b024b9b..238821f748 100644 --- a/internal/lsp/source/util.go +++ b/internal/lsp/source/util.go @@ -398,18 +398,18 @@ func formatResults(tup *types.Tuple, qf types.Qualifier) ([]string, bool) { } // formatType returns the detail and kind for an object of type *types.TypeName. -func formatType(typ types.Type, qf types.Qualifier) (detail string, kind CompletionItemKind) { +func formatType(typ types.Type, qf types.Qualifier) (detail string, kind protocol.CompletionItemKind) { if types.IsInterface(typ) { detail = "interface{...}" - kind = InterfaceCompletionItem + kind = protocol.InterfaceCompletion } else if _, ok := typ.(*types.Struct); ok { detail = "struct{...}" - kind = StructCompletionItem + kind = protocol.StructCompletion } else if typ != typ.Underlying() { detail, kind = formatType(typ.Underlying(), qf) } else { detail = types.TypeString(typ, qf) - kind = TypeCompletionItem + kind = protocol.ClassCompletion } return detail, kind } diff --git a/internal/lsp/testdata/bad/bad1.go b/internal/lsp/testdata/bad/bad1.go index c71d5557a1..1a9988c41c 100644 --- a/internal/lsp/testdata/bad/bad1.go +++ b/internal/lsp/testdata/bad/bad1.go @@ -13,7 +13,7 @@ func random() int { //@item(random, "random", "func() int", "func") return 0 } -func random2(y int) int { //@item(random2, "random2", "func(y int) int", "func"),item(bad_y_param, "y", "int", "parameter") +func random2(y int) int { //@item(random2, "random2", "func(y int) int", "func"),item(bad_y_param, "y", "int", "var") x := 6 //@item(x, "x", "int", "var"),diag("x", "LSP", "x declared but not used") var q blah //@item(q, "q", "blah", "var"),diag("q", "LSP", "q declared but not used"),diag("blah", "LSP", "undeclared name: blah") var t blob //@item(t, "t", "blob", "var"),diag("t", "LSP", "t declared but not used"),diag("blob", "LSP", "undeclared name: blob") @@ -22,6 +22,6 @@ func random2(y int) int { //@item(random2, "random2", "func(y int) int", "func") return y } -func random3(y ...int) { //@item(random3, "random3", "func(y ...int)", "func"),item(y_variadic_param, "y", "[]int", "parameter") +func random3(y ...int) { //@item(random3, "random3", "func(y ...int)", "func"),item(y_variadic_param, "y", "[]int", "var") //@complete("", y_variadic_param, global_a, bob, random, random2, random3, stuff) } diff --git a/internal/lsp/testdata/good/good1.go b/internal/lsp/testdata/good/good1.go index 306335f694..826b114c34 100644 --- a/internal/lsp/testdata/good/good1.go +++ b/internal/lsp/testdata/good/good1.go @@ -11,7 +11,7 @@ func random() int { //@item(good_random, "random", "func() int", "func") return y //@prepare("return", "","") } -func random2(y int) int { //@item(good_random2, "random2", "func(y int) int", "func"),item(good_y_param, "y", "int", "parameter") +func random2(y int) int { //@item(good_random2, "random2", "func(y int) int", "func"),item(good_y_param, "y", "int", "var") //@complete("", good_y_param, types_import, good_random, good_random2, good_stuff) var b types.Bob = &types.X{} //@prepare("ypes","types", "types") if _, ok := b.(*types.X); ok { //@complete("X", X_struct, Y_struct, Bob_interface) diff --git a/internal/lsp/tests/completion.go b/internal/lsp/tests/completion.go index d96b44d031..867ab09a81 100644 --- a/internal/lsp/tests/completion.go +++ b/internal/lsp/tests/completion.go @@ -21,7 +21,7 @@ func ToProtocolCompletionItems(items []source.CompletionItem) []protocol.Complet func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionItem { return protocol.CompletionItem{ Label: item.Label, - Kind: toProtocolCompletionItemKind(item.Kind), + Kind: item.Kind, Detail: item.Detail, Documentation: item.Documentation, InsertText: item.InsertText, @@ -31,31 +31,6 @@ func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionIte } } -func toProtocolCompletionItemKind(kind source.CompletionItemKind) protocol.CompletionItemKind { - switch kind { - case source.InterfaceCompletionItem: - return protocol.InterfaceCompletion - case source.StructCompletionItem: - return protocol.StructCompletion - case source.TypeCompletionItem: - return protocol.TypeParameterCompletion // ?? - case source.ConstantCompletionItem: - return protocol.ConstantCompletion - case source.FieldCompletionItem: - return protocol.FieldCompletion - case source.ParameterCompletionItem, source.VariableCompletionItem: - return protocol.VariableCompletion - case source.FunctionCompletionItem: - return protocol.FunctionCompletion - case source.MethodCompletionItem: - return protocol.MethodCompletion - case source.PackageCompletionItem: - return protocol.ModuleCompletion // ?? - default: - return protocol.TextCompletion - } -} - func FilterBuiltins(items []protocol.CompletionItem) []protocol.CompletionItem { var got []protocol.CompletionItem for _, item := range items { @@ -68,7 +43,7 @@ func FilterBuiltins(items []protocol.CompletionItem) []protocol.CompletionItem { } func isBuiltin(label, detail string, kind protocol.CompletionItemKind) bool { - if detail == "" && kind == protocol.TypeParameterCompletion { + if detail == "" && kind == protocol.ClassCompletion { return true } // Remaining builtin constants, variables, interfaces, and functions. diff --git a/internal/lsp/tests/tests.go b/internal/lsp/tests/tests.go index cc134f43bd..21241ac415 100644 --- a/internal/lsp/tests/tests.go +++ b/internal/lsp/tests/tests.go @@ -590,9 +590,9 @@ func (data *Data) collectDiagnostics(spn span.Span, msgSource, msg string) { if _, ok := data.Diagnostics[spn.URI()]; !ok { data.Diagnostics[spn.URI()] = []source.Diagnostic{} } - severity := source.SeverityError + severity := protocol.SeverityError if strings.Contains(string(spn.URI()), "analyzer") { - severity = source.SeverityWarning + severity = protocol.SeverityWarning } // This is not the correct way to do this, // but it seems excessive to do the full conversion here. @@ -657,7 +657,7 @@ func (data *Data) collectCompletionItems(pos token.Pos, args []string) { data.CompletionItems[pos] = &source.CompletionItem{ Label: label, Detail: detail, - Kind: source.ParseCompletionItemKind(kind), + Kind: protocol.ParseCompletionItemKind(kind), Documentation: documentation, } }