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.

Refactor word boundary detection into separate function and add corresponding tests (#296)

Myles Scolnickc4b15fd3d02e
diff --git a/frontend/src/core/codemirror/completion/__tests__/hints.test.ts b/frontend/src/core/codemirror/completion/__tests__/hints.test.ts
new file mode 100644
index 0000000000..39bf488a20
--- /dev/null
+++ b/frontend/src/core/codemirror/completion/__tests__/hints.test.ts
@@ -0,0 +1,84 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+
+import { describe, expect, it } from "vitest";
+import { getPositionAtWordBounds } from "../hints";
+import { Text } from "@codemirror/state";
+
+const doc = Text.of([
+ "# Update the data",
+ "set_state(get_state())",
+ "my_100_var = 100",
+]);
+
+describe("getPositionAtWordBounds", () => {
+ it("should return correct start and end positions", () => {
+ const pos = "# Update the data".length + 1;
+ const result = getPositionAtWordBounds(doc, pos);
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "endToken": 27,
+ "startToken": 18,
+ }
+ `);
+
+ expect(
+ doc.slice(result.startToken, result.endToken).toString()
+ ).toMatchInlineSnapshot('"set_state"');
+ });
+
+ it("should handle start of string", () => {
+ const pos = "# Update the data".length;
+ const result = getPositionAtWordBounds(doc, pos);
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "endToken": 17,
+ "startToken": 13,
+ }
+ `);
+ expect(
+ doc.slice(result.startToken, result.endToken).toString()
+ ).toMatchInlineSnapshot('"data"');
+ });
+
+ it("should handle end of string", () => {
+ const pos = "# Update the data".length + 4;
+ const result = getPositionAtWordBounds(doc, pos);
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "endToken": 27,
+ "startToken": 18,
+ }
+ `);
+ expect(
+ doc.slice(result.startToken, result.endToken).toString()
+ ).toMatchInlineSnapshot('"set_state"');
+ });
+
+ it("should handle nested words", () => {
+ const pos = "# Update the data".length + 12;
+ const result = getPositionAtWordBounds(doc, pos);
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "endToken": 37,
+ "startToken": 28,
+ }
+ `);
+ expect(
+ doc.slice(result.startToken, result.endToken).toString()
+ ).toMatchInlineSnapshot('"get_state"');
+ });
+
+ it("should handle numbers", () => {
+ const pos = doc.length - 10;
+ const result = getPositionAtWordBounds(doc, pos);
+ expect(result).toMatchInlineSnapshot(`
+ {
+ "endToken": 51,
+ "startToken": 41,
+ }
+ `);
+ expect(
+ doc.slice(result.startToken, result.endToken).toString()
+ ).toMatchInlineSnapshot('"my_100_var"');
+ });
+});
diff --git a/frontend/src/core/codemirror/completion/hints.ts b/frontend/src/core/codemirror/completion/hints.ts
index 58dca9cad4..136df7eb03 100644
--- a/frontend/src/core/codemirror/completion/hints.ts
+++ b/frontend/src/core/codemirror/completion/hints.ts
@@ -10,7 +10,7 @@ import {
} from "@codemirror/view";
import { AUTOCOMPLETER, Autocompleter } from "./Autocompleter";
import { Logger } from "@/utils/Logger";
-import { StateField, StateEffect, Prec } from "@codemirror/state";
+import { StateField, StateEffect, Prec, Text } from "@codemirror/state";
export function hintTooltip() {
return [
@@ -24,31 +24,10 @@ export function hintTooltip() {
const cellId = HTMLCellId.parse(cellContainer.id);
- let startToken = pos;
- let endToken = pos;
-
- // Start of word
- while (startToken > 0) {
- const prevChar = view.state.doc.sliceString(
- startToken - 1,
- startToken
- );
- // Anything but a letter or number
- if (!/[\dA-Za-z]/.test(prevChar)) {
- break;
- }
- startToken--;
- }
-
- // End of word
- while (endToken < view.state.doc.length) {
- const nextChar = view.state.doc.sliceString(endToken, endToken + 1);
- // Anything but a letter or number
- if (!/[\dA-Za-z]/.test(nextChar)) {
- break;
- }
- endToken++;
- }
+ const { startToken, endToken } = getPositionAtWordBounds(
+ view.state.doc,
+ pos
+ );
const result = await AUTOCOMPLETER.request({
document: view.state.doc.slice(0, endToken).toString(), // convert Text to string
@@ -143,3 +122,30 @@ const cursorTooltipField = StateField.define<Tooltip[]>({
});
},
});
+
+export function getPositionAtWordBounds(doc: Text, pos: number) {
+ let startToken = pos;
+ let endToken = pos;
+
+ // Start of word
+ while (startToken > 0) {
+ const prevChar = doc.sliceString(startToken - 1, startToken);
+ // Anything but a letter, number, or underscore
+ if (!/\w/.test(prevChar)) {
+ break;
+ }
+ startToken--;
+ }
+
+ // End of word
+ while (endToken < doc.length) {
+ const nextChar = doc.sliceString(endToken, endToken + 1);
+ // Anything but a letter, number, or underscore
+ if (!/\w/.test(nextChar)) {
+ break;
+ }
+ endToken++;
+ }
+
+ return { startToken, endToken };
+}