Skip to content

Commit 659da35

Browse files
committed
Fix C source line scrollTo
1 parent eadcb51 commit 659da35

File tree

4 files changed

+69
-34
lines changed

4 files changed

+69
-34
lines changed

src/App.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99

1010
import {
1111
fetchLogFromUrl,
12-
getVisibleIdxRange,
12+
getVisibleLogLineRange,
1313
scrollToLogLine,
1414
scrollToCLine,
1515
siblingInsLine,
@@ -157,17 +157,10 @@ function App() {
157157
memSlotId: string = "",
158158
) => {
159159
scrollToLogLine(nextInsLineIdx, logLines.length);
160-
const cLinesRange = getVisibleIdxRange(cLines.length);
161-
if (
162-
(nextCLineIdx < cLinesRange.min + 8 ||
163-
nextCLineIdx > cLinesRange.max - 8) &&
164-
!(nextCLineIdx < 0 || nextCLineIdx >= cLines.length)
165-
) {
166-
scrollToCLine(nextCLineIdx, cLines.length);
167-
}
160+
scrollToCLine(nextCLineIdx, cLines.length);
168161
setSelectedState({ line: nextInsLineId, memSlotId, cLine: nextCLineId });
169162
},
170-
[logLines, cLines],
163+
[logLines, cLineIdtoIdx],
171164
);
172165

