move required_hashes into utils
This commit is contained in:
parent
03bc46f96b
commit
5e66e49c73
@ -5,11 +5,10 @@
|
|||||||
AstToken, TextRange,
|
AstToken, TextRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{
|
||||||
|
handlers::convert_comment_block::{line_comment_text, relevant_line_comments},
|
||||||
use super::{
|
utils::required_hashes,
|
||||||
convert_comment_block::{line_comment_text, relevant_line_comments},
|
AssistContext, AssistId, AssistKind, Assists,
|
||||||
raw_string::required_hashes,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Assist: desugar_doc_comment
|
// Assist: desugar_doc_comment
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use syntax::{ast, ast::IsString, AstToken, TextRange, TextSize};
|
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
|
// 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)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
|
||||||
|
|
||||||
use super::*;
|
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]
|
#[test]
|
||||||
fn make_raw_string_target() {
|
fn make_raw_string_target() {
|
||||||
check_assist_target(
|
check_assist_target(
|
||||||
|
@ -758,3 +758,25 @@ pub(crate) fn convert_param_list_to_arg_list(list: ast::ParamList) -> ast::ArgLi
|
|||||||
}
|
}
|
||||||
make::arg_list(args)
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user