mirror of
https://github.com/golang/go
synced 2024-11-18 10:04:43 -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:
parent
a044388aa5
commit
9c4a82ab32
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
@ -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,
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
@ -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[:]))
|
||||
}
|
@ -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
|
||||
}
|
||||
|
4
internal/lsp/testdata/bad/bad1.go
vendored
4
internal/lsp/testdata/bad/bad1.go
vendored
@ -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)
|
||||
}
|
||||
|
2
internal/lsp/testdata/good/good1.go
vendored
2
internal/lsp/testdata/good/good1.go
vendored
@ -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)
|
||||
|
@ -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.
|
||||
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user