mirror of
https://github.com/sst/opencode.git
synced 2025-09-26 22:39:09 +00:00
chore: refactor diff
This commit is contained in:
parent
d941be3f1f
commit
e3eb9e5435
1 changed files with 79 additions and 83 deletions
|
@ -665,52 +665,98 @@ func applyHighlighting(content string, segments []Segment, segmentType LineType,
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// renderLeftColumn formats the left side of a side-by-side diff
|
// renderDiffColumnLine is a helper function that handles the common logic for rendering diff columns
|
||||||
func renderLeftColumn(fileName string, dl *DiffLine, colWidth int) string {
|
func renderDiffColumnLine(
|
||||||
t := theme.CurrentTheme()
|
fileName string,
|
||||||
|
dl *DiffLine,
|
||||||
|
colWidth int,
|
||||||
|
isLeftColumn bool,
|
||||||
|
t theme.Theme,
|
||||||
|
) string {
|
||||||
if dl == nil {
|
if dl == nil {
|
||||||
contextLineStyle := lipgloss.NewStyle().Background(t.DiffContextBg())
|
contextLineStyle := lipgloss.NewStyle().Background(t.DiffContextBg())
|
||||||
return contextLineStyle.Width(colWidth).Render("")
|
return contextLineStyle.Width(colWidth).Render("")
|
||||||
}
|
}
|
||||||
|
|
||||||
removedLineStyle, _, contextLineStyle, lineNumberStyle := createStyles(t)
|
removedLineStyle, addedLineStyle, contextLineStyle, lineNumberStyle := createStyles(t)
|
||||||
|
|
||||||
// Determine line style based on line type
|
// Determine line style based on line type and column
|
||||||
var marker string
|
var marker string
|
||||||
var bgStyle lipgloss.Style
|
var bgStyle lipgloss.Style
|
||||||
switch dl.Kind {
|
var lineNum string
|
||||||
case LineRemoved:
|
var highlightType LineType
|
||||||
marker = removedLineStyle.Foreground(t.DiffRemoved()).Render("-")
|
var highlightColor lipgloss.AdaptiveColor
|
||||||
bgStyle = removedLineStyle
|
|
||||||
lineNumberStyle = lineNumberStyle.Foreground(t.DiffRemoved()).Background(t.DiffRemovedLineNumberBg())
|
if isLeftColumn {
|
||||||
case LineAdded:
|
// Left column logic
|
||||||
marker = "?"
|
switch dl.Kind {
|
||||||
bgStyle = contextLineStyle
|
case LineRemoved:
|
||||||
case LineContext:
|
marker = "-"
|
||||||
marker = contextLineStyle.Render(" ")
|
bgStyle = removedLineStyle
|
||||||
bgStyle = contextLineStyle
|
lineNumberStyle = lineNumberStyle.Foreground(t.DiffRemoved()).Background(t.DiffRemovedLineNumberBg())
|
||||||
|
highlightType = LineRemoved
|
||||||
|
highlightColor = t.DiffHighlightRemoved()
|
||||||
|
case LineAdded:
|
||||||
|
marker = "?"
|
||||||
|
bgStyle = contextLineStyle
|
||||||
|
case LineContext:
|
||||||
|
marker = " "
|
||||||
|
bgStyle = contextLineStyle
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format line number for left column
|
||||||
|
if dl.OldLineNo > 0 {
|
||||||
|
lineNum = fmt.Sprintf("%6d", dl.OldLineNo)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Right column logic
|
||||||
|
switch dl.Kind {
|
||||||
|
case LineAdded:
|
||||||
|
marker = "+"
|
||||||
|
bgStyle = addedLineStyle
|
||||||
|
lineNumberStyle = lineNumberStyle.Foreground(t.DiffAdded()).Background(t.DiffAddedLineNumberBg())
|
||||||
|
highlightType = LineAdded
|
||||||
|
highlightColor = t.DiffHighlightAdded()
|
||||||
|
case LineRemoved:
|
||||||
|
marker = "?"
|
||||||
|
bgStyle = contextLineStyle
|
||||||
|
case LineContext:
|
||||||
|
marker = " "
|
||||||
|
bgStyle = contextLineStyle
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format line number for right column
|
||||||
|
if dl.NewLineNo > 0 {
|
||||||
|
lineNum = fmt.Sprintf("%6d", dl.NewLineNo)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format line number
|
// Style the marker based on line type
|
||||||
lineNum := ""
|
var styledMarker string
|
||||||
if dl.OldLineNo > 0 {
|
switch dl.Kind {
|
||||||
lineNum = fmt.Sprintf("%6d", dl.OldLineNo)
|
case LineRemoved:
|
||||||
|
styledMarker = removedLineStyle.Foreground(t.DiffRemoved()).Render(marker)
|
||||||
|
case LineAdded:
|
||||||
|
styledMarker = addedLineStyle.Foreground(t.DiffAdded()).Render(marker)
|
||||||
|
case LineContext:
|
||||||
|
styledMarker = contextLineStyle.Foreground(t.TextMuted()).Render(marker)
|
||||||
|
default:
|
||||||
|
styledMarker = marker
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the line prefix
|
// Create the line prefix
|
||||||
prefix := lineNumberStyle.Render(lineNum + " " + marker)
|
prefix := lineNumberStyle.Render(lineNum + " " + styledMarker)
|
||||||
|
|
||||||
// Apply syntax highlighting
|
// Apply syntax highlighting
|
||||||
content := highlightLine(fileName, dl.Content, bgStyle.GetBackground())
|
content := highlightLine(fileName, dl.Content, bgStyle.GetBackground())
|
||||||
|
|
||||||
// Apply intra-line highlighting for removed lines
|
// Apply intra-line highlighting if needed
|
||||||
if dl.Kind == LineRemoved && len(dl.Segments) > 0 {
|
if (dl.Kind == LineRemoved && isLeftColumn || dl.Kind == LineAdded && !isLeftColumn) && len(dl.Segments) > 0 {
|
||||||
content = applyHighlighting(content, dl.Segments, LineRemoved, t.DiffHighlightRemoved())
|
content = applyHighlighting(content, dl.Segments, highlightType, highlightColor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a padding space for removed lines
|
// Add a padding space for added/removed lines
|
||||||
if dl.Kind == LineRemoved {
|
if (dl.Kind == LineRemoved && isLeftColumn) || (dl.Kind == LineAdded && !isLeftColumn) {
|
||||||
content = bgStyle.Render(" ") + content
|
content = bgStyle.Render(" ") + content
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -725,64 +771,14 @@ func renderLeftColumn(fileName string, dl *DiffLine, colWidth int) string {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// renderLeftColumn formats the left side of a side-by-side diff
|
||||||
|
func renderLeftColumn(fileName string, dl *DiffLine, colWidth int) string {
|
||||||
|
return renderDiffColumnLine(fileName, dl, colWidth, true, theme.CurrentTheme())
|
||||||
|
}
|
||||||
|
|
||||||
// renderRightColumn formats the right side of a side-by-side diff
|
// renderRightColumn formats the right side of a side-by-side diff
|
||||||
func renderRightColumn(fileName string, dl *DiffLine, colWidth int) string {
|
func renderRightColumn(fileName string, dl *DiffLine, colWidth int) string {
|
||||||
t := theme.CurrentTheme()
|
return renderDiffColumnLine(fileName, dl, colWidth, false, theme.CurrentTheme())
|
||||||
|
|
||||||
if dl == nil {
|
|
||||||
contextLineStyle := lipgloss.NewStyle().Background(t.DiffContextBg())
|
|
||||||
return contextLineStyle.Width(colWidth).Render("")
|
|
||||||
}
|
|
||||||
|
|
||||||
_, addedLineStyle, contextLineStyle, lineNumberStyle := createStyles(t)
|
|
||||||
|
|
||||||
// Determine line style based on line type
|
|
||||||
var marker string
|
|
||||||
var bgStyle lipgloss.Style
|
|
||||||
switch dl.Kind {
|
|
||||||
case LineAdded:
|
|
||||||
marker = addedLineStyle.Foreground(t.DiffAdded()).Render("+")
|
|
||||||
bgStyle = addedLineStyle
|
|
||||||
lineNumberStyle = lineNumberStyle.Foreground(t.DiffAdded()).Background(t.DiffAddedLineNumberBg())
|
|
||||||
case LineRemoved:
|
|
||||||
marker = "?"
|
|
||||||
bgStyle = contextLineStyle
|
|
||||||
case LineContext:
|
|
||||||
marker = contextLineStyle.Render(" ")
|
|
||||||
bgStyle = contextLineStyle
|
|
||||||
}
|
|
||||||
|
|
||||||
// Format line number
|
|
||||||
lineNum := ""
|
|
||||||
if dl.NewLineNo > 0 {
|
|
||||||
lineNum = fmt.Sprintf("%6d", dl.NewLineNo)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the line prefix
|
|
||||||
prefix := lineNumberStyle.Render(lineNum + " " + marker)
|
|
||||||
|
|
||||||
// Apply syntax highlighting
|
|
||||||
content := highlightLine(fileName, dl.Content, bgStyle.GetBackground())
|
|
||||||
|
|
||||||
// Apply intra-line highlighting for added lines
|
|
||||||
if dl.Kind == LineAdded && len(dl.Segments) > 0 {
|
|
||||||
content = applyHighlighting(content, dl.Segments, LineAdded, t.DiffHighlightAdded())
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add a padding space for added lines
|
|
||||||
if dl.Kind == LineAdded {
|
|
||||||
content = bgStyle.Render(" ") + content
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create the final line and truncate if needed
|
|
||||||
lineText := prefix + content
|
|
||||||
return bgStyle.MaxHeight(1).Width(colWidth).Render(
|
|
||||||
ansi.Truncate(
|
|
||||||
lineText,
|
|
||||||
colWidth,
|
|
||||||
lipgloss.NewStyle().Background(bgStyle.GetBackground()).Foreground(t.TextMuted()).Render("..."),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------------------------------------------------------------------------
|
// -------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue