1// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package source
6
7import "fmt"
8
9var (
10	namesSymbolKind         [int(FieldSymbol) + 1]string
11	namesDiagnosticSeverity [int(SeverityError) + 1]string
12	namesCompletionItemKind [int(PackageCompletionItem) + 1]string
13)
14
15func init() {
16	namesSymbolKind[PackageSymbol] = "Package"
17	namesSymbolKind[StructSymbol] = "Struct"
18	namesSymbolKind[VariableSymbol] = "Variable"
19	namesSymbolKind[ConstantSymbol] = "Constant"
20	namesSymbolKind[FunctionSymbol] = "Function"
21	namesSymbolKind[MethodSymbol] = "Method"
22	namesSymbolKind[InterfaceSymbol] = "Interface"
23	namesSymbolKind[NumberSymbol] = "Number"
24	namesSymbolKind[StringSymbol] = "String"
25	namesSymbolKind[BooleanSymbol] = "Boolean"
26	namesSymbolKind[FieldSymbol] = "Field"
27
28	namesDiagnosticSeverity[SeverityWarning] = "Warning"
29	namesDiagnosticSeverity[SeverityError] = "Error"
30
31	namesCompletionItemKind[Unknown] = "Unknown"
32	namesCompletionItemKind[InterfaceCompletionItem] = "interface"
33	namesCompletionItemKind[StructCompletionItem] = "struct"
34	namesCompletionItemKind[TypeCompletionItem] = "type"
35	namesCompletionItemKind[ConstantCompletionItem] = "const"
36	namesCompletionItemKind[FieldCompletionItem] = "field"
37	namesCompletionItemKind[ParameterCompletionItem] = "parameter"
38	namesCompletionItemKind[VariableCompletionItem] = "var"
39	namesCompletionItemKind[FunctionCompletionItem] = "func"
40	namesCompletionItemKind[MethodCompletionItem] = "method"
41	namesCompletionItemKind[PackageCompletionItem] = "package"
42}
43
44func formatEnum(f fmt.State, c rune, i int, names []string, unknown string) {
45	s := ""
46	if i >= 0 && i < len(names) {
47		s = names[i]
48	}
49	if s != "" {
50		fmt.Fprint(f, s)
51	} else {
52		fmt.Fprintf(f, "%s(%d)", unknown, i)
53	}
54}
55
56func parseEnum(s string, names []string) int {
57	for i, name := range names {
58		if s == name {
59			return i
60		}
61	}
62	return 0
63}
64
65func (e SymbolKind) Format(f fmt.State, c rune) {
66	formatEnum(f, c, int(e), namesSymbolKind[:], "SymbolKind")
67}
68
69func ParseSymbolKind(s string) SymbolKind {
70	return SymbolKind(parseEnum(s, namesSymbolKind[:]))
71}
72
73func (e DiagnosticSeverity) Format(f fmt.State, c rune) {
74	formatEnum(f, c, int(e), namesDiagnosticSeverity[:], "DiagnosticSeverity")
75}
76
77func ParseDiagnosticSeverity(s string) DiagnosticSeverity {
78	return DiagnosticSeverity(parseEnum(s, namesDiagnosticSeverity[:]))
79}
80
81func (e CompletionItemKind) Format(f fmt.State, c rune) {
82	formatEnum(f, c, int(e), namesCompletionItemKind[:], "CompletionItemKind")
83}
84
85func ParseCompletionItemKind(s string) CompletionItemKind {
86	return CompletionItemKind(parseEnum(s, namesCompletionItemKind[:]))
87}
88