1
0
mirror of https://github.com/golang/go synced 2024-11-18 11:04:42 -07:00

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>
This commit is contained in:
Rebecca Stambler 2019-09-25 00:46:57 -04:00
parent a044388aa5
commit 9c4a82ab32
13 changed files with 43 additions and 203 deletions

View File

@ -82,7 +82,7 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
item := protocol.CompletionItem{ item := protocol.CompletionItem{
Label: candidate.Label, Label: candidate.Label,
Detail: candidate.Detail, Detail: candidate.Detail,
Kind: toProtocolCompletionItemKind(candidate.Kind), Kind: candidate.Kind,
TextEdit: &protocol.TextEdit{ TextEdit: &protocol.TextEdit{
NewText: insertText, NewText: insertText,
Range: rng, Range: rng,
@ -110,28 +110,3 @@ func toProtocolCompletionItems(candidates []source.CompletionItem, rng protocol.
} }
return items 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
}
}

View File

@ -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 { func toProtocolDiagnostics(ctx context.Context, diagnostics []source.Diagnostic) []protocol.Diagnostic {
reports := []protocol.Diagnostic{} reports := []protocol.Diagnostic{}
for _, diag := range diagnostics { 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 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,
}
}

View File

@ -61,7 +61,7 @@ func init() {
namesCompletionItemKind[int(ConstructorCompletion)] = "constructor" namesCompletionItemKind[int(ConstructorCompletion)] = "constructor"
namesCompletionItemKind[int(FieldCompletion)] = "field" namesCompletionItemKind[int(FieldCompletion)] = "field"
namesCompletionItemKind[int(VariableCompletion)] = "var" namesCompletionItemKind[int(VariableCompletion)] = "var"
namesCompletionItemKind[int(ClassCompletion)] = "class" namesCompletionItemKind[int(ClassCompletion)] = "type"
namesCompletionItemKind[int(InterfaceCompletion)] = "interface" namesCompletionItemKind[int(InterfaceCompletion)] = "interface"
namesCompletionItemKind[int(ModuleCompletion)] = "package" namesCompletionItemKind[int(ModuleCompletion)] = "package"
namesCompletionItemKind[int(PropertyCompletion)] = "property" namesCompletionItemKind[int(PropertyCompletion)] = "property"
@ -79,7 +79,7 @@ func init() {
namesCompletionItemKind[int(StructCompletion)] = "struct" namesCompletionItemKind[int(StructCompletion)] = "struct"
namesCompletionItemKind[int(EventCompletion)] = "event" namesCompletionItemKind[int(EventCompletion)] = "event"
namesCompletionItemKind[int(OperatorCompletion)] = "operator" namesCompletionItemKind[int(OperatorCompletion)] = "operator"
namesCompletionItemKind[int(TypeParameterCompletion)] = "type" namesCompletionItemKind[int(TypeParameterCompletion)] = "typeParam"
namesInsertTextFormat[int(PlainTextTextFormat)] = "PlainText" namesInsertTextFormat[int(PlainTextTextFormat)] = "PlainText"
namesInsertTextFormat[int(SnippetTextFormat)] = "Snippet" namesInsertTextFormat[int(SnippetTextFormat)] = "Snippet"

View File

@ -36,7 +36,7 @@ type CompletionItem struct {
// The insert text does not contain snippets. // The insert text does not contain snippets.
InsertText string InsertText string
Kind CompletionItemKind Kind protocol.CompletionItemKind
// An optional array of additional TextEdits that are applied when // An optional array of additional TextEdits that are applied when
// selecting this completion. // selecting this completion.
@ -85,22 +85,6 @@ func (i *CompletionItem) Snippet() string {
return i.InsertText 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. // Scoring constants are used for weighting the relevance of different candidates.
const ( const (
// stdScore is the base score for all completion items. // stdScore is the base score for all completion items.

View File

@ -34,7 +34,7 @@ func (c *completer) item(cand candidate) (CompletionItem, error) {
label = cand.name label = cand.name
detail = types.TypeString(obj.Type(), c.qf) detail = types.TypeString(obj.Type(), c.qf)
insert = label insert = label
kind CompletionItemKind kind = protocol.TextCompletion
snip *snippet.Builder snip *snippet.Builder
protocolEdits []protocol.TextEdit protocolEdits []protocol.TextEdit
) )
@ -52,18 +52,16 @@ func (c *completer) item(cand candidate) (CompletionItem, error) {
case *types.TypeName: case *types.TypeName:
detail, kind = formatType(obj.Type(), c.qf) detail, kind = formatType(obj.Type(), c.qf)
case *types.Const: case *types.Const:
kind = ConstantCompletionItem kind = protocol.ConstantCompletion
case *types.Var: case *types.Var:
if _, ok := obj.Type().(*types.Struct); ok { if _, ok := obj.Type().(*types.Struct); ok {
detail = "struct{...}" // for anonymous structs detail = "struct{...}" // for anonymous structs
} }
if obj.IsField() { if obj.IsField() {
kind = FieldCompletionItem kind = protocol.FieldCompletion
snip = c.structFieldSnippet(label, detail) snip = c.structFieldSnippet(label, detail)
} else if c.isParameter(obj) {
kind = ParameterCompletionItem
} else { } else {
kind = VariableCompletionItem kind = protocol.VariableCompletion
} }
if sig, ok := obj.Type().Underlying().(*types.Signature); ok && cand.expandFuncCall { 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 { if !ok {
break break
} }
kind = FunctionCompletionItem kind = protocol.FunctionCompletion
if sig != nil && sig.Recv() != nil { if sig != nil && sig.Recv() != nil {
kind = MethodCompletionItem kind = protocol.MethodCompletion
} }
if cand.expandFuncCall { if cand.expandFuncCall {
expandFuncCall(sig) expandFuncCall(sig)
} }
case *types.PkgName: case *types.PkgName:
kind = PackageCompletionItem kind = protocol.ModuleCompletion
detail = fmt.Sprintf("%q", obj.Imported().Path()) detail = fmt.Sprintf("%q", obj.Imported().Path())
} }
@ -150,20 +148,6 @@ func (c *completer) item(cand candidate) (CompletionItem, error) {
return item, nil 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 { func (c *completer) formatBuiltin(cand candidate) CompletionItem {
obj := cand.obj obj := cand.obj
item := CompletionItem{ item := CompletionItem{
@ -173,9 +157,9 @@ func (c *completer) formatBuiltin(cand candidate) CompletionItem {
} }
switch obj.(type) { switch obj.(type) {
case *types.Const: case *types.Const:
item.Kind = ConstantCompletionItem item.Kind = protocol.ConstantCompletion
case *types.Builtin: case *types.Builtin:
item.Kind = FunctionCompletionItem item.Kind = protocol.FunctionCompletion
builtin := c.view.BuiltinPackage().Lookup(obj.Name()) builtin := c.view.BuiltinPackage().Lookup(obj.Name())
if obj == nil { if obj == nil {
break break
@ -191,12 +175,12 @@ func (c *completer) formatBuiltin(cand candidate) CompletionItem {
item.snippet = c.functionCallSnippet(obj.Name(), params) item.snippet = c.functionCallSnippet(obj.Name(), params)
case *types.TypeName: case *types.TypeName:
if types.IsInterface(obj.Type()) { if types.IsInterface(obj.Type()) {
item.Kind = InterfaceCompletionItem item.Kind = protocol.InterfaceCompletion
} else { } else {
item.Kind = TypeCompletionItem item.Kind = protocol.ClassCompletion
} }
case *types.Nil: case *types.Nil:
item.Kind = VariableCompletionItem item.Kind = protocol.VariableCompletion
} }
return item return item
} }

View File

@ -9,6 +9,7 @@ import (
"strings" "strings"
"unicode" "unicode"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/snippet" "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{ c.items = append(c.items, CompletionItem{
Label: "func(...) {}", Label: "func(...) {}",
Score: matchScore * literalCandidateScore, Score: matchScore * literalCandidateScore,
Kind: VariableCompletionItem, Kind: protocol.VariableCompletion,
snippet: snip, snippet: snip,
}) })
} }
@ -263,7 +264,7 @@ func (c *completer) compositeLiteral(T types.Type, typeName string, matchScore f
Label: nonSnippet, Label: nonSnippet,
InsertText: nonSnippet, InsertText: nonSnippet,
Score: matchScore * literalCandidateScore, Score: matchScore * literalCandidateScore,
Kind: VariableCompletionItem, Kind: protocol.VariableCompletion,
snippet: snip, snippet: snip,
}) })
} }
@ -316,7 +317,7 @@ func (c *completer) makeCall(typeName string, secondArg string, matchScore float
Label: nonSnippet.String(), Label: nonSnippet.String(),
InsertText: nonSnippet.String(), InsertText: nonSnippet.String(),
Score: matchScore * literalCandidateScore, Score: matchScore * literalCandidateScore,
Kind: FunctionCompletionItem, Kind: protocol.FunctionCompletion,
snippet: snip, snippet: snip,
}) })
} }

View File

@ -25,7 +25,7 @@ type Diagnostic struct {
Range protocol.Range Range protocol.Range
Message string Message string
Source string Source string
Severity DiagnosticSeverity Severity protocol.DiagnosticSeverity
Tags []protocol.DiagnosticTag Tags []protocol.DiagnosticTag
SuggestedFixes []SuggestedFix SuggestedFixes []SuggestedFix
@ -113,7 +113,7 @@ func diagnostics(ctx context.Context, view View, pkg Package, reports map[span.U
URI: spn.URI(), URI: spn.URI(),
Message: err.Msg, Message: err.Msg,
Source: "LSP", Source: "LSP",
Severity: SeverityError, Severity: protocol.SeverityError,
} }
set, ok := diagSets[diag.URI] set, ok := diagSets[diag.URI]
if !ok { if !ok {
@ -249,7 +249,7 @@ func toDiagnostic(ctx context.Context, view View, diag analysis.Diagnostic, cate
Range: rng, Range: rng,
Source: category, Source: category,
Message: diag.Message, Message: diag.Message,
Severity: SeverityWarning, Severity: protocol.SeverityWarning,
SuggestedFixes: fixes, SuggestedFixes: fixes,
Tags: tags, Tags: tags,
}, nil }, nil
@ -302,7 +302,7 @@ func singleDiagnostic(uri span.URI, format string, a ...interface{}) map[span.UR
URI: uri, URI: uri,
Range: protocol.Range{}, Range: protocol.Range{},
Message: fmt.Sprintf(format, a...), Message: fmt.Sprintf(format, a...),
Severity: SeverityError, Severity: protocol.SeverityError,
}}, }},
} }
} }

