| 414 | } |
| 415 | |
| 416 | func findNestedSectionEnd(lines []string, foundLine, baseIndentLevel int) int { |
| 417 | endLine := len(lines) - 1 // Default to end of file |
| 418 | targetLevel := baseIndentLevel - 1 // The level of the key we found |
| 419 | |
| 420 | for lineNum := foundLine + 1; lineNum < len(lines); lineNum++ { |
| 421 | line := lines[lineNum] |
| 422 | trimmedLine := strings.TrimSpace(line) |
| 423 | |
| 424 | if trimmedLine == "" || strings.HasPrefix(trimmedLine, "#") { |
| 425 | continue |
| 426 | } |
| 427 | |
| 428 | lineLevel := (len(line) - len(strings.TrimLeft(line, " \t"))) / 2 |
| 429 | |
| 430 | // If we find a line at the same or lower level than our target, |
| 431 | // the nested section ends at the previous line |
| 432 | if lineLevel <= targetLevel { |
| 433 | endLine = lineNum - 1 |
| 434 | break |
| 435 | } |
| 436 | } |
| 437 | return endLine |
| 438 | } |