diff.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. package diff
  2. import (
  3. "bufio"
  4. "bytes"
  5. "fmt"
  6. "image/color"
  7. "io"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "github.com/alecthomas/chroma/v2"
  13. "github.com/alecthomas/chroma/v2/formatters"
  14. "github.com/alecthomas/chroma/v2/lexers"
  15. "github.com/alecthomas/chroma/v2/styles"
  16. "github.com/charmbracelet/lipgloss/v2"
  17. "github.com/charmbracelet/lipgloss/v2/compat"
  18. "github.com/charmbracelet/x/ansi"
  19. "github.com/sergi/go-diff/diffmatchpatch"
  20. stylesi "github.com/sst/opencode/internal/styles"
  21. "github.com/sst/opencode/internal/theme"
  22. "github.com/sst/opencode/internal/util"
  23. )
  24. // -------------------------------------------------------------------------
  25. // Core Types
  26. // -------------------------------------------------------------------------
  27. // LineType represents the kind of line in a diff.
  28. type LineType int
  29. const (
  30. LineContext LineType = iota // Line exists in both files
  31. LineAdded // Line added in the new file
  32. LineRemoved // Line removed from the old file
  33. )
  34. // Segment represents a portion of a line for intra-line highlighting
  35. type Segment struct {
  36. Start int
  37. End int
  38. Type LineType
  39. Text string
  40. }
  41. // DiffLine represents a single line in a diff
  42. type DiffLine struct {
  43. OldLineNo int // Line number in old file (0 for added lines)
  44. NewLineNo int // Line number in new file (0 for removed lines)
  45. Kind LineType // Type of line (added, removed, context)
  46. Content string // Content of the line
  47. Segments []Segment // Segments for intraline highlighting
  48. }
  49. // Hunk represents a section of changes in a diff
  50. type Hunk struct {
  51. Header string
  52. Lines []DiffLine
  53. }
  54. // DiffResult contains the parsed result of a diff
  55. type DiffResult struct {
  56. OldFile string
  57. NewFile string
  58. Hunks []Hunk
  59. }
  60. // linePair represents a pair of lines for side-by-side display
  61. type linePair struct {
  62. left *DiffLine
  63. right *DiffLine
  64. }
  65. // UnifiedConfig configures the rendering of unified diffs
  66. type UnifiedConfig struct {
  67. Width int
  68. }
  69. // UnifiedOption modifies a UnifiedConfig
  70. type UnifiedOption func(*UnifiedConfig)
  71. // NewUnifiedConfig creates a UnifiedConfig with default values
  72. func NewUnifiedConfig(opts ...UnifiedOption) UnifiedConfig {
  73. config := UnifiedConfig{
  74. Width: 80,
  75. }
  76. for _, opt := range opts {
  77. opt(&config)
  78. }
  79. return config
  80. }
  81. // NewSideBySideConfig creates a SideBySideConfig with default values
  82. func NewSideBySideConfig(opts ...UnifiedOption) UnifiedConfig {
  83. config := UnifiedConfig{
  84. Width: 160,
  85. }
  86. for _, opt := range opts {
  87. opt(&config)
  88. }
  89. return config
  90. }
  91. // WithWidth sets the width for unified view
  92. func WithWidth(width int) UnifiedOption {
  93. return func(u *UnifiedConfig) {
  94. if width > 0 {
  95. u.Width = width
  96. }
  97. }
  98. }
  99. // -------------------------------------------------------------------------
  100. // Diff Parsing
  101. // -------------------------------------------------------------------------
  102. // ParseUnifiedDiff parses a unified diff format string into structured data
  103. func ParseUnifiedDiff(diff string) (DiffResult, error) {
  104. var result DiffResult
  105. var currentHunk *Hunk
  106. result.Hunks = make([]Hunk, 0, 10) // Pre-allocate with a reasonable capacity
  107. scanner := bufio.NewScanner(strings.NewReader(diff))
  108. var oldLine, newLine int
  109. inFileHeader := true
  110. for scanner.Scan() {
  111. line := scanner.Text()
  112. if inFileHeader {
  113. if strings.HasPrefix(line, "--- a/") {
  114. result.OldFile = line[6:]
  115. continue
  116. }
  117. if strings.HasPrefix(line, "+++ b/") {
  118. result.NewFile = line[6:]
  119. inFileHeader = false
  120. continue
  121. }
  122. }
  123. if strings.HasPrefix(line, "@@") {
  124. if currentHunk != nil {
  125. result.Hunks = append(result.Hunks, *currentHunk)
  126. }
  127. currentHunk = &Hunk{
  128. Header: line,
  129. Lines: make([]DiffLine, 0, 10), // Pre-allocate
  130. }
  131. // Manual parsing of hunk header is faster than regex
  132. parts := strings.Split(line, " ")
  133. if len(parts) > 2 {
  134. oldRange := strings.Split(parts[1][1:], ",")
  135. newRange := strings.Split(parts[2][1:], ",")
  136. oldLine, _ = strconv.Atoi(oldRange[0])
  137. newLine, _ = strconv.Atoi(newRange[0])
  138. }
  139. continue
  140. }
  141. if strings.HasPrefix(line, "\\ No newline at end of file") || currentHunk == nil {
  142. continue
  143. }
  144. var dl DiffLine
  145. dl.Content = line
  146. if len(line) > 0 {
  147. switch line[0] {
  148. case '+':
  149. dl.Kind = LineAdded
  150. dl.NewLineNo = newLine
  151. dl.Content = line[1:]
  152. newLine++
  153. case '-':
  154. dl.Kind = LineRemoved
  155. dl.OldLineNo = oldLine
  156. dl.Content = line[1:]
  157. oldLine++
  158. default: // context line
  159. dl.Kind = LineContext
  160. dl.OldLineNo = oldLine
  161. dl.NewLineNo = newLine
  162. oldLine++
  163. newLine++
  164. }
  165. } else { // empty context line
  166. dl.Kind = LineContext
  167. dl.OldLineNo = oldLine
  168. dl.NewLineNo = newLine
  169. oldLine++
  170. newLine++
  171. }
  172. currentHunk.Lines = append(currentHunk.Lines, dl)
  173. }
  174. if currentHunk != nil {
  175. result.Hunks = append(result.Hunks, *currentHunk)
  176. }
  177. return result, scanner.Err()
  178. }
  179. // HighlightIntralineChanges updates lines in a hunk to show character-level differences
  180. func HighlightIntralineChanges(h *Hunk) {
  181. var updated []DiffLine
  182. dmp := diffmatchpatch.New()
  183. for i := 0; i < len(h.Lines); i++ {
  184. // Look for removed line followed by added line
  185. if i+1 < len(h.Lines) &&
  186. h.Lines[i].Kind == LineRemoved &&
  187. h.Lines[i+1].Kind == LineAdded {
  188. oldLine := h.Lines[i]
  189. newLine := h.Lines[i+1]
  190. // Find character-level differences
  191. patches := dmp.DiffMain(oldLine.Content, newLine.Content, false)
  192. patches = dmp.DiffCleanupSemantic(patches)
  193. patches = dmp.DiffCleanupMerge(patches)
  194. patches = dmp.DiffCleanupEfficiency(patches)
  195. segments := make([]Segment, 0)
  196. removeStart := 0
  197. addStart := 0
  198. for _, patch := range patches {
  199. switch patch.Type {
  200. case diffmatchpatch.DiffDelete:
  201. segments = append(segments, Segment{
  202. Start: removeStart,
  203. End: removeStart + len(patch.Text),
  204. Type: LineRemoved,
  205. Text: patch.Text,
  206. })
  207. removeStart += len(patch.Text)
  208. case diffmatchpatch.DiffInsert:
  209. segments = append(segments, Segment{
  210. Start: addStart,
  211. End: addStart + len(patch.Text),
  212. Type: LineAdded,
  213. Text: patch.Text,
  214. })
  215. addStart += len(patch.Text)
  216. default:
  217. // Context text, no highlighting needed
  218. removeStart += len(patch.Text)
  219. addStart += len(patch.Text)
  220. }
  221. }
  222. oldLine.Segments = segments
  223. newLine.Segments = segments
  224. updated = append(updated, oldLine, newLine)
  225. i++ // Skip the next line as we've already processed it
  226. } else {
  227. updated = append(updated, h.Lines[i])
  228. }
  229. }
  230. h.Lines = updated
  231. }
  232. // pairLines converts a flat list of diff lines to pairs for side-by-side display
  233. func pairLines(lines []DiffLine) []linePair {
  234. var pairs []linePair
  235. i := 0
  236. for i < len(lines) {
  237. switch lines[i].Kind {
  238. case LineRemoved:
  239. // Check if the next line is an addition, if so pair them
  240. if i+1 < len(lines) && lines[i+1].Kind == LineAdded {
  241. pairs = append(pairs, linePair{left: &lines[i], right: &lines[i+1]})
  242. i += 2
  243. } else {
  244. pairs = append(pairs, linePair{left: &lines[i], right: nil})
  245. i++
  246. }
  247. case LineAdded:
  248. pairs = append(pairs, linePair{left: nil, right: &lines[i]})
  249. i++
  250. case LineContext:
  251. pairs = append(pairs, linePair{left: &lines[i], right: &lines[i]})
  252. i++
  253. }
  254. }
  255. return pairs
  256. }
  257. // -------------------------------------------------------------------------
  258. // Syntax Highlighting
  259. // -------------------------------------------------------------------------
  260. // SyntaxHighlight applies syntax highlighting to text based on file extension
  261. func SyntaxHighlight(w io.Writer, source, fileName, formatter string, bg color.Color) error {
  262. t := theme.CurrentTheme()
  263. // Determine the language lexer to use
  264. l := lexers.Match(fileName)
  265. if l == nil {
  266. l = lexers.Analyse(source)
  267. }
  268. if l == nil {
  269. l = lexers.Fallback
  270. }
  271. l = chroma.Coalesce(l)
  272. // Get the formatter
  273. f := formatters.Get(formatter)
  274. if f == nil {
  275. f = formatters.Fallback
  276. }
  277. // Dynamic theme based on current theme values
  278. syntaxThemeXml := fmt.Sprintf(`
  279. <style name="opencode-theme">
  280. <!-- Base colors -->
  281. <entry type="Background" style="bg:%s"/>
  282. <entry type="Text" style="%s"/>
  283. <entry type="Other" style="%s"/>
  284. <entry type="Error" style="%s"/>
  285. <!-- Keywords -->
  286. <entry type="Keyword" style="%s"/>
  287. <entry type="KeywordConstant" style="%s"/>
  288. <entry type="KeywordDeclaration" style="%s"/>
  289. <entry type="KeywordNamespace" style="%s"/>
  290. <entry type="KeywordPseudo" style="%s"/>
  291. <entry type="KeywordReserved" style="%s"/>
  292. <entry type="KeywordType" style="%s"/>
  293. <!-- Names -->
  294. <entry type="Name" style="%s"/>
  295. <entry type="NameAttribute" style="%s"/>
  296. <entry type="NameBuiltin" style="%s"/>
  297. <entry type="NameBuiltinPseudo" style="%s"/>
  298. <entry type="NameClass" style="%s"/>
  299. <entry type="NameConstant" style="%s"/>
  300. <entry type="NameDecorator" style="%s"/>
  301. <entry type="NameEntity" style="%s"/>
  302. <entry type="NameException" style="%s"/>
  303. <entry type="NameFunction" style="%s"/>
  304. <entry type="NameLabel" style="%s"/>
  305. <entry type="NameNamespace" style="%s"/>
  306. <entry type="NameOther" style="%s"/>
  307. <entry type="NameTag" style="%s"/>
  308. <entry type="NameVariable" style="%s"/>
  309. <entry type="NameVariableClass" style="%s"/>
  310. <entry type="NameVariableGlobal" style="%s"/>
  311. <entry type="NameVariableInstance" style="%s"/>
  312. <!-- Literals -->
  313. <entry type="Literal" style="%s"/>
  314. <entry type="LiteralDate" style="%s"/>
  315. <entry type="LiteralString" style="%s"/>
  316. <entry type="LiteralStringBacktick" style="%s"/>
  317. <entry type="LiteralStringChar" style="%s"/>
  318. <entry type="LiteralStringDoc" style="%s"/>
  319. <entry type="LiteralStringDouble" style="%s"/>
  320. <entry type="LiteralStringEscape" style="%s"/>
  321. <entry type="LiteralStringHeredoc" style="%s"/>
  322. <entry type="LiteralStringInterpol" style="%s"/>
  323. <entry type="LiteralStringOther" style="%s"/>
  324. <entry type="LiteralStringRegex" style="%s"/>
  325. <entry type="LiteralStringSingle" style="%s"/>
  326. <entry type="LiteralStringSymbol" style="%s"/>
  327. <!-- Numbers -->
  328. <entry type="LiteralNumber" style="%s"/>
  329. <entry type="LiteralNumberBin" style="%s"/>
  330. <entry type="LiteralNumberFloat" style="%s"/>
  331. <entry type="LiteralNumberHex" style="%s"/>
  332. <entry type="LiteralNumberInteger" style="%s"/>
  333. <entry type="LiteralNumberIntegerLong" style="%s"/>
  334. <entry type="LiteralNumberOct" style="%s"/>
  335. <!-- Operators -->
  336. <entry type="Operator" style="%s"/>
  337. <entry type="OperatorWord" style="%s"/>
  338. <entry type="Punctuation" style="%s"/>
  339. <!-- Comments -->
  340. <entry type="Comment" style="%s"/>
  341. <entry type="CommentHashbang" style="%s"/>
  342. <entry type="CommentMultiline" style="%s"/>
  343. <entry type="CommentSingle" style="%s"/>
  344. <entry type="CommentSpecial" style="%s"/>
  345. <entry type="CommentPreproc" style="%s"/>
  346. <!-- Generic styles -->
  347. <entry type="Generic" style="%s"/>
  348. <entry type="GenericDeleted" style="%s"/>
  349. <entry type="GenericEmph" style="italic %s"/>
  350. <entry type="GenericError" style="%s"/>
  351. <entry type="GenericHeading" style="bold %s"/>
  352. <entry type="GenericInserted" style="%s"/>
  353. <entry type="GenericOutput" style="%s"/>
  354. <entry type="GenericPrompt" style="%s"/>
  355. <entry type="GenericStrong" style="bold %s"/>
  356. <entry type="GenericSubheading" style="bold %s"/>
  357. <entry type="GenericTraceback" style="%s"/>
  358. <entry type="GenericUnderline" style="underline"/>
  359. <entry type="TextWhitespace" style="%s"/>
  360. </style>
  361. `,
  362. getChromaColor(t.BackgroundPanel()), // Background
  363. getChromaColor(t.Text()), // Text
  364. getChromaColor(t.Text()), // Other
  365. getChromaColor(t.Error()), // Error
  366. getChromaColor(t.SyntaxKeyword()), // Keyword
  367. getChromaColor(t.SyntaxKeyword()), // KeywordConstant
  368. getChromaColor(t.SyntaxKeyword()), // KeywordDeclaration
  369. getChromaColor(t.SyntaxKeyword()), // KeywordNamespace
  370. getChromaColor(t.SyntaxKeyword()), // KeywordPseudo
  371. getChromaColor(t.SyntaxKeyword()), // KeywordReserved
  372. getChromaColor(t.SyntaxType()), // KeywordType
  373. getChromaColor(t.Text()), // Name
  374. getChromaColor(t.SyntaxVariable()), // NameAttribute
  375. getChromaColor(t.SyntaxType()), // NameBuiltin
  376. getChromaColor(t.SyntaxVariable()), // NameBuiltinPseudo
  377. getChromaColor(t.SyntaxType()), // NameClass
  378. getChromaColor(t.SyntaxVariable()), // NameConstant
  379. getChromaColor(t.SyntaxFunction()), // NameDecorator
  380. getChromaColor(t.SyntaxVariable()), // NameEntity
  381. getChromaColor(t.SyntaxType()), // NameException
  382. getChromaColor(t.SyntaxFunction()), // NameFunction
  383. getChromaColor(t.Text()), // NameLabel
  384. getChromaColor(t.SyntaxType()), // NameNamespace
  385. getChromaColor(t.SyntaxVariable()), // NameOther
  386. getChromaColor(t.SyntaxKeyword()), // NameTag
  387. getChromaColor(t.SyntaxVariable()), // NameVariable
  388. getChromaColor(t.SyntaxVariable()), // NameVariableClass
  389. getChromaColor(t.SyntaxVariable()), // NameVariableGlobal
  390. getChromaColor(t.SyntaxVariable()), // NameVariableInstance
  391. getChromaColor(t.SyntaxString()), // Literal
  392. getChromaColor(t.SyntaxString()), // LiteralDate
  393. getChromaColor(t.SyntaxString()), // LiteralString
  394. getChromaColor(t.SyntaxString()), // LiteralStringBacktick
  395. getChromaColor(t.SyntaxString()), // LiteralStringChar
  396. getChromaColor(t.SyntaxString()), // LiteralStringDoc
  397. getChromaColor(t.SyntaxString()), // LiteralStringDouble
  398. getChromaColor(t.SyntaxString()), // LiteralStringEscape
  399. getChromaColor(t.SyntaxString()), // LiteralStringHeredoc
  400. getChromaColor(t.SyntaxString()), // LiteralStringInterpol
  401. getChromaColor(t.SyntaxString()), // LiteralStringOther
  402. getChromaColor(t.SyntaxString()), // LiteralStringRegex
  403. getChromaColor(t.SyntaxString()), // LiteralStringSingle
  404. getChromaColor(t.SyntaxString()), // LiteralStringSymbol
  405. getChromaColor(t.SyntaxNumber()), // LiteralNumber
  406. getChromaColor(t.SyntaxNumber()), // LiteralNumberBin
  407. getChromaColor(t.SyntaxNumber()), // LiteralNumberFloat
  408. getChromaColor(t.SyntaxNumber()), // LiteralNumberHex
  409. getChromaColor(t.SyntaxNumber()), // LiteralNumberInteger
  410. getChromaColor(t.SyntaxNumber()), // LiteralNumberIntegerLong
  411. getChromaColor(t.SyntaxNumber()), // LiteralNumberOct
  412. getChromaColor(t.SyntaxOperator()), // Operator
  413. getChromaColor(t.SyntaxKeyword()), // OperatorWord
  414. getChromaColor(t.SyntaxPunctuation()), // Punctuation
  415. getChromaColor(t.SyntaxComment()), // Comment
  416. getChromaColor(t.SyntaxComment()), // CommentHashbang
  417. getChromaColor(t.SyntaxComment()), // CommentMultiline
  418. getChromaColor(t.SyntaxComment()), // CommentSingle
  419. getChromaColor(t.SyntaxComment()), // CommentSpecial
  420. getChromaColor(t.SyntaxKeyword()), // CommentPreproc
  421. getChromaColor(t.Text()), // Generic
  422. getChromaColor(t.Error()), // GenericDeleted
  423. getChromaColor(t.Text()), // GenericEmph
  424. getChromaColor(t.Error()), // GenericError
  425. getChromaColor(t.Text()), // GenericHeading
  426. getChromaColor(t.Success()), // GenericInserted
  427. getChromaColor(t.TextMuted()), // GenericOutput
  428. getChromaColor(t.Text()), // GenericPrompt
  429. getChromaColor(t.Text()), // GenericStrong
  430. getChromaColor(t.Text()), // GenericSubheading
  431. getChromaColor(t.Error()), // GenericTraceback
  432. getChromaColor(t.Text()), // TextWhitespace
  433. )
  434. r := strings.NewReader(syntaxThemeXml)
  435. style := chroma.MustNewXMLStyle(r)
  436. // Modify the style to use the provided background
  437. s, err := style.Builder().Transform(
  438. func(t chroma.StyleEntry) chroma.StyleEntry {
  439. if _, ok := bg.(lipgloss.NoColor); ok {
  440. return t
  441. }
  442. r, g, b, _ := bg.RGBA()
  443. t.Background = chroma.NewColour(uint8(r>>8), uint8(g>>8), uint8(b>>8))
  444. return t
  445. },
  446. ).Build()
  447. if err != nil {
  448. s = styles.Fallback
  449. }
  450. // Tokenize and format
  451. it, err := l.Tokenise(nil, source)
  452. if err != nil {
  453. return err
  454. }
  455. return f.Format(w, s, it)
  456. }
  457. // getColor returns the appropriate hex color string based on terminal background
  458. func getColor(adaptiveColor compat.AdaptiveColor) *string {
  459. return stylesi.AdaptiveColorToString(adaptiveColor)
  460. }
  461. func getChromaColor(adaptiveColor compat.AdaptiveColor) string {
  462. color := stylesi.AdaptiveColorToString(adaptiveColor)
  463. if color == nil {
  464. return ""
  465. }
  466. return *color
  467. }
  468. // highlightLine applies syntax highlighting to a single line
  469. func highlightLine(fileName string, line string, bg color.Color) string {
  470. var buf bytes.Buffer
  471. err := SyntaxHighlight(&buf, line, fileName, "terminal16m", bg)
  472. if err != nil {
  473. return line
  474. }
  475. return buf.String()
  476. }
  477. // createStyles generates the lipgloss styles needed for rendering diffs
  478. func createStyles(t theme.Theme) (removedLineStyle, addedLineStyle, contextLineStyle, lineNumberStyle stylesi.Style) {
  479. removedLineStyle = stylesi.NewStyle().Background(t.DiffRemovedBg())
  480. addedLineStyle = stylesi.NewStyle().Background(t.DiffAddedBg())
  481. contextLineStyle = stylesi.NewStyle().Background(t.DiffContextBg())
  482. lineNumberStyle = stylesi.NewStyle().Foreground(t.TextMuted()).Background(t.DiffLineNumber())
  483. return
  484. }
  485. // -------------------------------------------------------------------------
  486. // Rendering Functions
  487. // -------------------------------------------------------------------------
  488. // applyHighlighting applies intra-line highlighting to a piece of text
  489. func applyHighlighting(content string, segments []Segment, segmentType LineType, highlightBg compat.AdaptiveColor) string {
  490. // Find all ANSI sequences in the content
  491. ansiRegex := regexp.MustCompile(`\x1b(?:[@-Z\\-_]|\[[0-9?]*(?:;[0-9?]*)*[@-~])`)
  492. ansiMatches := ansiRegex.FindAllStringIndex(content, -1)
  493. // Build a mapping of visible character positions to their actual indices
  494. visibleIdx := 0
  495. ansiSequences := make(map[int]string)
  496. lastAnsiSeq := "\x1b[0m" // Default reset sequence
  497. for i := 0; i < len(content); {
  498. isAnsi := false
  499. for _, match := range ansiMatches {
  500. if match[0] == i {
  501. ansiSequences[visibleIdx] = content[match[0]:match[1]]
  502. lastAnsiSeq = content[match[0]:match[1]]
  503. i = match[1]
  504. isAnsi = true
  505. break
  506. }
  507. }
  508. if isAnsi {
  509. continue
  510. }
  511. // For non-ANSI positions, store the last ANSI sequence
  512. if _, exists := ansiSequences[visibleIdx]; !exists {
  513. ansiSequences[visibleIdx] = lastAnsiSeq
  514. }
  515. visibleIdx++
  516. i++
  517. }
  518. // Apply highlighting
  519. var sb strings.Builder
  520. inSelection := false
  521. currentPos := 0
  522. // Get the appropriate color based on terminal background
  523. bg := getColor(highlightBg)
  524. fg := getColor(theme.CurrentTheme().BackgroundPanel())
  525. var bgColor color.Color
  526. var fgColor color.Color
  527. if bg != nil {
  528. bgColor = lipgloss.Color(*bg)
  529. }
  530. if fg != nil {
  531. fgColor = lipgloss.Color(*fg)
  532. }
  533. for i := 0; i < len(content); {
  534. // Check if we're at an ANSI sequence
  535. isAnsi := false
  536. for _, match := range ansiMatches {
  537. if match[0] == i {
  538. sb.WriteString(content[match[0]:match[1]]) // Preserve ANSI sequence
  539. i = match[1]
  540. isAnsi = true
  541. break
  542. }
  543. }
  544. if isAnsi {
  545. continue
  546. }
  547. // Check for segment boundaries
  548. for _, seg := range segments {
  549. if seg.Type == segmentType {
  550. if currentPos == seg.Start {
  551. inSelection = true
  552. }
  553. if currentPos == seg.End {
  554. inSelection = false
  555. }
  556. }
  557. }
  558. // Get current character
  559. char := string(content[i])
  560. if inSelection {
  561. // Get the current styling
  562. currentStyle := ansiSequences[currentPos]
  563. // Apply foreground and background highlight
  564. if fgColor != nil {
  565. sb.WriteString("\x1b[38;2;")
  566. r, g, b, _ := fgColor.RGBA()
  567. sb.WriteString(fmt.Sprintf("%d;%d;%dm", r>>8, g>>8, b>>8))
  568. } else {
  569. sb.WriteString("\x1b[49m")
  570. }
  571. if bgColor != nil {
  572. sb.WriteString("\x1b[48;2;")
  573. r, g, b, _ := bgColor.RGBA()
  574. sb.WriteString(fmt.Sprintf("%d;%d;%dm", r>>8, g>>8, b>>8))
  575. } else {
  576. sb.WriteString("\x1b[39m")
  577. }
  578. sb.WriteString(char)
  579. // Full reset of all attributes to ensure clean state
  580. sb.WriteString("\x1b[0m")
  581. // Reapply the original ANSI sequence
  582. sb.WriteString(currentStyle)
  583. } else {
  584. // Not in selection, just copy the character
  585. sb.WriteString(char)
  586. }
  587. currentPos++
  588. i++
  589. }
  590. return sb.String()
  591. }
  592. // renderLinePrefix renders the line number and marker prefix for a diff line
  593. func renderLinePrefix(dl DiffLine, lineNum string, marker string, lineNumberStyle stylesi.Style, t theme.Theme) string {
  594. // Style the marker based on line type
  595. var styledMarker string
  596. switch dl.Kind {
  597. case LineRemoved:
  598. styledMarker = stylesi.NewStyle().Foreground(t.DiffRemoved()).Background(t.DiffRemovedBg()).Render(marker)
  599. case LineAdded:
  600. styledMarker = stylesi.NewStyle().Foreground(t.DiffAdded()).Background(t.DiffAddedBg()).Render(marker)
  601. case LineContext:
  602. styledMarker = stylesi.NewStyle().Foreground(t.TextMuted()).Background(t.DiffContextBg()).Render(marker)
  603. default:
  604. styledMarker = marker
  605. }
  606. return lineNumberStyle.Render(lineNum + " " + styledMarker)
  607. }
  608. // renderLineContent renders the content of a diff line with syntax and intra-line highlighting
  609. func renderLineContent(fileName string, dl DiffLine, bgStyle stylesi.Style, highlightColor compat.AdaptiveColor, width int) string {
  610. // Apply syntax highlighting
  611. content := highlightLine(fileName, dl.Content, bgStyle.GetBackground())
  612. // Apply intra-line highlighting if needed
  613. if len(dl.Segments) > 0 && (dl.Kind == LineRemoved || dl.Kind == LineAdded) {
  614. content = applyHighlighting(content, dl.Segments, dl.Kind, highlightColor)
  615. }
  616. // Add a padding space for added/removed lines
  617. if dl.Kind == LineRemoved || dl.Kind == LineAdded {
  618. content = bgStyle.Render(" ") + content
  619. }
  620. // Create the final line and truncate if needed
  621. return bgStyle.MaxHeight(1).Width(width).Render(
  622. ansi.Truncate(
  623. content,
  624. width,
  625. "...",
  626. ),
  627. )
  628. }
  629. // renderUnifiedLine renders a single line in unified diff format
  630. func renderUnifiedLine(fileName string, dl DiffLine, width int, t theme.Theme) string {
  631. removedLineStyle, addedLineStyle, contextLineStyle, lineNumberStyle := createStyles(t)
  632. // Determine line style and marker based on line type
  633. var marker string
  634. var bgStyle stylesi.Style
  635. var lineNum string
  636. var highlightColor compat.AdaptiveColor
  637. switch dl.Kind {
  638. case LineRemoved:
  639. marker = "-"
  640. bgStyle = removedLineStyle
  641. lineNumberStyle = lineNumberStyle.Background(t.DiffRemovedLineNumberBg()).Foreground(t.DiffRemoved())
  642. highlightColor = t.DiffHighlightRemoved() // TODO: handle "none"
  643. if dl.OldLineNo > 0 {
  644. lineNum = fmt.Sprintf("%6d ", dl.OldLineNo)
  645. } else {
  646. lineNum = " "
  647. }
  648. case LineAdded:
  649. marker = "+"
  650. bgStyle = addedLineStyle
  651. lineNumberStyle = lineNumberStyle.Background(t.DiffAddedLineNumberBg()).Foreground(t.DiffAdded())
  652. highlightColor = t.DiffHighlightAdded() // TODO: handle "none"
  653. if dl.NewLineNo > 0 {
  654. lineNum = fmt.Sprintf(" %7d", dl.NewLineNo)
  655. } else {
  656. lineNum = " "
  657. }
  658. case LineContext:
  659. marker = " "
  660. bgStyle = contextLineStyle
  661. if dl.OldLineNo > 0 && dl.NewLineNo > 0 {
  662. lineNum = fmt.Sprintf("%6d %6d", dl.OldLineNo, dl.NewLineNo)
  663. } else {
  664. lineNum = " "
  665. }
  666. }
  667. // Create the line prefix
  668. prefix := renderLinePrefix(dl, lineNum, marker, lineNumberStyle, t)
  669. // Render the content
  670. prefixWidth := ansi.StringWidth(prefix)
  671. contentWidth := width - prefixWidth
  672. content := renderLineContent(fileName, dl, bgStyle, highlightColor, contentWidth)
  673. return prefix + content
  674. }
  675. // renderDiffColumnLine is a helper function that handles the common logic for rendering diff columns
  676. func renderDiffColumnLine(
  677. fileName string,
  678. dl *DiffLine,
  679. colWidth int,
  680. isLeftColumn bool,
  681. t theme.Theme,
  682. ) string {
  683. if dl == nil {
  684. contextLineStyle := stylesi.NewStyle().Background(t.DiffContextBg())
  685. return contextLineStyle.Width(colWidth).Render("")
  686. }
  687. removedLineStyle, addedLineStyle, contextLineStyle, lineNumberStyle := createStyles(t)
  688. // Determine line style based on line type and column
  689. var marker string
  690. var bgStyle stylesi.Style
  691. var lineNum string
  692. var highlightColor compat.AdaptiveColor
  693. if isLeftColumn {
  694. // Left column logic
  695. switch dl.Kind {
  696. case LineRemoved:
  697. marker = "-"
  698. bgStyle = removedLineStyle
  699. lineNumberStyle = lineNumberStyle.Background(t.DiffRemovedLineNumberBg()).Foreground(t.DiffRemoved())
  700. highlightColor = t.DiffHighlightRemoved() // TODO: handle "none"
  701. case LineAdded:
  702. marker = "?"
  703. bgStyle = contextLineStyle
  704. case LineContext:
  705. marker = " "
  706. bgStyle = contextLineStyle
  707. }
  708. // Format line number for left column
  709. if dl.OldLineNo > 0 {
  710. lineNum = fmt.Sprintf("%6d", dl.OldLineNo)
  711. }
  712. } else {
  713. // Right column logic
  714. switch dl.Kind {
  715. case LineAdded:
  716. marker = "+"
  717. bgStyle = addedLineStyle
  718. lineNumberStyle = lineNumberStyle.Background(t.DiffAddedLineNumberBg()).Foreground(t.DiffAdded())
  719. highlightColor = t.DiffHighlightAdded()
  720. case LineRemoved:
  721. marker = "?"
  722. bgStyle = contextLineStyle
  723. case LineContext:
  724. marker = " "
  725. bgStyle = contextLineStyle
  726. }
  727. // Format line number for right column
  728. if dl.NewLineNo > 0 {
  729. lineNum = fmt.Sprintf("%6d", dl.NewLineNo)
  730. }
  731. }
  732. // Create the line prefix
  733. prefix := renderLinePrefix(*dl, lineNum, marker, lineNumberStyle, t)
  734. // Determine if we should render content
  735. shouldRenderContent := (dl.Kind == LineRemoved && isLeftColumn) ||
  736. (dl.Kind == LineAdded && !isLeftColumn) ||
  737. dl.Kind == LineContext
  738. if !shouldRenderContent {
  739. return bgStyle.Width(colWidth).Render("")
  740. }
  741. // Render the content
  742. prefixWidth := ansi.StringWidth(prefix)
  743. contentWidth := colWidth - prefixWidth
  744. content := renderLineContent(fileName, *dl, bgStyle, highlightColor, contentWidth)
  745. return prefix + content
  746. }
  747. // renderLeftColumn formats the left side of a side-by-side diff
  748. func renderLeftColumn(fileName string, dl *DiffLine, colWidth int) string {
  749. return renderDiffColumnLine(fileName, dl, colWidth, true, theme.CurrentTheme())
  750. }
  751. // renderRightColumn formats the right side of a side-by-side diff
  752. func renderRightColumn(fileName string, dl *DiffLine, colWidth int) string {
  753. return renderDiffColumnLine(fileName, dl, colWidth, false, theme.CurrentTheme())
  754. }
  755. // -------------------------------------------------------------------------
  756. // Public API
  757. // -------------------------------------------------------------------------
  758. // RenderUnifiedHunk formats a hunk for unified display
  759. func RenderUnifiedHunk(fileName string, h Hunk, opts ...UnifiedOption) string {
  760. // Apply options to create the configuration
  761. config := NewUnifiedConfig(opts...)
  762. // Make a copy of the hunk so we don't modify the original
  763. hunkCopy := Hunk{Lines: make([]DiffLine, len(h.Lines))}
  764. copy(hunkCopy.Lines, h.Lines)
  765. // Highlight changes within lines
  766. HighlightIntralineChanges(&hunkCopy)
  767. var sb strings.Builder
  768. sb.Grow(len(hunkCopy.Lines) * config.Width)
  769. util.WriteStringsPar(&sb, hunkCopy.Lines, func(line DiffLine) string {
  770. return renderUnifiedLine(fileName, line, config.Width, theme.CurrentTheme()) + "\n"
  771. })
  772. return sb.String()
  773. }
  774. // RenderSideBySideHunk formats a hunk for side-by-side display
  775. func RenderSideBySideHunk(fileName string, h Hunk, opts ...UnifiedOption) string {
  776. // Apply options to create the configuration
  777. config := NewSideBySideConfig(opts...)
  778. // Make a copy of the hunk so we don't modify the original
  779. hunkCopy := Hunk{Lines: make([]DiffLine, len(h.Lines))}
  780. copy(hunkCopy.Lines, h.Lines)
  781. // Highlight changes within lines
  782. HighlightIntralineChanges(&hunkCopy)
  783. // Pair lines for side-by-side display
  784. pairs := pairLines(hunkCopy.Lines)
  785. // Calculate column width
  786. colWidth := config.Width / 2
  787. leftWidth := colWidth
  788. rightWidth := config.Width - colWidth
  789. var sb strings.Builder
  790. util.WriteStringsPar(&sb, pairs, func(p linePair) string {
  791. wg := &sync.WaitGroup{}
  792. var leftStr, rightStr string
  793. wg.Add(2)
  794. go func() {
  795. defer wg.Done()
  796. leftStr = renderLeftColumn(fileName, p.left, leftWidth)
  797. }()
  798. go func() {
  799. defer wg.Done()
  800. rightStr = renderRightColumn(fileName, p.right, rightWidth)
  801. }()
  802. wg.Wait()
  803. return leftStr + rightStr + "\n"
  804. })
  805. return sb.String()
  806. }
  807. // FormatUnifiedDiff creates a unified formatted view of a diff
  808. func FormatUnifiedDiff(filename string, diffText string, opts ...UnifiedOption) (string, error) {
  809. diffResult, err := ParseUnifiedDiff(diffText)
  810. if err != nil {
  811. return "", err
  812. }
  813. var sb strings.Builder
  814. util.WriteStringsPar(&sb, diffResult.Hunks, func(h Hunk) string {
  815. return RenderUnifiedHunk(filename, h, opts...)
  816. })
  817. return sb.String(), nil
  818. }
  819. // FormatDiff creates a side-by-side formatted view of a diff
  820. func FormatDiff(filename string, diffText string, opts ...UnifiedOption) (string, error) {
  821. diffResult, err := ParseUnifiedDiff(diffText)
  822. if err != nil {
  823. return "", err
  824. }
  825. var sb strings.Builder
  826. util.WriteStringsPar(&sb, diffResult.Hunks, func(h Hunk) string {
  827. return RenderSideBySideHunk(filename, h, opts...)
  828. })
  829. return sb.String(), nil
  830. }