mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
Extract helper functions into a separate module
This commit is contained in:
parent
f5cea35986
commit
1773c6d154
2 changed files with 113 additions and 30 deletions
|
@ -9,6 +9,8 @@
|
||||||
// If you see these lines in the pull request, feel free to call me stupid :P.
|
// If you see these lines in the pull request, feel free to call me stupid :P.
|
||||||
#![allow(dead_code, unused_imports, unused_variables)]
|
#![allow(dead_code, unused_imports, unused_variables)]
|
||||||
|
|
||||||
|
mod str_helpers;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
|
@ -18,7 +20,7 @@ use hir_def::{
|
||||||
item_tree::ItemTreeNode,
|
item_tree::ItemTreeNode,
|
||||||
resolver::{resolver_for_expr, ResolveValueResult, ValueNs},
|
resolver::{resolver_for_expr, ResolveValueResult, ValueNs},
|
||||||
src::HasSource,
|
src::HasSource,
|
||||||
AdtId, FunctionId, Lookup, ModuleDefId,
|
AdtId, EnumId, FunctionId, Lookup, ModuleDefId, StructId,
|
||||||
};
|
};
|
||||||
use hir_expand::{diagnostics::DiagnosticSink, name::Name};
|
use hir_expand::{diagnostics::DiagnosticSink, name::Name};
|
||||||
use syntax::{
|
use syntax::{
|
||||||
|
@ -28,7 +30,7 @@ use syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::HirDatabase,
|
db::HirDatabase,
|
||||||
diagnostics::{CaseType, IncorrectCase},
|
diagnostics::{decl_check::str_helpers::*, CaseType, IncorrectCase},
|
||||||
lower::CallableDefId,
|
lower::CallableDefId,
|
||||||
ApplicationTy, InferenceResult, Ty, TypeCtor,
|
ApplicationTy, InferenceResult, Ty, TypeCtor,
|
||||||
};
|
};
|
||||||
|
@ -191,7 +193,23 @@ impl<'a, 'b> DeclValidator<'a, 'b> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_adt(&mut self, db: &dyn HirDatabase, adt: AdtId) {}
|
fn validate_adt(&mut self, db: &dyn HirDatabase, adt: AdtId) {
|
||||||
|
match adt {
|
||||||
|
AdtId::StructId(struct_id) => self.validate_struct(db, struct_id),
|
||||||
|
AdtId::EnumId(enum_id) => self.validate_enum(db, enum_id),
|
||||||
|
AdtId::UnionId(_) => {
|
||||||
|
// Unions aren't yet supported by this validator.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_struct(&mut self, db: &dyn HirDatabase, struct_id: StructId) {
|
||||||
|
let data = db.struct_data(struct_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn validate_enum(&mut self, db: &dyn HirDatabase, enum_id: EnumId) {
|
||||||
|
let data = db.enum_data(enum_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool {
|
fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool {
|
||||||
|
@ -202,33 +220,6 @@ fn pat_equals_to_name(pat: Option<ast::Pat>, name: &Name) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_lower_snake_case(ident: &str) -> Option<String> {
|
|
||||||
// First, assume that it's UPPER_SNAKE_CASE.
|
|
||||||
if let Some(normalized) = to_lower_snake_case_from_upper_snake_case(ident) {
|
|
||||||
return Some(normalized);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, assume that it's CamelCase.
|
|
||||||
let lower_snake_case = stdx::to_lower_snake_case(ident);
|
|
||||||
|
|
||||||
if lower_snake_case == ident {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(lower_snake_case)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn to_lower_snake_case_from_upper_snake_case(ident: &str) -> Option<String> {
|
|
||||||
let is_upper_snake_case = ident.chars().all(|c| c.is_ascii_uppercase() || c == '_');
|
|
||||||
|
|
||||||
if is_upper_snake_case {
|
|
||||||
let string = ident.chars().map(|c| c.to_ascii_lowercase()).collect();
|
|
||||||
Some(string)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::diagnostics::tests::check_diagnostics;
|
use crate::diagnostics::tests::check_diagnostics;
|
||||||
|
|
92
crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs
Normal file
92
crates/hir_ty/src/diagnostics/decl_check/str_helpers.rs
Normal file
|
@ -0,0 +1,92 @@
|
||||||
|
pub fn to_camel_case(ident: &str) -> Option<String> {
|
||||||
|
let mut output = String::new();
|
||||||
|
|
||||||
|
if is_camel_case(ident) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut capital_added = false;
|
||||||
|
for chr in ident.chars() {
|
||||||
|
if chr.is_alphabetic() {
|
||||||
|
if !capital_added {
|
||||||
|
output.push(chr.to_ascii_uppercase());
|
||||||
|
capital_added = true;
|
||||||
|
} else {
|
||||||
|
output.push(chr.to_ascii_lowercase());
|
||||||
|
}
|
||||||
|
} else if chr == '_' {
|
||||||
|
// Skip this character and make the next one capital.
|
||||||
|
capital_added = false;
|
||||||
|
} else {
|
||||||
|
// Put the characted as-is.
|
||||||
|
output.push(chr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if output == ident {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(output)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_lower_snake_case(ident: &str) -> Option<String> {
|
||||||
|
// First, assume that it's UPPER_SNAKE_CASE.
|
||||||
|
if let Some(normalized) = to_lower_snake_case_from_upper_snake_case(ident) {
|
||||||
|
return Some(normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, assume that it's CamelCase.
|
||||||
|
let lower_snake_case = stdx::to_lower_snake_case(ident);
|
||||||
|
|
||||||
|
if lower_snake_case == ident {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(lower_snake_case)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_lower_snake_case_from_upper_snake_case(ident: &str) -> Option<String> {
|
||||||
|
if is_upper_snake_case(ident) {
|
||||||
|
let string = ident.chars().map(|c| c.to_ascii_lowercase()).collect();
|
||||||
|
Some(string)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_upper_snake_case(ident: &str) -> bool {
|
||||||
|
ident.chars().all(|c| c.is_ascii_uppercase() || c == '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_camel_case(ident: &str) -> bool {
|
||||||
|
// We assume that the string is either snake case or camel case.
|
||||||
|
ident.chars().all(|c| c != '_')
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use expect_test::{expect, Expect};
|
||||||
|
|
||||||
|
fn check<F: Fn(&str) -> Option<String>>(fun: F, input: &str, expect: Expect) {
|
||||||
|
// `None` is translated to empty string, meaning that there is nothing to fix.
|
||||||
|
let output = fun(input).unwrap_or_default();
|
||||||
|
|
||||||
|
expect.assert_eq(&output);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_to_lower_snake_case() {
|
||||||
|
check(to_lower_snake_case, "lower_snake_case", expect![[""]]);
|
||||||
|
check(to_lower_snake_case, "UPPER_SNAKE_CASE", expect![["upper_snake_case"]]);
|
||||||
|
check(to_lower_snake_case, "CamelCase", expect![["camel_case"]]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_to_camel_case() {
|
||||||
|
check(to_camel_case, "CamelCase", expect![[""]]);
|
||||||
|
check(to_camel_case, "lower_snake_case", expect![["LowerSnakeCase"]]);
|
||||||
|
check(to_camel_case, "UPPER_SNAKE_CASE", expect![["UpperSnakeCase"]]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue