Skip to main content

Building autocomplete

The engine exposes four read-only methods for building editor tooling — a formula field with suggestions, an expression builder UI, inline diagnostics. They're the same methods the playground uses.

List what's available

$engine->availableMethods(); // every registered method name
$engine->availableMethods('string'); // methods valid on a string value
$engine->availableMethods('array'); // methods valid on an array value

$engine->availableFunctions(); // every function name, incl. set/get

availableMethods($type) returns the names registered for that type plus those registered for any. Types are the runtime type names the engine dispatches on: string, int, double, bool, array, Carbon\CarbonImmutable.

Suggest what comes next in a chain

suggestMethods() evaluates an expression against a context and reports what its result is and what could be chained onto it:

$engine->suggestMethods('trigger.user.name', $context);
// [
// 'type' => 'string',
// 'value' => 'Ada',
// 'methods' => ['after', 'before', 'camel', 'concat', 'contains', ...],
// ]

Use it after the user types a . — evaluate the expression so far, then offer methods as completions.

Suggest fields inside a list operation

Inside filter(), map(), or sort(), the useful completions are the fields of the list's items (current.<field>). suggestKeys() evaluates an expression that resolves to a list of records and returns the union of keys across its items:

$engine->suggestKeys('trigger.orders', $context);
// → ['id', 'status', 'total', 'customer']

Returns an empty array if the expression doesn't resolve to a list, or resolves to a list of non-records.

Handling errors while the user types

A partially typed expression will usually fail to parse or evaluate. Catch the exception and surface it — LexerException and ParserException messages include a position N, which you can use to point at the offending character:

try {
$result = $engine->suggestMethods($partialExpression, $context);
} catch (\Throwable $e) {
// show $e->getMessage(); parse it for "position (\d+)" to place a caret
}

See Error handling for the full exception list.