Skip to content

[Feature]: Expression completion everywhere expressions are typed, read from the language's own catalogs #1240

Done

Description

@jgregoriorincon

Before you start

What problem does this solve?

One expression language now serves Select By Attributes, Definition Query, display filters, label classes, attribute constraints and rules, reports, Calculate Field and symbols by expression (grammar). Nowhere it is typed offers any help with it:

  • In the Script pane, completion stops at the edge of an expression: inside "where": "…" nothing is offered (scriptcomplete.rs tests: labels.style {"where": " gives no offer). Fields, functions, keywords and variables must be typed from memory, and a field with spaces must be written as \"Pop Est\" inside the JSON string.
  • The dialogs and panes that take an expression show at most a line of help text (Calculate Field), with no completion and no function signatures.

There is also no single list of what the language accepts: functions are in a private table (FUNCS in crates/data/src/query.rs) without signatures or descriptions, variables are match arms in the parser, and keywords are scattered through eat_kw calls. Any completion built today would need its own copy of the vocabulary, and that copy would fall behind each time the language grows.

Proposed solution

Build the shared piece once: a completion engine below the UI that reads only what the parser itself uses, so a new function, variable or keyword is offered as soon as the parser accepts it, and a new command or tool parameter that takes an expression gets completion with no UI change.

Where the values come from

Vocabulary Source Grows by itself because
Functions FUNCS extended with signature and description, exposed as functions() the parser resolves calls through it; a Func missing from it cannot be called
Variables new VARS table (name, aliases, Var, description) the parser resolves @…/$… through it, replacing the literal match
Keywords and operators new KEYWORDS table (operators apart from CASE words) the parser reads its constants
Fields, layers, values the Session, at the moment of typing already live, as script completion reads them today
Where expressions are typed an Expression parameter kind in ParamSpec / CommandSpec the UI asks the model, not a list of key names

Guards in CI: every Func has a catalog entry with a signature and description (exhaustive match), the function table in docs/user-guide/expressions.md matches the catalog, and everything in the catalogs parses.

Descriptions stay in English in data and are shown through tl!(…) in the UI, as tool names and summaries already are. Function names, keywords and variables are syntax and are never translated.

Plan (one PR each)

  • PR 0 — data: catalogs as the single source. FuncSpec with signature, description and example (functions()), VARS, keywords as a Kw enum; the parser consumes them; coverage and docs tests. A refactor with no change in behaviour. refactor(data): expression functions, variables and keywords in catalogs the parser reads (#1240) #1957
  • PR 1 — ui-egui: Functions and Variables menus beside the field buttons, in Select By Attributes, the Layer Properties Query tab and the Labeling SQL class, listed from the catalogs. feat(ui): Functions and Variables menus beside the clause buttons, from the expression catalogs (#1240) #1983
  • PR 2 — data: complete_at(text, cursor). It reads a partial expression with the real lexer and tolerates unfinished input. It returns the word being typed, what is expected there (an operand: field, function, variable, literal; an operator; the next CASE word; nothing inside 'text'), the open function and argument index (for its signature), and the field of a comparison. Unit tests only, hostile input included (lengths, nesting). feat(data): complete_at reads what may come next at a point of an expression, from the parser itself (#1240) #1999
  • PR 3 — ui-egui: a reusable expr_edit widget. It has the popup list, the open function's signature, fields quoted when needed (spaces, punctuation, keyword names), and closing ( and '. The popup and keyboard handling are moved out of scriptcomplete into a shared module. The first use is Calculate Field. The menus of PR 1 take the shared popup's look, and the two rows of field and operator buttons (dialogs::field_chips, sqlbox::chips) become one.
  • PR 4 — Script pane. Inside a JSON string whose key takes an expression (by a new Expression parameter kind), the cursor maps to the unescaped expression and accepted items are escaped back (\"Pop Est\").
  • PR 5 — The other places. Select By Attributes, Definition Query, display filters, label classes, symbols by expression, attribute constraints and rules, reports.
  • PR 6 — Field values. After field = ' or field IN (, the field's distinct values. Streamed layers give a bounded sample, and the list says so (values from a sample of N).

Question for the maintainer

Should the function catalog stay static (const, behind functions()), or become a registry in the Session so plugins could add expression functions? Starting static behind functions() lets a registry replace it later without changing callers. A registry means parse and eval take it as an argument.

Estimated scope

Wishlist: a larger or longer-term idea

Alternatives considered

  • A tokenizer of its own in ui-egui: no change to data, but a second grammar that falls behind the real one.
  • Prefix matching only, no context: cheap, but it offers AND where a field goes and fields after a complete comparison.
  • Completion in the Script pane only: smaller, but the dialogs, where most expressions are typed, would still have none, and a later widget would duplicate it.

Activity

  1. added
    enhancement
    on Nov 1, 2026
  2. jgregoriorincon commented on Nov 1, 2026

    @jgregoriorincon
    ContributorAuthor

    @giswqs I'd like to take this one, one PR at a time as in the checklist above.

    Before I start PR 0 (the catalogs in data), one decision is yours: should the function catalog stay static (a const table behind functions()), or become a registry in the Session so plugins could add expression functions later?

    My suggestion is static behind functions() for now. It keeps parse and eval as they are, and a registry can replace it later without changing any caller. If you'd rather have the registry from the start, I'll plan PR 0 around it.

    Anything else in the plan you'd change (the order, or the Expression parameter kind in ParamSpec/CommandSpec), I'm happy to adjust before writing code.

  3. jgregoriorincon commented on Nov 13, 2026

    @jgregoriorincon
    ContributorAuthor

    lds up better than when I asked:

    • Plugins run as sandboxed WebAssembly or as external processes. An expression function from a plugin would be called once per row: a WebAssembly call per row at best, a round trip over stdin/stdout at worst.
    • A project whose definition query, display filter or label class used a plugin function would stop working on a computer without that plugin.
    • A registry would make parse and eval take it as an argument, which touches about 20 callers across engine, map, analysis and ui-egui.

    A registry can still replace it later behind functions() without changing callers.

    Since this issue was opened the language has grown (area(), perimeter() and line_length() with units, $area, $perimeter and $length, dates), and so have the places where expressions are typed (label text, size, colour and angle, Unique Values by expression, the Query tab in Layer Properties). None of them offers any help yet.

    Keeping the catalogs current. The parser reads the catalogs, so nothing can be added to the language without them:

    • FuncSpec with signature, description and an example, all required, and an exhaustive match from Func to its entry: a new Func doesn't compile without one.
    • Variables resolved through VARS instead of match arms; keywords through a Kw enum and KEYWORDS instead of eat_kw("…") strings.
    • Tests: every example parses and evaluates; the catalogs and docs/user-guide/expressions.md name the same functions, variables, keywords and units; command and tool parameters named like expressions (where, expression, filter…) are declared as Expression, with a list of exceptions.
    • One line in AGENTS.md saying where a new function, variable or keyword goes.

    One change to the plan. Select By Attributes and the Query tab already share buttons for fields and operators. A list of functions and variables next to them (click to insert, with signature and description) makes the catalogs useful right after PR 0, before the completion engine:

    • PR 0: the catalogs and the guards above. No change in behaviour.
    • PR 1: the list of functions and variables next to the field buttons, in Select By Attributes and the Query tab.
    • PR 2: complete_at(text, cursor).
    • PR 3: the expr_edit widget, first in Calculate Field.
    • PR 4: the Script pane.
    • PR 5: the other places.
    • PR 6: field values.

    I'll update the checklist in the issue body to match. If you'd rather have the registry from the start, or a different order, let me know before PR 0 lands.

  4. 40 remaining items

  5. Mavrick-F commented on Nov 19, 2026

    @Mavrick-F
    Member

    A request building on the Calculate Field work here (#2040, #2092, #2113): show what the expression gives while it's typed. A line under the box would show the result for the first three selected rows, or the first three rows when nothing is selected. Calculate Field runs on those same rows, so the preview matches what will be written. Today you run the expression, check the table, then undo and try again. This would mostly remove that loop for the most common edit.

    Details I'd suggest:

    • It updates at the same moment the Invalid: line does, once no suggestion is open. It reuses the same Calculation that check_calculation already parses, so there's no second evaluator.
    • It shows the value the chosen field would actually get. If the result is text going into a Long field, or null, or out of range, the line says that instead of showing the raw result. Then the preview can't hide data that will be lost.
    • It evaluates a fixed handful of rows, so it stays cheap on every keystroke, on any layer size, and in the browser. It never runs over the whole layer.
    • It changes nothing that's saved and nothing in the command, only the dialog.

    Other places where expressions are typed (label text, symbols by expression) could use the same line later through expr_edit. Filters are a different case: a count of matches grows with the layer size, so it would need its own design and isn't part of this.

    I can implement this myself in its own PR under this issue, or leave it as a design proposal to stay out of people's way.

  6. jgregoriorincon commented on Nov 23, 2026

    @jgregoriorincon
    ContributorAuthor

    The plan in this issue is complete, so I'm closing it.

    Follow-ups found along the way were done in their own issues (#2343, #2618, #2619, #2631, #2697, #2718).

    @Mavrick-F's proposal above, a preview of Calculate Field's result on the first rows while the expression is typed, is now #2817, with credit to them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions