-
-
Notifications
You must be signed in to change notification settings - Fork 94
Visualize modifiers on the docs page #3328
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a53f6bc
Started working on visualizing modifiers
AndreasArvidsson 35ef069
Recorded more visualizations
AndreasArvidsson f74c3a8
Handle cursors on empty lines
AndreasArvidsson da21e32
Small clean up
AndreasArvidsson 586bb8a
Allow multiple digits
AndreasArvidsson 9a4b826
Clean up path field
AndreasArvidsson 285a282
Additional inference tests
AndreasArvidsson bcf7b07
Updated changelog
AndreasArvidsson da3036a
Updated changelog
AndreasArvidsson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
206 changes: 206 additions & 0 deletions
206
packages/app-web-docs/src/docs/components/RecordedTestVisualizer.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| import { usePluginData } from "@docusaurus/useGlobalData"; | ||
| import type { Dispatch, JSX, ReactNode, SetStateAction } from "react"; | ||
| import { createContext, useContext, useMemo, useState } from "react"; | ||
| import type { DecorationItem } from "shiki"; | ||
| import type { | ||
| SelectionPlainObject, | ||
| TestCaseSnapshot, | ||
| } from "@cursorless/lib-common"; | ||
| import { BorderStyle, plainObjectToSelection } from "@cursorless/lib-common"; | ||
| import { Code } from "./Code"; | ||
| import { highlightColors } from "./highlightColors"; | ||
| import { highlightToDecoration } from "./highlightsToDecorations"; | ||
| import type { RecordedTest } from "./types"; | ||
|
|
||
| interface RecordedTestVisualizerContextValue { | ||
| fixtures: ReadonlyMap<string, RecordedTest>; | ||
| renderWhitespace: boolean; | ||
| setRenderWhitespace: Dispatch<SetStateAction<boolean>>; | ||
| } | ||
|
|
||
| const RecordedTestVisualizerContext = createContext< | ||
| RecordedTestVisualizerContextValue | undefined | ||
| >(undefined); | ||
|
|
||
| export function RecordedTestVisualizerProvider({ | ||
| children, | ||
| }: { | ||
| children: ReactNode; | ||
| }) { | ||
| const recordedTests = usePluginData( | ||
| "recorded-tests-plugin", | ||
| ) as RecordedTest[]; | ||
| const [renderWhitespace, setRenderWhitespace] = useState(true); | ||
| const fixtures = useMemo( | ||
| () => | ||
| new Map( | ||
| recordedTests.map((recordedTest) => [recordedTest.name, recordedTest]), | ||
| ), | ||
| [recordedTests], | ||
| ); | ||
| const value = useMemo( | ||
| () => ({ | ||
| fixtures, | ||
| renderWhitespace, | ||
| setRenderWhitespace, | ||
| }), | ||
| [fixtures, renderWhitespace], | ||
| ); | ||
|
|
||
| return ( | ||
| <RecordedTestVisualizerContext.Provider value={value}> | ||
| {children} | ||
| </RecordedTestVisualizerContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function RecordedTestVisualizerOptions() { | ||
| const { renderWhitespace, setRenderWhitespace } = useRecordedTestVisualizer(); | ||
|
|
||
| return ( | ||
| <div className="mb-4"> | ||
| <label className="ms-2"> | ||
| <input | ||
| type="checkbox" | ||
| className="me-1" | ||
| checked={renderWhitespace} | ||
| onChange={(event) => setRenderWhitespace(event.currentTarget.checked)} | ||
| /> | ||
| Render whitespace | ||
| </label> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| interface ModifierProps { | ||
| fixtureName: string; | ||
| } | ||
|
|
||
| export function RecordedTestVisualizer({ fixtureName }: ModifierProps) { | ||
| const { fixtures, renderWhitespace } = useRecordedTestVisualizer(); | ||
| const test = fixtures.get(fixtureName); | ||
|
|
||
| if (test == null) { | ||
| throw new Error(`Unknown recorded test fixture: ${fixtureName}`); | ||
| } | ||
|
|
||
| const { fixture, path } = test; | ||
| const { languageId, initialState, finalState } = fixture; | ||
|
|
||
| if (finalState == null) { | ||
| throw new Error(`Fixture ${fixtureName} does not have a final state`); | ||
| } | ||
|
|
||
| // oxlint-disable-next-line react_perf/jsx-no-new-object-as-prop | ||
| const link = { | ||
| name: "GitHub", | ||
| url: `https://github.com/cursorless-dev/cursorless/blob/main/resources/fixtures/recorded/docs/${path}`, | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="row"> | ||
| <div className="col"> | ||
| Input | ||
| <CodeState | ||
| renderWhitespace={renderWhitespace} | ||
| languageId={languageId} | ||
| link={link} | ||
| state={initialState} | ||
| /> | ||
| </div> | ||
| <div className="col"> | ||
| Output | ||
| <CodeState | ||
| renderWhitespace={renderWhitespace} | ||
| languageId={languageId} | ||
| link={link} | ||
| state={finalState} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| function CodeState({ | ||
| renderWhitespace, | ||
| languageId, | ||
| link, | ||
| state, | ||
| }: { | ||
| renderWhitespace: boolean; | ||
| languageId: string; | ||
| link: { | ||
| name: string; | ||
| url: string; | ||
| }; | ||
| state: TestCaseSnapshot; | ||
| }): JSX.Element { | ||
| return ( | ||
| <Code | ||
| link={link} | ||
| languageId={languageId} | ||
| renderWhitespace={renderWhitespace} | ||
| // oxlint-disable-next-line react-perf/jsx-no-new-array-as-prop | ||
| decorations={state.selections.map(toDecoration)} | ||
| > | ||
| {state.documentContents} | ||
| </Code> | ||
| ); | ||
| } | ||
|
|
||
| function toDecoration(plainSelection: SelectionPlainObject): DecorationItem { | ||
| const selection = plainObjectToSelection(plainSelection); | ||
|
|
||
| if (selection.isEmpty) { | ||
| return { | ||
| start: selection.start, | ||
| end: selection.start, | ||
| properties: { | ||
| className: ["code-cursor-before"], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const decoration = highlightToDecoration({ | ||
| range: selection, | ||
| style: { | ||
| backgroundColor: highlightColors.content.background, | ||
| borderColorSolid: highlightColors.content.borderSolid, | ||
| borderColorPorous: highlightColors.content.borderPorous, | ||
| borderStyle: { | ||
| top: BorderStyle.solid, | ||
| bottom: BorderStyle.solid, | ||
| left: BorderStyle.solid, | ||
| right: BorderStyle.solid, | ||
| }, | ||
| borderRadius: { | ||
| topLeft: true, | ||
| topRight: true, | ||
| bottomRight: true, | ||
| bottomLeft: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const className = selection.isReversed | ||
| ? "code-cursor-before" | ||
| : "code-cursor-after"; | ||
|
|
||
| return { | ||
| ...decoration, | ||
| properties: { | ||
| ...decoration.properties, | ||
| className: [className], | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function useRecordedTestVisualizer(): RecordedTestVisualizerContextValue { | ||
| const value = useContext(RecordedTestVisualizerContext); | ||
| if (value == null) { | ||
| throw new Error( | ||
| "Recorded test visualizer components must be used within RecordedTestVisualizerProvider", | ||
| ); | ||
| } | ||
| return value; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.