|
| 1 | +// Copyright 2024 The Go Language Server Authors |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +package generator |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/gobuffalo/flect" |
| 10 | + |
| 11 | + "go.lsp.dev/protocol/tools/protocol-gen/protocol" |
| 12 | +) |
| 13 | + |
| 14 | +var enumerationNames = map[string]string{ |
| 15 | + "SemanticTokenTypes": "language", |
| 16 | + "SemanticTokenModifiers": "language", |
| 17 | + "DocumentDiagnosticReportKind": "language", |
| 18 | + "ErrorCodes": "base", |
| 19 | + "LSPErrorCodes": "base", |
| 20 | + "FoldingRangeKind": "language", |
| 21 | + "SymbolKind": "language", |
| 22 | + "SymbolTag": "language", |
| 23 | + "UniquenessLevel": "language", |
| 24 | + "MonikerKind": "language", |
| 25 | + "InlayHintKind": "language", |
| 26 | + "MessageType": "window", |
| 27 | + "TextDocumentSyncKind": "lifecycle", |
| 28 | + "TextDocumentSaveReason": "document", |
| 29 | + "CompletionItemKind": "language", |
| 30 | + "CompletionItemTag": "language", |
| 31 | + "InsertTextFormat": "language", |
| 32 | + "InsertTextMode": "language", |
| 33 | + "DocumentHighlightKind": "language", |
| 34 | + "CodeActionKind": "language", |
| 35 | + "TraceValue": "basic", |
| 36 | + "MarkupKind": "basic", |
| 37 | + "LanguageKind": "basic", |
| 38 | + "InlineCompletionTriggerKind": "language", |
| 39 | + "PositionEncodingKind": "basic", |
| 40 | + "FileChangeType": "workspace", |
| 41 | + "WatchKind": "workspace", |
| 42 | + "DiagnosticSeverity": "basic", |
| 43 | + "DiagnosticTag": "basic", |
| 44 | + "CompletionTriggerKind": "language", |
| 45 | + "SignatureHelpTriggerKind": "language", |
| 46 | + "CodeActionTriggerKind": "language", |
| 47 | + "FileOperationPatternKind": "workspace", |
| 48 | + "NotebookCellKind": "document", |
| 49 | + "ResourceOperationKind": "basic", |
| 50 | + "FailureHandlingKind": "basic", |
| 51 | + "PrepareSupportDefaultBehavior": "language", |
| 52 | + "TokenFormat": "language", |
| 53 | +} |
| 54 | + |
| 55 | +// Enumerations generates Enumerations Go type from the metaModel schema definition. |
| 56 | +func (gen *Generator) Enumerations(enumerations []*protocol.Enumeration) error { |
| 57 | + for _, enum := range enumerations { |
| 58 | + enumName := flect.Pascalize(enum.Name) |
| 59 | + filename, ok := enumerationNames[enumName] |
| 60 | + if !ok { |
| 61 | + panic(fmt.Sprintf("not found %s enumerations file", enumName)) |
| 62 | + } |
| 63 | + |
| 64 | + // Init filename printers |
| 65 | + g := NewPrinter(filename) |
| 66 | + gen.enumerations = append(gen.enumerations, g) |
| 67 | + |
| 68 | + // write Documentation |
| 69 | + if enum.Documentation != "" { |
| 70 | + g.PP(`// `, enumName, normalizeDocumentation(enum.Documentation)) |
| 71 | + } |
| 72 | + if enum.Since != "" { |
| 73 | + if enum.Documentation != "" { |
| 74 | + g.PP(`//`) |
| 75 | + } |
| 76 | + g.P(`// @since `, enum.Since) |
| 77 | + if enum.Proposed { |
| 78 | + g.P(` proposed`) |
| 79 | + } |
| 80 | + g.P("\n") |
| 81 | + } |
| 82 | + |
| 83 | + g.P(`type `, enumName) |
| 84 | + switch e := enum.Type.(type) { |
| 85 | + case protocol.BaseType: |
| 86 | + g.P(` `, e.String()) |
| 87 | + default: |
| 88 | + panic(fmt.Sprintf("enumerations: %#v\n", e)) |
| 89 | + } |
| 90 | + g.PP("\n") |
| 91 | + |
| 92 | + g.PP(`const (`) |
| 93 | + for i, val := range enum.Values { |
| 94 | + // write Documentation |
| 95 | + if val.Documentation != "" { |
| 96 | + g.PP(` // `, flect.Pascalize(val.Name), enumName, normalizeDocumentation(val.Documentation)) |
| 97 | + } |
| 98 | + if val.Since != "" { |
| 99 | + if val.Documentation != "" { |
| 100 | + g.PP(` //`) |
| 101 | + } |
| 102 | + g.P(` // @since `, val.Since) |
| 103 | + if val.Proposed { |
| 104 | + g.P(` proposed`) |
| 105 | + } |
| 106 | + g.P("\n") |
| 107 | + } |
| 108 | + |
| 109 | + g.P(` `, flect.Pascalize(val.Name), enumName) |
| 110 | + g.P(` `, enumName, ` = `, val.Value) |
| 111 | + g.P("\n") |
| 112 | + |
| 113 | + // Add newline per fields |
| 114 | + if i < len(enum.Values)-1 { |
| 115 | + g.P("\n") |
| 116 | + } |
| 117 | + } |
| 118 | + g.PP(`)`, "\n") |
| 119 | + } |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
0 commit comments