Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 113 additions & 9 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ import {
Examples,
CSourceRow,
StoredLogs,
StoredCLines,
} from "./components";
import { getCLineId, ParsedLine, ParsedLineType } from "./parser";

// Increment the version if there is a breaking change
// in what we store
const STORED_LOG_KEY = "logs-v2";

function getEmptyVisualLogState(): VisualLogState {
return {
verifierLogState: getEmptyVerifierState(),
Expand Down Expand Up @@ -62,6 +67,7 @@ function getVisibleLogLines(

function getVisibleCLines(
verifierLogState: VerifierLogState,
storedCLines: StoredCLines = {},
): [CSourceRow[], Map<string, number>] {
const cLineIdToVisualIdx: Map<string, number> = new Map();
const cLines: CSourceRow[] = [];
Expand All @@ -74,6 +80,24 @@ function getVisibleCLines(
});
++j;

if (file in storedCLines) {
storedCLines[file].forEach((lineText, i) => {
const lineNum = i + 1;
const sourceId = getCLineId(file, lineNum);
cLines.push({
type: "c_line",
file,
lineNum: i + 1,
lineText,
sourceId,
ignore: false,
});
cLineIdToVisualIdx.set(sourceId, j);
++j;
});
continue;
}

let unknownStart = 0;
for (let i = range[0]; i < range[1]; ++i) {
const sourceId = getCLineId(file, i);
Expand Down Expand Up @@ -144,6 +168,7 @@ const ContentRaw = ({
handleLogLinesOut,
handleFullLogToggle,
testListHeight,
addPastedCSourceFile,
}: {
loadError: string | null;
visualLogState: VisualLogState;
Expand All @@ -154,6 +179,7 @@ const ContentRaw = ({
handleLogLinesOut: (event: React.MouseEvent<HTMLDivElement>) => void;
handleFullLogToggle: () => void;
testListHeight: number | undefined;
addPastedCSourceFile: (fileName: string, pastedLines: string[]) => void;
}) => {
if (loadError) {
return <div>{loadError}</div>;
Expand All @@ -167,6 +193,7 @@ const ContentRaw = ({
handleLogLinesOut={handleLogLinesOut}
handleFullLogToggle={handleFullLogToggle}
testListHeight={testListHeight}
addPastedCSourceFile={addPastedCSourceFile}
/>
);
} else {
Expand Down Expand Up @@ -209,7 +236,7 @@ function App({ testListHeight }: { testListHeight?: number }) {
logLineIdxToVisualIdx.get(hoveredState.line) || 0;

useEffect(() => {
ldb.get("logs", function (value) {
ldb.get(STORED_LOG_KEY, function (value) {
try {
const logs: StoredLogs = JSON.parse(value);
if (logs) {
Expand Down Expand Up @@ -245,19 +272,23 @@ function App({ testListHeight }: { testListHeight?: number }) {
});
}, [verifierLogState]);

const updateStoredLogs = useCallback(
const updateStoredLogs = useCallback((nextStoredLogs: StoredLogs) => {
ldb.set(STORED_LOG_KEY, JSON.stringify(nextStoredLogs));
setStoredLogs(nextStoredLogs);
}, []);

const addStoredLog = useCallback(
(lines: string[], name: string = "") => {
const now = new Date();
const nextName = name
? name
: `pasted log (${now.toDateString().toLowerCase()} - ${now.toLocaleTimeString().toLocaleLowerCase()})`;
// Only keep 5 stored logs for now
const nextStoredLogs: StoredLogs = [
[nextName, lines],
[nextName, { rawLogLines: lines, pastedCLines: {} }],
...storedLogs.slice(0, 5),
];
ldb.set("logs", JSON.stringify(nextStoredLogs));
setStoredLogs(nextStoredLogs);
updateStoredLogs(nextStoredLogs);
},
[storedLogs],
);
Expand All @@ -268,13 +299,62 @@ function App({ testListHeight }: { testListHeight?: number }) {
setIsLoading(false);
}, []);

const addPastedCSourceFile = useCallback(
(fileName: string, pastedLines: string[]) => {
const nextStoredLogs: StoredLogs = [];
storedLogs.forEach((storedLog, idx) => {
// The first stored log is the current one
if (idx !== 0) {
nextStoredLogs.push(storedLog);
return;
}

const nextPastedCLines: StoredCLines = {};
let found = false;
for (const [storedFileName, lines] of Object.entries(
storedLog[1].pastedCLines,
)) {
if (storedFileName !== fileName) {
nextPastedCLines[storedFileName] = lines;
return;
}
found = true;
nextPastedCLines[storedFileName] = pastedLines;
}
if (!found) {
nextPastedCLines[fileName] = pastedLines;
}
nextStoredLogs.push([
storedLog[0],
{
rawLogLines: storedLog[1].rawLogLines,
pastedCLines: nextPastedCLines,
},
]);
setVisualLogState((prevState) => {
const [cLines, cLineIdToVisualIdx] = getVisibleCLines(
verifierLogState,
nextPastedCLines,
);
return {
...prevState,
cLines,
cLineIdToVisualIdx,
};
});
});
updateStoredLogs(nextStoredLogs);
},
[verifierLogState, storedLogs],
);

const loadInputText = useCallback(
(text: string) => {
const rawLines = text.split("\n");
loadLog(rawLines);
updateStoredLogs(rawLines);
addStoredLog(rawLines);
},
[updateStoredLogs],
[addStoredLog],
);

const prepareNewLog = useCallback(() => {
Expand Down Expand Up @@ -311,7 +391,30 @@ function App({ testListHeight }: { testListHeight?: number }) {
if (!found) {
console.error("Couldn't load previous log", example);
} else {
loadLog(found[1]);
loadLog(found[1].rawLogLines);
// Move this to the most recent stored log
const nextStoredLogs: StoredLogs = [[found[0], found[1]]];
storedLogs.forEach((storedLog) => {
if (storedLog[0] !== example) {
nextStoredLogs.push(storedLog);
}
});
updateStoredLogs(nextStoredLogs);
const newVerifierLogState = processRawLines(found[1].rawLogLines);
const nextVisualLogState = getVisualLogState(
newVerifierLogState,
false,
);
const [cLines, cLineIdToVisualIdx] = getVisibleCLines(
newVerifierLogState,
found[1].pastedCLines,
);
setVisualLogState({
...nextVisualLogState,
cLines,
cLineIdToVisualIdx,
});
setIsLoading(false);
}
}
},
Expand Down Expand Up @@ -396,7 +499,7 @@ function App({ testListHeight }: { testListHeight?: number }) {
}
rawLines = rawLines.concat(lines);
}
updateStoredLogs(rawLines, fileBlob.name);
addStoredLog(rawLines, fileBlob.name);
const newVerifierLogState = processRawLines(rawLines);
setVisualLogState(
getVisualLogState(newVerifierLogState, visualLogState.showFullLog),
Expand Down Expand Up @@ -452,6 +555,7 @@ function App({ testListHeight }: { testListHeight?: number }) {
handleLogLinesOut={handleLogLinesOut}
handleFullLogToggle={handleFullLogToggle}
testListHeight={testListHeight}
addPastedCSourceFile={addPastedCSourceFile}
/>
<div id="hint">
<SelectedLineHint
Expand Down
Loading