Skip to content
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

Add edit/run for kata examples - issue 591 #1829

Merged
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
18 changes: 10 additions & 8 deletions playground/src/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { Exercise, getExerciseSources } from "qsharp-lang/katas-md";
import { codeToCompressedBase64, lsRangeToMonacoRange } from "./utils.js";
import { ActiveTab } from "./main.js";

import type { KataSection } from "qsharp-lang/katas";

type ErrCollection = {
checkDiags: VSDiagnostic[];
shotDiags: VSDiagnostic[];
Expand Down Expand Up @@ -80,7 +82,7 @@ export function Editor(props: {
compilerState: CompilerState;
defaultShots: number;
evtTarget: QscEventTarget;
kataExercise?: Exercise;
kataSection?: KataSection;
onRestartCompiler: () => void;
shotError?: VSDiagnostic;
showExpr: boolean;
Expand Down Expand Up @@ -197,9 +199,9 @@ export function Editor(props: {
} as ProgramConfig;

try {
if (props.kataExercise) {
if (props.kataSection?.type === "exercise") {
// This is for a kata exercise. Provide the sources that implement the solution verification.
const sources = await getExerciseSources(props.kataExercise);
const sources = await getExerciseSources(props.kataSection as Exercise);
// check uses the unrestricted profile and doesn't do code gen,
// so we just pass the sources
await props.compiler.checkExerciseSolution(
Expand Down Expand Up @@ -243,12 +245,12 @@ export function Editor(props: {
editor.current = newEditor;
const srcModel =
monaco.editor.getModel(
monaco.Uri.parse(props.kataExercise?.id ?? "main.qs"),
monaco.Uri.parse(props.kataSection?.id ?? "main.qs"),
) ??
monaco.editor.createModel(
"",
"qsharp",
monaco.Uri.parse(props.kataExercise?.id ?? "main.qs"),
monaco.Uri.parse(props.kataSection?.id ?? "main.qs"),
);
srcModel.setValue(props.code);
newEditor.setModel(srcModel);
Expand Down Expand Up @@ -293,8 +295,8 @@ export function Editor(props: {
useEffect(() => {
props.languageService.updateConfiguration({
targetProfile: profile,
packageType: props.kataExercise ? "lib" : "exe",
lints: props.kataExercise
packageType: props.kataSection ? "lib" : "exe",
lints: props.kataSection
? []
: [{ lint: "needlessOperation", level: "warn" }],
});
Expand All @@ -314,7 +316,7 @@ export function Editor(props: {
log.info("Removing diagnostics listener");
props.languageService.removeEventListener("diagnostics", onDiagnostics);
};
}, [props.languageService, props.kataExercise]);
}, [props.languageService, props.kataSection]);

useEffect(() => {
const theEditor = editor.current;
Expand Down
42 changes: 36 additions & 6 deletions playground/src/kata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function LessonElem(props: Props & { section: KataSection }) {
if (props.section.type !== "lesson") throw "Invalid section type";
const lesson = props.section;

const [shotError, setShotError] = useState<VSDiagnostic>();
const [evtHandler] = useState(() => new QscEventTarget(true));

return (
<div>
<div class="section-title">
Expand All @@ -94,9 +97,38 @@ function LessonElem(props: Props & { section: KataSection }) {
switch (item.type) {
case "example":
return (
<pre>
<code>{item.code}</code>
</pre>
<>
<Editor
code={item.code}
compiler={props.compiler}
compiler_worker_factory={props.compiler_worker_factory}
compilerState={props.compilerState}
onRestartCompiler={props.onRestartCompiler}
kataSection={lesson}
evtTarget={evtHandler}
defaultShots={1}
showShots={false}
showExpr={false}
shotError={shotError}
profile={getProfile()}
setAst={() => ({})}
setHir={() => ({})}
setQir={() => ({})}
activeTab="results-tab"
languageService={props.languageService}
></Editor>
<OutputTabs
evtTarget={evtHandler}
showPanel={false}
kataMode={true}
onShotError={(diag?: VSDiagnostic) => setShotError(diag)}
ast=""
hir=""
qir=""
activeTab="results-tab"
setActiveTab={() => undefined}
></OutputTabs>
</>
);
case "text-content":
return <Markdown markdown={item.content}></Markdown>;
Expand Down Expand Up @@ -140,8 +172,7 @@ function ExerciseElem(props: Props & { section: KataSection }) {
compiler_worker_factory={props.compiler_worker_factory}
onRestartCompiler={props.onRestartCompiler}
code={exercise.placeholderCode}
kataExercise={exercise}
key={exercise.id}
kataSection={exercise}
profile={getProfile()}
setAst={() => ({})}
setHir={() => ({})}
Expand All @@ -150,7 +181,6 @@ function ExerciseElem(props: Props & { section: KataSection }) {
languageService={props.languageService}
></Editor>
<OutputTabs
key={exercise.id + "-results"}
evtTarget={evtHandler}
showPanel={false}
kataMode={true}
Expand Down
Loading