google/keep-sorted

Allow string-valued options to contain spaces

JeffFaer opened this issue · 0 comments

For example, if I want to sort markdown lists:

  * foo
  * bar
  * [baz](./path/to/baz)

I would want that to be sorted as

  * bar
  * [baz](./path/to/baz)
  * foo

But the [ in front of baz interferes with that. One workaround would be to use ignore_prefixes, but there currently isn't any way to include a space in the ignored prefix. In this situation we would need something likeignore_prefixes="* ","* ["

This function needs to be updated so that string-valued options can accept values with spaces:

func parseBlockOption(f reflect.StructField, startLine string) (reflect.Value, error) {
key := strings.ToLower(f.Name)
if k, ok := f.Tag.Lookup("key"); ok {
key = k
}
regex := regexp.MustCompile(fmt.Sprintf(`(^|\s)%s=(?P<value>[^ ]+?)($|\s)`, regexp.QuoteMeta(key)))
if m := regex.FindStringSubmatchIndex(startLine); m != nil {
val := string(regex.ExpandString(nil, "${value}", startLine, m))
return parseValue(f, key, val)
}
return parseDefaultValue(f, key), nil
}