diff options
-rw-r--r-- | crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs | 9 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 6 |
2 files changed, 13 insertions, 2 deletions
diff --git a/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs b/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs index 3d8f1b5f2..953d0276f 100644 --- a/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs +++ b/crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs | |||
@@ -61,7 +61,9 @@ fn is_upper_snake_case(ident: &str) -> bool { | |||
61 | 61 | ||
62 | fn is_camel_case(ident: &str) -> bool { | 62 | fn is_camel_case(ident: &str) -> bool { |
63 | // We assume that the string is either snake case or camel case. | 63 | // We assume that the string is either snake case or camel case. |
64 | ident.chars().all(|c| c != '_') | 64 | // `_` is allowed only at the beginning or in the end of identifier, not between characters. |
65 | ident.trim_matches('_').chars().all(|c| c != '_') | ||
66 | && ident.chars().find(|c| c.is_alphabetic()).map(|c| c.is_ascii_uppercase()).unwrap_or(true) | ||
65 | } | 67 | } |
66 | 68 | ||
67 | #[cfg(test)] | 69 | #[cfg(test)] |
@@ -80,13 +82,18 @@ mod tests { | |||
80 | fn test_to_lower_snake_case() { | 82 | fn test_to_lower_snake_case() { |
81 | check(to_lower_snake_case, "lower_snake_case", expect![[""]]); | 83 | check(to_lower_snake_case, "lower_snake_case", expect![[""]]); |
82 | check(to_lower_snake_case, "UPPER_SNAKE_CASE", expect![["upper_snake_case"]]); | 84 | check(to_lower_snake_case, "UPPER_SNAKE_CASE", expect![["upper_snake_case"]]); |
85 | check(to_lower_snake_case, "Weird_Case", expect![["weird_case"]]); | ||
83 | check(to_lower_snake_case, "CamelCase", expect![["camel_case"]]); | 86 | check(to_lower_snake_case, "CamelCase", expect![["camel_case"]]); |
84 | } | 87 | } |
85 | 88 | ||
86 | #[test] | 89 | #[test] |
87 | fn test_to_camel_case() { | 90 | fn test_to_camel_case() { |
88 | check(to_camel_case, "CamelCase", expect![[""]]); | 91 | check(to_camel_case, "CamelCase", expect![[""]]); |
92 | check(to_camel_case, "CamelCase_", expect![[""]]); | ||
93 | check(to_camel_case, "_CamelCase", expect![[""]]); | ||
89 | check(to_camel_case, "lower_snake_case", expect![["LowerSnakeCase"]]); | 94 | check(to_camel_case, "lower_snake_case", expect![["LowerSnakeCase"]]); |
90 | check(to_camel_case, "UPPER_SNAKE_CASE", expect![["UpperSnakeCase"]]); | 95 | check(to_camel_case, "UPPER_SNAKE_CASE", expect![["UpperSnakeCase"]]); |
96 | check(to_camel_case, "Weird_Case", expect![["WeirdCase"]]); | ||
97 | check(to_camel_case, "name", expect![["Name"]]); | ||
91 | } | 98 | } |
92 | } | 99 | } |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 011935cad..522a9c1ab 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -32,8 +32,12 @@ pub fn to_lower_snake_case(s: &str) -> String { | |||
32 | let mut buf = String::with_capacity(s.len()); | 32 | let mut buf = String::with_capacity(s.len()); |
33 | let mut prev = false; | 33 | let mut prev = false; |
34 | for c in s.chars() { | 34 | for c in s.chars() { |
35 | // `&& prev` is required to not insert `_` before the first symbol. | ||
35 | if c.is_ascii_uppercase() && prev { | 36 | if c.is_ascii_uppercase() && prev { |
36 | buf.push('_') | 37 | // This check is required to not translate `Weird_Case` into `weird__case`. |
38 | if buf.chars().last() != Some('_') { | ||
39 | buf.push('_') | ||
40 | } | ||
37 | } | 41 | } |
38 | prev = true; | 42 | prev = true; |
39 | 43 | ||