feat(processor): route preview paths through StripComments
This commit is contained in:
@@ -220,3 +220,24 @@ func changed(changes []LineChange) bool {
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"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 {
|
||||
language, err := DetectLanguage(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg, _, err := config.Load()
|
||||
if err != nil {
|
||||
cfg = config.Default()
|
||||
@@ -264,17 +258,33 @@ func (p *Processor) showGitPreviewWithTotals(filename string, lineRanges []git.L
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
red := color.New(color.FgRed, color.CrossedOut)
|
||||
green := color.New(color.FgGreen)
|
||||
blue := color.New(color.FgBlue)
|
||||
gray := color.New(color.FgHiBlack)
|
||||
yellow := color.New(color.FgYellow)
|
||||
cyan := color.New(color.FgCyan)
|
||||
dimGray := color.New(color.FgWhite, color.Faint)
|
||||
|
||||
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()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
var lines []string
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
infos, changedCount, keptCount, preservedCount := changesToInfos(changes)
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
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 {
|
||||
if len(infos) == 0 {
|
||||
fmt.Printf("%s No comments found to remove\n", gray.Sprint("→"))
|
||||
fmt.Println()
|
||||
return nil
|
||||
}
|
||||
|
||||
if contextLines > 0 {
|
||||
p.displayChangesWithContext(lines, changes, contextLines, red, green, blue, gray, dimGray)
|
||||
p.displayChangesWithContext(lines, infos, contextLines, red, green, blue, gray, dimGray)
|
||||
} else {
|
||||
for _, change := range changes {
|
||||
for _, change := range infos {
|
||||
lineNumStr := gray.Sprintf("%4d", change.lineNum)
|
||||
switch change.changeType {
|
||||
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, green.Sprint("+"), green.Sprint(change.newLine))
|
||||
case "preserved":
|
||||
cyan := color.New(color.FgCyan)
|
||||
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 kept\n", green.Sprint("✓"), keptCount)
|
||||
if preservedCount > 0 {
|
||||
cyan := color.New(color.FgCyan)
|
||||
fmt.Printf("%s %d comments would be preserved\n", cyan.Sprint("P"), preservedCount)
|
||||
}
|
||||
fmt.Println()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
@@ -192,7 +191,6 @@ func (p *Processor) createBackup(filename string) error {
|
||||
}
|
||||
|
||||
func (p *Processor) showPreview(filename string, language types.Language) error {
|
||||
|
||||
cfg, _, err := config.Load()
|
||||
if err != nil {
|
||||
cfg = config.Default()
|
||||
@@ -203,11 +201,20 @@ func (p *Processor) showPreview(filename string, language types.Language) error
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
red := color.New(color.FgRed, color.CrossedOut)
|
||||
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)
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
var lines []string
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
infos, changedCount, keptCount, preservedCount := changesToInfos(changes)
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
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 {
|
||||
if len(infos) == 0 {
|
||||
fmt.Printf("%s No comments found to remove\n", gray.Sprint("→"))
|
||||
fmt.Println()
|
||||
return nil
|
||||
}
|
||||
|
||||
if contextLines > 0 {
|
||||
p.displayChangesWithContext(lines, changes, contextLines, red, green, nil, gray, dimGray)
|
||||
p.displayChangesWithContext(lines, infos, contextLines, red, green, nil, gray, dimGray)
|
||||
} else {
|
||||
for _, change := range changes {
|
||||
for _, change := range infos {
|
||||
lineNumStr := gray.Sprintf("%4d", change.lineNum)
|
||||
switch change.changeType {
|
||||
case "removed":
|
||||
|
||||
Reference in New Issue
Block a user