Repository navigation
feat(table): select and delete rows of a standalone table #3594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dwarburg
wants to merge
6
commits into
opengeos:main
Choose a base branch
from
dwarburg:feat/table-row-delete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+473
−30
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df83f92
feat(table): select and delete rows of a standalone table
dwarburg a55da4a
Merge branch 'main' into feat/table-row-delete
slowy07 a833735
fix(table): forget a table's selection on any change to its rows
dwarburg 3999119
Merge remote-tracking branch 'origin/feat/table-row-delete' into feat…
dwarburg 9eeacf1
Merge branch 'main' into feat/table-row-delete
dwarburg 27fb887
fix(table): draw the toolbar once, and keep a Delete binding's own rows
dwarburg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -28,7 +28,8 @@ pub fn commands() -> Vec<C> { | |||||
| C::new("edit.addFeature", "Create Feature", "Edit › Features", add_feature).inputs(super::design::new_feature_inputs).roles(&[]).effects(crate::policy::Effects::NONE) | ||||||
| .when(super::design::need_editable_or_table) | ||||||
| .params(r#"{"layer"?, "table"?: name|id (a standalone table: adds a row, without geometry or template), "geometry": GeoJSON geometry (lon/lat; left out for a table), "attributes"?: {field: value}, "template"?: feature template}"#), | ||||||
| C::new("edit.delete", "Delete", "Edit › Features", delete).roles(&[]).effects(crate::policy::Effects::NONE).key("Delete").when(super::design::need_editable_selection), | ||||||
| C::new("edit.delete", "Delete", "Edit › Features", delete).roles(&[]).effects(crate::policy::Effects::NONE).key("Delete").when(super::design::need_editable_selection_or_table) | ||||||
| .params(r#"{"table"?: name|id (a standalone table: deletes its "rows"; left out, every layer's selected features are deleted), "rows"?: [number] (with "table": the row numbers to delete, numbered from 0 as the read-rows tool's _row)}"#), | ||||||
| C::new("edit.setValue", "Edit Attribute", "Attribute Table", set_value).roles(&[]).effects(crate::policy::Effects::NONE).atomic() | ||||||
| .when(super::design::need_editable_or_table) | ||||||
| .params(r#"{"layer"?, "table"?: name|id (a standalone table, instead of a layer), "row": number, "field": string (the exact name first; a name matching several fields only ignoring case is refused), "value": any, "classStyle"?: bool (a layer text field: ensure the applied class has a Unique Values colour rule, in the same undoable transaction)}"#), | ||||||
|
|
@@ -927,7 +928,16 @@ fn decimal_text(t: &str) -> Option<(Value, Cut)> { | |||||
| }) | ||||||
| } | ||||||
|
|
||||||
| fn delete(s: &mut Session, _: &Json) -> CmdResult { | ||||||
| fn delete(s: &mut Session, p: &Json) -> CmdResult { | ||||||
| if let Some(result) = land_table_edit(s, p, "edit.delete") { | ||||||
| return result; | ||||||
| } | ||||||
| if p.get("table").is_some_and(|t| !t.is_null()) { | ||||||
| return delete_rows(s, p); | ||||||
| } | ||||||
| if p.get("rows").is_some_and(|r| !r.is_null()) { | ||||||
| return Err("rows are deleted from a standalone table (\"table\"); a layer's selected features are deleted without rows".into()); | ||||||
| } | ||||||
| let mut n = 0; | ||||||
| let mut removed: Vec<(u32, Vec<super::design::Change>)> = Vec::new(); | ||||||
| for l in &mut s.map_mut().layers { | ||||||
|
|
@@ -954,6 +964,42 @@ fn delete(s: &mut Session, _: &Json) -> CmdResult { | |||||
| Ok(json!({"deleted": n})) | ||||||
| } | ||||||
|
|
||||||
| /// `edit.delete` with `"table"`: deletes the `"rows"` of a standalone table, while editing is on. | ||||||
| /// A table's selection is the attribute table's own, so the rows to delete come with the call. | ||||||
| /// A row named twice is deleted once; a row the table doesn't have refuses the whole call, and | ||||||
| /// nothing is deleted. Runs on the desktop table worker ([`table_ahead`]): the rows to keep are | ||||||
| /// marked with progress and Cancel, then removed in one pass that can't be interrupted. | ||||||
| fn delete_rows(s: &mut Session, p: &Json) -> CmdResult { | ||||||
| let t = write_target(s, p)?; | ||||||
| let Target::Table(_) = t else { return Err("rows are deleted from a standalone table (\"table\")".into()) }; | ||||||
| editing_on(s)?; | ||||||
| let rows = p.get("rows").and_then(Json::as_array).ok_or("rows is required: the row numbers to delete, such as [0, 3]")?; | ||||||
| let ds = data_mut(s, t)?; | ||||||
| let len = ds.features.len(); | ||||||
| let mut doomed = Vec::with_capacity(rows.len()); | ||||||
| for row in rows { | ||||||
| match row.as_u64().and_then(|r| usize::try_from(r).ok()) { | ||||||
| Some(r) if r < len => doomed.push(r), | ||||||
| _ => return Err(format!("the table has no row {row} (it has {len}, numbered from 0); nothing was deleted")), | ||||||
| } | ||||||
| } | ||||||
| doomed.sort_unstable(); | ||||||
| doomed.dedup(); | ||||||
| let n = doomed.len(); | ||||||
| if n > 0 { | ||||||
| rustgis_data::progress::begin(len); | ||||||
| let mut keep = vec![true; len]; | ||||||
| for (i, k) in keep.iter_mut().enumerate() { | ||||||
| rustgis_data::progress::step(1).map_err(|e| e.to_string())?; | ||||||
| *k = doomed.binary_search(&i).is_err(); | ||||||
| } | ||||||
| let mut kept = keep.into_iter(); | ||||||
| ds.features.retain_rows(|_| kept.next().unwrap_or(true)); | ||||||
| } | ||||||
| s.notify(format!("Deleted {n} rows"), false); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: a single deleted row is reported as "Deleted 1 rows". Use the singular here. (The
Suggested change
Category: quality |
||||||
| Ok(json!({"deleted": n})) | ||||||
| } | ||||||
|
|
||||||
| /// A numeric conversion's loss, shared by edits and feature creation. Null entered explicitly is quiet. | ||||||
| fn numeric_loss(input: &Value, v: &Value, ty: FieldType) -> Option<CellLoss> { | ||||||
| if ty.is_numeric() && v.is_null() && !input.asks_null() { | ||||||
|
|
@@ -1831,6 +1877,7 @@ pub(crate) const TABLE_EDITS: &[(&str, &str, fn(&mut Session, &Json) -> CmdResul | |||||
| ("edit.deleteField", "Delete Field", delete_field), | ||||||
| ("edit.convertField", "Convert Field", convert_field), | ||||||
| ("edit.calculateField", "Calculate Field", calculate_field), | ||||||
| ("edit.delete", "Delete Rows", delete), | ||||||
| ]; | ||||||
|
|
||||||
| /// The registered command and progress label for a table write. Layer invocations keep their | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A row number that isn't a whole number is refused with the wrong reason here. For
2.0or1.5,as_u64()isNone, so this arm says "the table has no row 2.0 (it has 4, numbered from 0)", which suggests the row is out of range when the value is the wrong type. Splitting the two cases gives the caller the right fix. Confidence: high that the message is misleading; the refusal itself is correct and nothing is deleted.Category: quality