diff --git a/crates/ide-assists/src/handlers/desugar_doc_comment.rs b/crates/ide-assists/src/handlers/desugar_doc_comment.rs index ea7044a5445..226a5dd9fa8 100644 --- a/crates/ide-assists/src/handlers/desugar_doc_comment.rs +++ b/crates/ide-assists/src/handlers/desugar_doc_comment.rs @@ -5,11 +5,10 @@ AstToken, TextRange, }; -use crate::{AssistContext, AssistId, AssistKind, Assists}; - -use super::{ - convert_comment_block::{line_comment_text, relevant_line_comments}, - raw_string::required_hashes, +use crate::{ + handlers::convert_comment_block::{line_comment_text, relevant_line_comments}, + utils::required_hashes, + AssistContext, AssistId, AssistKind, Assists, }; // Assist: desugar_doc_comment diff --git a/crates/ide-assists/src/handlers/raw_string.rs b/crates/ide-assists/src/handlers/raw_string.rs index b1b3bab7e51..01420430bb4 100644 --- a/crates/ide-assists/src/handlers/raw_string.rs +++ b/crates/ide-assists/src/handlers/raw_string.rs @@ -2,7 +2,7 @@ use syntax::{ast, ast::IsString, AstToken, TextRange, TextSize}; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{utils::required_hashes, AssistContext, AssistId, AssistKind, Assists}; // Assist: make_raw_string // @@ -155,33 +155,12 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option< }) } -pub(crate) fn required_hashes(s: &str) -> usize { - let mut res = 0usize; - for idx in s.match_indices('"').map(|(i, _)| i) { - let (_, sub) = s.split_at(idx + 1); - let n_hashes = sub.chars().take_while(|c| *c == '#').count(); - res = res.max(n_hashes + 1) - } - res -} - #[cfg(test)] mod tests { use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target}; use super::*; - #[test] - fn test_required_hashes() { - assert_eq!(0, required_hashes("abc")); - assert_eq!(0, required_hashes("###")); - assert_eq!(1, required_hashes("\"")); - assert_eq!(2, required_hashes("\"#abc")); - assert_eq!(0, required_hashes("#abc")); - assert_eq!(3, required_hashes("#ab\"##c")); - assert_eq!(5, required_hashes("#ab\"##\"####c")); - } - #[test] fn make_raw_string_target() { check_assist_target( diff --git a/crates/ide-assists/src/utils.rs b/crates/ide-assists/src/utils.rs index 7add6606492..8ed2b5239bc 100644 --- a/crates/ide-assists/src/utils.rs +++ b/crates/ide-assists/src/utils.rs @@ -758,3 +758,25 @@ pub(crate) fn convert_param_list_to_arg_list(list: ast::ParamList) -> ast::ArgLi } make::arg_list(args) } + +/// Calculate the number of hashes required for a raw string containing `s` +pub(crate) fn required_hashes(s: &str) -> usize { + #[test] + fn test_required_hashes() { + assert_eq!(0, required_hashes("abc")); + assert_eq!(0, required_hashes("###")); + assert_eq!(1, required_hashes("\"")); + assert_eq!(2, required_hashes("\"#abc")); + assert_eq!(0, required_hashes("#abc")); + assert_eq!(3, required_hashes("#ab\"##c")); + assert_eq!(5, required_hashes("#ab\"##\"####c")); + } + + let mut res = 0usize; + for idx in s.match_indices('"').map(|(i, _)| i) { + let (_, sub) = s.split_at(idx + 1); + let n_hashes = sub.chars().take_while(|c| *c == '#').count(); + res = res.max(n_hashes + 1) + } + res +}