forked from microsoft/typescript-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproto.go
More file actions
231 lines (193 loc) · 7.06 KB
/
proto.go
File metadata and controls
231 lines (193 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package api
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/checker"
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/project"
)
var (
ErrInvalidRequest = errors.New("api: invalid request")
ErrClientError = errors.New("api: client error")
)
type Method string
type Handle[T any] string
const (
handlePrefixProject = 'p'
handlePrefixSymbol = 's'
handlePrefixType = 't'
handlePrefixFile = 'f'
handlePrefixNode = 'n'
)
func ProjectHandle(p *project.Project) Handle[project.Project] {
return createHandle[project.Project](handlePrefixProject, p.Name())
}
func SymbolHandle(symbol *ast.Symbol) Handle[ast.Symbol] {
return createHandle[ast.Symbol](handlePrefixSymbol, ast.GetSymbolId(symbol))
}
func TypeHandle(t *checker.Type) Handle[checker.Type] {
return createHandle[checker.Type](handlePrefixType, t.Id())
}
func FileHandle(file *ast.SourceFile) Handle[ast.SourceFile] {
return createHandle[ast.SourceFile](handlePrefixFile, ast.GetNodeId(file.AsNode()))
}
func NodeHandle(node *ast.Node) Handle[ast.Node] {
fileHandle := FileHandle(ast.GetSourceFileOfNode(node))
return Handle[ast.Node](fmt.Sprintf("%s.%d.%d", fileHandle, node.Pos(), node.Kind))
}
func parseNodeHandle(handle Handle[ast.Node]) (Handle[ast.SourceFile], int, ast.Kind, error) {
parts := strings.SplitN(string(handle), ".", 3)
if len(parts) != 3 {
return "", 0, 0, fmt.Errorf("invalid node handle %q", handle)
}
fileHandle := Handle[ast.SourceFile](parts[0])
pos, err := strconv.ParseInt(parts[1], 10, 32)
if err != nil {
return "", 0, 0, fmt.Errorf("invalid node handle %q: %w", handle, err)
}
kind, err := strconv.ParseInt(parts[2], 10, 16)
if err != nil {
return "", 0, 0, fmt.Errorf("invalid node handle %q: %w", handle, err)
}
return fileHandle, int(pos), ast.Kind(kind), nil
}
func createHandle[T any](prefix rune, id any) Handle[T] {
return Handle[T](fmt.Sprintf("%c%016x", prefix, id))
}
const (
MethodConfigure Method = "configure"
MethodRelease Method = "release"
MethodParseConfigFile Method = "parseConfigFile"
MethodLoadProject Method = "loadProject"
MethodGetSymbolAtPosition Method = "getSymbolAtPosition"
MethodGetSymbolsAtPositions Method = "getSymbolsAtPositions"
MethodGetSymbolAtLocation Method = "getSymbolAtLocation"
MethodGetSymbolsAtLocations Method = "getSymbolsAtLocations"
MethodGetTypeOfSymbol Method = "getTypeOfSymbol"
MethodGetTypesOfSymbols Method = "getTypesOfSymbols"
MethodGetSourceFile Method = "getSourceFile"
MethodGetDiagnostics Method = "getDiagnostics"
)
var unmarshalers = map[Method]func([]byte) (any, error){
MethodRelease: unmarshallerFor[string],
MethodParseConfigFile: unmarshallerFor[ParseConfigFileParams],
MethodLoadProject: unmarshallerFor[LoadProjectParams],
MethodGetSourceFile: unmarshallerFor[GetSourceFileParams],
MethodGetSymbolAtPosition: unmarshallerFor[GetSymbolAtPositionParams],
MethodGetSymbolsAtPositions: unmarshallerFor[GetSymbolsAtPositionsParams],
MethodGetSymbolAtLocation: unmarshallerFor[GetSymbolAtLocationParams],
MethodGetSymbolsAtLocations: unmarshallerFor[GetSymbolsAtLocationsParams],
MethodGetTypeOfSymbol: unmarshallerFor[GetTypeOfSymbolParams],
MethodGetTypesOfSymbols: unmarshallerFor[GetTypesOfSymbolsParams],
MethodGetDiagnostics: unmarshallerFor[GetDiagnosticsParams],
}
type ForkContextInfo struct {
TypesNodeIgnorableNames []string `json:"typesNodeIgnorableNames"`
NodeOnlyGlobalNames []string `json:"nodeOnlyGlobalNames"`
}
type ConfigureParams struct {
Callbacks []string `json:"callbacks"`
LogFile string `json:"logFile"`
Fork ForkContextInfo `json:"forkContextInfo"`
}
type ParseConfigFileParams struct {
FileName string `json:"fileName"`
}
type ConfigFileResponse struct {
FileNames []string `json:"fileNames"`
Options *core.CompilerOptions `json:"options"`
}
type LoadProjectParams struct {
ConfigFileName string `json:"configFileName"`
}
type ProjectResponse struct {
Id Handle[project.Project] `json:"id"`
ConfigFileName string `json:"configFileName"`
RootFiles []string `json:"rootFiles"`
CompilerOptions *core.CompilerOptions `json:"compilerOptions"`
}
func NewProjectResponse(project *project.Project) *ProjectResponse {
return &ProjectResponse{
Id: ProjectHandle(project),
ConfigFileName: project.Name(),
RootFiles: project.CommandLine.FileNames(),
CompilerOptions: project.CommandLine.CompilerOptions(),
}
}
type GetSymbolAtPositionParams struct {
Project Handle[project.Project] `json:"project"`
FileName string `json:"fileName"`
Position uint32 `json:"position"`
}
type GetSymbolsAtPositionsParams struct {
Project Handle[project.Project] `json:"project"`
FileName string `json:"fileName"`
Positions []uint32 `json:"positions"`
}
type GetSymbolAtLocationParams struct {
Project Handle[project.Project] `json:"project"`
Location Handle[ast.Node] `json:"location"`
}
type GetSymbolsAtLocationsParams struct {
Project Handle[project.Project] `json:"project"`
Locations []Handle[ast.Node] `json:"locations"`
}
type SymbolResponse struct {
Id Handle[ast.Symbol] `json:"id"`
Name string `json:"name"`
Flags uint32 `json:"flags"`
CheckFlags uint32 `json:"checkFlags"`
}
func NewSymbolResponse(symbol *ast.Symbol) *SymbolResponse {
return &SymbolResponse{
Id: SymbolHandle(symbol),
Name: symbol.Name,
Flags: uint32(symbol.Flags),
CheckFlags: uint32(symbol.CheckFlags),
}
}
type GetDiagnosticsParams struct {
Project Handle[project.Project] `json:"project"`
FileNames []string `json:"fileNames"`
}
type GetTypeOfSymbolParams struct {
Project Handle[project.Project] `json:"project"`
Symbol Handle[ast.Symbol] `json:"symbol"`
}
type GetTypesOfSymbolsParams struct {
Project Handle[project.Project] `json:"project"`
Symbols []Handle[ast.Symbol] `json:"symbols"`
}
type TypeResponse struct {
Id Handle[checker.Type] `json:"id"`
Flags uint32 `json:"flags"`
}
func NewTypeData(t *checker.Type) *TypeResponse {
return &TypeResponse{
Id: TypeHandle(t),
Flags: uint32(t.Flags()),
}
}
type GetSourceFileParams struct {
Project Handle[project.Project] `json:"project"`
FileName string `json:"fileName"`
}
func unmarshalPayload(method string, payload jsontext.Value) (any, error) {
unmarshaler, ok := unmarshalers[Method(method)]
if !ok {
return nil, fmt.Errorf("unknown API method %q", method)
}
return unmarshaler(payload)
}
func unmarshallerFor[T any](data []byte) (any, error) {
var v T
if err := json.Unmarshal(data, &v); err != nil {
return nil, fmt.Errorf("failed to unmarshal %T: %w", (*T)(nil), err)
}
return &v, nil
}