aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_tools/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_tools/src')
-rw-r--r--crates/ra_tools/src/boilerplate_gen.rs39
1 files changed, 33 insertions, 6 deletions
diff --git a/crates/ra_tools/src/boilerplate_gen.rs b/crates/ra_tools/src/boilerplate_gen.rs
index 01d092d48..578f13a3e 100644
--- a/crates/ra_tools/src/boilerplate_gen.rs
+++ b/crates/ra_tools/src/boilerplate_gen.rs
@@ -5,7 +5,6 @@ use std::{
5 process::{Command, Stdio}, 5 process::{Command, Stdio},
6}; 6};
7 7
8use heck::{ShoutySnakeCase, SnakeCase};
9use proc_macro2::{Punct, Spacing}; 8use proc_macro2::{Punct, Spacing};
10use quote::{format_ident, quote}; 9use quote::{format_ident, quote};
11use ron; 10use ron;
@@ -38,7 +37,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
38 let name = format_ident!("{}", name); 37 let name = format_ident!("{}", name);
39 38
40 let adt = if variants.is_empty() { 39 let adt = if variants.is_empty() {
41 let kind = format_ident!("{}", name.to_string().to_shouty_snake_case()); 40 let kind = format_ident!("{}", to_upper_snake_case(&name.to_string()));
42 quote! { 41 quote! {
43 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 42 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
44 pub struct #name { 43 pub struct #name {
@@ -61,7 +60,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
61 } else { 60 } else {
62 let kinds = variants 61 let kinds = variants
63 .iter() 62 .iter()
64 .map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case())) 63 .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))
65 .collect::<Vec<_>>(); 64 .collect::<Vec<_>>();
66 65
67 quote! { 66 quote! {
@@ -122,7 +121,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
122 121
123 let options = ast_node.options.iter().map(|attr| { 122 let options = ast_node.options.iter().map(|attr| {
124 let method_name = match attr { 123 let method_name = match attr {
125 Attr::Type(t) => format_ident!("{}", t.to_snake_case()), 124 Attr::Type(t) => format_ident!("{}", to_lower_snake_case(&t)),
126 Attr::NameType(n, _) => format_ident!("{}", n), 125 Attr::NameType(n, _) => format_ident!("{}", n),
127 }; 126 };
128 let ty = match attr { 127 let ty = match attr {
@@ -189,14 +188,14 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
189 188
190 let full_keywords_values = &grammar.keywords; 189 let full_keywords_values = &grammar.keywords;
191 let full_keywords = 190 let full_keywords =
192 full_keywords_values.iter().map(|kw| format_ident!("{}_KW", kw.to_shouty_snake_case())); 191 full_keywords_values.iter().map(|kw| format_ident!("{}_KW", to_upper_snake_case(&kw)));
193 192
194 let all_keywords_values = 193 let all_keywords_values =
195 grammar.keywords.iter().chain(grammar.contextual_keywords.iter()).collect::<Vec<_>>(); 194 grammar.keywords.iter().chain(grammar.contextual_keywords.iter()).collect::<Vec<_>>();
196 let all_keywords_idents = all_keywords_values.iter().map(|kw| format_ident!("{}", kw)); 195 let all_keywords_idents = all_keywords_values.iter().map(|kw| format_ident!("{}", kw));
197 let all_keywords = all_keywords_values 196 let all_keywords = all_keywords_values
198 .iter() 197 .iter()
199 .map(|name| format_ident!("{}_KW", name.to_shouty_snake_case())) 198 .map(|name| format_ident!("{}_KW", to_upper_snake_case(&name)))
200 .collect::<Vec<_>>(); 199 .collect::<Vec<_>>();
201 200
202 let literals = 201 let literals =
@@ -325,3 +324,31 @@ enum Attr {
325 Type(String), 324 Type(String),
326 NameType(String, String), 325 NameType(String, String),
327} 326}
327
328fn to_upper_snake_case(s: &str) -> String {
329 let mut buf = String::with_capacity(s.len());
330 let mut prev_is_upper = None;
331 for c in s.chars() {
332 if c.is_ascii_uppercase() && prev_is_upper == Some(false) {
333 buf.push('_')
334 }
335 prev_is_upper = Some(c.is_ascii_uppercase());
336
337 buf.push(c.to_ascii_uppercase());
338 }
339 buf
340}
341
342fn to_lower_snake_case(s: &str) -> String {
343 let mut buf = String::with_capacity(s.len());
344 let mut prev_is_upper = None;
345 for c in s.chars() {
346 if c.is_ascii_uppercase() && prev_is_upper == Some(false) {
347 buf.push('_')
348 }
349 prev_is_upper = Some(c.is_ascii_uppercase());
350
351 buf.push(c.to_ascii_lowercase());
352 }
353 buf
354}