mirror of
https://github.com/golang/go
synced 2024-11-18 22:24:50 -07:00
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>
This commit is contained in:
parent
f6a9ef9393
commit
e528388d46
244
internal/lsp/protocol/enums.go
Normal file
244
internal/lsp/protocol/enums.go
Normal file
@ -0,0 +1,244 @@
|
|||||||
|
// 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 protocol
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
namesTextDocumentSyncKind [int(Incremental) + 1]string
|
||||||
|
namesInitializeError [int(UnknownProtocolVersion) + 1]string
|
||||||
|
namesMessageType [int(Log) + 1]string
|
||||||
|
namesFileChangeType [int(Deleted) + 1]string
|
||||||
|
namesWatchKind [int(Change) + 1]string
|
||||||
|
namesCompletionTriggerKind [int(TriggerForIncompleteCompletions) + 1]string
|
||||||
|
namesDiagnosticSeverity [int(SeverityHint) + 1]string
|
||||||
|
namesDiagnosticTag [int(Unnecessary) + 1]string
|
||||||
|
namesCompletionItemKind [int(TypeParameterCompletion) + 1]string
|
||||||
|
namesInsertTextFormat [int(SnippetTextFormat) + 1]string
|
||||||
|
namesDocumentHighlightKind [int(Write) + 1]string
|
||||||
|
namesSymbolKind [int(TypeParameter) + 1]string
|
||||||
|
namesTextDocumentSaveReason [int(FocusOut) + 1]string
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
namesTextDocumentSyncKind[int(None)] = "None"
|
||||||
|
namesTextDocumentSyncKind[int(Full)] = "Full"
|
||||||
|
namesTextDocumentSyncKind[int(Incremental)] = "Incremental"
|
||||||
|
|
||||||
|
namesInitializeError[int(UnknownProtocolVersion)] = "UnknownProtocolVersion"
|
||||||
|
|
||||||
|
namesMessageType[int(Error)] = "Error"
|
||||||
|
namesMessageType[int(Warning)] = "Warning"
|
||||||
|
namesMessageType[int(Info)] = "Info"
|
||||||
|
namesMessageType[int(Log)] = "Log"
|
||||||
|
|
||||||
|
namesFileChangeType[int(Created)] = "Created"
|
||||||
|
namesFileChangeType[int(Changed)] = "Changed"
|
||||||
|
namesFileChangeType[int(Deleted)] = "Deleted"
|
||||||
|
|
||||||
|
namesWatchKind[int(Change)] = "Change"
|
||||||
|
|
||||||
|
namesCompletionTriggerKind[int(Invoked)] = "Invoked"
|
||||||
|
namesCompletionTriggerKind[int(TriggerCharacter)] = "TriggerCharacter"
|
||||||
|
namesCompletionTriggerKind[int(TriggerForIncompleteCompletions)] = "TriggerForIncompleteCompletions"
|
||||||
|
|
||||||
|
namesDiagnosticSeverity[int(SeverityError)] = "Error"
|
||||||
|
namesDiagnosticSeverity[int(SeverityWarning)] = "Warning"
|
||||||
|
namesDiagnosticSeverity[int(SeverityInformation)] = "Information"
|
||||||
|
namesDiagnosticSeverity[int(SeverityHint)] = "Hint"
|
||||||
|
|
||||||
|
namesDiagnosticTag[int(Unnecessary)] = "Unnecessary"
|
||||||
|
|
||||||
|
namesCompletionItemKind[int(TextCompletion)] = "TextCompletion"
|
||||||
|
namesCompletionItemKind[int(MethodCompletion)] = "MethodCompletion"
|
||||||
|
namesCompletionItemKind[int(FunctionCompletion)] = "FunctionCompletion"
|
||||||
|
namesCompletionItemKind[int(ConstructorCompletion)] = "ConstructorCompletion"
|
||||||
|
namesCompletionItemKind[int(FieldCompletion)] = "FieldCompletion"
|
||||||
|
namesCompletionItemKind[int(VariableCompletion)] = "VariableCompletion"
|
||||||
|
namesCompletionItemKind[int(ClassCompletion)] = "ClassCompletion"
|
||||||
|
namesCompletionItemKind[int(InterfaceCompletion)] = "InterfaceCompletion"
|
||||||
|
namesCompletionItemKind[int(ModuleCompletion)] = "ModuleCompletion"
|
||||||
|
namesCompletionItemKind[int(PropertyCompletion)] = "PropertyCompletion"
|
||||||
|
namesCompletionItemKind[int(UnitCompletion)] = "UnitCompletion"
|
||||||
|
namesCompletionItemKind[int(ValueCompletion)] = "ValueCompletion"
|
||||||
|
namesCompletionItemKind[int(EnumCompletion)] = "EnumCompletion"
|
||||||
|
namesCompletionItemKind[int(KeywordCompletion)] = "KeywordCompletion"
|
||||||
|
namesCompletionItemKind[int(SnippetCompletion)] = "SnippetCompletion"
|
||||||
|
namesCompletionItemKind[int(ColorCompletion)] = "ColorCompletion"
|
||||||
|
namesCompletionItemKind[int(FileCompletion)] = "FileCompletion"
|
||||||
|
namesCompletionItemKind[int(ReferenceCompletion)] = "ReferenceCompletion"
|
||||||
|
namesCompletionItemKind[int(FolderCompletion)] = "FolderCompletion"
|
||||||
|
namesCompletionItemKind[int(EnumMemberCompletion)] = "EnumMemberCompletion"
|
||||||
|
namesCompletionItemKind[int(ConstantCompletion)] = "ConstantCompletion"
|
||||||
|
namesCompletionItemKind[int(StructCompletion)] = "StructCompletion"
|
||||||
|
namesCompletionItemKind[int(EventCompletion)] = "EventCompletion"
|
||||||
|
namesCompletionItemKind[int(OperatorCompletion)] = "OperatorCompletion"
|
||||||
|
namesCompletionItemKind[int(TypeParameterCompletion)] = "TypeParameterCompletion"
|
||||||
|
|
||||||
|
namesInsertTextFormat[int(PlainTextTextFormat)] = "PlainText"
|
||||||
|
namesInsertTextFormat[int(SnippetTextFormat)] = "Snippet"
|
||||||
|
|
||||||
|
namesDocumentHighlightKind[int(Text)] = "Text"
|
||||||
|
namesDocumentHighlightKind[int(Read)] = "Read"
|
||||||
|
namesDocumentHighlightKind[int(Write)] = "Write"
|
||||||
|
|
||||||
|
namesSymbolKind[int(File)] = "File"
|
||||||
|
namesSymbolKind[int(Module)] = "Module"
|
||||||
|
namesSymbolKind[int(Namespace)] = "Namespace"
|
||||||
|
namesSymbolKind[int(Package)] = "Package"
|
||||||
|
namesSymbolKind[int(Class)] = "Class"
|
||||||
|
namesSymbolKind[int(Method)] = "Method"
|
||||||
|
namesSymbolKind[int(Property)] = "Property"
|
||||||
|
namesSymbolKind[int(Field)] = "Field"
|
||||||
|
namesSymbolKind[int(Constructor)] = "Constructor"
|
||||||
|
namesSymbolKind[int(Enum)] = "Enum"
|
||||||
|
namesSymbolKind[int(Interface)] = "Interface"
|
||||||
|
namesSymbolKind[int(Function)] = "Function"
|
||||||
|
namesSymbolKind[int(Variable)] = "Variable"
|
||||||
|
namesSymbolKind[int(Constant)] = "Constant"
|
||||||
|
namesSymbolKind[int(String)] = "String"
|
||||||
|
namesSymbolKind[int(Number)] = "Number"
|
||||||
|
namesSymbolKind[int(Boolean)] = "Boolean"
|
||||||
|
namesSymbolKind[int(Array)] = "Array"
|
||||||
|
namesSymbolKind[int(Object)] = "Object"
|
||||||
|
namesSymbolKind[int(Key)] = "Key"
|
||||||
|
namesSymbolKind[int(Null)] = "Null"
|
||||||
|
namesSymbolKind[int(EnumMember)] = "EnumMember"
|
||||||
|
namesSymbolKind[int(Struct)] = "Struct"
|
||||||
|
namesSymbolKind[int(Event)] = "Event"
|
||||||
|
namesSymbolKind[int(Operator)] = "Operator"
|
||||||
|
namesSymbolKind[int(TypeParameter)] = "TypeParameter"
|
||||||
|
|
||||||
|
namesTextDocumentSaveReason[int(Manual)] = "Manual"
|
||||||
|
namesTextDocumentSaveReason[int(AfterDelay)] = "AfterDelay"
|
||||||
|
namesTextDocumentSaveReason[int(FocusOut)] = "FocusOut"
|
||||||
|
}
|
||||||
|
|
||||||
|
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 TextDocumentSyncKind) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesTextDocumentSyncKind[:], "TextDocumentSyncKind")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseTextDocumentSyncKind(s string) TextDocumentSyncKind {
|
||||||
|
return TextDocumentSyncKind(parseEnum(s, namesTextDocumentSyncKind[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e InitializeError) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesInitializeError[:], "InitializeError")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseInitializeError(s string) InitializeError {
|
||||||
|
return InitializeError(parseEnum(s, namesInitializeError[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e MessageType) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesMessageType[:], "MessageType")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseMessageType(s string) MessageType {
|
||||||
|
return MessageType(parseEnum(s, namesMessageType[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e FileChangeType) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesFileChangeType[:], "FileChangeType")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseFileChangeType(s string) FileChangeType {
|
||||||
|
return FileChangeType(parseEnum(s, namesFileChangeType[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e WatchKind) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesWatchKind[:], "WatchKind")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseWatchKind(s string) WatchKind {
|
||||||
|
return WatchKind(parseEnum(s, namesWatchKind[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CompletionTriggerKind) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesCompletionTriggerKind[:], "CompletionTriggerKind")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseCompletionTriggerKind(s string) CompletionTriggerKind {
|
||||||
|
return CompletionTriggerKind(parseEnum(s, namesCompletionTriggerKind[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 DiagnosticTag) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesDiagnosticTag[:], "DiagnosticTag")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseDiagnosticTag(s string) DiagnosticTag {
|
||||||
|
return DiagnosticTag(parseEnum(s, namesDiagnosticTag[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
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[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e InsertTextFormat) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesInsertTextFormat[:], "InsertTextFormat")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseInsertTextFormat(s string) InsertTextFormat {
|
||||||
|
return InsertTextFormat(parseEnum(s, namesInsertTextFormat[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e DocumentHighlightKind) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesDocumentHighlightKind[:], "DocumentHighlightKind")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseDocumentHighlightKind(s string) DocumentHighlightKind {
|
||||||
|
return DocumentHighlightKind(parseEnum(s, namesDocumentHighlightKind[:]))
|
||||||
|
}
|
||||||
|
|
||||||
|
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 TextDocumentSaveReason) Format(f fmt.State, c rune) {
|
||||||
|
formatEnum(f, c, int(e), namesTextDocumentSaveReason[:], "TextDocumentSaveReason")
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseTextDocumentSaveReason(s string) TextDocumentSaveReason {
|
||||||
|
return TextDocumentSaveReason(parseEnum(s, namesTextDocumentSaveReason[:]))
|
||||||
|
}
|
@ -1,53 +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.
|
|
||||||
|
|
||||||
// This file contains formatting functions for types that
|
|
||||||
// are commonly printed in debugging information.
|
|
||||||
// They are separated from their types and gathered here as
|
|
||||||
// they are hand written and not generated from the spec.
|
|
||||||
// They should not be relied on for programmatic use (their
|
|
||||||
// results should never be parsed for instance) but are meant
|
|
||||||
// for temporary debugging and error messages.
|
|
||||||
|
|
||||||
package protocol
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (s DiagnosticSeverity) Format(f fmt.State, c rune) {
|
|
||||||
switch s {
|
|
||||||
case SeverityError:
|
|
||||||
fmt.Fprint(f, "Error")
|
|
||||||
case SeverityWarning:
|
|
||||||
fmt.Fprint(f, "Warning")
|
|
||||||
case SeverityInformation:
|
|
||||||
fmt.Fprint(f, "Information")
|
|
||||||
case SeverityHint:
|
|
||||||
fmt.Fprint(f, "Hint")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (k CompletionItemKind) Format(f fmt.State, c rune) {
|
|
||||||
switch k {
|
|
||||||
case StructCompletion:
|
|
||||||
fmt.Fprintf(f, "struct")
|
|
||||||
case FunctionCompletion:
|
|
||||||
fmt.Fprintf(f, "func")
|
|
||||||
case VariableCompletion:
|
|
||||||
fmt.Fprintf(f, "var")
|
|
||||||
case TypeParameterCompletion:
|
|
||||||
fmt.Fprintf(f, "type")
|
|
||||||
case FieldCompletion:
|
|
||||||
fmt.Fprintf(f, "field")
|
|
||||||
case InterfaceCompletion:
|
|
||||||
fmt.Fprintf(f, "interface")
|
|
||||||
case ConstantCompletion:
|
|
||||||
fmt.Fprintf(f, "const")
|
|
||||||
case MethodCompletion:
|
|
||||||
fmt.Fprintf(f, "method")
|
|
||||||
case ModuleCompletion:
|
|
||||||
fmt.Fprintf(f, "package")
|
|
||||||
}
|
|
||||||
}
|
|
87
internal/lsp/source/enums.go
Normal file
87
internal/lsp/source/enums.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
// 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[:]))
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user