golang.org/x/tools/gopls@v0.15.3/internal/settings/json.go (about) 1 // Copyright 2023 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 settings 6 7 import ( 8 "fmt" 9 "io" 10 "regexp" 11 "strings" 12 ) 13 14 type APIJSON struct { 15 Options map[string][]*OptionJSON 16 Commands []*CommandJSON 17 Lenses []*LensJSON 18 Analyzers []*AnalyzerJSON 19 Hints []*HintJSON 20 } 21 22 type OptionJSON struct { 23 Name string 24 Type string 25 Doc string 26 EnumKeys EnumKeys 27 EnumValues []EnumValue 28 Default string 29 Status string 30 Hierarchy string 31 } 32 33 func (o *OptionJSON) String() string { 34 return o.Name 35 } 36 37 func (o *OptionJSON) Write(w io.Writer) { 38 fmt.Fprintf(w, "**%v** *%v*\n\n", o.Name, o.Type) 39 writeStatus(w, o.Status) 40 enumValues := collectEnums(o) 41 fmt.Fprintf(w, "%v%v\nDefault: `%v`.\n\n", o.Doc, enumValues, o.Default) 42 } 43 44 func writeStatus(section io.Writer, status string) { 45 switch status { 46 case "": 47 case "advanced": 48 fmt.Fprint(section, "**This is an advanced setting and should not be configured by most `gopls` users.**\n\n") 49 case "debug": 50 fmt.Fprint(section, "**This setting is for debugging purposes only.**\n\n") 51 case "experimental": 52 fmt.Fprint(section, "**This setting is experimental and may be deleted.**\n\n") 53 default: 54 fmt.Fprintf(section, "**Status: %s.**\n\n", status) 55 } 56 } 57 58 var parBreakRE = regexp.MustCompile("\n{2,}") 59 60 func collectEnums(opt *OptionJSON) string { 61 var b strings.Builder 62 write := func(name, doc string) { 63 if doc != "" { 64 unbroken := parBreakRE.ReplaceAllString(doc, "\\\n") 65 fmt.Fprintf(&b, "* %s\n", strings.TrimSpace(unbroken)) 66 } else { 67 fmt.Fprintf(&b, "* `%s`\n", name) 68 } 69 } 70 if len(opt.EnumValues) > 0 && opt.Type == "enum" { 71 b.WriteString("\nMust be one of:\n\n") 72 for _, val := range opt.EnumValues { 73 write(val.Value, val.Doc) 74 } 75 } else if len(opt.EnumKeys.Keys) > 0 && shouldShowEnumKeysInSettings(opt.Name) { 76 b.WriteString("\nCan contain any of:\n\n") 77 for _, val := range opt.EnumKeys.Keys { 78 write(val.Name, val.Doc) 79 } 80 } 81 return b.String() 82 } 83 84 func shouldShowEnumKeysInSettings(name string) bool { 85 // These fields have too many possible options to print. 86 return !(name == "analyses" || name == "codelenses" || name == "hints") 87 } 88 89 type EnumKeys struct { 90 ValueType string 91 Keys []EnumKey 92 } 93 94 type EnumKey struct { 95 Name string 96 Doc string 97 Default string 98 } 99 100 type EnumValue struct { 101 Value string 102 Doc string 103 } 104 105 type CommandJSON struct { 106 Command string 107 Title string 108 Doc string 109 ArgDoc string 110 ResultDoc string 111 } 112 113 func (c *CommandJSON) String() string { 114 return c.Command 115 } 116 117 func (c *CommandJSON) Write(w io.Writer) { 118 fmt.Fprintf(w, "### **%v**\nIdentifier: `%v`\n\n%v\n\n", c.Title, c.Command, c.Doc) 119 if c.ArgDoc != "" { 120 fmt.Fprintf(w, "Args:\n\n```\n%s\n```\n\n", c.ArgDoc) 121 } 122 if c.ResultDoc != "" { 123 fmt.Fprintf(w, "Result:\n\n```\n%s\n```\n\n", c.ResultDoc) 124 } 125 } 126 127 type LensJSON struct { 128 Lens string 129 Title string 130 Doc string 131 } 132 133 func (l *LensJSON) String() string { 134 return l.Title 135 } 136 137 func (l *LensJSON) Write(w io.Writer) { 138 fmt.Fprintf(w, "%s (%s): %s", l.Title, l.Lens, l.Doc) 139 } 140 141 type AnalyzerJSON struct { 142 Name string 143 Doc string 144 URL string 145 Default bool 146 } 147 148 func (a *AnalyzerJSON) String() string { 149 return a.Name 150 } 151 152 func (a *AnalyzerJSON) Write(w io.Writer) { 153 fmt.Fprintf(w, "%s (%s): %v", a.Name, a.Doc, a.Default) 154 } 155 156 type HintJSON struct { 157 Name string 158 Doc string 159 Default bool 160 } 161 162 func (h *HintJSON) String() string { 163 return h.Name 164 } 165 166 func (h *HintJSON) Write(w io.Writer) { 167 fmt.Fprintf(w, "%s (%s): %v", h.Name, h.Doc, h.Default) 168 }