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
61 changes: 59 additions & 2 deletions src/components/Instructions/InstructionItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { argType, mapInstructionsArgsByType, valueToBinary, valueToNumeralSystem
import classNames from "classnames";
import { ExpectedState, RegistersArray, Status } from "@/types/pvm";
import { InstructionMode } from "./types";
import { ForwardedRef, forwardRef, useCallback, useContext, useState } from "react";
import { ForwardedRef, forwardRef, useCallback, useContext, useMemo, useState } from "react";
import { NumeralSystemContext } from "@/context/NumeralSystemContext";
import { TableCell, TableRow } from "../ui/table";
import { ProgramRow } from "./InstructionsTable";
Expand All @@ -13,6 +13,7 @@ import { Tooltip, TooltipContent, TooltipPortal, TooltipTrigger } from "@/compon
import { useIsDarkMode } from "@/packages/ui-kit/DarkMode/utils";
import { selectProgram } from "@/store/debugger/debuggerSlice.ts";
import { getStatusColor } from "@/utils/colors";
import { NumeralSystem } from "@/context/NumeralSystem";

const getWorkerValueFromState = (
worker: WorkerState,
Expand Down Expand Up @@ -166,6 +167,7 @@ export const InstructionItem = forwardRef(
<TableCell className="p-1.5 whitespace-nowrap border-b font-inconsolata">
<span className="">
{"args" in programRow && (
/** Width of this cell is adjusted to the widest item by WidestInstructionItem component */
<span
dangerouslySetInnerHTML={{
__html:
Expand Down Expand Up @@ -193,7 +195,6 @@ export const InstructionItem = forwardRef(
bits={valueToBinary(programRow.instructionCode, 8)}
value={valueToNumeralSystem(programRow.instructionCode, numeralSystem)}
/>

{"args" in programRow &&
mapInstructionsArgsByType(programRow.args, numeralSystem, programRow.counter, program)
?.filter((instruction) => !instruction.hiddenFromDetails)
Expand Down Expand Up @@ -329,3 +330,59 @@ function DetailsColumn({ kind, bits, value }: DetailsColumnProps) {
</div>
);
}

/** A hiddent item of instruction table that ensures the table is adjusted to the widest instruction item */
export function WidestInstructionItem({
instructionMode,
programRows,
}: {
instructionMode: InstructionMode;
programRows: ProgramRow[] | undefined;
}) {
const program = useAppSelector(selectProgram);

const widestItem = useMemo(
() =>
programRows?.reduce<{ widestItem: ProgramRow | null; maxWidth: number }>(
(acc, item) => {
const { maxWidth } = acc;
if (!("args" in item)) {
return acc;
}
const argsLength =
mapInstructionsArgsByType(item.args, NumeralSystem.HEXADECIMAL, 0, program)
?.map((x) => x.value)
.join("").length ?? 0;

if (argsLength > maxWidth) {
return { widestItem: item, maxWidth: argsLength };
} else {
return acc;
}
},
{ widestItem: null, maxWidth: 0 },
).widestItem,
[programRows, program],
);

if (!widestItem) {
return null;
}

return (
<InstructionItem
index={0}
status={Status.OK}
isLast={false}
onClick={() => {}}
instructionMode={instructionMode}
programRow={widestItem}
currentPc={0}
onAddressClick={() => {}}
breakpointAddresses={[]}
style={{
visibility: "hidden",
}}
/>
);
}
7 changes: 6 additions & 1 deletion src/components/Instructions/InstructionsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ReactNode, useCallback, useContext, useEffect, useMemo, useRef } from "react";
import { useVirtualizer } from "@tanstack/react-virtual";
import { InstructionItem } from "./InstructionItem";
import { InstructionItem, WidestInstructionItem } from "./InstructionItem";
import { NumeralSystem } from "@/context/NumeralSystem";
import { NumeralSystemContext } from "@/context/NumeralSystemContext";
import { useAppSelector } from "@/store/hooks";
Expand Down Expand Up @@ -118,6 +118,11 @@ export const InstructionsTable = ({
/>
);
})}

{
// an additional hidden item that prevents column resizing when table is scrolled
}
<WidestInstructionItem programRows={programRows} instructionMode={instructionMode} />
</tbody>
</table>
</div>
Expand Down
30 changes: 15 additions & 15 deletions src/components/Instructions/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.registerIndex,
valueDecimal: args?.registerIndex,
valueFormatted: `ω<sub>${args?.registerIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.registerIndex}</sub>`,
argumentBitLength: bitLengths?.registerIndexBits || 4,
},
{
Expand All @@ -137,7 +137,7 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.registerIndex,
valueDecimal: args?.registerIndex,
valueFormatted: `ω<sub>${args?.registerIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.registerIndex}</sub>`,
argumentBitLength: bitLengths?.registerIndexBits || 4,
},
{
Expand Down Expand Up @@ -168,7 +168,7 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.registerIndex,
valueDecimal: args?.registerIndex,
valueFormatted: `ω<sub>${args?.registerIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.registerIndex}</sub>`,
argumentBitLength: bitLengths?.registerIndexBits || 4,
},
{
Expand Down Expand Up @@ -199,7 +199,7 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.registerIndex,
valueDecimal: args?.registerIndex,
valueFormatted: `ω<sub>${args?.registerIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.registerIndex}</sub>`,
argumentBitLength: bitLengths?.registerIndexBits || 4,
},
{
Expand All @@ -216,14 +216,14 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.firstRegisterIndex,
valueDecimal: args?.firstRegisterIndex,
valueFormatted: `ω<sub>${args?.firstRegisterIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.firstRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.firstRegisterIndexBits || 4,
},
{
type: argType.REGISTER,
value: args?.secondRegisterIndex,
valueDecimal: args?.secondRegisterIndex,
valueFormatted: `ω<sub>${args?.secondRegisterIndex}</sub>`,
valueFormatted: `ω<sub>D=${args?.secondRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.secondRegisterIndexBits || 4,
},
];
Expand All @@ -233,14 +233,14 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.firstRegisterIndex,
valueDecimal: args?.firstRegisterIndex,
valueFormatted: `ω<sub>${args?.firstRegisterIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.firstRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.firstRegisterIndexBits || 4,
},
{
type: argType.REGISTER,
value: args?.secondRegisterIndex,
valueDecimal: args?.secondRegisterIndex,
valueFormatted: `ω<sub>${args?.secondRegisterIndex}</sub>`,
valueFormatted: `ω<sub>B=${args?.secondRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.secondRegisterIndexBits || 4,
},
{
Expand All @@ -257,14 +257,14 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.firstRegisterIndex,
valueDecimal: args?.firstRegisterIndex,
valueFormatted: `ω<sub>${args?.firstRegisterIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.firstRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.firstRegisterIndexBits || 4,
},
{
type: argType.REGISTER,
value: args?.secondRegisterIndex,
valueDecimal: args?.secondRegisterIndex,
valueFormatted: `ω<sub>${args?.secondRegisterIndex}</sub>`,
valueFormatted: `ω<sub>B=${args?.secondRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.secondRegisterIndexBits || 4,
},
{
Expand All @@ -281,14 +281,14 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.firstRegisterIndex,
valueDecimal: args?.firstRegisterIndex,
valueFormatted: `ω<sub>${args?.firstRegisterIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.firstRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.firstRegisterIndexBits || 4,
},
{
type: argType.REGISTER,
value: args?.secondRegisterIndex,
valueDecimal: args?.secondRegisterIndex,
valueFormatted: `ω<sub>${args?.secondRegisterIndex}</sub>`,
valueFormatted: `ω<sub>B=${args?.secondRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.secondRegisterIndexBits || 4,
},
{
Expand Down Expand Up @@ -319,21 +319,21 @@ export const mapInstructionsArgsByType = (
type: argType.REGISTER,
value: args?.firstRegisterIndex,
valueDecimal: args?.firstRegisterIndex,
valueFormatted: `ω<sub>${args?.firstRegisterIndex}</sub>`,
valueFormatted: `ω<sub>A=${args?.firstRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.firstRegisterIndexBits || 4,
},
{
type: argType.REGISTER,
value: args?.secondRegisterIndex,
valueDecimal: args?.secondRegisterIndex,
valueFormatted: `ω<sub>${args?.secondRegisterIndex}</sub>`,
valueFormatted: `ω<sub>B=${args?.secondRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.secondRegisterIndexBits || 4,
},
{
type: argType.REGISTER,
value: args?.thirdRegisterIndex,
valueDecimal: args?.thirdRegisterIndex,
valueFormatted: `ω<sub>${args?.thirdRegisterIndex}</sub>`,
valueFormatted: `ω<sub>D=${args?.thirdRegisterIndex}</sub>`,
argumentBitLength: bitLengths?.thirdRegisterIndexBits || 4,
},
];
Expand Down
Loading