mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
replace heck with just ascii transformation
This commit is contained in:
parent
8704a74cd5
commit
58e9d7ef6f
3 changed files with 33 additions and 8 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1290,7 +1290,6 @@ name = "ra_tools"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"proc-macro2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -12,5 +12,4 @@ clap = "2.32.0"
|
||||||
quote = "1.0.2"
|
quote = "1.0.2"
|
||||||
proc-macro2 = "1.0.1"
|
proc-macro2 = "1.0.1"
|
||||||
ron = "0.5.1"
|
ron = "0.5.1"
|
||||||
heck = "0.3.0"
|
|
||||||
serde = { version = "1.0.0", features = ["derive"] }
|
serde = { version = "1.0.0", features = ["derive"] }
|
||||||
|
|
|
@ -5,7 +5,6 @@ use std::{
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
use heck::{ShoutySnakeCase, SnakeCase};
|
|
||||||
use proc_macro2::{Punct, Spacing};
|
use proc_macro2::{Punct, Spacing};
|
||||||
use quote::{format_ident, quote};
|
use quote::{format_ident, quote};
|
||||||
use ron;
|
use ron;
|
||||||
|
@ -38,7 +37,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
|
||||||
let name = format_ident!("{}", name);
|
let name = format_ident!("{}", name);
|
||||||
|
|
||||||
let adt = if variants.is_empty() {
|
let adt = if variants.is_empty() {
|
||||||
let kind = format_ident!("{}", name.to_string().to_shouty_snake_case());
|
let kind = format_ident!("{}", to_upper_snake_case(&name.to_string()));
|
||||||
quote! {
|
quote! {
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub struct #name {
|
pub struct #name {
|
||||||
|
@ -61,7 +60,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
|
||||||
} else {
|
} else {
|
||||||
let kinds = variants
|
let kinds = variants
|
||||||
.iter()
|
.iter()
|
||||||
.map(|name| format_ident!("{}", name.to_string().to_shouty_snake_case()))
|
.map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
quote! {
|
quote! {
|
||||||
|
@ -122,7 +121,7 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
|
||||||
|
|
||||||
let options = ast_node.options.iter().map(|attr| {
|
let options = ast_node.options.iter().map(|attr| {
|
||||||
let method_name = match attr {
|
let method_name = match attr {
|
||||||
Attr::Type(t) => format_ident!("{}", t.to_snake_case()),
|
Attr::Type(t) => format_ident!("{}", to_lower_snake_case(&t)),
|
||||||
Attr::NameType(n, _) => format_ident!("{}", n),
|
Attr::NameType(n, _) => format_ident!("{}", n),
|
||||||
};
|
};
|
||||||
let ty = match attr {
|
let ty = match attr {
|
||||||
|
@ -189,14 +188,14 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
|
||||||
|
|
||||||
let full_keywords_values = &grammar.keywords;
|
let full_keywords_values = &grammar.keywords;
|
||||||
let full_keywords =
|
let full_keywords =
|
||||||
full_keywords_values.iter().map(|kw| format_ident!("{}_KW", kw.to_shouty_snake_case()));
|
full_keywords_values.iter().map(|kw| format_ident!("{}_KW", to_upper_snake_case(&kw)));
|
||||||
|
|
||||||
let all_keywords_values =
|
let all_keywords_values =
|
||||||
grammar.keywords.iter().chain(grammar.contextual_keywords.iter()).collect::<Vec<_>>();
|
grammar.keywords.iter().chain(grammar.contextual_keywords.iter()).collect::<Vec<_>>();
|
||||||
let all_keywords_idents = all_keywords_values.iter().map(|kw| format_ident!("{}", kw));
|
let all_keywords_idents = all_keywords_values.iter().map(|kw| format_ident!("{}", kw));
|
||||||
let all_keywords = all_keywords_values
|
let all_keywords = all_keywords_values
|
||||||
.iter()
|
.iter()
|
||||||
.map(|name| format_ident!("{}_KW", name.to_shouty_snake_case()))
|
.map(|name| format_ident!("{}_KW", to_upper_snake_case(&name)))
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
let literals =
|
let literals =
|
||||||
|
@ -325,3 +324,31 @@ enum Attr {
|
||||||
Type(String),
|
Type(String),
|
||||||
NameType(String, String),
|
NameType(String, String),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn to_upper_snake_case(s: &str) -> String {
|
||||||
|
let mut buf = String::with_capacity(s.len());
|
||||||
|
let mut prev_is_upper = None;
|
||||||
|
for c in s.chars() {
|
||||||
|
if c.is_ascii_uppercase() && prev_is_upper == Some(false) {
|
||||||
|
buf.push('_')
|
||||||
|
}
|
||||||
|
prev_is_upper = Some(c.is_ascii_uppercase());
|
||||||
|
|
||||||
|
buf.push(c.to_ascii_uppercase());
|
||||||
|
}
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_lower_snake_case(s: &str) -> String {
|
||||||
|
let mut buf = String::with_capacity(s.len());
|
||||||
|
let mut prev_is_upper = None;
|
||||||
|
for c in s.chars() {
|
||||||
|
if c.is_ascii_uppercase() && prev_is_upper == Some(false) {
|
||||||
|
buf.push('_')
|
||||||
|
}
|
||||||
|
prev_is_upper = Some(c.is_ascii_uppercase());
|
||||||
|
|
||||||
|
buf.push(c.to_ascii_lowercase());
|
||||||
|
}
|
||||||
|
buf
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue