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 <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
diff --git a/internal/lsp/completion.go b/internal/lsp/completion.go
index c176be4..7b64a99 100644
--- a/internal/lsp/completion.go
+++ b/internal/lsp/completion.go
@@ -82,7 +82,7 @@
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 @@
}
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 7818027..4f137b6 100644
--- a/internal/lsp/diagnostics.go
+++ b/internal/lsp/diagnostics.go
@@ -84,24 +84,13 @@
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 c2ee277..434808e 100644
--- a/internal/lsp/protocol/enums.go
+++ b/internal/lsp/protocol/enums.go
@@ -61,7 +61,7 @@
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 @@
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 b713182..651c0b2 100644
--- a/internal/lsp/source/completion.go
+++ b/internal/lsp/source/completion.go
@@ -36,7 +36,7 @@
// 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 @@
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 98a4213..9ccb573 100644
--- a/internal/lsp/source/completion_format.go
+++ b/internal/lsp/source/completion_format.go
@@ -34,7 +34,7 @@
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 @@
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 @@
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 @@
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 @@
}
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 @@
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 9254605..4769b0f 100644
--- a/internal/lsp/source/completion_literal.go
+++ b/internal/lsp/source/completion_literal.go
@@ -9,6 +9,7 @@
"strings"
"unicode"
+ "golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/snippet"
)
@@ -216,7 +217,7 @@
c.items = append(c.items, CompletionItem{
Label: "func(...) {}",
Score: matchScore * literalCandidateScore,
- Kind: VariableCompletionItem,
+ Kind: protocol.VariableCompletion,
snippet: snip,
})
}
@@ -263,7 +264,7 @@
Label: nonSnippet,
InsertText: nonSnippet,
Score: matchScore * literalCandidateScore,
- Kind: VariableCompletionItem,
+ Kind: protocol.VariableCompletion,
snippet: snip,
})
}
@@ -316,7 +317,7 @@
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 1278131..f6a42b7 100644
--- a/internal/lsp/source/diagnostics.go
+++ b/internal/lsp/source/diagnostics.go
@@ -25,7 +25,7 @@
Range protocol.Range
Message string
Source string
- Severity DiagnosticSeverity
+ Severity protocol.DiagnosticSeverity
Tags []protocol.DiagnosticTag
SuggestedFixes []SuggestedFix
@@ -113,7 +113,7 @@
URI: spn.URI(),
Message: err.Msg,
Source: "LSP",
- Severity: SeverityError,
+ Severity: protocol.SeverityError,
}
set, ok := diagSets[diag.URI]
if !ok {
@@ -249,7 +249,7 @@
Range: rng,
Source: category,
Message: diag.Message,
- Severity: SeverityWarning,
+ Severity: protocol.SeverityWarning,
SuggestedFixes: fixes,
Tags: tags,
}, nil
@@ -302,7 +302,7 @@
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 d6b033a..0000000
--- 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 586b024..238821f 100644
--- a/internal/lsp/source/util.go
+++ b/internal/lsp/source/util.go
@@ -398,18 +398,18 @@
}
// 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 c71d555..1a9988c 100644
--- a/internal/lsp/testdata/bad/bad1.go
+++ b/internal/lsp/testdata/bad/bad1.go
@@ -13,7 +13,7 @@
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 @@
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 306335f..826b114 100644
--- a/internal/lsp/testdata/good/good1.go
+++ b/internal/lsp/testdata/good/good1.go
@@ -11,7 +11,7 @@
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 d96b44d..867ab09 100644
--- a/internal/lsp/tests/completion.go
+++ b/internal/lsp/tests/completion.go
@@ -21,7 +21,7 @@
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 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 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 cc134f4..21241ac 100644
--- a/internal/lsp/tests/tests.go
+++ b/internal/lsp/tests/tests.go
@@ -590,9 +590,9 @@
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 @@
data.CompletionItems[pos] = &source.CompletionItem{
Label: label,
Detail: detail,
- Kind: source.ParseCompletionItemKind(kind),
+ Kind: protocol.ParseCompletionItemKind(kind),
Documentation: documentation,
}
}