This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
website(docs): More playground IDE features (#3711)
- Loading branch information
Sebastian
authored
Nov 14, 2022
1 parent
69aab49
commit 240a70e
Showing
34 changed files
with
2,448 additions
and
1,264 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 |
---|---|---|
@@ -1,15 +1,112 @@ | ||
import type { Diagnostic as RomeDiagnostic } from "@rometools/wasm-web"; | ||
import type { | ||
ReactCodeMirrorProps, | ||
ReactCodeMirrorRef, | ||
} from "@uiw/react-codemirror"; | ||
import type { Extension } from "@codemirror/state"; | ||
import type { Diagnostic as CodeMirrorDiagnostic } from "@codemirror/lint"; | ||
import { EditorView } from "@codemirror/view"; | ||
import RealCodeMirror from "@uiw/react-codemirror"; | ||
import { forwardRef } from "react"; | ||
import { forwardRef, useEffect, useMemo, useState } from "react"; | ||
import { useTheme } from "./utils"; | ||
import { lintGutter, setDiagnostics } from "@codemirror/lint"; | ||
|
||
export default forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>( | ||
function CodeMirror(props, ref) { | ||
const theme = useTheme(); | ||
interface Props extends ReactCodeMirrorProps { | ||
diagnostics?: RomeDiagnostic[]; | ||
} | ||
|
||
return <RealCodeMirror {...props} ref={ref} theme={theme} />; | ||
}, | ||
); | ||
function getDiagnosticMessage(diagnostic: RomeDiagnostic): string { | ||
let buf = ""; | ||
for (const elem of diagnostic.message) { | ||
buf += elem.content; | ||
} | ||
return buf; | ||
} | ||
|
||
function romeDiagnosticsToCodeMirror( | ||
rome: RomeDiagnostic[], | ||
): CodeMirrorDiagnostic[] { | ||
const codeMirror: CodeMirrorDiagnostic[] = []; | ||
|
||
for (const diag of rome) { | ||
const span = diag.location?.span; | ||
if (span === undefined) { | ||
continue; | ||
} | ||
|
||
let severity: CodeMirrorDiagnostic["severity"]; | ||
switch (diag.severity) { | ||
case "Error": | ||
case "Fatal": { | ||
severity = "error"; | ||
break; | ||
} | ||
|
||
case "Information": { | ||
severity = "info"; | ||
break; | ||
} | ||
|
||
case "Warning": { | ||
severity = "warning"; | ||
break; | ||
} | ||
|
||
default: { | ||
severity = "error"; | ||
} | ||
} | ||
|
||
codeMirror.push({ | ||
from: span[0], | ||
to: span[1], | ||
severity, | ||
message: getDiagnosticMessage(diag), | ||
}); | ||
} | ||
|
||
return codeMirror; | ||
} | ||
|
||
function getDefaultExtensions(extensions: Extension[] = []) { | ||
return [EditorView.lineWrapping, ...extensions]; | ||
} | ||
|
||
export default forwardRef<ReactCodeMirrorRef, Props>(function CodeMirror( | ||
{ diagnostics, ...props }, | ||
ref, | ||
) { | ||
const theme = useTheme(); | ||
|
||
let [editor, setEditor] = useState<EditorView>(); | ||
|
||
function onCreateEditor(editor: EditorView) { | ||
setEditor(editor); | ||
} | ||
|
||
const extensions = useMemo(() => { | ||
if (diagnostics === undefined) { | ||
return getDefaultExtensions(props.extensions); | ||
} | ||
|
||
return [lintGutter(), ...getDefaultExtensions(props.extensions)]; | ||
}, [diagnostics, props.extensions]); | ||
|
||
useEffect(() => { | ||
if (editor !== undefined && diagnostics !== undefined) { | ||
editor.dispatch( | ||
setDiagnostics(editor.state, romeDiagnosticsToCodeMirror(diagnostics)), | ||
); | ||
} | ||
}, [editor, diagnostics]); | ||
|
||
return ( | ||
<RealCodeMirror | ||
{...props} | ||
extensions={extensions} | ||
onCreateEditor={onCreateEditor} | ||
ref={ref} | ||
theme={theme} | ||
/> | ||
); | ||
}); |
This file contains 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.