diff --git a/internal/channels/telegram/richblocks.go b/internal/channels/telegram/richblocks.go index 95271d960..a9ac94d8d 100644 --- a/internal/channels/telegram/richblocks.go +++ b/internal/channels/telegram/richblocks.go @@ -124,7 +124,7 @@ func markdownToBlocks(md string) []models.InputRichBlock { return nil } p := &markdownBlockParser{} - for _, raw := range strings.Split(md, "\n") { + for raw := range strings.SplitSeq(md, "\n") { p.handleLine(strings.TrimRight(raw, "\r")) } p.closeCode() diff --git a/internal/infrastructure/configuration/tomlwrite/tomlwrite.go b/internal/infrastructure/configuration/tomlwrite/tomlwrite.go index 7fe731425..ebb22dfad 100644 --- a/internal/infrastructure/configuration/tomlwrite/tomlwrite.go +++ b/internal/infrastructure/configuration/tomlwrite/tomlwrite.go @@ -89,11 +89,51 @@ func Generate(template []byte, edits []Edit) ([]byte, error) { // edit's table, and returns the result. See the package doc for the // matching and placement rules, and their limitations. func Apply(src []byte, edits []Edit) ([]byte, error) { + pending, order, err := collectPending(edits) + if err != nil { + return nil, err + } + + trailingNewline := len(src) == 0 || src[len(src)-1] == '\n' + text := strings.TrimSuffix(string(src), "\n") + var lines []string + if text != "" || len(src) > 0 { + lines = strings.Split(text, "\n") + } + + a := &applier{ + lines: lines, + pending: pending, + order: order, + replace: map[int]string{}, + insertAfter: map[int][]string{}, + } + a.indexTables() + + tables := make([]string, 0, len(order)) + for t := range order { + tables = append(tables, t) + } + sort.Strings(tables) + for _, t := range tables { + a.resolveTable(t) + } + + result := a.render() + if trailingNewline { + result += "\n" + } + return []byte(result), nil +} + +// collectPending groups edits by table and key, and records each table's +// keys in the caller's edit order for deterministic appends. +func collectPending(edits []Edit) (map[string]map[string]string, map[string][]string, error) { pending := map[string]map[string]string{} - order := map[string][]string{} // preserves caller's edit order per table for deterministic appends + order := map[string][]string{} for _, e := range edits { if e.Key == "" { - return nil, fmt.Errorf("tomlwrite: edit has empty key (table %q)", e.Table) + return nil, nil, fmt.Errorf("tomlwrite: edit has empty key (table %q)", e.Table) } t := pending[e.Table] if t == nil { @@ -105,18 +145,30 @@ func Apply(src []byte, edits []Edit) ([]byte, error) { } t[e.Key] = e.Value } + return pending, order, nil +} - trailingNewline := len(src) == 0 || src[len(src)-1] == '\n' - text := strings.TrimSuffix(string(src), "\n") - var lines []string - if text != "" || len(src) > 0 { - lines = strings.Split(text, "\n") - } +// applier holds the line-indexed state Apply threads through resolving +// each edited table: which table each line belongs to (active or +// commented-out), and the accumulated replace/insert/append operations. +type applier struct { + lines []string + pending map[string]map[string]string + order map[string][]string + activeTableAt []string + commentedTableAt []string + replace map[int]string + insertAfter map[int][]string + appendBlocks [][]string +} - activeTableAt := make([]string, len(lines)) - commentedTableAt := make([]string, len(lines)) +// indexTables computes, for every line, which active or commented-out +// table it belongs to. +func (a *applier) indexTables() { + a.activeTableAt = make([]string, len(a.lines)) + a.commentedTableAt = make([]string, len(a.lines)) activeTable, commentedTable := "", "" - for i, line := range lines { + for i, line := range a.lines { switch { case activeHeaderRe.MatchString(line): activeTable = activeHeaderRe.FindStringSubmatch(line)[1] @@ -126,139 +178,136 @@ func Apply(src []byte, edits []Edit) ([]byte, error) { case strings.TrimSpace(line) == "": commentedTable = "" } - activeTableAt[i] = activeTable - commentedTableAt[i] = commentedTable + a.activeTableAt[i] = activeTable + a.commentedTableAt[i] = commentedTable + } +} + +// resolveTable satisfies every pending edit for one table, in priority +// order: rewrite an active "key = value" line, else uncomment a commented +// one (and its header), else append a new key. +func (a *applier) resolveTable(table string) { + keys := a.order[table] + if len(keys) == 0 { + return + } + remaining := map[string]bool{} + for _, k := range keys { + remaining[k] = true } - replace := map[int]string{} - insertAfter := map[int][]string{} - var appendBlocks [][]string + a.matchActiveKeys(table, remaining) + if len(remaining) == 0 { + return + } + headerLine := a.uncommentKeys(table, remaining) + if len(remaining) == 0 { + return + } + a.appendMissingKeys(table, keys, remaining, headerLine) +} - resolveTable := func(table string) { - keys := order[table] - if len(keys) == 0 { - return +// matchActiveKeys rewrites every remaining key that already has an active +// "key = value" line in table, removing it from remaining. +func (a *applier) matchActiveKeys(table string, remaining map[string]bool) { + for i, line := range a.lines { + if a.activeTableAt[i] != table { + continue } - remaining := map[string]bool{} - for _, k := range keys { - remaining[k] = true + m := activeKeyRe.FindStringSubmatch(line) + if m == nil || !remaining[m[2]] { + continue } + a.replace[i] = m[1] + m[2] + m[3] + a.pending[table][m[2]] + trailingComment(m[4]) + delete(remaining, m[2]) + } +} - // 1) active "key = value" lines in this table. - for i, line := range lines { - if activeTableAt[i] != table { - continue - } - m := activeKeyRe.FindStringSubmatch(line) - if m == nil || !remaining[m[2]] { - continue - } - replace[i] = m[1] + m[2] + m[3] + pending[table][m[2]] + trailingComment(m[4]) - delete(remaining, m[2]) +// uncommentKeys rewrites every remaining key that has a commented-out +// "# key = value" line in table, uncommenting the table header (once) and +// the key line, removing matched keys from remaining. Returns the header +// line index, or -1 if table has no commented header. +func (a *applier) uncommentKeys(table string, remaining map[string]bool) int { + headerLine := -1 + for i, line := range a.lines { + if a.commentedTableAt[i] == table && commentedHeaderRe.MatchString(line) { + headerLine = i // last matching header wins; there should only be one } - if len(remaining) == 0 { - return + } + for i, line := range a.lines { + if a.commentedTableAt[i] != table { + continue } + if commentedHeaderRe.MatchString(line) { + continue + } + m := commentedKeyRe.FindStringSubmatch(line) + if m == nil || !remaining[m[2]] { + continue + } + if headerLine >= 0 { + if _, done := a.replace[headerLine]; !done { + a.replace[headerLine] = "[" + table + "]" + } + } + a.replace[i] = m[1] + m[2] + m[3] + a.pending[table][m[2]] + trailingComment(m[4]) + delete(remaining, m[2]) + } + return headerLine +} - // 2) commented "# key = value" lines in this table -- uncomment - // the header (once) and the key line. - headerLine := -1 - for i, line := range lines { - if commentedTableAt[i] == table && commentedHeaderRe.MatchString(line) { - headerLine = i // last matching header wins; there should only be one - } +// appendMissingKeys adds every key in remaining that no line in table +// satisfied: after the table's last active line, after its header if it +// only exists commented-out, or as a brand-new table block. +func (a *applier) appendMissingKeys(table string, keys []string, remaining map[string]bool, headerLine int) { + var toAdd []string + for _, k := range keys { + if remaining[k] { + toAdd = append(toAdd, k) } - lastCommentedLine := -1 - for i, line := range lines { - if commentedTableAt[i] != table { - continue - } - if commentedHeaderRe.MatchString(line) { - lastCommentedLine = i - continue - } - m := commentedKeyRe.FindStringSubmatch(line) - if m == nil { - lastCommentedLine = i - continue - } - lastCommentedLine = i - if !remaining[m[2]] { - continue - } - if headerLine >= 0 { - if _, done := replace[headerLine]; !done { - replace[headerLine] = "[" + table + "]" - } - } - replace[i] = m[1] + m[2] + m[3] + pending[table][m[2]] + trailingComment(m[4]) - delete(remaining, m[2]) - } - if len(remaining) == 0 { - return - } - - // 3) key not found anywhere -- add it. - var toAdd []string - for _, k := range keys { - if remaining[k] { - toAdd = append(toAdd, k) - } - } - sort.Strings(toAdd) - var newLines []string - for _, k := range toAdd { - newLines = append(newLines, k+" = "+pending[table][k]) - } - - lastActiveLine := -1 - for i := range lines { - if activeTableAt[i] == table { - lastActiveLine = i - } - } - switch { - case lastActiveLine >= 0: - insertAfter[lastActiveLine] = append(insertAfter[lastActiveLine], newLines...) - case headerLine >= 0: - if _, done := replace[headerLine]; !done { - replace[headerLine] = "[" + table + "]" - } - insertAfter[headerLine] = append(insertAfter[headerLine], newLines...) - default: - block := append([]string{"", "[" + table + "]"}, newLines...) - appendBlocks = append(appendBlocks, block) - } - _ = lastCommentedLine + } + sort.Strings(toAdd) + var newLines []string + for _, k := range toAdd { + newLines = append(newLines, k+" = "+a.pending[table][k]) } - tables := make([]string, 0, len(order)) - for t := range order { - tables = append(tables, t) + lastActiveLine := -1 + for i := range a.lines { + if a.activeTableAt[i] == table { + lastActiveLine = i + } } - sort.Strings(tables) - for _, t := range tables { - resolveTable(t) + switch { + case lastActiveLine >= 0: + a.insertAfter[lastActiveLine] = append(a.insertAfter[lastActiveLine], newLines...) + case headerLine >= 0: + if _, done := a.replace[headerLine]; !done { + a.replace[headerLine] = "[" + table + "]" + } + a.insertAfter[headerLine] = append(a.insertAfter[headerLine], newLines...) + default: + block := append([]string{"", "[" + table + "]"}, newLines...) + a.appendBlocks = append(a.appendBlocks, block) } +} - out := make([]string, 0, len(lines)+8) - for i, line := range lines { - if r, ok := replace[i]; ok { +// render assembles the final text from the original lines plus every +// replace/insert/append operation resolveTable recorded. +func (a *applier) render() string { + out := make([]string, 0, len(a.lines)+8) + for i, line := range a.lines { + if r, ok := a.replace[i]; ok { out = append(out, r) } else { out = append(out, line) } - out = append(out, insertAfter[i]...) + out = append(out, a.insertAfter[i]...) } - for _, block := range appendBlocks { + for _, block := range a.appendBlocks { out = append(out, block...) } - - result := strings.Join(out, "\n") - if trailingNewline { - result += "\n" - } - return []byte(result), nil + return strings.Join(out, "\n") } // trailingComment returns the " # ..." suffix of rest, if rest carries an diff --git a/internal/tools/builtin/find.go b/internal/tools/builtin/find.go index 59bb4b976..8b73f395f 100644 --- a/internal/tools/builtin/find.go +++ b/internal/tools/builtin/find.go @@ -210,60 +210,16 @@ func findWalkVisitor( default: } - // Calculate depth relative to searchPath. - if p.MaxDepth > 0 && walkPath != searchPath { - relDepth := strings.Count(strings.TrimPrefix(walkPath, searchPath+string(filepath.Separator)), string(filepath.Separator)) - if relDepth >= p.MaxDepth { - if d.IsDir() { - return filepath.SkipDir - } - return nil - } - } - - base := d.Name() - // Skip hidden entries like .git, .env, etc. - if strings.HasPrefix(base, ".") && walkPath != searchPath { - if d.IsDir() { - return filepath.SkipDir - } - return nil - } - - // Apply exclude filter to both files and directories. - if excludeRe != nil { - if excludeRe.MatchString(base) { - if d.IsDir() { - return filepath.SkipDir - } - return nil - } - } - - // Skip root directory itself. if walkPath == searchPath { + return nil // skip root directory itself + } + if skip, skipErr := findSkipEntry(walkPath, d, searchPath, p, excludeRe); skip || skipErr != nil { + return skipErr + } + if !findMatchesFilters(d, p, patternRe) { return nil } - // Type filter. - switch p.Type { - case "file": - if d.IsDir() { - return nil - } - case "directory": - if !d.IsDir() { - return nil - } - } - - // Pattern filter. - if patternRe != nil { - if !patternRe.MatchString(base) { - return nil - } - } - // Use path relative to cwd for output consistency. outputPath, err := filepath.Rel(cwd, walkPath) if err != nil { @@ -274,6 +230,58 @@ func findWalkVisitor( } } +// findSkipEntry reports whether walkPath should be skipped entirely (depth +// limit, hidden entry, exclude pattern), returning filepath.SkipDir as the +// error when d is a directory so WalkDir does not descend into it. +func findSkipEntry(walkPath string, d os.DirEntry, searchPath string, p FindParams, excludeRe *regexp.Regexp) (bool, error) { + skipOrDescend := func() error { + if d.IsDir() { + return filepath.SkipDir + } + return nil + } + + // Calculate depth relative to searchPath. + if p.MaxDepth > 0 { + relDepth := strings.Count(strings.TrimPrefix(walkPath, searchPath+string(filepath.Separator)), string(filepath.Separator)) + if relDepth >= p.MaxDepth { + return true, skipOrDescend() + } + } + + base := d.Name() + // Skip hidden entries like .git, .env, etc. + if strings.HasPrefix(base, ".") { + return true, skipOrDescend() + } + + // Apply exclude filter to both files and directories. + if excludeRe != nil && excludeRe.MatchString(base) { + return true, skipOrDescend() + } + + return false, nil +} + +// findMatchesFilters reports whether d passes the type and name-pattern +// filters. +func findMatchesFilters(d os.DirEntry, p FindParams, patternRe *regexp.Regexp) bool { + switch p.Type { + case "file": + if d.IsDir() { + return false + } + case "directory": + if !d.IsDir() { + return false + } + } + if patternRe != nil && !patternRe.MatchString(d.Name()) { + return false + } + return true +} + func isFdBinary(binary string) bool { base := filepath.Base(binary) base = strings.TrimSuffix(base, filepath.Ext(base)) // strip .exe on Windows