Skip to content

Commit

Permalink
Retain :: after >, ) or ] when shortening type names (#7755)
Browse files Browse the repository at this point in the history
# Objective

While working on #7442 i discovered that `get_short_name` does not work well with sub paths after closing brackets. It currently turns `bevy_asset::assets::Assets<bevy_scene::dynamic_scene::DynamicScene>::asset_event_system` into `Assets<DynamicScene>asset_event_system`. This PR fixes that.

## Solution
- Retain `::` after a closing bracket like `>`, `)` or `]`.
- Add a test for all sub path after closing bracket cases.
  • Loading branch information
geieredgar committed Feb 20, 2023
1 parent ca1802b commit e1a8123
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions crates/bevy_utils/src/short_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ pub fn get_short_name(full_name: &str) -> String {
let special_character =
&rest_of_string[special_character_index..=special_character_index];
parsed_name.push_str(special_character);
// Move the index just past the special character
index += special_character_index + 1;

match special_character {
">" | ")" | "]"
if rest_of_string[special_character_index + 1..].starts_with("::") =>
{
parsed_name.push_str("::");
// Move the index past the "::"
index += special_character_index + 3;
}
// Move the index just past the special character
_ => index += special_character_index + 1,
}
} else {
// If there are no special characters left, we're done!
parsed_name += collapse_type_name(rest_of_string);
Expand Down Expand Up @@ -107,4 +117,20 @@ mod name_formatting_tests {
"do_mad_science<Test<Tube>, TypeSystemAbuse>".to_string()
);
}

#[test]
fn sub_path_after_closing_bracket() {
assert_eq!(
get_short_name("bevy_asset::assets::Assets<bevy_scene::dynamic_scene::DynamicScene>::asset_event_system"),
"Assets<DynamicScene>::asset_event_system".to_string()
);
assert_eq!(
get_short_name("(String, String)::default"),
"(String, String)::default".to_string()
);
assert_eq!(
get_short_name("[i32; 16]::default"),
"[i32; 16]::default".to_string()
);
}
}

0 comments on commit e1a8123

Please sign in to comment.