Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 164 additions & 6 deletions crates/engine/src/cmd/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown
Contributor

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 mask equal to old to new without checking whether another layer still bears old, or whether new now collides. references_follow_layer applies 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 layer Countries (B), and Cities is masked by Countries. Renaming A to Nations silently 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 rename and handle masks inside references_follow_layer with the same replace closure, 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.

Copy link
Copy Markdown
Member Author

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.

Ok(r)
}).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layer"?, "name": string}"#)
.when(need_layer),
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug (confidence ~65%): references that already name the destination are silently redirected. layer_by_name (crates/map/src/lib.rs:2541) returns the first case-insensitive match. The collision branch only counts references to old, so a reference that already named new is never counted or noticed.

Failure scenario: layers [Parcels, Cities (mask = "Countries"), Countries]. Renaming Parcels to Countries makes the first match the renamed layer. Cities now masks by Parcels, where before it masked by the original Countries. Nothing is kept, and collision_left stays 0, so no notice fires. The project-wide path has the same gap: a report on Countries resolves through find_layer to the renamed layer, with no notice.

Suggested direction: when collision is true, also count references already named new (in the same scopes) and say so in the collision notice, or keep the rename from making them ambiguous. A regression test is needed: the existing layer_rename_never_redirects_a_table_to_another_layer_of_the_new_name only covers references to old.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 (collision is false), a reference that already names new is left alone by replace, but it becomes resolvable after the rename. Example: a report still names Nations after that layer was removed, or a mask names Nations as a dangling reference. Renaming Countries to Nations silently attaches those references to the renamed layer, with no notice. destination_refs only counts references when collision is true, so this case is not reported.

If the redirect is not intended, count references already named new in every case and say so in the notice. If it is intended (the old reference is treated as a pre-selected name), a short comment at this line and a test would make that explicit. Either way the AGENTS.md "never silent" rule applies to the outcome.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

References resolve by their current layer name (Map::layer_by_name and the report’s find_layer_map). A dangling Nations reference starts resolving when a layer named Nations is added or renamed into place; it had no target before that operation. This preserves the existing name-based contract. The collision notices added here cover existing targets made ambiguous by duplicate names.

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);
}
}
Comment thread
Mavrick-F marked this conversation as resolved.
Resolved
}
return;
}
Comment thread
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)));
Comment thread
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]) {
Expand Down
Loading
Loading