Debian Codegraph @

gopls/internal/protocol/generate/types.go

raw symbols SCIP
1 // Copyright 2022 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
5 package main
6
7 import (
8 "fmt"
9 "sort"
10 )
11
12 // Model contains the parsed version of the spec
13 type Model struct {
14 Version Metadata `json:"metaData"`
15 Requests []*Request `json:"requests"`
16 Notifications []*Notification `json:"notifications"`
17 Structures []*Structure `json:"structures"`
18 Enumerations []*Enumeration `json:"enumerations"`
19 TypeAliases []*TypeAlias `json:"typeAliases"`
20 Line int `json:"line"`
21 }
22
23 // Metadata is information about the version of the spec
24 type Metadata struct {
25 Version string `json:"version"`
26 Line int `json:"line"`
27 }
28
29 // A Request is the parsed version of an LSP request
30 type Request struct {
31 Documentation string `json:"documentation"`
32 ErrorData *Type `json:"errorData"`
33 Direction string `json:"messageDirection"`
34 Method string `json:"method"`
35 Params *Type `json:"params"`
36 PartialResult *Type `json:"partialResult"`
37 Proposed bool `json:"proposed"`
38 RegistrationMethod string `json:"registrationMethod"`
39 RegistrationOptions *Type `json:"registrationOptions"`
40 Result *Type `json:"result"`
41 Since string `json:"since"`
42 Line int `json:"line"`
43 }
44
45 // A Notification is the parsed version of an LSP notification
46 type Notification struct {
47 Documentation string `json:"documentation"`
48 Direction string `json:"messageDirection"`
49 Method string `json:"method"`
50 Params *Type `json:"params"`
51 Proposed bool `json:"proposed"`
52 RegistrationMethod string `json:"registrationMethod"`
53 RegistrationOptions *Type `json:"registrationOptions"`
54 Since string `json:"since"`
55 Line int `json:"line"`
56 }
57
58 // A Structure is the parsed version of an LSP structure from the spec
59 type Structure struct {
60 Documentation string `json:"documentation"`
61 Extends []*Type `json:"extends"`
62 Mixins []*Type `json:"mixins"`
63 Name string `json:"name"`
64 Properties []NameType `json:"properties"`
65 Proposed bool `json:"proposed"`
66 Since string `json:"since"`
67 Line int `json:"line"`
68 }
69
70 // An enumeration is the parsed version of an LSP enumeration from the spec
71 type Enumeration struct {
72 Documentation string `json:"documentation"`
73 Name string `json:"name"`
74 Proposed bool `json:"proposed"`
75 Since string `json:"since"`
76 SupportsCustomValues bool `json:"supportsCustomValues"`
77 Type *Type `json:"type"`
78 Values []NameValue `json:"values"`
79 Line int `json:"line"`
80 }
81
82 // A TypeAlias is the parsed version of an LSP type alias from the spec
83 type TypeAlias struct {
84 Documentation string `json:"documentation"`
85 Deprecated string `json:"deprecated"`
86 Name string `json:"name"`
87 Proposed bool `json:"proposed"`
88 Since string `json:"since"`
89 Type *Type `json:"type"`
90 Line int `json:"line"`
91 }
92
93 // A NameValue describes an enumeration constant
94 type NameValue struct {
95 Documentation string `json:"documentation"`
96 Name string `json:"name"`
97 Proposed bool `json:"proposed"`
98 Since string `json:"since"`
99 Value any `json:"value"` // number or string
100 Line int `json:"line"`
101 }
102
103 // A Type is the parsed version of an LSP type from the spec,
104 // or a Type the code constructs
105 type Type struct {
106 Kind string `json:"kind"` // -- which kind goes with which field --
107 Items []*Type `json:"items"` // "and", "or", "tuple"
108 Element *Type `json:"element"` // "array"
109 Name string `json:"name"` // "base", "reference"
110 Key *Type `json:"key"` // "map"
111 Value any `json:"value"` // "map", "stringLiteral", "literal"
112 Line int `json:"line"` // JSON source line
113 }
114
115 // ParseLiteral is Type.Value when Type.Kind is "literal"
116 type ParseLiteral struct {
117 Properties `json:"properties"`
118 }
119
120 // A NameType represents the name and type of a structure element
121 type NameType struct {
122 Name string `json:"name"`
123 Type *Type `json:"type"`
124 Optional bool `json:"optional"`
125 Documentation string `json:"documentation"`
126 Deprecated string `json:"deprecated"`
127 Since string `json:"since"`
128 Proposed bool `json:"proposed"`
129 Line int `json:"line"`
130 }
131
132 // Properties are the collection of structure fields
133 type Properties []NameType
134
135 // addLineNumbers adds a "line" field to each object in the JSON.
136 func addLineNumbers(buf []byte) []byte {
137 var ans []byte
138 // In the specification .json file, the delimiter '{' is
139 // always followed by a newline. There are other {s embedded in strings.
140 // json.Token does not return \n, or :, or , so using it would
141 // require parsing the json to reconstruct the missing information.
142 for linecnt, i := 1, 0; i < len(buf); i++ {
143 ans = append(ans, buf[i])
144 switch buf[i] {
145 case '{':
146 if buf[i+1] == '\n' {
147 ans = append(ans, fmt.Sprintf(`"line": %d, `, linecnt)...)
148 // warning: this would fail if the spec file had
149 // `"value": {\n}`, but it does not, as comma is a separator.
150 }
151 case '\n':
152 linecnt++
153 }
154 }
155 return ans
156 }
157
158 type sortedMap[T any] map[string]T
159
160 func (s sortedMap[T]) keys() []string {
161 var keys []string
162 for k := range s {
163 keys = append(keys, k)
164 }
165 sort.Strings(keys)
166 return keys
167 }