Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(title-case): retain all characters from input #397

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions harper-core/src/title_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ pub fn make_title_case(toks: &[Token], source: &[char], dict: &impl Dictionary)
return Vec::new();
}

let start_index = toks.first().unwrap().span.start;

let mut words = toks.iter_word_likes().enumerate().peekable();
let mut output = toks.span().unwrap().get_content(source).to_vec();
let mut output = source.to_vec();

// Only specific conjunctions are not capitalized.
lazy_static! {
Expand Down Expand Up @@ -62,17 +60,16 @@ pub fn make_title_case(toks: &[Token], source: &[char], dict: &impl Dictionary)
|| words.peek().is_none();

if should_capitalize {
output[word.span.start - start_index] =
output[word.span.start - start_index].to_ascii_uppercase();
output[word.span.start] = output[word.span.start].to_ascii_uppercase();

// The rest of the word should be lowercase.
for v in &mut output[word.span.start + 1 - start_index..word.span.end - start_index] {
for v in &mut output[word.span.start + 1..word.span.end] {
*v = v.to_ascii_lowercase();
}
} else {
// The whole word should be lowercase.
for i in word.span {
output[i - start_index] = output[i].to_ascii_lowercase();
output[i] = output[i].to_ascii_lowercase();
}
}
}
Expand Down Expand Up @@ -120,6 +117,29 @@ mod tests {
)
}

#[test]
fn span_breaks_in_source() {
use super::make_title_case;
use crate::{CharStringExt, Span, Token, TokenKind, WordMetadata};
use itertools::Itertools;

let source = "// linted
// linted";
let source = source.chars().collect_vec();

let toks = &[
Token::new(Span::new(3, 9), TokenKind::Word(WordMetadata::default())),
Token::new(Span::new(9, 10), TokenKind::Newline(1)),
Token::new(Span::new(13, 19), TokenKind::Word(WordMetadata::default())),
];

assert_eq!(
make_title_case(toks, &source, &FstDictionary::curated()).to_string(),
"// Linted
// Linted"
)
}

#[derive(Debug, Clone)]
struct Word(String);

Expand Down
Loading