Repository navigation
fix(engine): update typed references when a layer is renamed #1718
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
Changes from all commits
12b5ba7
6041f09
9e03e84
5d1b445
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -93,14 +93,9 @@ pub fn commands() -> Vec<C> { | |
| s.notify(said, false); | ||
| } | ||
| } | ||
| // Layers masked by this one follow the new name. | ||
| for l in &mut s.map_mut().layers { | ||
| if l.mask.as_deref().is_some_and(|m| m.eq_ignore_ascii_case(&old)) { | ||
| l.mask = Some(n.clone()); | ||
| } | ||
| } | ||
| // So do legends that leave it out. | ||
| legends_follow_layer(s, &old, Some(&n)); | ||
| references_follow_layer(s, &old, &n); | ||
| Ok(r) | ||
| }).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?, "name": string}"#) | ||
| .when(need_layer), | ||
|
|
@@ -4712,6 +4707,169 @@ pub(crate) fn legends_follow_layer(s: &mut Session, old: &str, new: Option<&str> | |
| } | ||
| } | ||
|
|
||
| /// Carries typed references to a renamed layer along with it. Map-local references and layout | ||
| /// frames stay in their map. A name still present there keeps its references, as legends do; | ||
| /// project-wide references without a map stay unchanged when another map still has that name. | ||
| /// Script text, tool parameters and history are not rewritten. | ||
| fn references_follow_layer(s: &mut Session, old: &str, new: &str) { | ||
| use rustgis_map::design::{Elevation, TopologyRule}; | ||
| use rustgis_map::layout::ElementKind; | ||
|
|
||
| let case_only = old.eq_ignore_ascii_case(new); | ||
| let old_present = !case_only && s.map().layers.iter().any(|l| l.name.eq_ignore_ascii_case(old)); | ||
| let mut old_left = 0usize; | ||
| let collision = !case_only && s.map().layers.iter().filter(|l| l.name.eq_ignore_ascii_case(new)).count() > 1; | ||
|
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. Bug (confidence ~65%): references that already name the destination are silently redirected. Failure scenario: layers Suggested direction: when
Member
Author
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. Fixed in 5d1b445. The collision notice now also counts references that already name the destination, including project-wide references when another map already has that name. The regression checks the mask, table and report together (count 3), a cross-map report (count 1), Undo and a clean unique rename. The actual counted notice was rendered and inspected. |
||
| let mut collision_left = 0usize; | ||
| let mut destination_refs = 0usize; | ||
| let mut replace = |name: &mut String| { | ||
| if name.eq_ignore_ascii_case(old) { | ||
| if old_present { | ||
| old_left += 1; | ||
| } else if collision { | ||
| collision_left += 1; | ||
| } else { | ||
| *name = new.to_string(); | ||
|
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. Possible silent redirect of dangling references (confidence ~35%). When no other layer has the new name ( If the redirect is not intended, count references already named
Member
Author
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. References resolve by their current layer name ( Preventing a dangling name from binding later would require a separate reference-identity design. The optional explanatory comment and additional test are left for the release quality pass. |
||
| } | ||
| } else if collision && name.eq_ignore_ascii_case(new) { | ||
| destination_refs += 1; | ||
| } | ||
| }; | ||
| for layer in &mut s.map_mut().layers { | ||
| if let Some(mask) = &mut layer.mask { | ||
| replace(mask); | ||
| } | ||
| } | ||
| // Elevation and Map Series selectors compare spellings exactly, even though lookup does not. | ||
| if case_only { | ||
| if s.map().layers.iter().filter(|l| l.name.eq_ignore_ascii_case(old)).count() == 1 { | ||
| if let Elevation::Surface(layer) = &mut s.map_mut().editing.settings.elevation { | ||
| replace(layer); | ||
| } | ||
| let map_name = s.map().name.clone(); | ||
| for layout in &mut s.project.layouts { | ||
| if let Some(series) = &mut layout.series | ||
| && layout.elements.iter().any(|e| { | ||
| e.id == series.frame && matches!(&e.kind, ElementKind::MapFrame { map, .. } if map.eq_ignore_ascii_case(&map_name)) | ||
| }) | ||
| { | ||
| replace(&mut series.layer); | ||
| } | ||
| } | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| } | ||
| return; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| let map_name = s.map().name.clone(); | ||
| let editing = &mut s.map_mut().editing; | ||
| for relation in &mut editing.relationships { | ||
| replace(&mut relation.origin); | ||
| replace(&mut relation.destination); | ||
| } | ||
| for rule in &mut editing.topology { | ||
| match rule { | ||
| TopologyRule::MustNotOverlap { layer } | ||
| | TopologyRule::MustNotHaveGaps { layer } | ||
| | TopologyRule::MustNotSelfIntersect { layer } | ||
| | TopologyRule::MustNotHaveDangles { layer } => replace(layer), | ||
| TopologyRule::MustBeCoveredBy { layer, by } => { | ||
| replace(layer); | ||
| replace(by); | ||
| } | ||
| } | ||
| } | ||
| if let Elevation::Surface(layer) = &mut editing.settings.elevation { | ||
| replace(layer); | ||
| } | ||
| for layout in &mut s.project.layouts { | ||
| let frames: Vec<u32> = layout | ||
| .elements | ||
| .iter() | ||
| .filter_map(|e| match &e.kind { | ||
| ElementKind::MapFrame { map, .. } if map.eq_ignore_ascii_case(&map_name) => Some(e.id), | ||
| _ => None, | ||
| }) | ||
| .collect(); | ||
| for element in &mut layout.elements { | ||
| match &mut element.kind { | ||
| ElementKind::Table { frame, layer, .. } | ElementKind::Chart { frame, layer, .. } if frames.contains(frame) => { | ||
| replace(layer) | ||
| } | ||
| ElementKind::Colorbar { frame, layer: Some(layer), .. } if frames.contains(frame) => replace(layer), | ||
| _ => {} | ||
| } | ||
| } | ||
| if let Some(series) = &mut layout.series | ||
| && frames.contains(&series.frame) | ||
| { | ||
| replace(&mut series.layer); | ||
| } | ||
| } | ||
| // These items do not record a map; changing an ambiguous name would redirect references | ||
| // used with another map. Keep those spellings until the user chooses their intended layer. | ||
| let shared = s.project.maps.iter().enumerate().any(|(i, map)| { | ||
| i != s.project.active_map | ||
| && map.layers.iter().any(|layer| layer.name.eq_ignore_ascii_case(old) || layer.name.eq_ignore_ascii_case(new)) | ||
| }); | ||
| let mut left = 0usize; | ||
| let shared_new = s | ||
| .project | ||
| .maps | ||
| .iter() | ||
| .enumerate() | ||
| .any(|(i, map)| i != s.project.active_map && map.layers.iter().any(|layer| layer.name.eq_ignore_ascii_case(new))); | ||
|
Mavrick-F marked this conversation as resolved.
Resolved
|
||
| let mut project_destination_refs = 0usize; | ||
| let mut project_reference = |name: &mut String| { | ||
| if shared_new && name.eq_ignore_ascii_case(new) { | ||
| project_destination_refs += 1; | ||
| } else if shared && name.eq_ignore_ascii_case(old) { | ||
| left += 1; | ||
| } else { | ||
| replace(name); | ||
| } | ||
| }; | ||
| for report in &mut s.project.reports { | ||
| project_reference(&mut report.layer); | ||
| } | ||
| for model in &mut s.project.models { | ||
| if let Some(graph) = &mut model.graph { | ||
| for node in &mut graph.nodes { | ||
| if let Some(layer) = &mut node.layer { | ||
| project_reference(layer); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| for workspace in &mut s.project.workspaces { | ||
| for image in &mut workspace.images { | ||
| project_reference(image); | ||
| } | ||
| } | ||
| if left > 0 { | ||
| s.notify_text(crate::notice_text::NoticeText::format( | ||
| "{count} project reference(s) still name '{old}': another map has a layer of the old or new name; choose the intended layer in those items", | ||
| [("count", left.to_string().into()), ("old", old.to_string().into())], | ||
| ), false); | ||
| } | ||
| if old_left > 0 { | ||
| s.notify_text(crate::notice_text::NoticeText::format( | ||
| "{count} layer reference(s) still name '{old}': another layer in this map has that name; choose the intended layer in those items", | ||
| [("count", old_left.to_string().into()), ("old", old.to_string().into())], | ||
| ), false); | ||
| } | ||
| if collision_left > 0 { | ||
| s.notify_text(crate::notice_text::NoticeText::format( | ||
| "{count} layer reference(s) still name '{old}': two layers are now called '{new}'; give the layer a unique name and choose it in those items", | ||
| [("count", collision_left.to_string().into()), ("old", old.to_string().into()), ("new", new.to_string().into())], | ||
| ), false); | ||
| } | ||
| let destination_refs = destination_refs + project_destination_refs; | ||
| if destination_refs > 0 { | ||
| s.notify_text(crate::notice_text::NoticeText::format( | ||
| "{count} layer or project reference(s) already name '{new}': the rename created duplicate layer names and may change which layer they use; give the renamed layer a unique name", | ||
| [("count", destination_refs.to_string().into()), ("new", new.to_string().into())], | ||
| ), false); | ||
| } | ||
| } | ||
|
|
||
| /// Removes the layers `ids` from the active map, and legends that left them out forget them, as | ||
| /// `layer.remove` does. Paths that delete several layers at once go through here. | ||
| pub(crate) fn drop_layers(s: &mut Session, ids: &[u32]) { | ||
|
|
||
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.
Layer masks ignore the ambiguity rule this PR adds (confidence ~60%). The mask loop just above (lines 97-101, not in the diff, so pre-existing) renames every
maskequal tooldtonewwithout checking whether another layer still bearsold, or whethernewnow collides.references_follow_layerapplies exactly that check to every other map-local reference (its doc: "A name still present there keeps its references"), so the mask is the one map-local reference left out of the policy.Failure scenario: a map has
Countries(A) and a second layerCountries(B), andCitiesis masked byCountries. Renaming A toNationssilently moves the mask to A. Before the rename the mask was ambiguous, so the PR's rule says keep it and count the notice.Suggested fix: remove the mask loop from
renameand handle masks insidereferences_follow_layerwith the samereplaceclosure, e.g.for layer in &mut s.map_mut().layers { if let Some(mask) = layer.mask.as_mut() { replace(mask); } }. A test with a duplicate old name and a masked layer would cover it.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.
Fixed in 9e03e84. Layer masks now use the same ambiguity checks and counted notices as the other references. Duplicate old names and destination collisions keep the mask unchanged; case-only renames preserve the existing spelling update.
The duplicate-name regression checks the retained mask, combined count of two and complete Undo restoration. All five focused tests passed, followed by full CI (5,365 tests), WebAssembly and hooks. The actual UI notice was rendered and inspected.