View File

@ -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[:]))
}

View File

@ -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. // 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) { if types.IsInterface(typ) {
detail = "interface{...}" detail = "interface{...}"
kind = InterfaceCompletionItem kind = protocol.InterfaceCompletion
} else if _, ok := typ.(*types.Struct); ok { } else if _, ok := typ.(*types.Struct); ok {
detail = "struct{...}" detail = "struct{...}"
kind = StructCompletionItem kind = protocol.StructCompletion
} else if typ != typ.Underlying() { } else if typ != typ.Underlying() {
detail, kind = formatType(typ.Underlying(), qf) detail, kind = formatType(typ.Underlying(), qf)
} else { } else {
detail = types.TypeString(typ, qf) detail = types.TypeString(typ, qf)
kind = TypeCompletionItem kind = protocol.ClassCompletion
} }
return detail, kind return detail, kind
} }

View File

@ -13,7 +13,7 @@ func random() int { //@item(random, "random", "func() int", "func")
return 0 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") 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 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") 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 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) //@complete("", y_variadic_param, global_a, bob, random, random2, random3, stuff)
} }

View File

@ -11,7 +11,7 @@ func random() int { //@item(good_random, "random", "func() int", "func")
return y //@prepare("return", "","") 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) //@complete("", good_y_param, types_import, good_random, good_random2, good_stuff)
var b types.Bob = &types.X{} //@prepare("ypes","types", "types") var b types.Bob = &types.X{} //@prepare("ypes","types", "types")
if _, ok := b.(*types.X); ok { //@complete("X", X_struct, Y_struct, Bob_interface) if _, ok := b.(*types.X); ok { //@complete("X", X_struct, Y_struct, Bob_interface)

View File

@ -21,7 +21,7 @@ func ToProtocolCompletionItems(items []source.CompletionItem) []protocol.Complet
func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionItem { func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionItem {
return protocol.CompletionItem{ return protocol.CompletionItem{
Label: item.Label, Label: item.Label,
Kind: toProtocolCompletionItemKind(item.Kind), Kind: item.Kind,
Detail: item.Detail, Detail: item.Detail,
Documentation: item.Documentation, Documentation: item.Documentation,
InsertText: item.InsertText, 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 { func FilterBuiltins(items []protocol.CompletionItem) []protocol.CompletionItem {
var got []protocol.CompletionItem var got []protocol.CompletionItem
for _, item := range items { for _, item := range items {
@ -68,7 +43,7 @@ func FilterBuiltins(items []protocol.CompletionItem) []protocol.CompletionItem {
} }
func isBuiltin(label, detail string, kind protocol.CompletionItemKind) bool { func isBuiltin(label, detail string, kind protocol.CompletionItemKind) bool {
if detail == "" && kind == protocol.TypeParameterCompletion { if detail == "" && kind == protocol.ClassCompletion {
return true return true
} }
// Remaining builtin constants, variables, interfaces, and functions. // Remaining builtin constants, variables, interfaces, and functions.

View File

@ -590,9 +590,9 @@ func (data *Data) collectDiagnostics(spn span.Span, msgSource, msg string) {
if _, ok := data.Diagnostics[spn.URI()]; !ok { if _, ok := data.Diagnostics[spn.URI()]; !ok {
data.Diagnostics[spn.URI()] = []source.Diagnostic{} data.Diagnostics[spn.URI()] = []source.Diagnostic{}
} }
severity := source.SeverityError severity := protocol.SeverityError
if strings.Contains(string(spn.URI()), "analyzer") { if strings.Contains(string(spn.URI()), "analyzer") {
severity = source.SeverityWarning severity = protocol.SeverityWarning
} }
// This is not the correct way to do this, // This is not the correct way to do this,
// but it seems excessive to do the full conversion here. // 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{ data.CompletionItems[pos] = &source.CompletionItem{
Label: label, Label: label,
Detail: detail, Detail: detail,
Kind: source.ParseCompletionItemKind(kind), Kind: protocol.ParseCompletionItemKind(kind),
Documentation: documentation, Documentation: documentation,
} }
} }