Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix: one source of truth for language adapter state (#1027)

Myles Scolnick206cf19b225f
diff --git a/frontend/src/components/editor/cell/code/cell-editor.tsx b/frontend/src/components/editor/cell/code/cell-editor.tsx
index 61d0a95807..1a9296b25d 100644
--- a/frontend/src/components/editor/cell/code/cell-editor.tsx
+++ b/frontend/src/components/editor/cell/code/cell-editor.tsx
@@ -32,6 +32,7 @@ import { useAtom, useSetAtom } from "jotai";
import { aiCompletionCellAtom } from "@/core/ai/state";
import { mergeRefs } from "@/utils/mergeRefs";
import { lastFocusedCellIdAtom } from "@/core/cells/focus";
+import { LanguageAdapter } from "@/core/codemirror/language/types";
export interface CellEditorProps
extends Pick<CellRuntimeState, "status">,
@@ -83,6 +84,8 @@ const CellEditorInternal = ({
}: CellEditorProps) => {
const [canUseMarkdown, setCanUseMarkdown] = useState(false);
const [aiCompletionCell, setAiCompletionCell] = useAtom(aiCompletionCellAtom);
+ const [languageAdapter, setLanguageAdapter] =
+ useState<LanguageAdapter["type"]>();
const setLastFocusedCellId = useSetAtom(lastFocusedCellIdAtom);
// DOM node where the editorView will be mounted
const editorViewParentRef = useRef<HTMLDivElement>(null);
@@ -169,18 +172,28 @@ const CellEditorInternal = ({
theme,
});
- // listen to code changes if we can use markdown
extensions.push(
- ViewPlugin.define(() => ({
- update(view) {
- const code = view.state.doc.toString();
- const languageAdapter = view.state.field(languageAdapterState);
- // If its not markdown, set if we can use markdown
- if (languageAdapter.type !== "markdown") {
- setCanUseMarkdown(LanguageAdapters.markdown().isSupported(code));
- }
- },
- })),
+ // Listen to code changes if we can use markdown
+ // Also update the language adapter
+ ViewPlugin.define((view) => {
+ // Init
+ const languageAdapter = view.state.field(languageAdapterState);
+ setLanguageAdapter(languageAdapter.type);
+
+ return {
+ update(view) {
+ const code = view.state.doc.toString();
+ const languageAdapter = view.state.field(languageAdapterState);
+ // If its not markdown, set if we can use markdown
+ if (languageAdapter.type !== "markdown") {
+ setCanUseMarkdown(LanguageAdapters.markdown().isSupported(code));
+ }
+
+ // Set the language adapter
+ setLanguageAdapter(languageAdapter.type);
+ },
+ };
+ }),
);
return extensions;
@@ -326,6 +339,7 @@ const CellEditorInternal = ({
<div className="absolute top-1 right-1">
<LanguageToggle
editorView={derefNotNull(editorViewRef)}
+ languageAdapter={languageAdapter}
canUseMarkdown={canUseMarkdown}
/>
</div>
diff --git a/frontend/src/components/editor/cell/code/language-toggle.tsx b/frontend/src/components/editor/cell/code/language-toggle.tsx
index 64efa40c07..0748bf5b8c 100644
--- a/frontend/src/components/editor/cell/code/language-toggle.tsx
+++ b/frontend/src/components/editor/cell/code/language-toggle.tsx
@@ -1,28 +1,24 @@
/* Copyright 2024 Marimo. All rights reserved. */
-import {
- languageAdapterState,
- switchLanguage,
-} from "@/core/codemirror/language/extension";
+import { switchLanguage } from "@/core/codemirror/language/extension";
import { EditorView } from "@codemirror/view";
-import React, { useState } from "react";
+import React from "react";
import { MarkdownIcon, PythonIcon } from "./icons";
import { Button } from "@/components/ui/button";
import { Tooltip } from "@/components/ui/tooltip";
import { startCase } from "lodash-es";
+import { LanguageAdapter } from "@/core/codemirror/language/types";
interface Props {
editorView: EditorView;
canUseMarkdown: boolean;
+ languageAdapter: LanguageAdapter["type"] | undefined;
}
export const LanguageToggle: React.FC<Props> = ({
editorView,
+ languageAdapter,
canUseMarkdown,
}) => {
- const [languageAdapter, setLanguageAdapter] = useState(
- editorView.state.field(languageAdapterState).type,
- );
-
if (!canUseMarkdown && languageAdapter !== "markdown") {
return null;
}
@@ -44,7 +40,6 @@ export const LanguageToggle: React.FC<Props> = ({
fill={"var(--sky-11)"}
fontSize={20}
onClick={() => {
- setLanguageAdapter(otherLanguage);
switchLanguage(editorView, otherLanguage);
}}
/>
diff --git a/frontend/src/core/codemirror/language/markdown.ts b/frontend/src/core/codemirror/language/markdown.ts
index e66973fb25..8bd970d431 100644
--- a/frontend/src/core/codemirror/language/markdown.ts
+++ b/frontend/src/core/codemirror/language/markdown.ts
@@ -90,6 +90,7 @@ export class MarkdownLanguageAdapter implements LanguageAdapter {
isSupported(pythonCode: string): boolean {
const markdownLines = pythonCode
+ .trim()
.split("\n")
.map((line) => line.startsWith("mo.md("))
.filter(Boolean);