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: Python highlighting inside Markdown LaTeX (#11281)

Akshay Agrawal242b7fc63657
diff --git a/frontend/src/core/codemirror/language/embedded/__tests__/embedded-python.test.ts b/frontend/src/core/codemirror/language/embedded/__tests__/embedded-python.test.ts
index e95ed73ccb..74ef44c2e6 100644
--- a/frontend/src/core/codemirror/language/embedded/__tests__/embedded-python.test.ts
+++ b/frontend/src/core/codemirror/language/embedded/__tests__/embedded-python.test.ts
@@ -2,13 +2,17 @@
import type { CompletionContext } from "@codemirror/autocomplete";
import { python } from "@codemirror/lang-python";
+import { StreamLanguage } from "@codemirror/language";
+import { stexMath } from "@codemirror/legacy-modes/mode/stex";
import { EditorState } from "@codemirror/state";
-import type { InlineContext } from "@lezer/markdown";
-import { beforeEach, describe, expect, it } from "vitest";
+import { classHighlighter, highlightTree } from "@lezer/highlight";
+import { parser as markdownParser } from "@lezer/markdown";
+import { beforeEach, describe, expect, it, vi } from "vitest";
import { cellId, variableName } from "@/__tests__/branded";
import { store } from "@/core/state/jotai";
import { variablesAtom } from "@/core/variables/state";
import { parsePython, variableCompletionSource } from "../embedded-python";
+import { parseLatex } from "../latex";
const IS_ACTIVE = () => true;
@@ -23,25 +27,133 @@ describe("parsePython", () => {
]);
});
- it("should not parse double curly braces", () => {
+ const cases = [
+ "{math.exp(3)}",
+ '${math.exp(3) + len("$}")} $',
+ "${math.exp(3)} + {math.sin(3)}$",
+ "${math.exp(3)}$",
+ "$$ {math.exp(3)} $$",
+ String.raw`\({math.exp(3)}\)`,
+ String.raw`\[{math.exp(3)}\]`,
+ String.raw`\\({math.exp(3)}\\)`,
+ String.raw`\\[{math.exp(3)}\\]`,
+ String.raw`$\frac{{{math.exp(3)}}}{{2}}$`,
+ String.raw`$\{math.exp(3)}$`,
+ '${ {"}": math.exp(3)} }$',
+ "${math.exp(3):{width}.{precision}f}$",
+ ];
+
+ function highlightedProperties(doc: string, prefix: string) {
+ const parser = markdownParser.configure([
+ parseLatex(StreamLanguage.define(stexMath).parser),
+ parsePython(python().language.parser, () => prefix.includes("f")),
+ ]);
+ const properties: string[] = [];
+ highlightTree(parser.parse(doc), classHighlighter, (from, to, classes) => {
+ if (classes.split(" ").includes("tok-propertyName")) {
+ properties.push(doc.slice(from, to));
+ }
+ });
+ return properties;
+ }
+
+ it.each(["f", "rf", "fr"])(
+ "highlights interpolation with %s strings",
+ (prefix) => {
+ for (const doc of cases) {
+ expect(highlightedProperties(doc, prefix)).toEqual(
+ doc.includes("math.sin") ? ["exp", "sin"] : ["exp"],
+ );
+ }
+ },
+ );
+
+ it.each(["", "r"])("does not interpolate %s strings", (prefix) => {
+ for (const doc of cases) {
+ expect(highlightedProperties(doc, prefix)).toEqual([]);
+ }
+ });
+
+ it("parses replacement boundaries once for a paragraph with many interpolations", () => {
const pythonParser = python().language.parser;
- const config = parsePython(pythonParser, IS_ACTIVE);
+ const parse = vi.spyOn(pythonParser, "parse");
+ try {
+ const doc = "{math.exp(3)} ".repeat(1000);
+ const parser = markdownParser.configure([
+ parseLatex(StreamLanguage.define(stexMath).parser),
+ parsePython(pythonParser, IS_ACTIVE),
+ ]);
+ const properties: string[] = [];
+ highlightTree(
+ parser.parse(doc),
+ classHighlighter,
+ (from, to, classes) => {
+ if (classes.split(" ").includes("tok-propertyName")) {
+ properties.push(doc.slice(from, to));
+ }
+ },
+ );
+ expect(properties).toEqual(Array.from({ length: 1000 }, () => "exp"));
+ expect(parse).toHaveBeenCalledTimes(1);
+ } finally {
+ parse.mockRestore();
+ }
+ });
- // Create a mock context
- const mockContext = {
- slice: (from: number, to: number) => {
- if (from === 5 && to === 6) {
- return "{";
- }
- return "";
- },
- addDelimiter: () => -1,
- } as unknown as InlineContext;
+ it("preserves raw backslashes between replacements", () => {
+ expect(
+ highlightedProperties(String.raw`{math.exp(3)} \N{math.sin(3)}`, "rf"),
+ ).toEqual(["exp", "sin"]);
+ });
+
+ it("finds replacements after triple quotes in markdown text", () => {
+ expect(
+ highlightedProperties('{math.exp(3)} """ {math.sin(3)}', "f"),
+ ).toEqual(["exp", "sin"]);
+ });
- // Test with double curly braces
- const result = config.parseInline![0].parse(mockContext, 123, 6); // 123 is OPEN_BRACE
- expect(result).toBe(-1);
+ it("keeps replacement positions local to each paragraph", () => {
+ expect(
+ highlightedProperties("${math.exp(3)}$\n\n${math.sin(3)}$", "rf"),
+ ).toEqual(["exp", "sin"]);
});
+
+ it("preserves LaTeX highlighting around Python replacements", () => {
+ const doc = String.raw`$\frac{{{math.exp(3)}}}{{2}} + \alpha$`;
+ const parser = markdownParser.configure([
+ parseLatex(StreamLanguage.define(stexMath).parser),
+ parsePython(python().language.parser, IS_ACTIVE),
+ ]);
+ const highlighted: string[] = [];
+ highlightTree(parser.parse(doc), classHighlighter, (from, to) => {
+ highlighted.push(doc.slice(from, to));
+ });
+ expect(highlighted).toEqual(
+ expect.arrayContaining(["\\frac", "exp", "\\alpha"]),
+ );
+ });
+
+ it.each(["${math.exp(3)$", "${math.exp(3)", "${{math.exp(3)}$"])(
+ "does not treat incomplete or escaped braces as replacements: %s",
+ (doc) => {
+ expect(highlightedProperties(doc, "f")).toEqual([]);
+ },
+ );
+
+ it.each(["f", "rf", "fr"])(
+ "keeps doubled braces literal in %s strings",
+ (prefix) => {
+ for (const doc of [
+ "{{math.exp(3)}}",
+ "${{math.exp(3)}}$",
+ "$ {{{{math.exp(3)}}}} $",
+ String.raw`$\{{math.exp(3)}}$`,
+ String.raw`$\frac{{1}}{{2}}$`,
+ ]) {
+ expect(highlightedProperties(doc, prefix)).toEqual([]);
+ }
+ },
+ );
});
describe("variableCompletionSource", () => {
@@ -72,10 +184,10 @@ describe("variableCompletionSource", () => {
const context: Partial<CompletionContext> = {
pos: 8,
explicit: false,
- matchBefore: (expr: RegExp) => ({ from: 7, to: 8, text: "v" }),
+ matchBefore: (_expr: RegExp) => ({ from: 7, to: 8, text: "v" }),
state,
aborted: false,
- tokenBefore: (types: readonly string[]) => null,
+ tokenBefore: (_types: readonly string[]) => null,
};
const result = variableCompletionSource(context as CompletionContext);
@@ -92,10 +204,10 @@ describe("variableCompletionSource", () => {
const context: Partial<CompletionContext> = {
pos: 7,
explicit: false,
- matchBefore: (expr: RegExp) => ({ from: 6, to: 7, text: "v" }),
+ matchBefore: (_expr: RegExp) => ({ from: 6, to: 7, text: "v" }),
state,
aborted: false,
- tokenBefore: (types: readonly string[]) => null,
+ tokenBefore: (_types: readonly string[]) => null,
};
const result = variableCompletionSource(context as CompletionContext);
@@ -110,10 +222,10 @@ describe("variableCompletionSource", () => {
const context: Partial<CompletionContext> = {
pos: 13,
explicit: false,
- matchBefore: (expr: RegExp) => ({ from: 12, to: 13, text: "v" }),
+ matchBefore: (_expr: RegExp) => ({ from: 12, to: 13, text: "v" }),
state,
aborted: false,
- tokenBefore: (types: readonly string[]) => null,
+ tokenBefore: (_types: readonly string[]) => null,
};
const result = variableCompletionSource(context as CompletionContext);
@@ -131,7 +243,7 @@ describe("variableCompletionSource", () => {
matchBefore: () => null,
state,
aborted: false,
- tokenBefore: (types: readonly string[]) => null,
+ tokenBefore: (_types: readonly string[]) => null,
};
const result = variableCompletionSource(context as CompletionContext);
@@ -146,10 +258,10 @@ describe("variableCompletionSource", () => {
const context: Partial<CompletionContext> = {
pos: 9,
explicit: false,
- matchBefore: (expr: RegExp) => ({ from: 8, to: 9, text: "v" }),
+ matchBefore: (_expr: RegExp) => ({ from: 8, to: 9, text: "v" }),
state,
aborted: false,
- tokenBefore: (types: readonly string[]) => null,
+ tokenBefore: (_types: readonly string[]) => null,
};
const result = variableCompletionSource(context as CompletionContext);
diff --git a/frontend/src/core/codemirror/language/embedded/embedded-python.ts b/frontend/src/core/codemirror/language/embedded/embedded-python.ts
index b417f64b0c..a58ef9f7f4 100644
--- a/frontend/src/core/codemirror/language/embedded/embedded-python.ts
+++ b/frontend/src/core/codemirror/language/embedded/embedded-python.ts
@@ -16,11 +16,8 @@ import { getVariableCompletions } from "../../completion/variable-completions";
const PYTHON = "Python";
const OPEN_BRACE = 123; // {
-const CLOSE_BRACE = 125; // }
const DELIMITER_LENGTH = 1;
-const MARK = { mark: `${PYTHON}Mark`, resolve: PYTHON };
-
/**
* Define a Python code block parser for Markdown.
*
@@ -31,6 +28,11 @@ export function parsePython(
pythonParser: Parser,
isActivated: () => boolean,
): MarkdownConfig {
+ const replacements = new WeakMap<
+ InlineContext,
+ { ends: Map<number, number>; parsedTo: number }
+ >();
+
const defineNodes = [
{ name: PYTHON, style: tags.emphasis },
{ name: `${PYTHON}Mark`, style: tags.processingInstruction },
@@ -41,26 +43,58 @@ export function parsePython(
parseInline: [
{
name: PYTHON,
+ before: "Escape",
parse(cx: InlineContext, next: number, pos: number): number {
if (!isActivated()) {
return -1;
}
- if (next !== OPEN_BRACE && next !== CLOSE_BRACE) {
+ // A backslash does not escape a brace in Python f-strings.
+ if (next === 92 && cx.char(pos + 1) === OPEN_BRACE) {
+ return pos + 1;
+ }
+ if (next !== OPEN_BRACE) {
return -1;
}
+ if (cx.char(pos + 1) === OPEN_BRACE) {
+ return pos + 2;
+ }
- // Check for double curly braces
- if (next === OPEN_BRACE && cx.slice(pos - 1, pos) === "{") {
+ let cached = replacements.get(cx);
+ if (!cached || pos >= cached.parsedTo) {
+ // Cache all replacements so each brace does not reparse the paragraph.
+ // Keep Markdown backslashes from consuming braces as Python escapes.
+ const prefix = 'rf"""';
+ const tree = pythonParser.parse(
+ `${prefix}${cx.slice(pos, cx.end)}\n"""`,
+ );
+ const formatString = tree.topNode
+ .getChild("ExpressionStatement")
+ ?.getChild("FormatString");
+ const offset = pos - prefix.length;
+ const ends = new Map<number, number>();
+ for (const replacement of formatString?.getChildren(
+ "FormatReplacement",
+ ) ?? []) {
+ if (replacement.lastChild?.name === "}") {
+ ends.set(offset + replacement.from, offset + replacement.to);
+ }
+ }
+ cached = {
+ ends,
+ parsedTo: formatString ? offset + formatString.to : cx.end,
+ };
+ replacements.set(cx, cached);
+ }
+ const end = cached.ends.get(pos);
+ if (end === undefined) {
return -1;
}
-
- return cx.addDelimiter(
- MARK,
- pos,
- pos + DELIMITER_LENGTH,
- next === OPEN_BRACE,
- next === CLOSE_BRACE,
+ return cx.addElement(
+ cx.elt(PYTHON, pos, end, [
+ cx.elt(`${PYTHON}Mark`, pos, pos + 1),
+ cx.elt(`${PYTHON}Mark`, end - 1, end),
+ ]),
);
},
},
diff --git a/frontend/src/core/codemirror/language/embedded/latex.ts b/frontend/src/core/codemirror/language/embedded/latex.ts
index 427bd42542..ca64abaa8e 100644
--- a/frontend/src/core/codemirror/language/embedded/latex.ts
+++ b/frontend/src/core/codemirror/language/embedded/latex.ts
@@ -29,7 +29,7 @@
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-import { type Parser, parseMixed } from "@lezer/common";
+import { IterMode, type Parser, parseMixed } from "@lezer/common";
import { tags } from "@lezer/highlight";
import type {
DelimiterType,
@@ -231,24 +231,31 @@ export function parseLatex(latexParser: Parser): MarkdownConfig {
// Test if the node type is one of the math expression
const delimiterLength = DELIMITER_LENGTH[node.type.name];
if (delimiterLength) {
- const from = node.from + delimiterLength;
- const to = node.to - delimiterLength;
+ const from = node.node.firstChild?.to ?? node.from + delimiterLength;
+ const to = node.node.lastChild?.from ?? node.to - delimiterLength;
// Ensure valid ranges
if (from >= to || from < 0 || to < 0) {
return null;
}
- return {
- parser: latexParser,
- // Remove delimiter from LaTeX parser otherwise it won't be highlighted
- overlay: [
- {
- from,
- to,
- },
- ],
- };
+ // Leave Python replacements to their own parser and highlighter.
+ const overlay: { from: number; to: number }[] = [];
+ let start = from;
+ node.node.cursor(IterMode.IgnoreMounts).iterate((child) => {
+ if (child.name !== "Python") {
+ return;
+ }
+ if (start < child.from) {
+ overlay.push({ from: start, to: child.from });
+ }
+ start = child.to;
+ return false;
+ });
+ if (start < to) {
+ overlay.push({ from: start, to });
+ }
+ return overlay.length ? { parser: latexParser, overlay } : null;
}
return null;