Skip to content

Commit 7d8bac0

Browse files
fix: ensure minimum row height for empty lines and correct 'No newline' row calculation
- Fix getCurrentLineRow to return minimum 1 row for empty lines (both CodeView and DiffView) - Fix 'No newline at end of file' row height calculation to include actual text length instead of blindly adding +1
1 parent c92d12d commit 7d8bac0

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

packages/cli/src/components/DiffSplitContentLine.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,21 @@ const InternalDiffSplitLine = ({
5656

5757
// Calculate row heights - must match the actual wrap width used in DiffContent
5858
// DiffContent receives contentWidth and wraps at (contentWidth - 2) for the operator column and end padding
59-
let oldRow = getCurrentLineRow({ content: oldLine?.value || "", width: contentWidth - 2 });
60-
let newRow = getCurrentLineRow({ content: newLine?.value || "", width: contentWidth - 2 });
59+
const noNewlineText = "\\ No newline at end of file";
6160

62-
oldRow =
61+
// Include "No newline" text in row calculation if present
62+
const oldContent =
6363
oldLine?.diff?.changes?.hasLineChange && oldLine?.diff?.changes.newLineSymbol === NewLineSymbol.NEWLINE
64-
? oldRow + 1
65-
: oldRow;
64+
? (oldLine?.value || "") + noNewlineText
65+
: oldLine?.value || "";
6666

67-
newRow =
67+
const newContent =
6868
newLine?.diff?.changes?.hasLineChange && newLine?.diff?.changes.newLineSymbol === NewLineSymbol.NEWLINE
69-
? newRow + 1
70-
: newRow;
69+
? (newLine?.value || "") + noNewlineText
70+
: newLine?.value || "";
71+
72+
const oldRow = getCurrentLineRow({ content: oldContent, width: contentWidth - 2 });
73+
const newRow = getCurrentLineRow({ content: newContent, width: contentWidth - 2 });
7174

7275
const row = Math.max(oldRow, newRow);
7376

packages/cli/src/components/DiffUnifiedContentLine.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,15 @@ const InternalDiffUnifiedLine = ({
181181

182182
const contentWidth = columns - (lineNumWidth + 1) * 2 - 1;
183183

184-
// Use contentWidth - 2 to match the actual wrap width in DiffContent (operator column takes 1 char, end padding takes 1 char)
185-
let row = getCurrentLineRow({ content: rawLine, width: contentWidth - 2 });
184+
// Include "No newline" text in row calculation if present
185+
const noNewlineText = "\\ No newline at end of file";
186+
const contentWithSymbol =
187+
diffLine?.changes?.hasLineChange && diffLine.changes.newLineSymbol === NewLineSymbol.NEWLINE
188+
? rawLine + noNewlineText
189+
: rawLine;
186190

187-
row = diffLine?.changes?.hasLineChange && diffLine.changes.newLineSymbol === NewLineSymbol.NEWLINE ? row + 1 : row;
191+
// Use contentWidth - 2 to match the actual wrap width in DiffContent (operator column takes 1 char, end padding takes 1 char)
192+
const row = getCurrentLineRow({ content: contentWithSymbol, width: contentWidth - 2 });
188193

189194
const color = hasDiff
190195
? theme === "light"

packages/cli/src/components/codeTools.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,5 +93,6 @@ export const createCodeConfigStore = <T = any>(props: CodeViewProps<T> & { isMou
9393
};
9494

9595
export const getCurrentLineRow = ({ content, width }: { content: string; width: number }) => {
96-
return Math.ceil(stringWidth(content) / width);
96+
// Ensure minimum of 1 row for empty lines
97+
return Math.max(1, Math.ceil(stringWidth(content) / width));
9798
};

packages/cli/src/components/tools.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ export const createDiffConfigStore = <T = any>(
127127
};
128128

129129
export const getCurrentLineRow = ({ content, width }: { content: string; width: number }) => {
130-
return Math.ceil(stringWidth(content) / width);
130+
// Ensure minimum of 1 row for empty lines
131+
return Math.max(1, Math.ceil(stringWidth(content) / width));
131132
};
132133

133134
export const getStringContentWithFixedWidth = ({

packages/cli/test/code.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ export const highlighterReady = new Promise<DiffHighlighter>((r) => {
210210
getDiffViewHighlighter().then((highlighter) => {
211211
render(
212212
createElement(CodeView, {
213-
data: {content: temp1, fileLang: 'ts'},
213+
data: { content: temp1, fileLang: "ts" },
214214
// width: 80,
215215
codeViewTheme: "dark",
216216
extendData: {
217-
newFile: { 107: { data: "test extend data" } },
217+
107: { data: "test extend data" },
218218
},
219219
renderExtendLine: ({ data }) => {
220220
return createElement(

packages/cli/test/file.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ getDiffViewHighlighter().then((highlighter) => {
422422
// diffViewTabWidth: 'small',
423423
// diffViewTabSpace: true,
424424
extendData: {
425-
newFile: { 107: { data: "test extend data" } },
425+
newFile: { 97: { data: "test extend data" } },
426426
},
427427
renderExtendLine: ({ data }) => {
428428
return createElement(

0 commit comments

Comments
 (0)