on main help sign in
refactor(diffview): refactor order of functions to match call order
commitac33d35a98e81f0b133f6e8b1dc1aa29787a2995 authorAndrey Nering <andreynering@users.noreply.github.com> dateMon 9 Jun 2025, 17:19 (1 year ago) parentbbc3122
1 file changed+94−84
internal/exp/diffview/diffview.go+94−84
internal/exp/diffview/diffview.go+94−84
@@ -149,6 +149,40 @@ func (dv *DiffView) String() string {
149149 }
150150}
151151
152+// computeDiff computes the differences between the "before" and "after" files.
153+func (dv *DiffView) computeDiff() error {
154+ if dv.isComputed {
155+ return dv.err
156+ }
157+ dv.isComputed = true
158+ dv.edits = myers.ComputeEdits( //nolint:staticcheck
159+ dv.before.content,
160+ dv.after.content,
161+ )
162+ dv.unified, dv.err = udiff.ToUnifiedDiff(
163+ dv.before.path,
164+ dv.after.path,
165+ dv.before.content,
166+ dv.edits,
167+ dv.contextLines,
168+ )
169+ return dv.err
170+}
171+
172+// convertDiffToSplit converts the unified diff to a split diff if the layout is
173+// set to split.
174+func (dv *DiffView) convertDiffToSplit() {
175+ if dv.layout != layoutSplit {
176+ return
177+ }
178+
179+ dv.splitHunks = make([]splitHunk, len(dv.unified.Hunks))
180+ for i, h := range dv.unified.Hunks {
181+ dv.splitHunks[i] = hunkToSplit(h)
182+ }
183+}
184+
185+// adjustStyles adjusts adds padding and alignment to the styles.
152186func (dv *DiffView) adjustStyles() {
153187 dv.style.MissingLine.LineNumber = setPadding(dv.style.MissingLine.LineNumber)
154188 dv.style.DividerLine.LineNumber = setPadding(dv.style.DividerLine.LineNumber)
@@ -157,10 +191,69 @@ func (dv *DiffView) adjustStyles() {
157191 dv.style.DeleteLine.LineNumber = setPadding(dv.style.DeleteLine.LineNumber)
158192}
159193
194+// detectNumDigits calculates the maximum number of digits needed for before and
195+// after line numbers.
196+func (dv *DiffView) detectNumDigits() {
197+ dv.beforeNumDigits = 0
198+ dv.afterNumDigits = 0
199+
200+ for _, h := range dv.unified.Hunks {
201+ dv.beforeNumDigits = max(dv.beforeNumDigits, len(strconv.Itoa(h.FromLine+len(h.Lines))))
202+ dv.afterNumDigits = max(dv.afterNumDigits, len(strconv.Itoa(h.ToLine+len(h.Lines))))
203+ }
204+}
205+
160206func setPadding(s lipgloss.Style) lipgloss.Style {
161207 return s.Padding(0, lineNumPadding).Align(lipgloss.Right)
162208}
163209
210+// detectCodeWidth calculates the maximum width of code lines in the diff view.
211+func (dv *DiffView) detectCodeWidth() {
212+ switch dv.layout {
213+ case layoutUnified:
214+ dv.detectUnifiedCodeWidth()
215+ case layoutSplit:
216+ dv.detectSplitCodeWidth()
217+ }
218+ dv.fullCodeWidth = dv.codeWidth + leadingSymbolsSize
219+}
220+
221+// detectUnifiedCodeWidth calculates the maximum width of code lines in a
222+// unified diff.
223+func (dv *DiffView) detectUnifiedCodeWidth() {
224+ dv.codeWidth = 0
225+
226+ for _, h := range dv.unified.Hunks {
227+ shownLines := ansi.StringWidth(dv.hunkLineFor(h))
228+
229+ for _, l := range h.Lines {
230+ lineWidth := ansi.StringWidth(strings.TrimSuffix(l.Content, "\n")) + 1
231+ dv.codeWidth = max(dv.codeWidth, lineWidth, shownLines)
232+ }
233+ }
234+}
235+
236+// detectSplitCodeWidth calculates the maximum width of code lines in a
237+// split diff.
238+func (dv *DiffView) detectSplitCodeWidth() {
239+ dv.codeWidth = 0
240+
241+ for i, h := range dv.splitHunks {
242+ shownLines := ansi.StringWidth(dv.hunkLineFor(dv.unified.Hunks[i]))
243+
244+ for _, l := range h.lines {
245+ if l.before != nil {
246+ codeWidth := ansi.StringWidth(strings.TrimSuffix(l.before.Content, "\n")) + 1
247+ dv.codeWidth = max(dv.codeWidth, codeWidth, shownLines)
248+ }
249+ if l.after != nil {
250+ codeWidth := ansi.StringWidth(strings.TrimSuffix(l.after.Content, "\n")) + 1
251+ dv.codeWidth = max(dv.codeWidth, codeWidth, shownLines)
252+ }
253+ }
254+ }
255+}
256+
164257// renderUnified renders the unified diff view as a string.
165258func (dv *DiffView) renderUnified() string {
166259 var b strings.Builder
@@ -289,37 +382,7 @@ func (dv *DiffView) renderSplit() string {
289382 return b.String()
290383}
291384
292−func (dv *DiffView) computeDiff() error {
293− if dv.isComputed {
294− return dv.err
295− }
296− dv.isComputed = true
297− dv.edits = myers.ComputeEdits( //nolint:staticcheck
298− dv.before.content,
299− dv.after.content,
300− )
301− dv.unified, dv.err = udiff.ToUnifiedDiff(
302− dv.before.path,
303− dv.after.path,
304− dv.before.content,
305− dv.edits,
306− dv.contextLines,
307− )
308− return dv.err
309−}
310−
311−// detectNumDigits calculates the maximum number of digits needed for before and
312−// after line numbers.
313−func (dv *DiffView) detectNumDigits() {
314− dv.beforeNumDigits = 0
315− dv.afterNumDigits = 0
316−
317− for _, h := range dv.unified.Hunks {
318− dv.beforeNumDigits = max(dv.beforeNumDigits, len(strconv.Itoa(h.FromLine+len(h.Lines))))
319− dv.afterNumDigits = max(dv.afterNumDigits, len(strconv.Itoa(h.ToLine+len(h.Lines))))
320− }
321−}
322−
385+// hunkLineFor formats the header line for a hunk in the unified diff view.
323386func (dv *DiffView) hunkLineFor(h *udiff.Hunk) string {
324387 beforeShownLines, afterShownLines := dv.hunkShownLines(h)
325388
@@ -348,56 +411,3 @@ func (dv *DiffView) hunkShownLines(h *udiff.Hunk) (before, after int) {
348411 }
349412 return
350413}
351−
352−func (dv *DiffView) detectCodeWidth() {
353− switch dv.layout {
354− case layoutUnified:
355− dv.detectUnifiedCodeWidth()
356− case layoutSplit:
357− dv.detectSplitCodeWidth()
358− }
359− dv.fullCodeWidth = dv.codeWidth + leadingSymbolsSize
360−}
361−
362−func (dv *DiffView) detectUnifiedCodeWidth() {
363− dv.codeWidth = 0
364−
365− for _, h := range dv.unified.Hunks {
366− shownLines := ansi.StringWidth(dv.hunkLineFor(h))
367−
368− for _, l := range h.Lines {
369− lineWidth := ansi.StringWidth(strings.TrimSuffix(l.Content, "\n")) + 1
370− dv.codeWidth = max(dv.codeWidth, lineWidth, shownLines)
371− }
372− }
373−}
374−
375−func (dv *DiffView) convertDiffToSplit() {
376− if dv.layout != layoutSplit {
377− return
378− }
379−
380− dv.splitHunks = make([]splitHunk, len(dv.unified.Hunks))
381− for i, h := range dv.unified.Hunks {
382− dv.splitHunks[i] = hunkToSplit(h)
383− }
384−}
385−
386−func (dv *DiffView) detectSplitCodeWidth() {
387− dv.codeWidth = 0
388−
389− for i, h := range dv.splitHunks {
390− shownLines := ansi.StringWidth(dv.hunkLineFor(dv.unified.Hunks[i]))
391−
392− for _, l := range h.lines {
393− if l.before != nil {
394− codeWidth := ansi.StringWidth(strings.TrimSuffix(l.before.Content, "\n")) + 1
395− dv.codeWidth = max(dv.codeWidth, codeWidth, shownLines)
396− }
397− if l.after != nil {
398− codeWidth := ansi.StringWidth(strings.TrimSuffix(l.after.Content, "\n")) + 1
399− dv.codeWidth = max(dv.codeWidth, codeWidth, shownLines)
400− }
401− }
402− }
403−}