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.

improvement: tab to start completion (#1220)

Akshay Agrawal20e21cf125ea
diff --git a/frontend/src/core/codemirror/cm.ts b/frontend/src/core/codemirror/cm.ts
index 594244d606..bc676fe931 100644
--- a/frontend/src/core/codemirror/cm.ts
+++ b/frontend/src/core/codemirror/cm.ts
@@ -3,6 +3,7 @@ import {
acceptCompletion,
closeBrackets,
closeBracketsKeymap,
+ startCompletion,
} from "@codemirror/autocomplete";
import {
history,
@@ -103,6 +104,20 @@ export const setupCodeMirror = ({
];
};
+const startCompletionAtEndOfLine = (cm: EditorView): boolean => {
+ const { from, to } = cm.state.selection.main;
+ if (from !== to) {
+ // this is a selection
+ return false;
+ }
+
+ const line = cm.state.doc.lineAt(to);
+ return line.text.slice(0, to - line.from).trim() === ""
+ ? // in the whitespace prefix of a line
+ false
+ : startCompletion(cm);
+};
+
// Based on codemirror's basicSetup extension
export const basicBundle = (
completionConfig: CompletionConfig,
@@ -152,7 +167,11 @@ export const basicBundle = (
{
key: "Tab",
run: (cm) => {
- return acceptCompletion(cm) || indentMore(cm);
+ return (
+ acceptCompletion(cm) ||
+ startCompletionAtEndOfLine(cm) ||
+ indentMore(cm)
+ );
},
preventDefault: true,
},
diff --git a/frontend/src/css/codehilite.css b/frontend/src/css/codehilite.css
index 70ac768833..4c550d5fa4 100644
--- a/frontend/src/css/codehilite.css
+++ b/frontend/src/css/codehilite.css
@@ -365,36 +365,47 @@ span.linenos.special {
.highlight {
color: var(--sky-8);
}
+
.gt {
color: var(--red-9);
}
+
.nb {
color: var(--grass-8);
}
+
.m {
color: var(--sky-11);
}
+
.n {
color: var(--slate-10);
}
+
.w {
color: var(--slate-10);
}
+
.o {
color: var(--slate-10);
}
+
.p {
color: var(--slate-10);
}
+
.bp {
color: var(--slate-10);
}
+
.pm {
color: var(--slate-10);
}
+
.k {
color: var(--slate-10);
}
+
.x {
color: var(--red-9);
}