diff.go 28 KB

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