Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

feat: expression string method autocomplete (#5421)

zehjotkah8b70dea163d4
diff --git a/apps/builder/app/builder/shared/expression-editor.tsx b/apps/builder/app/builder/shared/expression-editor.tsx
index aa8a040582..d5a5ad4139 100644
--- a/apps/builder/app/builder/shared/expression-editor.tsx
+++ b/apps/builder/app/builder/shared/expression-editor.tsx
@@ -27,7 +27,12 @@ import {
} from "@codemirror/autocomplete";
import { javascript } from "@codemirror/lang-javascript";
import { textVariants, css, rawTheme } from "@webstudio-is/design-system";
-import { decodeDataVariableId, lintExpression } from "@webstudio-is/sdk";
+import {
+ decodeDataVariableId,
+ lintExpression,
+ allowedStringMethods,
+ allowedArrayMethods,
+} from "@webstudio-is/sdk";
import {
EditorContent,
EditorDialog,
@@ -244,6 +249,56 @@ const scopeCompletionSource: CompletionSource = (context) => {
},
});
}
+
+ // Add safe string and array method completions
+ if (path.path.length > 0) {
+ const methodOptions: Completion[] = [];
+
+ // Determine if target is likely a string or array based on its value
+ const isString = typeof target === "string";
+ const isArray = Array.isArray(target);
+
+ // Add string methods only if target is a string
+ if (isString) {
+ for (const method of allowedStringMethods) {
+ if (method.toLowerCase().includes(path.name.toLowerCase())) {
+ methodOptions.push({
+ label: `${method}()`,
+ detail: "string method",
+ type: "method",
+ apply: (view, completion, from, to) => {
+ view.dispatch({
+ ...insertCompletionText(view.state, `${method}()`, from, to),
+ annotations: pickedCompletion.of(completion),
+ });
+ },
+ });
+ }
+ }
+ }
+
+ // Add array methods if target is an array
+ if (isArray) {
+ for (const method of allowedArrayMethods) {
+ if (method.toLowerCase().includes(path.name.toLowerCase())) {
+ methodOptions.push({
+ label: `${method}()`,
+ detail: "array method",
+ type: "method",
+ apply: (view, completion, from, to) => {
+ view.dispatch({
+ ...insertCompletionText(view.state, `${method}()`, from, to),
+ annotations: pickedCompletion.of(completion),
+ });
+ },
+ });
+ }
+ }
+ }
+
+ options = [...options, ...methodOptions];
+ }
+
return {
from: context.pos - path.name.length,
filter: false,
diff --git a/packages/sdk/src/expression.ts b/packages/sdk/src/expression.ts
index c4a066202a..48510fd40b 100644
--- a/packages/sdk/src/expression.ts
+++ b/packages/sdk/src/expression.ts
@@ -29,7 +29,7 @@ type ExpressionVisitor = {
[K in Expression["type"]]: (node: Extract<Expression, { type: K }>) => void;
};
-const allowedStringMethods = new Set([
+export const allowedStringMethods = new Set([
"toLowerCase",
"replace",
"split",
@@ -42,7 +42,7 @@ const allowedStringMethods = new Set([
"toLocaleUpperCase",
]);
-const allowedArrayMethods = new Set(["at", "includes", "join", "slice"]);
+export const allowedArrayMethods = new Set(["at", "includes", "join", "slice"]);
export const lintExpression = ({
expression,