Skip to content

Commit d9ff419

Browse files
render whitespace
1 parent a04835b commit d9ff419

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/cursorless-org-docs/src/docs/user/languages/components/Code.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,33 @@ export interface Highlight {
1414

1515
interface Props {
1616
languageId: string;
17+
renderWhitespace?: boolean;
1718
highlights?: Highlight[];
1819
children: string;
1920
}
2021

2122
const myTheme = createCssVariablesTheme();
2223

23-
export function Code({ languageId, highlights, children }: Props) {
24+
export function Code({
25+
languageId,
26+
renderWhitespace,
27+
highlights,
28+
children,
29+
}: Props) {
2430
const [html, setHtml] = React.useState("");
2531
const [copied, setCopied] = useState(false);
2632

2733
useEffect(() => {
34+
if (renderWhitespace) {
35+
children = children.replaceAll(" ", "·");
36+
}
37+
2838
codeToHtml(children, {
2939
lang: languageId,
3040
theme: "nord",
3141
decorations: getDecorations(highlights),
3242
}).then(setHtml);
33-
}, [languageId, highlights, children]);
43+
}, [languageId, renderWhitespace, highlights, children]);
3444

3545
const handleCopy = async () => {
3646
try {

packages/cursorless-org-docs/src/docs/user/languages/components/ScopeVisualizer.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function ScopeVisualizer({ languageId }: Props) {
3434
scopeTests.filter((s) => s.languageId === languageId),
3535
);
3636
const [rangeType, setRangeType] = useState<RangeType>("content");
37+
const [renderWhitespace, setRenderWhitespace] = useState(false);
3738

3839
return (
3940
<div>
@@ -45,12 +46,25 @@ export function ScopeVisualizer({ languageId }: Props) {
4546
<option value="removal">Removal</option>
4647
</select>
4748

48-
{fixtures.map((f) => renderFixture(f, rangeType))}
49+
<label>
50+
<input
51+
type="checkbox"
52+
checked={renderWhitespace}
53+
onChange={(e) => setRenderWhitespace(e.target.checked)}
54+
/>
55+
Render whitespace
56+
</label>
57+
58+
{fixtures.map((f) => renderFixture(f, rangeType, renderWhitespace))}
4959
</div>
5060
);
5161
}
5262

53-
function renderFixture(fixture: Fixture, rangeType: RangeType) {
63+
function renderFixture(
64+
fixture: Fixture,
65+
rangeType: RangeType,
66+
renderWhitespace: boolean,
67+
) {
5468
const highlights: Highlight[] = [];
5569

5670
let previousRange: Range | undefined;
@@ -87,7 +101,11 @@ function renderFixture(fixture: Fixture, rangeType: RangeType) {
87101
return (
88102
<div key={fixture.name}>
89103
{fixture.facet}
90-
<Code languageId={fixture.languageId} highlights={highlights}>
104+
<Code
105+
languageId={fixture.languageId}
106+
renderWhitespace={renderWhitespace}
107+
highlights={highlights}
108+
>
91109
{fixture.code}
92110
</Code>
93111
</div>

0 commit comments

Comments
 (0)