173166
const onGotoStart = useCallback(() => {
@@ -212,7 +205,7 @@ function App() {
212205
const handleKeyDown = (e: KeyboardEvent) => {
213206
let delta = 0;
214207
let areCLinesInFocus = selectedState.cLine !== "";
215-
let { min, max } = getVisibleIdxRange(
208+
let { min, max } = getVisibleLogLineRange(
216209
areCLinesInFocus ? cLines.length : logLines.length,
217210
);
218211
let page = max - min + 1;

src/components.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { CSourceMap, getMemSlotDependencies } from "./analyzer";
1717

1818
import { BpfState, getBpfState, VerifierLogState } from "./analyzer";
1919

20-
import { getVisibleIdxRange, scrollToLogLine } from "./utils";
20+
import { getVisibleLogLineRange, scrollToLogLine } from "./utils";
2121

2222
import BPF_HELPERS_JSON from "./bpf-helpers.json";
2323

@@ -1207,7 +1207,7 @@ export function MainContent({
12071207
}
12081208
}
12091209

1210-
let { min, max } = getVisibleIdxRange(verifierLogState.lines.length);
1210+
let { min, max } = getVisibleLogLineRange(logLines.length);
12111211
const isVisible = (idx: number) => {
12121212
return min < idx && idx < max;
12131213
};
@@ -1235,7 +1235,7 @@ export function MainContent({
12351235
}
12361236
}
12371237
},
1238-
[verifierLogState, memSlotDependencies, selectedLine],
1238+
[logLines, memSlotDependencies, selectedLine],
12391239
);
12401240

12411241
return (

src/index.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ h1 {
8585

8686
#c-source-container {
8787
display: flex;
88-
flex-direction: column;
88+
flex-direction: row;
8989
flex: 1;
9090
min-width: 0;
9191
gap: 0;
92-
overflow: hidden;
92+
overflow: auto;
93+
flex-grow: 1;
9394
}
9495

9596
#c-source-content {
96-
overflow-y: scroll;
97-
height: 100%;
97+
height: fit-content;
9898
}
9999

100100
.c-source-panel {

src/utils.tsx

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,62 @@ export function normalIdx(idx: number, linesLen: number): number {
1717
return Math.min(Math.max(0, idx), linesLen - 1);
1818
}
1919

20-
export function getVisibleIdxRange(linesLen: number): {
20+
function getVisibleRange(
21+
containerEl: HTMLElement,
22+
contentEl: HTMLElement,
23+
linesLen: number,
24+
): {
2125
min: number;
2226
max: number;
2327
} {
24-
const formattedLogLines = document.getElementById("formatted-log-lines");
25-
const logContainer = document.getElementById("log-container");
26-
if (!formattedLogLines || !logContainer) {
27-
return { min: 0, max: 0 }; // Throw here
28-
}
29-
const linesRect = formattedLogLines.getBoundingClientRect();
30-
const containerRect = logContainer.getBoundingClientRect();
28+
const contentRect = contentEl.getBoundingClientRect();
29+
const containerRect = containerEl.getBoundingClientRect();
3130

32-
if (containerRect.height > linesRect.height) {
31+
if (containerRect.height > contentRect.height) {
3332
return { min: 0, max: linesLen - 1 };
3433
}
3534

36-
const relativeStart = (containerRect.top - linesRect.top) / linesRect.height;
37-
const relativeEnd = relativeStart + containerRect.height / linesRect.height;
35+
const relativeStart =
36+
(containerRect.top - contentRect.top) / contentRect.height;
37+
const relativeEnd = relativeStart + containerRect.height / contentRect.height;
3838
const min = Math.floor(relativeStart * linesLen);
3939
const max = Math.ceil(relativeEnd * linesLen);
4040

4141
return { min, max };
4242
}
4343

44-
function scrollToLine(el: HTMLElement, idx: number, linesLen: number) {
45-
const { min, max } = getVisibleIdxRange(linesLen);
44+
export function getVisibleLogLineRange(linesLen: number): {
45+
min: number;
46+
max: number;
47+
} {
48+
const formattedLogLines = document.getElementById("formatted-log-lines");
49+
const logContainer = document.getElementById("log-container");
50+
if (!formattedLogLines || !logContainer) {
51+
throw new Error("Missing formattedLogLines or logContainer");
52+
}
53+
return getVisibleRange(logContainer, formattedLogLines, linesLen);
54+
}
55+
56+
export function getVisibleCSourceRange(linesLen: number): {
57+
min: number;
58+
max: number;
59+
} {
60+
const csourceContent = document.getElementById("c-source-content");
61+
const csourceContainer = document.getElementById("c-source-container");
62+
if (!csourceContent || !csourceContainer) {
63+
// Don't throw the container might be collapsed
64+
return { min: 0, max: 0 };
65+
}
66+
return getVisibleRange(csourceContainer, csourceContent, linesLen);
67+
}
68+
69+
function scrollToLine(
70+
el: HTMLElement,
71+
idx: number,
72+
max: number,
73+
min: number,
74+
linesLen: number,
75+
) {
4676
const page = max - min + 1;
4777
const relativePosition = normalIdx(idx - page * 0.618, linesLen) / linesLen;
4878
el.scrollTop = relativePosition * el.scrollHeight;
@@ -53,12 +83,12 @@ export function scrollToLogLine(idx: number, linesLen: number) {
5383
if (!logContainer) {
5484
throw new Error("Log line container is not in the DOM");
5585
}
56-
const logRange = getVisibleIdxRange(linesLen);
86+
const logRange = getVisibleLogLineRange(linesLen);
5787
if (
5888
(idx < logRange.min + 8 || idx > logRange.max - 8) &&
5989
!(idx < 0 || idx >= linesLen)
6090
) {
61-
scrollToLine(logContainer, idx, linesLen);
91+
scrollToLine(logContainer, idx, logRange.max, logRange.min, linesLen);
6292
}
6393
}
6494

@@ -68,7 +98,19 @@ export function scrollToCLine(idx: number, linesLen: number) {
6898
// This won't exist if the container is collapsed
6999
return;
70100
}
71-
scrollToLine(cSourceContainer, idx, linesLen);
101+
const cLinesRange = getVisibleCSourceRange(linesLen);
102+
if (
103+
(idx < cLinesRange.min + 8 || idx > cLinesRange.max - 8) &&
104+
!(idx < 0 || idx >= linesLen)
105+
) {
106+
scrollToLine(
107+
cSourceContainer,
108+
idx,
109+
cLinesRange.max,
110+
cLinesRange.min,
111+
linesLen,
112+
);
113+
}
72114
}
73115

74116
export function siblingInsLine(

0 commit comments

Comments
 (0)