Skip to content

feat(ui): Calculate Field previews what it would write in the first rows as the expression is typed (#2817) - #2822

Merged
jgregoriorincon merged 3 commits into
opengeos:mainfrom
jgregoriorincon:feat/2817-calculate-field-preview
Nov 24, 2026
Merged

jgregoriorincon merged 3 commits into
opengeos:mainfrom
jgregoriorincon:feat/2817-calculate-field-preview

Conversation

@jgregoriorincon

@jgregoriorincon jgregoriorincon commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Calculate Field shows what the expression would write before it is run. Once the expression reads and no suggestion is open (when the Invalid: line would otherwise show), a line under the box gives the first three rows Calculate would write (the first three of the selection, or the first three rows when none is selected; a standalone table: its first three rows), as the chosen field would hold them and the attribute table shows them, each value cut at forty characters, a line break in it (Unicode's line and paragraph separators too) read as a space, with what writing them loses:

Preview: 2 (fraction dropped) · <Null> (a division by zero) · 6

  • Same reading as the run. preview_calculation in engine/src/cmd/edit.rs parses the expression once, as check_calculation did, and passes each row through held, the step WriteLosses::write now shares: the coercion to the field's type and the loss it causes. Each value is what the run computes for that row and the field holds, and each reason (PreviewLoss) is a kind the run's notices count: a cut, a result the field can't hold, an expression error. The preview doesn't run the layer's checks (domains, allowed combinations of values, constraint rules) or its calculation rules: those still run when Calculate is clicked and can refuse the edit, as the guide now says.
  • At most three rows evaluated per frame. The rows are the first three of the selection as Calculate takes it (a selected row the layer no longer has, which Calculate writes nothing to, takes no place), so the selection's size doesn't matter, and a long text value stops being written at the cut. The expression is still parsed on each redraw, as the check already did (an $area expression also checks the layer's areas then).
  • Nothing else changes. No new state, no change to edit.calculateField or anything saved. With no valid field chosen, the box is checked as before, with no preview.
  • WriteLosses::write keeps its counting; calculate_field_notices_count_what_the_preview_reads pins it.
  • New strings (Preview: {values}, the loss reasons in tl_raw!) wait for the release translation pass.

Surfaces: the preview is a dialog aid, so it is in the desktop and browser apps (ui-egui). The CLI, Python, the Assistant and MCP run edit.calculateField directly and show its notices; they have no dialog to preview in. The phone interface (ui-touch) has no Calculate Field dialog.

Not in this PR: the same line in other expression boxes (label text, symbols by expression), and filters.

Testing

  • cargo fmt --all --check, cargo clippy -p rustgis-engine -p rustgis-ui-egui --all-targets -- -D warnings, cargo nextest run --workspace (9216 passed), doctests, cargo xtask layers and cargo xtask schema --check: all pass.
  • New engine tests: a_calculation_preview_is_what_the_run_writes (clean values, fraction, past the 64-bit range, text a whole-number field can't hold, division by zero, date-time into a date, asked null, no rows, rows past the end, the check's error) and calculate_field_notices_count_what_the_preview_reads.
  • New UI tests, seen failing before the dialog change: calculate_field_previews_the_first_rows_of_a_table (values, losses, an error row, no preview of an invalid expression or while a suggestion is offered), calculate_field_previews_the_selected_rows_of_a_layer (first three selected, a selected row the layer doesn't have, first three rows, empty layer) and a_calculation_preview_stays_on_one_line (a line break or tab in text reads as a space, a long value is cut at forty characters).
  • ui_shot before and after on the World Atlas sample: Countries with South America selected, field pop_est, pop_est / 7.

UI evidence

Before / after (Countries, 13 selected, pop_est = pop_est / 7):

2817-calculate-field-preview-before-after.png

Related issue

Closes #2817

Summary by CodeRabbit

  • New Features
    • Calculate Field now displays a live preview of results for up to three rows, reflecting how values will appear in the target field.
    • Previews indicate row-level errors and potential value losses, such as dropped fractions. Previewing does not change data; values are written only when Calculate is clicked.
  • Documentation
    • Updated the expressions guide to explain preview behavior, row selection, and possible differences between previewed and calculated results.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Calculate Field now previews field-coerced expression results for up to three rows. The preview reports applicable losses and row-level errors. The calculation dialog selects and formats preview rows, and the user guide describes preview behavior.

Changes: Calculate Field preview

Layer / File(s) Summary
Shared value coercion and loss reporting
crates/engine/src/cmd/edit.rs
A shared helper derives field-coerced values and loss categories. WriteLosses::write uses it while retaining loss tallying and per-row deduplication.
Preview API and engine tests
crates/engine/src/cmd/edit.rs
preview_calculation evaluates valid requested rows and returns coerced values with the first applicable loss. Tests cover preview values, losses, invalid inputs, and existing write-loss reporting.
Dialog preview and user guide
crates/ui-egui/src/dialogs.rs, docs/user-guide/expressions.md
The dialog selects up to three rows, formats preview values and losses on one line, and suppresses previews for empty expressions or open suggestions. Tests cover row selection and formatting. The guide describes preview behavior and notes that changes occur only after Calculate.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant CalculateFieldDialog
  participant preview_calculation
  participant Dataset
  CalculateFieldDialog->>preview_calculation: expression, field, and selected row indices
  preview_calculation->>Dataset: evaluate valid requested rows
  Dataset-->>preview_calculation: evaluated row results
  preview_calculation-->>CalculateFieldDialog: field-coerced values and preview losses
Loading

Possibly related PRs

Suggested reviewers: mavrick-f, giswqs, pick2510

Merge Risk: Minimal · up to b76f2

The Calculate Field dialog gains a read-only preview of the first three rows' values and write-loss reasons. It does not alter stored data or the calculation command. No actionable merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a Calculate Field live preview of the first rows as the expression is typed, which matches the summary, objectives, and dialog changes. It is specific and descriptive,…
Description check Passed The description follows the template. It has a Summary, a Testing section with commands and new tests, and a UI evidence section with a before/after screenshot. The Related issue section says Closes #…
Linked Issues check Passed [#2817] The only directly linked issue is open and has coding requirements, so they apply. The PR implements each one:

1. Preview rows: preview_calculation in crates/engine/src/cmd/edit.rs takes …
Out of Scope Changes check Passed The changes fall into these groups, and each supports the linked objective:

- crates/engine/src/cmd/edit.rs: new PreviewLoss, CalculationPreview, and preview_calculation. These provide the pr…
Full details: Title check

Explanation

The title names the main change: a Calculate Field live preview of the first rows as the expression is typed, which matches the summary, objectives, and dialog changes. It is specific and descriptive, and it includes a conventional type and scope prefix with the issue number.

Full details: Description check

Explanation

The description follows the template. It has a Summary, a Testing section with commands and new tests, and a UI evidence section with a before/after screenshot. The Related issue section says Closes #2817. The template's UI evidence section asks for screenshots when UI changes, and this PR includes one. The template's 'No visible UI change.' line was replaced, as the template instructs. The description is detailed and matches the objectives and file summaries.

Full details: Linked Issues check

Explanation

[#2817] The only directly linked issue is open and has coding requirements, so they apply. The PR implements each one:

  1. Preview rows: preview_calculation in crates/engine/src/cmd/edit.rs takes the first three selected rows, or the first three rows when none is selected. The dialog in crates/ui-egui/src/dialogs.rs also covers standalone tables. A selected row the layer no longer has is skipped.
  2. Display gate: the preview appears only after the expression reads, when no suggestion is open, and in the place where the Invalid: line would show. An empty expression or an invalid field shows no preview.
  3. Value and losses: each value goes through the shared held step, which WriteLosses::write also uses. The reasons (PreviewLoss) match the run's notices: fraction, range, not-a-number, not-finite, time, unheld, and expression error. An explicitly requested null is shown as null.
  4. Cost: the expression is parsed once per redraw. At most three rows are evaluated per redraw.
  5. Scope of change: the PR adds no saved state and does not change edit.calculateField. The layer checks and calculation rules still run on Calculate.
  6. Tests: the PR names engine tests (a_calculation_preview_is_what_the_run_writes, calculate_field_notices_count_what_the_preview_reads) and UI tests (calculate_field_previews_the_first_rows_of_a_table, calculate_field_previews_the_selected_rows_of_a_layer, a_calculation_preview_stays_on_one_line) that cover these behaviors.

The 40-character cut and the one-line formatting are not in the issue text. They serve the preview display and do not contradict the issue.

This assessment relies on the PR summary and the AI-generated code summaries. I did not inspect the source hunks directly.

Full details: Out of Scope Changes check

Explanation

The changes fall into these groups, and each supports the linked objective:

  • crates/engine/src/cmd/edit.rs: new PreviewLoss, CalculationPreview, and preview_calculation. These provide the preview data. The WriteLosses::write refactor shares the held logic so the preview and the run cannot drift. The PR states that the tally stays the same, and a test pins it.
  • crates/ui-egui/src/dialogs.rs: the preview line, its row selection, and its one-line formatting. This is the dialog surface the issue asks for.
  • docs/user-guide/expressions.md: documents the preview and the checks that still run on Calculate. This matches the new behavior.

The PR does not change the CLI, Python, Assistant, MCP, or ui-touch. It does not add previews to other expression boxes or to filters. The PR description and the issue both exclude these, so their absence is not a scope problem. No change was found that is unrelated to #2817.

This assessment relies on the PR summary and the AI-generated code summaries. I did not inspect the source hunks directly.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks three rows at play,
Then sees what fields would hold today.
A fraction lost, an error shown,
The preview leaves saved values alone.
“I’ll click Calculate,” says the hare,
With carrots tucked beside the chair.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 3604ba90-1830-41a0-9efc-f99935b46990
Commits

Reviewing files that changed from the base of the PR and between 3e3af48 and b76f23d.

Files selected for processing: 3

  • crates/engine/src/cmd/edit.rs
  • crates/ui-egui/src/dialogs.rs
  • docs/user-guide/expressions.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment thread crates/engine/src/cmd/edit.rs Resolved
Comment thread crates/ui-egui/src/dialogs.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, crash paths, silent losses or AGENTS.md breaks. The preview reads each row through the same written step as the run, so the preview and what Calculate writes agree on rows and losses. The two quality nits are posted inline.

Bugs

  • None found. I checked the run path (calculate_field, evaluate, WriteLosses::write), and the refactor keeps the old per-row counting order, including the case where a cut and an error both occur.

Security

  • None found. The preview shows user data only through egui labels, with no injection surface.

Performance

  • None new. The preview evaluates at most three rows per redraw. Calculation::parse runs per redraw as check_calculation already did. The $area scan at crates/data/src/query.rs:1245 is pre-existing, so I did not raise it.

Quality

  • crates/engine/src/cmd/edit.rs:506 (confidence high): written returns a positional [Option<Loss>; 2], and callers depend on the slot order. A named struct would be clearer. Posted inline.
  • crates/ui-egui/src/dialogs.rs:3157 (confidence high): the use sits inside the closure body. Posted inline.
  • Low confidence: preview_calculation takes the first three selected indices before dropping out-of-range ones. A selection with an out-of-range index could show fewer than three rows, although the run writes all in-range rows. Selections shouldn't contain such indices, so I did not post this.

AGENTS.md

  • None found. Checked: no new command or parameter, so command-surface.txt is unchanged. Features::len is not a deprecated copy, so the new ds.features.len() doesn't move the xtask/row-uses.txt ratchet. The UI strings use tl!, i18n::fmt and fmt_raw. The preview is bounded at three rows, so it needs no Cancel. Tests cover both clean and lossy input.

Prior reviews

  • No review threads existed on this PR, so none were left alone.

Change size

Compared with the base branch (merge base 06657ca to PR head f22fa03).

Measure Value
Files changed 3 (0 added, 3 modified, 0 removed, 0 renamed)
Size before 345.6 KB
Size after 360.1 KB
Total size change +14.2 KB
Path Before After Change
crates/engine/src/cmd/edit.rs 101.6 KB 108.8 KB +7.2 KB
crates/ui-egui/src/dialogs.rs 223.6 KB 230.2 KB +6.6 KB
docs/user-guide/expressions.md 20.4 KB 20.9 KB +0.4 KB

@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-2822/
Web app https://preview.rustgis.app/pr-2822/app/
Commit b76f23d

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

… rows the layer lacks (opengeos#2817)

A line break or tab in a text value reads as a space, and a long value
stops being written past the cut. A selected row the layer no longer has
takes no place among the three. The coercion step returns a named Held
struct instead of a positional pair.
Comment thread crates/engine/src/cmd/edit.rs Resolved
Comment thread crates/ui-egui/src/dialogs.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs

  • Preview can show values that Calculate Field then refuses to write (confidence: high that the mismatch exists; medium on how much it matters here). apply_with checks the layer's data design (field domains and validation) after evaluation, so one out-of-domain row fails the whole run and writes nothing. preview_calculation never runs that check, so the dialog can show values with no warning. This contradicts the PR's claim that the preview "can't disagree with what Calculate writes". The existing test calculate_field_bulk_edit_keeps_validation_archive_and_undo (crates/engine/src/tests.rs:50906) shows the refusal. Posted inline at crates/engine/src/cmd/edit.rs:1659.

Security

  • None found.

Performance

  • None new. The dialog still parses the expression on every redraw, and that predates this PR. The preview itself only evaluates up to three rows.

Quality

  • Dense dialog closure (confidence: medium). The Calculate Field arm now mixes the selection rule, row choice, and parse/preview/check branches in one closure. A small preview_rows helper would make the "takes no place" rule testable. Posted inline at crates/ui-egui/src/dialogs.rs:823.
  • Unicode line separators (confidence: low). calculation_preview treats only char::is_control as a break, so U+2028 and U+2029 would pass through. Posted with the item above.

AGENTS.md

  • None found. The new strings use tl_raw! and i18n::fmt, the surfaces are addressed in the PR body, there are no new commands, and the tests use no timer waits.

Prior concerns

  • Two earlier threads, both marked outdated and answered by the author: the written slot-order contract (now held) and the import placement. I left both alone, so 2 earlier concerns were not re-raised.

Change size

Compared with the base branch, main (merge base 06657ca to PR head d765663).

Metric Value
Files changed 3 (0 added, 3 modified, 0 removed, 0 renamed)
Size before 345.6 KB
Size after 362.1 KB
Total size change +16.5 KB
Path Before After Change
crates/ui-egui/src/dialogs.rs 223.6 KB 232.3 KB +8.7 KB
crates/engine/src/cmd/edit.rs 101.6 KB 108.9 KB +7.4 KB
docs/user-guide/expressions.md 20.4 KB 20.8 KB +393 B

…d preview, and the guide says the layer's checks still run (opengeos#2817)

The preview's value is what the expression gives for the row as the
field holds it; domains, allowed combinations, constraint rules and
calculation rules still run when Calculate is clicked.
Comment thread crates/engine/src/cmd/edit.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

Bugs: None found on the changed lines. I checked the shared held / WriteLosses::write refactor against the old branches (same losses, same once-per-row counting, same order), the preview's selection order (select.rs keeps selections sorted through normalize_selection, so the preview's first three match Calculate's), the Fault/Cut mapping, and the 40-character cut in OneLine. The Preview: and fmt_raw placeholders are single-pass, so row data can't be re-read as a template.

Security: None found.

Performance:

  • Quality-side note, not a new cost: Calculation::parse runs on every redraw, and an $area expression scans every feature in query.rs (ambiguous_area_message) on each parse. That cost predates this PR and the preview doesn't add to it. Low confidence it matters in practice; worth a follow-up on large layers.

Quality:

  • crates/engine/src/cmd/edit.rs:1669 (posted inline): preview_calculation evaluates every row it's given, and the three-row cap is only in the dialog caller. Capping it in the engine function would keep the "at most three rows per frame" rule local. Confidence: medium.

AGENTS.md: No breaks found. Strings go through tl_raw! / fmt_raw, the tests are the expected kind, and there's no new command, saved state or dependency. The surfaces claim in the PR body is consistent with the diff.

Already raised, left alone (2):

  • The preview not running layer checks (domains and similar). The author replied, and the guide and PR body were corrected.
  • The U+2028 / U+2029 line-break nit. The author fixed it and added a test.

The held struct refactor and the import placement were also already answered by the author. I left those alone too, which makes 4 earlier concerns in total.

Change size

Compared with main (merge base 06657ca to PR head b76f23d). Sizes are file sizes in bytes, not line counts.

Files changed Added Modified Removed Renamed Size before Size after Total change
3 0 3 0 0 345.6 KB 362.5 KB +16.8 KB
Path Before After Change
crates/ui-egui/src/dialogs.rs 223.6 KB 232.5 KB +8.9 KB
crates/engine/src/cmd/edit.rs 101.6 KB 109.0 KB +7.4 KB
docs/user-guide/expressions.md 20.4 KB 21.0 KB +0.6 KB

@jgregoriorincon
jgregoriorincon marked this pull request as ready for review November 24, 2026 02:39
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I found no bugs in this diff. I checked the preview against the run it claims to mirror: calculate_field, calculate_table, evaluate, and WriteLosses::write, which now share held. I also checked the dialog's data source and row choice, the single-pass placeholder fill (so cell text containing {loss} is not re-substituted), and the rows ratchet. I could not build or run the tests in this environment, so the PR's test claims are unverified by me.

Bugs

  • None in this diff (high confidence).
  • Pre-existing, not from this PR (low confidence, about 60%): calculate_field (crates/engine/src/cmd/edit.rs:1597) passes stored selection indices to evaluate without filtering rows the layer no longer has. A stale index reads as null, so the run can report an "is null in N rows" notice for a row it never writes. The preview deliberately skips such rows (crates/ui-egui/src/dialogs.rs:829), so the two can disagree. This is out of scope here and worth its own issue.

Security

  • None.

Performance

  • None new (high confidence). The preview evaluates at most three rows per frame. It parses the expression once per redraw, as check_calculation already did, so that cost is not new.

Quality

  • Low: calculation_preview (crates/ui-egui/src/dialogs.rs, around line 3147) is a long function with a nested OneLine writer. Its .1 counter and use of a write error as the stop signal are hard to follow. Splitting out the one-line value and the loss phrase would read better. This can wait for the release quality pass.

AGENTS.md

  • None (high confidence). I checked: no command or parameter changed, so the command-surface snapshot is unaffected; no unwrap/expect in non-test code; user-visible strings use tl_raw!, fmt, and fmt_raw; no new asset; the expressions guide matches the code.

Earlier review threads: I left 6 concerns across 5 threads alone, because each is answered or fixed. The domain-check mismatch was answered by correcting the claim in the PR body and the guide. The row-cap concern was answered. U+2028/2029, the tuple-to-struct change, and the import placement were fixed.

Change size

Compared: the whole PR against main (merge base 06657ca to PR head b76f23d).

Measure Value
Files changed 3 (0 added, 3 modified, 0 removed, 0 renamed)
Size before 345.6 KB
Size after 362.5 KB
Total size change +16.8 KB
Path Before After Change
crates/ui-egui/src/dialogs.rs 223.6 KB 232.5 KB +8.9 KB
crates/engine/src/cmd/edit.rs 101.6 KB 109.0 KB +7.4 KB
docs/user-guide/expressions.md 20.4 KB 21.0 KB +567 B

@jgregoriorincon
jgregoriorincon added this pull request to the merge queue Nov 24, 2026
Merged via the queue into opengeos:main with commit d28f020 Nov 24, 2026
30 checks passed
@jgregoriorincon
jgregoriorincon deleted the feat/2817-calculate-field-preview branch November 24, 2026 04:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Calculate Field previews the expression's result on a few rows while it is typed

1 participant