Skip to content

Commit 85b7f80

Browse files
authored
[red-knot] Default playground to Python 3.13 (astral-sh#16952)
## Summary Default playground to Python 3.13 if there's no setting present. Fix errors when a setting was added / removed.
1 parent 3a97bdf commit 85b7f80

File tree

1 file changed

+64
-21
lines changed

1 file changed

+64
-21
lines changed

playground/knot/src/Editor/Chrome.tsx

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,24 @@ export default function Chrome() {
8484
setVersion(version);
8585
setWorkspace(workspace);
8686

87+
let hasSettings = false;
88+
8789
for (const [name, content] of Object.entries(fetchedWorkspace.files)) {
8890
let handle = null;
89-
if (name !== SETTINGS_FILE) {
91+
if (name === SETTINGS_FILE) {
92+
updateOptions(workspace, content, setUpdateError);
93+
hasSettings = true;
94+
} else {
9095
handle = workspace.openFile(name, content);
9196
}
9297

9398
dispatchFiles({ type: "add", handle, name, content });
9499
}
95100

101+
if (!hasSettings) {
102+
workspace.updateOptions(null);
103+
}
104+
96105
dispatchFiles({
97106
type: "selectFileByName",
98107
name: fetchedWorkspace.current,
@@ -119,25 +128,12 @@ export default function Chrome() {
119128
const handle = files.handles[files.selected];
120129

121130
if (handle != null) {
122-
try {
123-
workspace?.updateFile(handle, source);
124-
setUpdateError(null);
125-
} catch (error) {
126-
setUpdateError(`Failed to update file: ${formatError(error)}`);
127-
}
131+
updateFile(workspace, handle, source, setUpdateError);
128132
} else if (fileName === SETTINGS_FILE) {
129-
try {
130-
const settings = JSON.parse(source);
131-
workspace?.updateOptions(settings);
132-
setUpdateError(null);
133-
} catch (error) {
134-
setUpdateError(
135-
`Failed to update 'knot.json' options: ${formatError(error)}`,
136-
);
137-
}
133+
updateOptions(workspace, source, setUpdateError);
138134
}
139135
},
140-
[files.selected, workspace, files.handles, fileName],
136+
[files.selected, files.handles, fileName, workspace],
141137
);
142138

143139
const handleFileClicked = useCallback((file: FileId) => {
@@ -152,7 +148,9 @@ export default function Chrome() {
152148

153149
let handle = null;
154150

155-
if (name !== SETTINGS_FILE) {
151+
if (name === SETTINGS_FILE) {
152+
updateOptions(workspace, "{}", setUpdateError);
153+
} else {
156154
handle = workspace.openFile(name, "");
157155
}
158156

@@ -165,7 +163,9 @@ export default function Chrome() {
165163
(file: FileId) => {
166164
if (workspace != null) {
167165
const handle = files.handles[file];
168-
if (handle != null) {
166+
if (handle == null) {
167+
updateOptions(workspace, null, setUpdateError);
168+
} else {
169169
workspace.closeFile(handle);
170170
}
171171
}
@@ -183,11 +183,15 @@ export default function Chrome() {
183183

184184
const handle = files.handles[file];
185185
let newHandle: FileHandle | null = null;
186-
if (handle != null) {
186+
if (handle == null) {
187+
updateOptions(workspace, null, setUpdateError);
188+
} else {
187189
workspace.closeFile(handle);
188190
}
189191

190-
if (newName !== SETTINGS_FILE) {
192+
if (newName === SETTINGS_FILE) {
193+
updateOptions(workspace, files.contents[file], setUpdateError);
194+
} else {
191195
newHandle = workspace.openFile(newName, files.contents[file]);
192196
}
193197

@@ -267,6 +271,7 @@ export default function Chrome() {
267271
<PanelGroup id="vertical" direction="vertical">
268272
<Panel minSize={10} className="my-2" order={0}>
269273
<Editor
274+
key={fileName}
270275
theme={theme}
271276
visible={true}
272277
fileName={fileName ?? "lib.py"}
@@ -648,3 +653,41 @@ function formatError(error: unknown): string {
648653
? message.slice("Error: ".length)
649654
: message;
650655
}
656+
657+
function updateOptions(
658+
workspace: Workspace | null,
659+
content: string | null,
660+
setError: (error: string | null) => void,
661+
) {
662+
if (workspace == null) {
663+
return;
664+
}
665+
666+
content = content ?? DEFAULT_SETTINGS;
667+
668+
try {
669+
const settings = JSON.parse(content);
670+
workspace?.updateOptions(settings);
671+
setError(null);
672+
} catch (error) {
673+
setError(`Failed to update 'knot.json' options: ${formatError(error)}`);
674+
}
675+
}
676+
677+
function updateFile(
678+
workspace: Workspace | null,
679+
handle: FileHandle,
680+
content: string,
681+
setError: (error: string | null) => void,
682+
) {
683+
if (workspace == null) {
684+
return;
685+
}
686+
687+
try {
688+
workspace.updateFile(handle, content);
689+
setError(null);
690+
} catch (error) {
691+
setError(`Failed to update file: ${formatError(error)}`);
692+
}
693+
}

0 commit comments

Comments
 (0)