Unnamed repository; edit this file 'description' to name the repository.
fix: restrict whitespace handling to Rust Pattern_White_Space
vikashsiwach 3 weeks ago
parent f22fe44 · commit d420641
-rw-r--r--crates/parser/src/frontmatter.rs2
-rw-r--r--crates/parser/src/lib.rs7
-rw-r--r--crates/rust-analyzer/src/cli/analysis_stats.rs11
3 files changed, 14 insertions, 6 deletions
diff --git a/crates/parser/src/frontmatter.rs b/crates/parser/src/frontmatter.rs
index 2747db4327..1edcad64cf 100644
--- a/crates/parser/src/frontmatter.rs
+++ b/crates/parser/src/frontmatter.rs
@@ -263,7 +263,7 @@ pub fn strip_ws_lines(input: &str) -> Option<usize> {
/// True if `c` is considered a whitespace according to Rust language definition.
/// See [Rust language reference](https://doc.rust-lang.org/reference/whitespace.html)
/// for definitions of these classes.
-fn is_whitespace(c: char) -> bool {
+pub(crate) fn is_whitespace(c: char) -> bool {
// This is Pattern_White_Space.
//
// Note that this set is stable (ie, it doesn't change with different
diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs
index 4478bf4e37..c94df0a4ec 100644
--- a/crates/parser/src/lib.rs
+++ b/crates/parser/src/lib.rs
@@ -56,6 +56,13 @@ pub use crate::{
syntax_kind::SyntaxKind,
};
+/// True if `c` is whitespace in Rust source (Unicode Pattern_White_Space as in the language reference).
+///
+/// See <https://doc.rust-lang.org/reference/whitespace.html>.
+pub fn is_rust_whitespace(c: char) -> bool {
+ frontmatter::is_whitespace(c)
+}
+
/// Parse the whole of the input as a given syntactic construct.
///
/// This covers two main use-cases:
diff --git a/crates/rust-analyzer/src/cli/analysis_stats.rs b/crates/rust-analyzer/src/cli/analysis_stats.rs
index e56727d39d..246c2b6baf 100644
--- a/crates/rust-analyzer/src/cli/analysis_stats.rs
+++ b/crates/rust-analyzer/src/cli/analysis_stats.rs
@@ -587,8 +587,8 @@ impl flags::AnalysisStats {
continue;
};
- fn trim(s: &str) -> String {
- s.chars().filter(|c| !c.is_whitespace()).collect()
+ fn drop_whitespace(s: &str) -> String {
+ s.chars().filter(|c| !parser::is_rust_whitespace(*c)).collect()
}
let todo = syntax::ast::make::ext::expr_todo().to_string();
@@ -608,7 +608,8 @@ impl flags::AnalysisStats {
display_target,
)
.unwrap();
- syntax_hit_found |= trim(&original_text) == trim(&generated);
+ syntax_hit_found |=
+ drop_whitespace(&original_text) == drop_whitespace(&generated);
// Validate if type-checks
let mut txt = file_txt.text(db).to_string();
@@ -662,7 +663,7 @@ impl flags::AnalysisStats {
let msg = move || {
format!(
"processing: {:<50}",
- trim(&original_text).chars().take(50).collect::<String>()
+ drop_whitespace(&original_text).chars().take(50).collect::<String>()
)
};
if verbosity.is_spammy() {
@@ -1646,5 +1647,5 @@ impl fmt::Display for PrettyItemStats {
// fn syntax_len(node: SyntaxNode) -> usize {
// // Macro expanded code doesn't contain whitespace, so erase *all* whitespace
// // to make macro and non-macro code comparable.
-// node.to_string().replace(|it: char| it.is_ascii_whitespace(), "").len()
+// drop_whitespace(&node.to_string()).len()
// }