Skip to content

Commit

Permalink
fix: dont auto-run markdown on mount, dont auto-scroll unfocused cells (
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick authored Sep 6, 2024
1 parent 7df87d6 commit bcab319
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
26 changes: 19 additions & 7 deletions frontend/src/core/codemirror/cells/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,12 @@ export function cellCodeEditingBundle(
const { updateCellCode } = callbacks;

const onChangePlugin = EditorView.updateListener.of((update) => {
// Check if the doc update was a formatting change
// e.g. changing from python to markdown
const isFormattingChange = update.transactions.some((tr) =>
tr.effects.some((effect) => effect.is(formattingChangeEffect)),
);
if (update.docChanged) {
// Check if the doc update was a formatting change
// e.g. changing from python to markdown
const isFormattingChange = update.transactions.some((tr) =>
tr.effects.some((effect) => effect.is(formattingChangeEffect)),
);
const nextCode = getEditorCodeAsPython(update.view);
updateCellCode({
cellId,
Expand All @@ -318,8 +318,20 @@ export function markdownAutoRunExtension(
callbacks: MovementCallbacks,
): Extension {
return EditorView.updateListener.of((update) => {
if (update.docChanged) {
callbacks.onRun();
// If the doc didn't change, ignore
if (!update.docChanged) {
return;
}

// This happens on mount when we start in markdown mode
const isFormattingChange = update.transactions.some((tr) =>
tr.effects.some((effect) => effect.is(formattingChangeEffect)),
);
if (isFormattingChange) {
// Ignore formatting changes
return;
}

callbacks.onRun();
});
}
4 changes: 3 additions & 1 deletion frontend/src/core/codemirror/copilot/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ export const copilotBundle = (config: CompletionConfig): Extension => {
request.doc.position,
state,
);
Logger.debug("Copilot suggestion:", suggestion);
if (suggestion) {
Logger.debug("Copilot suggestion:", suggestion);
}
return suggestion;
},
}),
Expand Down
15 changes: 14 additions & 1 deletion frontend/src/core/codemirror/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { EditorView, keymap } from "@codemirror/view";
import type { CellId } from "../cells/ids";
import { formatEditorViews } from "./format";
import { formatEditorViews, formattingChangeEffect } from "./format";
import {
getCurrentLanguageAdapter,
toggleToLanguage,
Expand Down Expand Up @@ -60,8 +60,21 @@ export function formatKeymapExtension(
*/
export function scrollActiveLineIntoView() {
return EditorView.updateListener.of((update) => {
// Ignore the editor does not have focus, ignore
if (!update.view.hasFocus) {
return;
}

// A new line was added, scroll the active line into view
if (update.heightChanged && update.docChanged) {
// Ignore formatting changes
const isFormattingChange = update.transactions.some((tr) =>
tr.effects.some((effect) => effect.is(formattingChangeEffect)),
);
if (isFormattingChange) {
return;
}

const activeLines = update.view.dom.getElementsByClassName(
"cm-activeLine cm-line",
);
Expand Down

0 comments on commit bcab319

Please sign in to comment.