Skip to content

Commit 7c67959

Browse files
committed
Update "Example program" page to use "pick"
- Add code to check for `"pick"` contexts - Highlight multiple source ranges with different color
1 parent e469502 commit 7c67959

File tree

5 files changed

+101
-51
lines changed

5 files changed

+101
-51
lines changed

packages/web/spec/program/example.mdx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ code {
2828
2929
let localValue = storedValue + 1;
3030
storedValue = localValue;
31+
return;
3132
}
3233
`}]}
3334
instructions={[
@@ -285,8 +286,10 @@ code {
285286
mnemonic: "JUMPDEST"
286287
},
287288
context: ({ findSourceRange }) => ({
288-
code: findSourceRange("return;"),
289-
remark: "skip to here if not enough paid"
289+
pick: [
290+
{ code: findSourceRange("return;") },
291+
{ code: findSourceRange("return;", { after: "return;" }) },
292+
]
290293
})
291294
}
292295
]}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import Admonition from "@theme/Admonition";
2+
import Link from "@docusaurus/Link";
3+
import { Program } from "@ethdebug/format";
4+
import { useProgramExampleContext } from "./ProgramExampleContext";
5+
import { HighlightedInstruction } from "./HighlightedInstruction";
6+
import { Variables } from "./Variables";
7+
8+
// imported for style legend
9+
import "./SourceContents.css";
10+
11+
export interface Props {
12+
}
13+
14+
export function Details(props: Props): JSX.Element {
15+
const { highlightedInstruction, highlightMode } = useProgramExampleContext();
16+
17+
if (highlightMode === "simple" || !highlightedInstruction) {
18+
return <>
19+
<h3>Details</h3>
20+
<BasicAdmonition />
21+
</>;
22+
}
23+
24+
25+
return <>
26+
<h3>Details</h3>
27+
<InstructionAdmonition instruction={highlightedInstruction} />
28+
<details>
29+
<summary>See full <strong>ethdebug/format/program/instruction</strong> object</summary>
30+
<HighlightedInstruction />
31+
</details>
32+
</>;
33+
}
34+
35+
interface InstructionAdmonitionProps {
36+
instruction: Program.Instruction;
37+
}
38+
function InstructionAdmonition({
39+
instruction
40+
}: InstructionAdmonitionProps): JSX.Element {
41+
return <Admonition type="info">
42+
<p>
43+
The selected instruction provides the following <Link to="/spec/program/context">
44+
<strong>ethdebug/format</strong> Program contexts
45+
</Link>:
46+
</p>
47+
<ul>
48+
<li>
49+
<strong>Code context</strong> is highlighted <span className="highlighted-code">in this
50+
style</span> above.
51+
</li>
52+
<li>
53+
<strong>Variables context</strong> is indicated by variable declarations
54+
highlighted <span className="highlighted-variable-declaration">in this
55+
style</span> above.
56+
</li>
57+
</ul>
58+
</Admonition>;
59+
}
60+
61+
function BasicAdmonition(props: {}): JSX.Element {
62+
return <Admonition type="tip">
63+
Select an instruction offset to see associated <strong>
64+
ethdebug/format
65+
</strong> debugging information.
66+
</Admonition>;
67+
}

packages/web/src/theme/ProgramExample/SourceContents.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
background-color: var(--ifm-color-primary-lightest);
55
}
66

7+
.highlighted-ambiguous-code {
8+
font-weight: bold;
9+
background-color: var(--ifm-color-warning-lightest);
10+
}
11+
712
.highlighted-variable-declaration {
813
text-decoration: underline;
914
text-decoration-style: wavy;

packages/web/src/theme/ProgramExample/SourceContents.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ export function SourceContents(
3131

3232
const simpleDecorations = Program.Context.isCode(context)
3333
? decorateCodeContext(context, source)
34-
: [];
34+
: Program.Context.isPick(context)
35+
? decoratePickContext(context, source)
36+
: [];
3537

3638
const detailedDecorations = [
3739
...simpleDecorations,
@@ -56,7 +58,8 @@ export function SourceContents(
5658

5759
function decorateCodeContext(
5860
{ code }: Program.Context.Code,
59-
source: Materials.Source
61+
source: Materials.Source,
62+
className: string = "highlighted-code"
6063
): Shiki.DecorationItem[] {
6164
const { offset, length } = normalizeRange(code.range, source);
6265

@@ -65,12 +68,28 @@ function decorateCodeContext(
6568
start: offset,
6669
end: offset + length,
6770
properties: {
68-
class: "highlighted-code"
71+
class: className
6972
}
7073
}
7174
];
7275
}
7376

77+
function decoratePickContext(
78+
{ pick }: Program.Context.Pick,
79+
source: Materials.Source
80+
): Shiki.DecorationItem[] {
81+
// HACK this only supports picking from a choice of several different code
82+
// contexts
83+
if (!pick.every(Program.Context.isCode)) {
84+
return [];
85+
}
86+
87+
return pick.flatMap(
88+
(choice) => decorateCodeContext(choice, source, "highlighted-ambiguous-code")
89+
);
90+
}
91+
92+
7493
function decorateVariablesContext(
7594
{ variables }: Program.Context.Variables,
7695
source: Materials.Source
Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,16 @@
11
import Admonition from "@theme/Admonition";
22
import Link from "@docusaurus/Link";
3-
import { useProgramExampleContext } from "./ProgramExampleContext";
43
import { SourceContents } from "./SourceContents";
54
import { Opcodes } from "./Opcodes";
6-
import { HighlightedInstruction } from "./HighlightedInstruction";
5+
import { Details } from "./Details";
76
import { Variables } from "./Variables";
87

98
import "./Viewer.css";
10-
import "./SourceContents.css";
119

1210
export interface Props {
1311
}
1412

1513
export function Viewer(props: Props): JSX.Element {
16-
const { highlightedInstruction, highlightMode } = useProgramExampleContext();
17-
18-
const basicAdmonition = <Admonition type="tip">
19-
Select an instruction offset to see associated <strong>
20-
ethdebug/format
21-
</strong> debugging information.
22-
</Admonition>;
23-
24-
const detailedAdmonition = <Admonition type="info">
25-
<p>
26-
The selected instruction provides the following <Link to="/spec/program/context">
27-
<strong>ethdebug/format</strong> Program contexts
28-
</Link>:
29-
</p>
30-
<ul>
31-
<li>
32-
<strong>Code context</strong> is highlighted <span className="highlighted-code">in this
33-
style</span> above.
34-
</li>
35-
<li>
36-
<strong>Variables context</strong> is indicated by variable declarations
37-
highlighted <span className="highlighted-variable-declaration">in this
38-
style</span> above.
39-
</li>
40-
</ul>
41-
</Admonition>;
42-
43-
const details = highlightedInstruction && highlightMode === "detailed"
44-
? <>
45-
<h3>Details</h3>
46-
{detailedAdmonition}
47-
<details>
48-
<summary>See full <strong>ethdebug/format/program/instruction</strong> object</summary>
49-
<HighlightedInstruction />
50-
</details>
51-
</>
52-
: <>
53-
<h3>Details</h3>
54-
{basicAdmonition}
55-
</>;
56-
57-
5814
return <>
5915
<h2>Interactive example</h2>
6016
<div className="viewer-row">
@@ -67,6 +23,6 @@ export function Viewer(props: Props): JSX.Element {
6723
<Opcodes />
6824
</div>
6925
</div>
70-
{details}
26+
<Details />
7127
</>;
7228
}

0 commit comments

Comments
 (0)