Skip to content

Commit

Permalink
Move functions to where they are used
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 23, 2024
1 parent 30b6018 commit b03acb6
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 222 deletions.
222 changes: 222 additions & 0 deletions src/refiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,69 @@ pub fn to_highlighted_tokens(
return (old_tokens, new_tokens);
}

/// Highlight single space between two highlighted tokens
fn bridge_consecutive_highlighted_tokens(tokens: &mut [StyledToken]) {
fn bridgable(candidate: &StyledToken) -> bool {
if candidate.style as u8 > Style::DiffPartUnchanged as u8 {
return false;
}
if candidate.token.len() != 1 {
return false;
}

let rune = candidate.token.chars().next().unwrap();
return rune == ' ' || rune.is_ascii_punctuation();
}

for i in 1..(tokens.len() - 1) {
if tokens[i - 1].style != Style::DiffPartHighlighted
|| tokens[i + 1].style != Style::DiffPartHighlighted
{
continue;
}
if bridgable(&tokens[i - 1]) || !bridgable(&tokens[i]) || bridgable(&tokens[i + 1]) {
continue;
}

tokens[i].style = Style::DiffPartHighlighted;
}
}

fn errorlight_trailing_whitespace(tokens: &mut [StyledToken]) {
let mut in_trailer = true;
for token in tokens.iter_mut().rev() {
if token.token == "\n" {
in_trailer = true;
continue;
}

if in_trailer && token.is_whitespace() {
token.style = Style::Error;
continue;
}

in_trailer = false;
}
}

fn errorlight_nonleading_tabs(tokens: &mut [StyledToken]) {
let mut leading = true;
for token in tokens.iter_mut() {
if token.token == "\n" {
leading = true;
continue;
}

if token.token != "\t" {
leading = false;
}

if token.token == "\t" && !leading {
token.style = Style::Error;
}
}
}

/// Splits text into lines. If the text doesn't end in a newline, a no-newline
/// marker will be added at the end.
#[must_use]
Expand Down Expand Up @@ -539,4 +602,163 @@ pub(crate) mod tests {
]
);
}

fn is_char_bridged(before: char, victim: char, after: char) -> bool {
let mut row = [
StyledToken::new(before.to_string(), Style::DiffPartHighlighted),
StyledToken::new(victim.to_string(), Style::DiffPartUnchanged),
StyledToken::new(after.to_string(), Style::DiffPartHighlighted),
];

bridge_consecutive_highlighted_tokens(&mut row);

return row[1].style == Style::DiffPartHighlighted;
}

#[test]
fn test_bridge_consecutive_highlighted_tokens() {
assert!(is_char_bridged('a', ' ', 'b'));
assert!(is_char_bridged('>', ' ', '5'));
assert!(is_char_bridged('a', ' ', ' ')); // Because the second space is highlighted
assert!(!is_char_bridged('\'', '1', '\''));
assert!(is_char_bridged('a', '.', 'b')); // Bridge separators
}

#[test]
fn test_four_tokens_highlighting() {
let mut row = [
StyledToken::new("\n".to_string(), Style::DiffPartHighlighted),
StyledToken::new("*".to_string(), Style::DiffPartHighlighted),
StyledToken::new(" ".to_string(), Style::DiffPartUnchanged),
StyledToken::new("Hello".to_string(), Style::DiffPartHighlighted),
];

bridge_consecutive_highlighted_tokens(&mut row);

assert_eq!(
row,
[
StyledToken::new("\n".to_string(), Style::DiffPartHighlighted),
StyledToken::new("*".to_string(), Style::DiffPartHighlighted),
StyledToken::new(" ".to_string(), Style::DiffPartHighlighted),
StyledToken::new("Hello".to_string(), Style::DiffPartHighlighted),
]
);
}

#[test]
fn test_errorlight_nonleading_tabs() {
let mut tokens = vec![
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("one".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\n".to_string(), Style::DiffPartUnchanged),
StyledToken::new("two".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
];
errorlight_nonleading_tabs(&mut tokens);

assert_eq!(
tokens,
vec![
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("one".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\n".to_string(), Style::DiffPartUnchanged),
StyledToken::new("two".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::Error),
]
);
}

#[test]
fn test_errorlight_trailing_whitespace() {
// Just a whitespace
let mut row = [StyledToken::new(" ".to_string(), Style::DiffPartUnchanged)];
errorlight_trailing_whitespace(&mut row);
assert_eq!(row, [StyledToken::new(" ".to_string(), Style::Error)]);

// Trailing whitespace
let mut row = [
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
StyledToken::new(" ".to_string(), Style::DiffPartUnchanged),
];
errorlight_trailing_whitespace(&mut row);
assert_eq!(
row,
[
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
StyledToken::new(" ".to_string(), Style::Error),
]
);

// Leading whitespace
let mut row = [
StyledToken::new(" ".to_string(), Style::DiffPartUnchanged),
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
];
errorlight_trailing_whitespace(&mut row);
assert_eq!(
row,
[
StyledToken::new(" ".to_string(), Style::DiffPartUnchanged),
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
]
);
}

#[test]
fn test_add_nonleading_tab() {
// Trailing TAB
let mut row = [
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
];
errorlight_nonleading_tabs(&mut row);
assert_eq!(
row,
[
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::Error),
]
);

// Middle TAB
let mut row = [
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("y".to_string(), Style::DiffPartUnchanged),
];
errorlight_nonleading_tabs(&mut row);
assert_eq!(
row,
[
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
StyledToken::new("\t".to_string(), Style::Error),
StyledToken::new("y".to_string(), Style::DiffPartUnchanged),
]
);

// Leading TAB (don't highlight)
let mut row = [
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
];
errorlight_nonleading_tabs(&mut row);
assert_eq!(
row,
[
StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),
StyledToken::new("x".to_string(), Style::DiffPartUnchanged),
]
);

// Single TAB (don't highlight because it is leading)
let mut row = [StyledToken::new("\t".to_string(), Style::DiffPartUnchanged)];
errorlight_nonleading_tabs(&mut row);
assert_eq!(
row,
[StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),]
);
}
}
Loading

0 comments on commit b03acb6

Please sign in to comment.