move required_hashes into utils

This commit is contained in:
Roland Fredenhagen 2023-01-16 18:39:16 +01:00
parent 03bc46f96b
commit 5e66e49c73
No known key found for this signature in database
GPG Key ID: 094AF99241035EB6
3 changed files with 27 additions and 27 deletions

View File

@ -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

View File

@ -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(

View File

@ -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
}