mirror of
https://github.com/golang/go
synced 2024-11-18 10:44:45 -07:00
6a8222ee25
gopls has many settings. We want to automatically generate documentation, and we want to allow clients to perform their own validation if they so desire. Using all three of AST, type information, and reflection, generate a JSON description of the settings and their default values. Add a gopls command that prints it. Add a documentation generator that uses it to write settings.md. I assumed that everything not explicitly documented was experimental, and moved it into that section. I also moved expandWorkspaceToModule to experimental; I hope it's not long for this world, personally. Along the way, rename many fields, make the enum matching case insensitive, and add a stringer call so that the defaults print nicely. Fixes golang/go#33544. Change-Id: Ibb652002933e355ed3c6038d6ca48345b39b3025 Reviewed-on: https://go-review.googlesource.com/c/tools/+/252322 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
// Copyright 2020 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
// Command generate updates settings.md from the UserOptions struct.
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"regexp"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
)
|
|
|
|
func main() {
|
|
if err := doMain(); err != nil {
|
|
fmt.Fprintf(os.Stderr, "Generation failed: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func doMain() error {
|
|
var opts map[string][]option
|
|
if err := json.Unmarshal([]byte(source.OptionsJson), &opts); err != nil {
|
|
return err
|
|
}
|
|
|
|
doc, err := ioutil.ReadFile("gopls/doc/settings.md")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
content, err := rewriteDoc(doc, opts)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := ioutil.WriteFile("gopls/doc/settings.md", content, 0); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type option struct {
|
|
Name string
|
|
Type string
|
|
Doc string
|
|
Default string
|
|
}
|
|
|
|
func rewriteDoc(doc []byte, categories map[string][]option) ([]byte, error) {
|
|
var err error
|
|
for _, cat := range []string{"User", "Experimental"} {
|
|
doc, err = rewriteSection(doc, categories, cat)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return doc, nil
|
|
}
|
|
|
|
func rewriteSection(doc []byte, categories map[string][]option, category string) ([]byte, error) {
|
|
section := bytes.NewBuffer(nil)
|
|
for _, opt := range categories[category] {
|
|
fmt.Fprintf(section, "### **%v** *%v*\n%v\nDefault: `%v`.\n", opt.Name, opt.Type, opt.Doc, opt.Default)
|
|
}
|
|
re := regexp.MustCompile(fmt.Sprintf(`(?s)<!-- BEGIN %v.* -->\n(.*?)<!-- END %v.* -->`, category, category))
|
|
idx := re.FindSubmatchIndex(doc)
|
|
if idx == nil {
|
|
return nil, fmt.Errorf("could not find section %v", category)
|
|
}
|
|
result := append([]byte(nil), doc[:idx[2]]...)
|
|
result = append(result, section.Bytes()...)
|
|
result = append(result, doc[idx[3]:]...)
|
|
return result, nil
|
|
}
|