| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"log" |
| 11 |
"slices" |
| 12 |
"sort" |
| 13 |
"strings" |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
|
| 18 |
cdecls = make(sortedMap[string]) |
| 19 |
ccases = make(sortedMap[string]) |
| 20 |
cfuncs = make(sortedMap[string]) |
| 21 |
|
| 22 |
sdecls = make(sortedMap[string]) |
| 23 |
scases = make(sortedMap[string]) |
| 24 |
sfuncs = make(sortedMap[string]) |
| 25 |
|
| 26 |
types = make(sortedMap[string]) |
| 27 |
consts = make(sortedMap[string]) |
| 28 |
|
| 29 |
jsons = make(sortedMap[string]) |
| 30 |
) |
| 31 |
|
| 32 |
func generateOutput(model *Model) { |
| 33 |
for _, r := range model.Requests { |
| 34 |
genDecl(model, r.Method, r.Params, r.Result, r.Direction, r.Line == 0) |
| 35 |
genCase(model, r.Method, r.Params, r.Result, r.Direction) |
| 36 |
genFunc(model, r.Method, r.Params, r.Result, r.Direction, false) |
| 37 |
} |
| 38 |
for _, n := range model.Notifications { |
| 39 |
if n.Method == "$/cancelRequest" { |
| 40 |
continue |
| 41 |
} |
| 42 |
genDecl(model, n.Method, n.Params, nil, n.Direction, n.Line == 0) |
| 43 |
genCase(model, n.Method, n.Params, nil, n.Direction) |
| 44 |
genFunc(model, n.Method, n.Params, nil, n.Direction, true) |
| 45 |
} |
| 46 |
genStructs(model) |
| 47 |
genAliases(model) |
| 48 |
genGenTypes() |
| 49 |
genConsts(model) |
| 50 |
genMarshal() |
| 51 |
} |
| 52 |
|
| 53 |
func genDecl(model *Model, method string, param, result *Type, dir string, experiment bool) { |
| 54 |
fname := methodName(method) |
| 55 |
p := "" |
| 56 |
if notNil(param) { |
| 57 |
p = ", *" + goplsName(param) |
| 58 |
} |
| 59 |
ret := "error" |
| 60 |
if notNil(result) { |
| 61 |
tp := goplsName(result) |
| 62 |
if !hasNilValue(tp) { |
| 63 |
tp = "*" + tp |
| 64 |
} |
| 65 |
ret = fmt.Sprintf("(%s, error)", tp) |
| 66 |
} |
| 67 |
|
| 68 |
switch method { |
| 69 |
case "workspace/configuration": |
| 70 |
|
| 71 |
|
| 72 |
p = ", *ParamConfiguration" |
| 73 |
ret = "([]LSPAny, error)" |
| 74 |
} |
| 75 |
|
| 76 |
var msg string |
| 77 |
{ |
| 78 |
var sb strings.Builder |
| 79 |
|
| 80 |
if doc, ok := prependMethodDocComments[fname]; ok { |
| 81 |
sb.WriteString(doc) |
| 82 |
sb.WriteString("\n\t//\n") |
| 83 |
} |
| 84 |
|
| 85 |
if experiment { |
| 86 |
sb.WriteString("\t// Note: This is a non-standard protocol extension.\n") |
| 87 |
} else { |
| 88 |
fragment := strings.ReplaceAll(strings.TrimPrefix(method, "$/"), "/", "_") |
| 89 |
fmt.Fprintf(&sb, "\t%s", lspLink(model, fragment)) |
| 90 |
} |
| 91 |
|
| 92 |
fmt.Fprintf(&sb, "\t%s(context.Context%s) %s\n", fname, p, ret) |
| 93 |
|
| 94 |
msg = sb.String() |
| 95 |
} |
| 96 |
|
| 97 |
switch dir { |
| 98 |
case "clientToServer": |
| 99 |
sdecls[method] = msg |
| 100 |
case "serverToClient": |
| 101 |
cdecls[method] = msg |
| 102 |
case "both": |
| 103 |
sdecls[method] = msg |
| 104 |
cdecls[method] = msg |
| 105 |
default: |
| 106 |
log.Fatalf("impossible direction %q", dir) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
func genCase(model *Model, method string, param, result *Type, dir string) { |
| 111 |
extends := func(x, y string) bool { |
| 112 |
for _, struc := range model.Structures { |
| 113 |
if struc.Name == x { |
| 114 |
if contains := slices.ContainsFunc(struc.Extends, func(t *Type) bool { |
| 115 |
return t.Name == y |
| 116 |
}); contains { |
| 117 |
return true |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
return false |
| 122 |
} |
| 123 |
out := new(bytes.Buffer) |
| 124 |
fmt.Fprintf(out, "\tcase %q:\n", method) |
| 125 |
var p string |
| 126 |
fname := methodName(method) |
| 127 |
if notNil(param) { |
| 128 |
nm := goplsName(param) |
| 129 |
if method == "workspace/configuration" { |
| 130 |
|
| 131 |
|
| 132 |
nm = "ParamConfiguration" |
| 133 |
} |
| 134 |
fmt.Fprintf(out, "\t\tvar params %s\n", nm) |
| 135 |
out.WriteString("\t\tif err := UnmarshalJSON(raw, ¶ms); err != nil {\n") |
| 136 |
out.WriteString("\t\t\treturn nil, true, fmt.Errorf(\"%%w: %%s\", jsonrpc2.ErrParse, err)\n\t\t}\n") |
| 137 |
p = ", ¶ms" |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if extends(nm, "TextDocumentPositionParams") { |
| 149 |
out.WriteString(` if params.Range == (Range{}) { |
| 150 |
params.Range = Range{ |
| 151 |
Start: params.Position, |
| 152 |
End: params.Position, |
| 153 |
} |
| 154 |
} else if !params.Range.Contains(params.Position) { |
| 155 |
return nil, true, fmt.Errorf("position %%v is outside the provided range %%v.", params.Position, params.Range) |
| 156 |
} |
| 157 |
`) |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
if notNil(result) { |
| 162 |
fmt.Fprintf(out, "\t\tresp, err := %%s.%s(ctx%s)\n", fname, p) |
| 163 |
out.WriteString("\t\tif err != nil {\n") |
| 164 |
out.WriteString("\t\t\treturn nil, true, err\n") |
| 165 |
out.WriteString("\t\t}\n") |
| 166 |
out.WriteString("\t\treturn resp, true, nil\n") |
| 167 |
} else { |
| 168 |
fmt.Fprintf(out, "\t\terr := %%s.%s(ctx%s)\n", fname, p) |
| 169 |
out.WriteString("\t\treturn nil, true, err\n") |
| 170 |
} |
| 171 |
out.WriteString("\n") |
| 172 |
msg := out.String() |
| 173 |
switch dir { |
| 174 |
case "clientToServer": |
| 175 |
scases[method] = fmt.Sprintf(msg, "server") |
| 176 |
case "serverToClient": |
| 177 |
ccases[method] = fmt.Sprintf(msg, "client") |
| 178 |
case "both": |
| 179 |
scases[method] = fmt.Sprintf(msg, "server") |
| 180 |
ccases[method] = fmt.Sprintf(msg, "client") |
| 181 |
default: |
| 182 |
log.Fatalf("impossible direction %q", dir) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func genFunc(_ *Model, method string, param, result *Type, dir string, isnotify bool) { |
| 187 |
out := new(bytes.Buffer) |
| 188 |
var p, r string |
| 189 |
var goResult string |
| 190 |
if notNil(param) { |
| 191 |
p = ", params *" + goplsName(param) |
| 192 |
} |
| 193 |
if notNil(result) { |
| 194 |
goResult = goplsName(result) |
| 195 |
if !hasNilValue(goResult) { |
| 196 |
goResult = "*" + goResult |
| 197 |
} |
| 198 |
r = fmt.Sprintf("(%s, error)", goResult) |
| 199 |
} else { |
| 200 |
r = "error" |
| 201 |
} |
| 202 |
|
| 203 |
switch method { |
| 204 |
case "workspace/configuration": |
| 205 |
|
| 206 |
|
| 207 |
p = ", params *ParamConfiguration" |
| 208 |
r = "([]LSPAny, error)" |
| 209 |
goResult = "[]LSPAny" |
| 210 |
} |
| 211 |
fname := methodName(method) |
| 212 |
fmt.Fprintf(out, "func (s *%%sDispatcher) %s(ctx context.Context%s) %s {\n", |
| 213 |
fname, p, r) |
| 214 |
|
| 215 |
if !notNil(result) { |
| 216 |
if isnotify { |
| 217 |
if notNil(param) { |
| 218 |
fmt.Fprintf(out, "\treturn s.sender.Notify(ctx, %q, params)\n", method) |
| 219 |
} else { |
| 220 |
fmt.Fprintf(out, "\treturn s.sender.Notify(ctx, %q, nil)\n", method) |
| 221 |
} |
| 222 |
} else { |
| 223 |
if notNil(param) { |
| 224 |
fmt.Fprintf(out, "\treturn s.sender.Call(ctx, %q, params, nil)\n", method) |
| 225 |
} else { |
| 226 |
fmt.Fprintf(out, "\treturn s.sender.Call(ctx, %q, nil, nil)\n", method) |
| 227 |
} |
| 228 |
} |
| 229 |
} else { |
| 230 |
fmt.Fprintf(out, "\tvar result %s\n", goResult) |
| 231 |
if isnotify { |
| 232 |
if notNil(param) { |
| 233 |
fmt.Fprintf(out, "\ts.sender.Notify(ctx, %q, params)\n", method) |
| 234 |
} else { |
| 235 |
fmt.Fprintf(out, "\t\tif err := s.sender.Notify(ctx, %q, nil); err != nil {\n", method) |
| 236 |
} |
| 237 |
} else { |
| 238 |
if notNil(param) { |
| 239 |
fmt.Fprintf(out, "\t\tif err := s.sender.Call(ctx, %q, params, &result); err != nil {\n", method) |
| 240 |
} else { |
| 241 |
fmt.Fprintf(out, "\t\tif err := s.sender.Call(ctx, %q, nil, &result); err != nil {\n", method) |
| 242 |
} |
| 243 |
} |
| 244 |
fmt.Fprintf(out, "\t\treturn nil, err\n\t}\n\treturn result, nil\n") |
| 245 |
} |
| 246 |
out.WriteString("}\n") |
| 247 |
msg := out.String() |
| 248 |
switch dir { |
| 249 |
case "clientToServer": |
| 250 |
sfuncs[method] = fmt.Sprintf(msg, "server") |
| 251 |
case "serverToClient": |
| 252 |
cfuncs[method] = fmt.Sprintf(msg, "client") |
| 253 |
case "both": |
| 254 |
sfuncs[method] = fmt.Sprintf(msg, "server") |
| 255 |
cfuncs[method] = fmt.Sprintf(msg, "client") |
| 256 |
default: |
| 257 |
log.Fatalf("impossible direction %q", dir) |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
func genStructs(model *Model) { |
| 262 |
structures := make(map[string]*Structure) |
| 263 |
for _, s := range model.Structures { |
| 264 |
structures[s.Name] = s |
| 265 |
} |
| 266 |
for _, s := range model.Structures { |
| 267 |
out := new(bytes.Buffer) |
| 268 |
generateDoc(out, s.Documentation) |
| 269 |
nm := goName(s.Name) |
| 270 |
if nm == "string" { |
| 271 |
|
| 272 |
nm = "DocumentDiagnosticParams" |
| 273 |
} |
| 274 |
fmt.Fprintf(out, "//\n") |
| 275 |
out.WriteString(lspLink(model, camelCase(s.Name))) |
| 276 |
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(s.Line)) |
| 277 |
|
| 278 |
props := slices.Clone(s.Properties) |
| 279 |
if s.Name == "SymbolInformation" { |
| 280 |
for _, ex := range s.Extends { |
| 281 |
fmt.Fprintf(out, "\t// extends %s\n", ex.Name) |
| 282 |
props = append(props, structures[ex.Name].Properties...) |
| 283 |
} |
| 284 |
genProps(out, props, nm) |
| 285 |
} else { |
| 286 |
genProps(out, props, nm) |
| 287 |
for _, ex := range s.Extends { |
| 288 |
fmt.Fprintf(out, "\t%s\n", goName(ex.Name)) |
| 289 |
} |
| 290 |
} |
| 291 |
for _, ex := range s.Mixins { |
| 292 |
fmt.Fprintf(out, "\t%s\n", goName(ex.Name)) |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
if nm == "TextDocumentPositionParams" { |
| 297 |
out.WriteString("\t// Range is an optional field representing the user's text selection in the document.\n") |
| 298 |
out.WriteString("\t// If provided, the Position must be contained within this range.\n") |
| 299 |
out.WriteString("\t//\n") |
| 300 |
out.WriteString("\t// Note: This is a non-standard protocol extension. See microsoft/language-server-protocol#377.\n") |
| 301 |
out.WriteString("\tRange Range `json:\"range\"`") |
| 302 |
} |
| 303 |
out.WriteString("}\n") |
| 304 |
types[nm] = out.String() |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
types["LSPAny"] = "type LSPAny = any\n" |
| 310 |
|
| 311 |
types["DocumentDiagnosticReport"] = "type DocumentDiagnosticReport = Or_DocumentDiagnosticReport // (alias) \n" |
| 312 |
|
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
func camelCase(TitleCased string) string { |
| 317 |
return strings.ToLower(TitleCased[:1]) + TitleCased[1:] |
| 318 |
} |
| 319 |
|
| 320 |
func lspLink(model *Model, fragment string) string { |
| 321 |
|
| 322 |
parts := strings.Split(model.Version.Version, ".") |
| 323 |
return fmt.Sprintf("// See https://microsoft.github.io/language-server-protocol/specifications/lsp/%s.%s/specification#%s\n", |
| 324 |
parts[0], parts[1], |
| 325 |
fragment) |
| 326 |
} |
| 327 |
|
| 328 |
func genProps(out *bytes.Buffer, props []NameType, name string) { |
| 329 |
for _, p := range props { |
| 330 |
tp := goplsName(p.Type) |
| 331 |
if newNm, ok := renameProp[prop{name, p.Name}]; ok { |
| 332 |
usedRenameProp[prop{name, p.Name}] = true |
| 333 |
if tp == newNm { |
| 334 |
log.Printf("renameProp useless {%q, %q} for %s", name, p.Name, tp) |
| 335 |
} |
| 336 |
tp = newNm |
| 337 |
} |
| 338 |
|
| 339 |
omit, star := propStar(name, p, tp) |
| 340 |
json := fmt.Sprintf(" `json:\"%s\"`", p.Name) |
| 341 |
if omit { |
| 342 |
json = fmt.Sprintf(" `json:\"%s,omitempty\"`", p.Name) |
| 343 |
} |
| 344 |
generateDoc(out, p.Documentation) |
| 345 |
if docs := appendTypePropDocComments[name]; docs != nil { |
| 346 |
if doc, ok := docs[p.Name]; ok { |
| 347 |
out.WriteString(doc) |
| 348 |
} |
| 349 |
} |
| 350 |
if star { |
| 351 |
fmt.Fprintf(out, "\t%s *%s %s\n", goName(p.Name), tp, json) |
| 352 |
} else { |
| 353 |
fmt.Fprintf(out, "\t%s %s %s\n", goName(p.Name), tp, json) |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
if block, ok := appendTypeProp[name]; ok { |
| 358 |
out.WriteString(block) |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
func genAliases(model *Model) { |
| 363 |
for _, ta := range model.TypeAliases { |
| 364 |
out := new(bytes.Buffer) |
| 365 |
generateDoc(out, ta.Documentation) |
| 366 |
nm := goName(ta.Name) |
| 367 |
if nm != ta.Name { |
| 368 |
continue |
| 369 |
} |
| 370 |
tp := goplsName(ta.Type) |
| 371 |
fmt.Fprintf(out, "//\n") |
| 372 |
out.WriteString(lspLink(model, camelCase(ta.Name))) |
| 373 |
fmt.Fprintf(out, "type %s = %s // (alias)\n", nm, tp) |
| 374 |
types[nm] = out.String() |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
func genGenTypes() { |
| 379 |
for _, nt := range genTypes { |
| 380 |
out := new(bytes.Buffer) |
| 381 |
nm := goplsName(nt.typ) |
| 382 |
switch nt.kind { |
| 383 |
case "literal": |
| 384 |
fmt.Fprintf(out, "// created for Literal (%s)\n", nt.name) |
| 385 |
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1)) |
| 386 |
genProps(out, nt.properties, nt.name) |
| 387 |
case "or": |
| 388 |
if !strings.HasPrefix(nm, "Or") { |
| 389 |
|
| 390 |
continue |
| 391 |
} |
| 392 |
names := []string{} |
| 393 |
for _, t := range nt.items { |
| 394 |
if notNil(t) { |
| 395 |
names = append(names, goplsName(t)) |
| 396 |
} |
| 397 |
} |
| 398 |
sort.Strings(names) |
| 399 |
fmt.Fprintf(out, "// created for Or %v\n", names) |
| 400 |
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1)) |
| 401 |
fmt.Fprintf(out, "\tValue any `json:\"value\"`\n") |
| 402 |
case "and": |
| 403 |
fmt.Fprintf(out, "// created for And\n") |
| 404 |
fmt.Fprintf(out, "type %s struct {%s\n", nm, linex(nt.line+1)) |
| 405 |
for _, x := range nt.items { |
| 406 |
nm := goplsName(x) |
| 407 |
fmt.Fprintf(out, "\t%s\n", nm) |
| 408 |
} |
| 409 |
case "tuple": |
| 410 |
nt.name = "UIntCommaUInt" |
| 411 |
fmt.Fprintf(out, "//created for Tuple\ntype %s struct {%s\n", nm, linex(nt.line+1)) |
| 412 |
fmt.Fprintf(out, "\tFld0 uint32 `json:\"fld0\"`\n") |
| 413 |
fmt.Fprintf(out, "\tFld1 uint32 `json:\"fld1\"`\n") |
| 414 |
default: |
| 415 |
log.Fatalf("%s not handled", nt.kind) |
| 416 |
} |
| 417 |
out.WriteString("}\n") |
| 418 |
types[nm] = out.String() |
| 419 |
} |
| 420 |
} |
| 421 |
func genConsts(model *Model) { |
| 422 |
for _, e := range model.Enumerations { |
| 423 |
out := new(bytes.Buffer) |
| 424 |
generateDoc(out, e.Documentation) |
| 425 |
tp := goplsName(e.Type) |
| 426 |
nm := goName(e.Name) |
| 427 |
fmt.Fprintf(out, "type %s %s%s\n", nm, tp, linex(e.Line)) |
| 428 |
types[nm] = out.String() |
| 429 |
vals := new(bytes.Buffer) |
| 430 |
generateDoc(vals, e.Documentation) |
| 431 |
for _, v := range e.Values { |
| 432 |
generateDoc(vals, v.Documentation) |
| 433 |
nm := goName(v.Name) |
| 434 |
more, ok := disambiguate[e.Name] |
| 435 |
if ok { |
| 436 |
usedDisambiguate[e.Name] = true |
| 437 |
nm = more.prefix + nm + more.suffix |
| 438 |
nm = goName(nm) |
| 439 |
} |
| 440 |
var val string |
| 441 |
switch v := v.Value.(type) { |
| 442 |
case string: |
| 443 |
val = fmt.Sprintf("%q", v) |
| 444 |
case float64: |
| 445 |
val = fmt.Sprintf("%d", int(v)) |
| 446 |
default: |
| 447 |
log.Fatalf("impossible type %T", v) |
| 448 |
} |
| 449 |
fmt.Fprintf(vals, "\t%s %s = %s%s\n", nm, e.Name, val, linex(v.Line)) |
| 450 |
} |
| 451 |
consts[nm] = vals.String() |
| 452 |
} |
| 453 |
} |
| 454 |
func genMarshal() { |
| 455 |
for _, nt := range genTypes { |
| 456 |
nm := goplsName(nt.typ) |
| 457 |
if !strings.HasPrefix(nm, "Or") { |
| 458 |
continue |
| 459 |
} |
| 460 |
names := []string{} |
| 461 |
for _, t := range nt.items { |
| 462 |
if notNil(t) { |
| 463 |
names = append(names, goplsName(t)) |
| 464 |
} |
| 465 |
} |
| 466 |
sort.Strings(names) |
| 467 |
var buf bytes.Buffer |
| 468 |
fmt.Fprintf(&buf, "func (t %s) MarshalJSON() ([]byte, error) {\n", nm) |
| 469 |
buf.WriteString("\tswitch x := t.Value.(type){\n") |
| 470 |
for _, nmx := range names { |
| 471 |
fmt.Fprintf(&buf, "\tcase %s:\n", nmx) |
| 472 |
fmt.Fprintf(&buf, "\t\treturn json.Marshal(x)\n") |
| 473 |
} |
| 474 |
buf.WriteString("\tcase nil:\n\t\treturn []byte(\"null\"), nil\n\t}\n") |
| 475 |
fmt.Fprintf(&buf, "\treturn nil, fmt.Errorf(\"type %%T not one of %v\", t)\n", names) |
| 476 |
buf.WriteString("}\n\n") |
| 477 |
|
| 478 |
fmt.Fprintf(&buf, "func (t *%s) UnmarshalJSON(x []byte) error {\n", nm) |
| 479 |
buf.WriteString("\tif string(x) == \"null\" {\n\t\tt.Value = nil\n\t\t\treturn nil\n\t}\n") |
| 480 |
for i, nmx := range names { |
| 481 |
fmt.Fprintf(&buf, "\tvar h%d %s\n", i, nmx) |
| 482 |
fmt.Fprintf(&buf, "\tif err := json.Unmarshal(x, &h%d); err == nil {\n\t\tt.Value = h%d\n\t\t\treturn nil\n\t\t}\n", i, i) |
| 483 |
} |
| 484 |
fmt.Fprintf(&buf, "return &UnmarshalError{\"unmarshal failed to match one of %v\"}", names) |
| 485 |
buf.WriteString("}\n\n") |
| 486 |
jsons[nm] = buf.String() |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
func linex(n int) string { |
| 491 |
if *lineNumbers { |
| 492 |
return fmt.Sprintf(" // line %d", n) |
| 493 |
} |
| 494 |
return "" |
| 495 |
} |
| 496 |
|
| 497 |
func goplsName(t *Type) string { |
| 498 |
nm := typeNames[t] |
| 499 |
|
| 500 |
if newNm, ok := goplsType[nm]; ok { |
| 501 |
usedGoplsType[nm] = true |
| 502 |
nm = newNm |
| 503 |
} |
| 504 |
return nm |
| 505 |
} |
| 506 |
|
| 507 |
func notNil(t *Type) bool { |
| 508 |
return t != nil && (t.Kind != "base" || t.Name != "null") |
| 509 |
} |
| 510 |
|
| 511 |
func hasNilValue(t string) bool { |
| 512 |
|
| 513 |
if strings.HasPrefix(t, "[]") || strings.HasPrefix(t, "*") { |
| 514 |
return true |
| 515 |
} |
| 516 |
if t == "interface{}" || t == "any" { |
| 517 |
return true |
| 518 |
} |
| 519 |
|
| 520 |
return false |
| 521 |
} |