Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.
TypeScript
mods/diff: the closing empty row is the git backend's to add after parsing, so hunks parsed for another backend stay as printed
poteatddb122f0b875
diff --git a/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts b/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.tsindex 49cf11d3..200dbfd9 100644--- a/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts+++ b/mods/diff/hooks/git/fetch-file-hunks/fetch-file-hunks.ts@@ -3,6 +3,7 @@ import { EMPTY_FILE_HUNKS } from '../empty-file-hunks'import Parse from '../parse'import type Types from '../types'import { isLastOfDiff } from './is-last-of-diff'+import { withClosingLine } from './with-closing-line'/*** One file's hunks against the base its row was read against, so body and@@ -10,7 +11,7 @@ import { isLastOfDiff } from './is-last-of-diff'** An untracked, binary or renamed row (the built-in shows a rename's* counts, never its body), or on an unborn HEAD a file edited after- * staging, answers no hunks; the diff's last file, no closing empty line.+ * staging, answers no hunks; each file but the last, a closing empty row.** @param run runs git against the pinned repository* @param data the fetch the row belongs to@@ -40,7 +41,11 @@ export async function fetchFileHunks(file.path,])- const printed = isLastOfDiff(data, file) ? stdout.replace(/\n$/, '') : stdout+ if (exitCode !== 0) {+ return null+ }++ const body = Parse.parseFileDiff(stdout)- return exitCode === 0 ? Parse.parseFileDiff(printed) : null+ return isLastOfDiff(data, file) ? body : withClosingLine(body)}diff --git a/mods/diff/hooks/git/fetch-file-hunks/index.ts b/mods/diff/hooks/git/fetch-file-hunks/index.tsindex d842c72e..e07f872d 100644--- a/mods/diff/hooks/git/fetch-file-hunks/index.ts+++ b/mods/diff/hooks/git/fetch-file-hunks/index.ts@@ -1,4 +1,5 @@export * from './fetch-file-hunks.js'export * from './is-last-of-diff'+export * from './with-closing-line'export * as default from '.'diff --git a/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.ts b/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.tsindex 930c178c..0681af90 100644--- a/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.ts+++ b/mods/diff/hooks/git/fetch-file-hunks/is-last-of-diff/is-last-of-diff.ts@@ -5,7 +5,7 @@ import type Types from '../../types'* fetch: the last tracked row, the untracked ones being listed apart.** The built-in reads that diff in one piece and trims its end, so the last- * file alone loses git's closing empty line and the context row it becomes.+ * file alone has no closing empty line to draw (withClosingLine).** @param data the fetch the row belongs to* @param file the rowdiff --git a/mods/diff/hooks/git/fetch-file-hunks/with-closing-line/index.ts b/mods/diff/hooks/git/fetch-file-hunks/with-closing-line/index.tsnew file mode 100644index 00000000..c6bf8e1c--- /dev/null+++ b/mods/diff/hooks/git/fetch-file-hunks/with-closing-line/index.ts@@ -0,0 +1,3 @@+export * from './with-closing-line.js'++export * as default from '.'diff --git a/mods/diff/hooks/git/fetch-file-hunks/with-closing-line/with-closing-line.ts b/mods/diff/hooks/git/fetch-file-hunks/with-closing-line/with-closing-line.tsnew file mode 100644index 00000000..2c81e001--- /dev/null+++ b/mods/diff/hooks/git/fetch-file-hunks/with-closing-line/with-closing-line.ts@@ -0,0 +1,27 @@+import type Types from '../../types'++/**+ * A parsed body with one empty context row after its last hunk's lines, as+ * the built-in panel draws the empty line that closes git's output.+ *+ * The built-in reads every file's diff in one piece: the newline before the+ * next file's header parses as a context line of the file before it. A+ * body with no hunk, or one already cut, gains nothing.+ *+ * @param body the parsed body+ * @returns the body, its last hunk one row longer+ */+export function withClosingLine(body: Types.FileHunks): Types.FileHunks {+ const last = body.hunks.at(-1)+ const isClosed = last !== undefined && !body.isTruncated++ return isClosed+ ? {+ ...body,+ hunks: [+ ...body.hunks.slice(0, -1),+ { ...last, lines: [...last.lines, ' '] },+ ],+ }+ : body+}diff --git a/mods/diff/hooks/git/parse/body-line-of/body-line-of.ts b/mods/diff/hooks/git/parse/body-line-of/body-line-of.tsdeleted file mode 100644index ffe42e3e..00000000--- a/mods/diff/hooks/git/parse/body-line-of/body-line-of.ts+++ /dev/null@@ -1,8 +0,0 @@-/**- * A body line as a hunk keeps it: git's closing empty line becomes the- * empty context row the built-in panel draws for it; the rest stand.- *- * @param line a line isBodyLine passed- * @returns the line, a lone space for the empty one- */-export const bodyLineOf = (line: string) => (line === '' ? ' ' : line)diff --git a/mods/diff/hooks/git/parse/body-line-of/index.ts b/mods/diff/hooks/git/parse/body-line-of/index.tsdeleted file mode 100644index c263ed3e..00000000--- a/mods/diff/hooks/git/parse/body-line-of/index.ts+++ /dev/null@@ -1,3 +0,0 @@-export * from './body-line-of.js'--export * as default from '.'diff --git a/mods/diff/hooks/git/parse/index.ts b/mods/diff/hooks/git/parse/index.tsindex cb229a3d..b3f08909 100644--- a/mods/diff/hooks/git/parse/index.ts+++ b/mods/diff/hooks/git/parse/index.ts@@ -1,4 +1,3 @@-export * from './body-line-of'export * from './hunk-header'export * from './is-body-line'export * from './is-whole-answer'diff --git a/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts b/mods/diff/hooks/git/parse/is-body-line/is-body-line.tsindex 09054a5a..a0bc81fd 100644--- a/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts+++ b/mods/diff/hooks/git/parse/is-body-line/is-body-line.ts@@ -2,14 +2,8 @@* Whether a line after a hunk header is a body line (added, removed or* context) rather than a note such as `\ No newline at end of file`.*- * The empty line that ends git's output counts, as the built-in panel- * counts it: the last hunk of a file closes on one more context row.- ** @param line one line of `git diff` output- * @returns whether it starts with `+`, `-` or a space, or is empty+ * @returns whether it starts with `+`, `-` or a space*/export const isBodyLine = (line: string) =>- line === '' ||- line.startsWith('+') ||- line.startsWith('-') ||- line.startsWith(' ')+ line.startsWith('+') || line.startsWith('-') || line.startsWith(' ')diff --git a/mods/diff/hooks/git/parse/parse-file-diff.ts b/mods/diff/hooks/git/parse/parse-file-diff.tsindex 947de76c..60c31d66 100644--- a/mods/diff/hooks/git/parse/parse-file-diff.ts+++ b/mods/diff/hooks/git/parse/parse-file-diff.ts@@ -3,7 +3,6 @@ import { cutHunks } from '../cut-hunks'import { hunkOf } from '../hunk-of'import { LARGE_FILE_HUNKS } from '../large-file-hunks'import type Types from '../types'-import { bodyLineOf } from './body-line-of'import { HUNK_HEADER } from './hunk-header'import { isBodyLine } from './is-body-line'@@ -33,10 +32,7 @@ export function parseFileDiff(stdout: string): Types.FileHunks {headerRows.map((at, ordinal) =>hunkOf(HUNK_HEADER.exec(rows[at] ?? '')?.groups,- rows- .slice(at + 1, headerRows[ordinal + 1])- .filter(isBodyLine)- .map(bodyLineOf),+ rows.slice(at + 1, headerRows[ordinal + 1]).filter(isBodyLine),),),),diff --git a/mods/diff/tests/views.test.ts b/mods/diff/tests/views.test.tsindex 59d660dd..96ce1740 100644--- a/mods/diff/tests/views.test.ts+++ b/mods/diff/tests/views.test.ts@@ -15,8 +15,12 @@ describe('views', () => {const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))expect(drawn).toContain('2 files changed +3 -1')- expect(drawn).toContain('+const a = 2')- expect(drawn).toContain('+export const c = 2')++ expect(drawn, "a closing empty row, as the built-in's").toContain(+ '+const a = 2\n ',+ )++ expect(drawn, 'none after the last file').toMatch(/\+export const c = 2$/)expect(drawn).not.toContain('❯')})