| 396 | } |
| 397 | |
| 398 | func (c *configAST) beforeComment(path ...any) []byte { |
| 399 | elem := c.root |
| 400 | for _, pathItem := range path { |
| 401 | obj := elem.Value.(*hujson.Object) |
| 402 | i, ok := pathItem.(int) |
| 403 | if !ok { |
| 404 | i = c.memberIndex(obj, pathItem.(string)) |
| 405 | } |
| 406 | if i == -1 { |
| 407 | return nil |
| 408 | } |
| 409 | elem = obj.Members[i].Value |
| 410 | } |
| 411 | |
| 412 | // Match all single are multi line comments. |
| 413 | re := regexp.MustCompile(`(?:\/\/(.*?)\n)|(?s:\/\*(.*?)\*\/)`) |
| 414 | |
| 415 | return bytes.TrimSpace( |
| 416 | re.ReplaceAllFunc(elem.BeforeExtra, func(s []byte) []byte { |
| 417 | singleLineRe := regexp.MustCompile(`\/\/(.*?)\n`) |
| 418 | multiLineRe := regexp.MustCompile(`(?s:\/\*(.*?)\*\/)`) |
| 419 | |
| 420 | if singleLineRe.Match(s) { |
| 421 | return singleLineRe.ReplaceAll(s, []byte("$1\n")) |
| 422 | } else if multiLineRe.Match(s) { |
| 423 | return multiLineRe.ReplaceAll(s, []byte("$1")) |
| 424 | } |
| 425 | return s |
| 426 | }), |
| 427 | ) |
| 428 | } |
| 429 | |
| 430 | func (c *configAST) createMemberIfMissing(key string) *hujson.ObjectMember { |
| 431 | i := c.memberIndex(c.root.Value.(*hujson.Object), key) |