дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 01:57:13 +02:00
Notebook cell editors used CodeMirror's default indent of 4 regardless of settings. Cells now take the Python (code cells) or Markdown Code Style, falling back to the editor tab size, through an indent compartment that is reconfigured in place when settings change, so the cell editor is not rebuilt. Co-authored-by: qslia <[email protected]>
224 рядки
8.9 KiB
TypeScript
224 рядки
8.9 KiB
TypeScript
import { useLayoutEffect, useRef } from "react";
|
|
import { Annotation, Compartment, EditorSelection, EditorState, Prec, StateEffect } from "@codemirror/state";
|
|
import {
|
|
EditorView,
|
|
drawSelection,
|
|
highlightActiveLine,
|
|
highlightSpecialChars,
|
|
keymap,
|
|
lineNumbers,
|
|
tooltips,
|
|
} from "@codemirror/view";
|
|
import { defaultKeymap, indentWithTab } from "@codemirror/commands";
|
|
import { bracketMatching, indentOnInput, syntaxHighlighting } from "@codemirror/language";
|
|
import type { NotebookCellDoc } from "@/bindings";
|
|
import {
|
|
_setFocusedCellEditor,
|
|
adjacentEditor,
|
|
cellEditorExtensions,
|
|
embeddedEditorExtensions,
|
|
externalHistory,
|
|
focusedCellEditor,
|
|
} from "@/editor/api";
|
|
import { openideaTheme } from "@/editor/core/cm/theme";
|
|
import { openideaHighlightStyle } from "@/editor/core/cm/highlight";
|
|
import { selectionMarks } from "@/editor/core/cm/selectionMarks";
|
|
import { seamlessSelection } from "@/editor/core/cm/selectionLayer";
|
|
import { multiCaretMouse } from "@/editor/core/cm/multiCaret";
|
|
import { findLanguageDescription, languageInfo, loadSupport } from "@/editor/core/cm/languages";
|
|
import { effectiveIndent, indentExtension, tooltipBounds } from "@/editor/core/cm/setup";
|
|
import { useProject } from "@/platform/project";
|
|
import type { EditorSettings } from "@/platform/ipc";
|
|
import { tx } from "@/platform/i18n";
|
|
import { setContext } from "@/platform/context";
|
|
import type { CellReveal, NotebookSession } from "./session";
|
|
import styles from "./Notebook.module.css";
|
|
|
|
const external = Annotation.define<boolean>();
|
|
|
|
/** Completion, hover, diagnostics and navigation for a code cell its language servers know. */
|
|
function languageServices(type: string, doc: NotebookCellDoc | undefined) {
|
|
if (type !== "code" || !doc) return [];
|
|
return cellEditorExtensions({ doc: doc.doc, path: doc.uri, language: "python", uiMode: "Python", readOnly: false });
|
|
}
|
|
|
|
/** A cell's indentation: the Python (code) or Markdown Code Style, else the editor's tab size. */
|
|
function cellIndent(settings: EditorSettings | undefined, type: string) {
|
|
return indentExtension(effectiveIndent(settings, languageInfo(type === "code" ? "python" : "markdown")));
|
|
}
|
|
|
|
/** Created once: every `EditorView.theme` call mounts new CSS rules into the document. */
|
|
const CELL_EDITOR_THEME = EditorView.theme({
|
|
"&.cm-editor": { height: "auto" },
|
|
// `auto`: past either end of a long cell the wheel scrolls the notebook.
|
|
"&.cm-editor .cm-scroller": { maxHeight: "640px", overflow: "auto", overscrollBehavior: "auto" },
|
|
});
|
|
|
|
export function CellEditor({
|
|
session,
|
|
id,
|
|
source,
|
|
type,
|
|
frozen,
|
|
doc,
|
|
reveal,
|
|
}: {
|
|
session: NotebookSession;
|
|
id: string;
|
|
source: string;
|
|
type: string;
|
|
frozen: boolean;
|
|
doc?: NotebookCellDoc;
|
|
/** A location to show in this cell. */
|
|
reveal?: CellReveal | null;
|
|
}) {
|
|
const host = useRef<HTMLDivElement>(null);
|
|
const view = useRef<EditorView | null>(null);
|
|
const editorSettings = useProject((s) => s.settings?.editor);
|
|
const indent = useRef(new Compartment());
|
|
const readonly = useRef(new Compartment());
|
|
const services = useRef(new Compartment());
|
|
const initialSource = useRef(source);
|
|
const initialDoc = useRef(doc);
|
|
useLayoutEffect(() => {
|
|
if (!host.current) return;
|
|
const language = new Compartment();
|
|
const configuration = [
|
|
// Completion and hover popups leave the cell, whose border clips its content.
|
|
tooltips({ parent: document.body, tooltipSpace: tooltipBounds }),
|
|
openideaTheme,
|
|
syntaxHighlighting(openideaHighlightStyle),
|
|
lineNumbers(),
|
|
drawSelection(),
|
|
selectionMarks,
|
|
seamlessSelection(),
|
|
multiCaretMouse,
|
|
highlightActiveLine(),
|
|
highlightSpecialChars(),
|
|
bracketMatching(),
|
|
indentOnInput(),
|
|
indent.current.of(cellIndent(useProject.getState().settings?.editor, type)),
|
|
Prec.highest(
|
|
keymap.of([
|
|
{
|
|
key: "Mod-z",
|
|
run: () => {
|
|
void session.undo().catch((e) => session.report(e));
|
|
return true;
|
|
},
|
|
},
|
|
{
|
|
key: "Mod-Shift-z",
|
|
run: () => {
|
|
void session.redo().catch((e) => session.report(e));
|
|
return true;
|
|
},
|
|
},
|
|
{
|
|
key: "Mod-y",
|
|
run: () => {
|
|
void session.redo().catch((e) => session.report(e));
|
|
return true;
|
|
},
|
|
},
|
|
]),
|
|
),
|
|
keymap.of([indentWithTab, ...defaultKeymap]),
|
|
externalHistory.of({
|
|
undo: () => void session.undo().catch((e) => session.report(e)),
|
|
redo: () => void session.redo().catch((e) => session.report(e)),
|
|
}),
|
|
embeddedEditorExtensions(session.path),
|
|
language.of([]),
|
|
services.current.of(languageServices(type, initialDoc.current)),
|
|
readonly.current.of(EditorState.readOnly.of(session.getState().frozen)),
|
|
EditorView.lineWrapping,
|
|
CELL_EDITOR_THEME,
|
|
adjacentEditor.of((direction, col) => session.revealAdjacent(id, direction, col)),
|
|
EditorView.contentAttributes.of({
|
|
"aria-label": tx(type === "code" ? "Python cell source" : "Markdown cell source"),
|
|
}),
|
|
EditorState.transactionFilter.of((transaction) =>
|
|
transaction.docChanged && session.getState().frozen && !transaction.annotation(external) ? [] : transaction,
|
|
),
|
|
EditorView.updateListener.of((update) => {
|
|
session.editors.set(id, update.state);
|
|
if (update.focusChanged) {
|
|
// The session puts the caret back here after a backend undo/redo (session.ts history).
|
|
session.focusedCell = update.view.hasFocus ? id : null;
|
|
// While the cell editor has focus the notebook publishes the editor context keys a
|
|
// text tab does, and the cell's view is what editor actions resolve (@/editor/api,
|
|
// re-read by editor/core's publishContext and the action resolvers).
|
|
_setFocusedCellEditor(update.view.hasFocus ? update.view : null);
|
|
setContext("textEditorActive", update.view.hasFocus);
|
|
setContext("editorTextFocus", update.view.hasFocus);
|
|
}
|
|
if (update.docChanged && update.transactions.some((tr) => !tr.annotation(external))) {
|
|
session.edit({ type: "setSource", cell: id, source: update.state.doc.toString() });
|
|
}
|
|
}),
|
|
];
|
|
// Reconfigure saved state to replace stale view closures; retain selection.
|
|
const saved = session.editors.get(id);
|
|
const state = saved
|
|
? saved.update({ effects: StateEffect.reconfigure.of(configuration) }).state
|
|
: EditorState.create({ doc: initialSource.current, extensions: configuration });
|
|
const editor = new EditorView({ state, parent: host.current });
|
|
view.current = editor;
|
|
let alive = true;
|
|
const description = findLanguageDescription(
|
|
type === "code" ? "Python" : "Markdown",
|
|
type === "code" ? "cell.py" : "cell.md",
|
|
);
|
|
if (description)
|
|
void loadSupport(description).then((support) => {
|
|
if (alive && support) editor.dispatch({ effects: language.reconfigure(support) });
|
|
});
|
|
return () => {
|
|
alive = false;
|
|
session.editors.set(id, editor.state);
|
|
if (session.focusedCell === id) session.focusedCell = null;
|
|
if (focusedCellEditor() === editor) _setFocusedCellEditor(null);
|
|
view.current = null;
|
|
editor.destroy();
|
|
};
|
|
}, [session, id, type]);
|
|
useLayoutEffect(() => {
|
|
view.current?.dispatch({ effects: indent.current.reconfigure(cellIndent(editorSettings, type)) });
|
|
}, [editorSettings, type]);
|
|
useLayoutEffect(() => {
|
|
const editor = view.current;
|
|
if (!editor) return;
|
|
if (editor.state.doc.toString() !== source)
|
|
editor.dispatch({
|
|
changes: { from: 0, to: editor.state.doc.length, insert: source },
|
|
annotations: external.of(true),
|
|
});
|
|
editor.dispatch({ effects: readonly.current.reconfigure(EditorState.readOnly.of(frozen)) });
|
|
}, [source, frozen]);
|
|
const docId = doc?.doc;
|
|
const docUri = doc?.uri;
|
|
useLayoutEffect(() => {
|
|
const editor = view.current;
|
|
if (!editor) return;
|
|
initialDoc.current = doc;
|
|
editor.dispatch({ effects: services.current.reconfigure(languageServices(type, doc)) });
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- a refetch gives the same document a new object
|
|
}, [docId, docUri, type]);
|
|
useLayoutEffect(() => {
|
|
const editor = view.current;
|
|
if (!editor || !reveal || reveal.cell !== id || session.revealed >= reveal.seq) return;
|
|
session.revealed = reveal.seq;
|
|
if (reveal.selection) {
|
|
const text = editor.state.doc;
|
|
const line = text.line(Math.min(reveal.selection.line + 1, text.lines));
|
|
const at = line.from + Math.min(reveal.selection.col, line.length);
|
|
editor.dispatch({ selection: EditorSelection.cursor(at), scrollIntoView: true });
|
|
}
|
|
editor.focus();
|
|
}, [reveal, id, session]);
|
|
return <div ref={host} className={styles.cellEditor} />;
|
|
}
|
|
|
|
// No CodeMirror history extension: source and structural undo share backend history.
|