1
0
mirror of https://github.com/golang/go synced 2024-11-19 03:14:42 -07:00
go/internal/lsp/source/enums.go
Ian Cottrell e528388d46 internal/lsp: Add to and from string handling for the enums
This is to help with debugging, and to allow us to write tests that do not
rely on hard coded constants.

Change-Id: Ica9ed5eee3d35539fe50d76276988852e62b81ca
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172401
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-04-16 21:24:01 +00:00

88 lines
2.8 KiB
Go

// 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 (
namesSymbolKind [int(FieldSymbol) + 1]string
namesDiagnosticSeverity [int(SeverityError) + 1]string
namesCompletionItemKind [int(PackageCompletionItem) + 1]string
)
func init() {
namesSymbolKind[PackageSymbol] = "Package"
namesSymbolKind[StructSymbol] = "Struct"
namesSymbolKind[VariableSymbol] = "Variable"
namesSymbolKind[ConstantSymbol] = "Constant"
namesSymbolKind[FunctionSymbol] = "Function"
namesSymbolKind[MethodSymbol] = "Method"
namesSymbolKind[InterfaceSymbol] = "Interface"
namesSymbolKind[NumberSymbol] = "Number"
namesSymbolKind[StringSymbol] = "String"
namesSymbolKind[BooleanSymbol] = "Boolean"
namesSymbolKind[FieldSymbol] = "Field"
namesDiagnosticSeverity[SeverityWarning] = "Warning"
namesDiagnosticSeverity[SeverityError] = "Error"
namesCompletionItemKind[Unknown] = "Unknown"
namesCompletionItemKind[InterfaceCompletionItem] = "InterfaceCompletion"
namesCompletionItemKind[StructCompletionItem] = "StructCompletion"
namesCompletionItemKind[TypeCompletionItem] = "TypeCompletion"
namesCompletionItemKind[ConstantCompletionItem] = "ConstantCompletion"
namesCompletionItemKind[FieldCompletionItem] = "FieldCompletion"
namesCompletionItemKind[ParameterCompletionItem] = "ParameterCompletion"
namesCompletionItemKind[VariableCompletionItem] = "VariableCompletion"
namesCompletionItemKind[FunctionCompletionItem] = "FunctionCompletion"
namesCompletionItemKind[MethodCompletionItem] = "MethodCompletion"
namesCompletionItemKind[PackageCompletionItem] = "PackageCompletion"
}
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 SymbolKind) Format(f fmt.State, c rune) {
formatEnum(f, c, int(e), namesSymbolKind[:], "SymbolKind")
}
func ParseSymbolKind(s string) SymbolKind {
return SymbolKind(parseEnum(s, namesSymbolKind[:]))
}
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[:]))
}