feat(processor): route preview paths through StripComments

This commit is contained in:
2026-07-02 09:20:06 -05:00
parent 7ef198016e
commit bbc49efbfe
3 changed files with 58 additions and 100 deletions
+21
View File
@@ -220,3 +220,24 @@ func changed(changes []LineChange) bool {
} }
return false return false
} }
func changesToInfos(changes []LineChange) ([]changeInfo, int, int, int) {
var infos []changeInfo
changedCount, keptCount, preservedCount := 0, 0, 0
for _, c := range changes {
switch c.Action {
case "removed":
changedCount++
infos = append(infos, changeInfo{c.Num, c.Original, "", "removed"})
case "modified":
changedCount++
infos = append(infos, changeInfo{c.Num, c.Original, c.Result, "modified"})
case "preserved":
preservedCount++
infos = append(infos, changeInfo{c.Num, c.Original, c.Original, "preserved"})
default:
keptCount++
}
}
return infos, changedCount, keptCount, preservedCount
}
+22 -56
View File
@@ -1,7 +1,6 @@
package processor package processor
import ( import (
"bufio"
"fmt" "fmt"
"os" "os"
"strings" "strings"
@@ -249,11 +248,6 @@ func (p *Processor) removeCommentsFromLine(line string, language types.Language,
} }
func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.LineRange, totals *GitTotals) error { func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.LineRange, totals *GitTotals) error {
language, err := DetectLanguage(filename)
if err != nil {
return err
}
cfg, _, err := config.Load() cfg, _, err := config.Load()
if err != nil { if err != nil {
cfg = config.Default() cfg = config.Default()
@@ -264,17 +258,33 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
contextLines = p.cli.ContextLines contextLines = p.cli.ContextLines
} }
file, err := os.Open(filename) src, err := os.ReadFile(filename)
if err != nil {
return err
}
lines := strings.Split(strings.TrimSuffix(string(src), "\n"), "\n")
var inRange func(int) bool
if len(lineRanges) > 0 {
inRange = func(n int) bool { return git.IsInLineRanges(n, lineRanges) }
}
_, changes, err := StripComments(src, filename, cfg, Options{
Inline: p.cli.Inline,
Block: p.cli.Block,
PreserveLines: p.cli.PreserveLines,
InRange: inRange,
})
if err != nil { if err != nil {
return err return err
} }
defer file.Close()
red := color.New(color.FgRed, color.CrossedOut) red := color.New(color.FgRed, color.CrossedOut)
green := color.New(color.FgGreen) green := color.New(color.FgGreen)
blue := color.New(color.FgBlue) blue := color.New(color.FgBlue)
gray := color.New(color.FgHiBlack) gray := color.New(color.FgHiBlack)
yellow := color.New(color.FgYellow) yellow := color.New(color.FgYellow)
cyan := color.New(color.FgCyan)
dimGray := color.New(color.FgWhite, color.Faint) dimGray := color.New(color.FgWhite, color.Faint)
fmt.Printf("\n%s %s\n", yellow.Sprint("Git Preview:"), filename) fmt.Printf("\n%s %s\n", yellow.Sprint("Git Preview:"), filename)
@@ -285,60 +295,18 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
} }
fmt.Println() fmt.Println()
scanner := bufio.NewScanner(file) infos, changedCount, keptCount, preservedCount := changesToInfos(changes)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil { if len(infos) == 0 {
return err
}
keptCount := 0
changedCount := 0
preservedCount := 0
processEntireFile := len(lineRanges) == 0
var changes []changeInfo
for i, line := range lines {
lineNum := i + 1
shouldProcess := processEntireFile || git.IsInLineRanges(lineNum, lineRanges)
if shouldProcess {
newLine := p.removeCommentsFromLine(line, language, cfg)
if newLine != line {
changedCount++
if newLine == "" {
changes = append(changes, changeInfo{lineNum, line, newLine, "removed"})
} else {
changes = append(changes, changeInfo{lineNum, line, newLine, "modified"})
}
} else {
hasComment := p.lineHasComment(line, language)
if hasComment {
preservedCount++
changes = append(changes, changeInfo{lineNum, line, line, "preserved"})
} else {
keptCount++
}
}
} else {
keptCount++
}
}
if len(changes) == 0 {
fmt.Printf("%s No comments found to remove\n", gray.Sprint("→")) fmt.Printf("%s No comments found to remove\n", gray.Sprint("→"))
fmt.Println() fmt.Println()
return nil return nil
} }
if contextLines > 0 { if contextLines > 0 {
p.displayChangesWithContext(lines, changes, contextLines, red, green, blue, gray, dimGray) p.displayChangesWithContext(lines, infos, contextLines, red, green, blue, gray, dimGray)
} else { } else {
for _, change := range changes { for _, change := range infos {
lineNumStr := gray.Sprintf("%4d", change.lineNum) lineNumStr := gray.Sprintf("%4d", change.lineNum)
switch change.changeType { switch change.changeType {
case "removed": case "removed":
@@ -347,7 +315,6 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
fmt.Printf("%s %s %s\n", lineNumStr, red.Sprint("~"), red.Sprint(change.oldLine)) fmt.Printf("%s %s %s\n", lineNumStr, red.Sprint("~"), red.Sprint(change.oldLine))
fmt.Printf("%s %s %s\n", lineNumStr, green.Sprint("+"), green.Sprint(change.newLine)) fmt.Printf("%s %s %s\n", lineNumStr, green.Sprint("+"), green.Sprint(change.newLine))
case "preserved": case "preserved":
cyan := color.New(color.FgCyan)
fmt.Printf("%s %s %s\n", lineNumStr, cyan.Sprint("P"), change.oldLine) fmt.Printf("%s %s %s\n", lineNumStr, cyan.Sprint("P"), change.oldLine)
} }
} }
@@ -357,7 +324,6 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
fmt.Printf("%s %d lines would be changed\n", yellow.Sprint("~"), changedCount) fmt.Printf("%s %d lines would be changed\n", yellow.Sprint("~"), changedCount)
fmt.Printf("%s %d lines would be kept\n", green.Sprint("✓"), keptCount) fmt.Printf("%s %d lines would be kept\n", green.Sprint("✓"), keptCount)
if preservedCount > 0 { if preservedCount > 0 {
cyan := color.New(color.FgCyan)
fmt.Printf("%s %d comments would be preserved\n", cyan.Sprint("P"), preservedCount) fmt.Printf("%s %d comments would be preserved\n", cyan.Sprint("P"), preservedCount)
} }
fmt.Println() fmt.Println()
+15 -44
View File
@@ -1,7 +1,6 @@
package processor package processor
import ( import (
"bufio"
"fmt" "fmt"
"io" "io"
"io/fs" "io/fs"
@@ -192,7 +191,6 @@ func (p *Processor) createBackup(filename string) error {
} }
func (p *Processor) showPreview(filename string, language types.Language) error { func (p *Processor) showPreview(filename string, language types.Language) error {
cfg, _, err := config.Load() cfg, _, err := config.Load()
if err != nil { if err != nil {
cfg = config.Default() cfg = config.Default()
@@ -203,11 +201,20 @@ func (p *Processor) showPreview(filename string, language types.Language) error
contextLines = p.cli.ContextLines contextLines = p.cli.ContextLines
} }
file, err := os.Open(filename) src, err := os.ReadFile(filename)
if err != nil {
return err
}
lines := strings.Split(strings.TrimSuffix(string(src), "\n"), "\n")
_, changes, err := StripComments(src, filename, cfg, Options{
Inline: p.cli.Inline,
Block: p.cli.Block,
PreserveLines: p.cli.PreserveLines,
})
if err != nil { if err != nil {
return err return err
} }
defer file.Close()
red := color.New(color.FgRed, color.CrossedOut) red := color.New(color.FgRed, color.CrossedOut)
green := color.New(color.FgGreen) green := color.New(color.FgGreen)
@@ -218,54 +225,18 @@ func (p *Processor) showPreview(filename string, language types.Language) error
fmt.Printf("\n%s %s\n\n", yellow.Sprint("Preview:"), filename) fmt.Printf("\n%s %s\n\n", yellow.Sprint("Preview:"), filename)
scanner := bufio.NewScanner(file) infos, changedCount, keptCount, preservedCount := changesToInfos(changes)
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil { if len(infos) == 0 {
return err
}
changedCount := 0
keptCount := 0
preservedCount := 0
var changes []changeInfo
for i, line := range lines {
lineNum := i + 1
newLine := p.removeCommentsFromLine(line, language, cfg)
if newLine != line {
changedCount++
if newLine == "" {
changes = append(changes, changeInfo{lineNum, line, newLine, "removed"})
} else {
changes = append(changes, changeInfo{lineNum, line, newLine, "modified"})
}
} else {
hasComment := p.lineHasComment(line, language)
if hasComment {
preservedCount++
changes = append(changes, changeInfo{lineNum, line, line, "preserved"})
} else {
keptCount++
}
}
}
if len(changes) == 0 {
fmt.Printf("%s No comments found to remove\n", gray.Sprint("→")) fmt.Printf("%s No comments found to remove\n", gray.Sprint("→"))
fmt.Println() fmt.Println()
return nil return nil
} }
if contextLines > 0 { if contextLines > 0 {
p.displayChangesWithContext(lines, changes, contextLines, red, green, nil, gray, dimGray) p.displayChangesWithContext(lines, infos, contextLines, red, green, nil, gray, dimGray)
} else { } else {
for _, change := range changes { for _, change := range infos {
lineNumStr := gray.Sprintf("%4d", change.lineNum) lineNumStr := gray.Sprintf("%4d", change.lineNum)
switch change.changeType { switch change.changeType {
case "removed": case "removed":