pkg/elvdoc: Fix the handling of options with spaces in the value.

Instead of using strings.Fields, use the actual Elvish parser to retrieve
options and arguments.

Among probably others, this fixes the elvdoc of "echo", whose usage previously
read (note the bogus $ in the default value of &sep):

echo &sep=' $' $value...
This commit is contained in:
Qi Xiao 2023-01-02 03:00:24 +00:00
parent 08007a52ce
commit 5fae5b29b6
2 changed files with 36 additions and 1 deletions

View File

@ -6,6 +6,8 @@ import (
"io"
"regexp"
"strings"
"src.elv.sh/pkg/parse"
)
// Docs records doc comments.
@ -90,7 +92,7 @@ func fnUsage(name, sig string) string {
var sb strings.Builder
sb.WriteString("```elvish\n")
sb.WriteString(name)
for _, field := range strings.Fields(sig) {
for _, field := range sigFields(sig) {
sb.WriteByte(' ')
if strings.HasPrefix(field, "&") {
sb.WriteString(field)
@ -104,6 +106,23 @@ func fnUsage(name, sig string) string {
return sb.String()
}
func sigFields(sig string) []string {
pn := &parse.Primary{}
// TODO: Handle error
parse.ParseAs(parse.Source{Code: "{|" + sig + "|}"}, pn, parse.Config{})
var fields []string
for _, n := range parse.Children(pn) {
if _, isSep := n.(*parse.Sep); isSep {
continue
}
s := strings.TrimSpace(parse.SourceText(n))
if s != "" {
fields = append(fields, s)
}
}
return fields
}
type docBlock struct {
id string
lines []string

View File

@ -67,6 +67,22 @@ var extractTests = []struct {
},
},
},
{
name: "option with space",
text: dedent(`
fn add {|a b &k=' '| }
`),
wantFns: []Entry{
{
Name: "add",
Content: dedent(tildeToBackquote(`
~~~elvish
add $a $b &k=' '
~~~
`)),
},
},
},
{
name: "fn with rest argument",
text: dedent(`