Skip to content

Commit

Permalink
Tune output from <git show 57f27da>
Browse files Browse the repository at this point in the history
  • Loading branch information
walles committed Oct 1, 2024
1 parent e19176a commit acf99a8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 44 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,9 @@ If you want to test the release script without actually releasing anything, do:
- Render ESC characters in the diff as Unicode ␛
- `--help`: Only print installing-into-`$PATH` help if we aren't already being
executed from inside of the `$PATH`
- Do `git show 57f27da` and think about what rule we should use to get
the REVERSE vs reversed() lines highlighted.
- Add test for never changing the number of lines in the input, that
messes up `git add -p` behavior.
- Think about how to visualize an added line break together with some
indentation on the following line.
- Make sure we highlight the output of `git show --stat` properly
- Make sure we can handle a `git` conflict
resolution diff. File format is described at
http://git-scm.com/docs/git-diff#_combined_diff_format.
- Given three files on the command line, we should pass them and any
options on to `diff3` and highlight the result

Expand Down Expand Up @@ -235,3 +228,10 @@ If you want to test the release script without actually releasing anything, do:
- Do `git show -b 77c8f77` and think about what rule we should use to
highlight the leading spaces of the `+ refined` and `+ page` lines
at the end of the file.
- Do `git show 57f27da` and think about what rule we should use to get
the REVERSE vs reversed() lines highlighted.
- Think about how to visualize an added line break together with some
indentation on the following line.
- Make sure we can handle a `git` conflict
resolution diff. File format is described at
http://git-scm.com/docs/git-diff#_combined_diff_format.
67 changes: 30 additions & 37 deletions src/token_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,15 +414,27 @@ pub fn contextualize_unhighlighted_lines(tokens: &mut [StyledToken]) {

/// Highlight single space between two highlighted tokens
pub fn bridge_consecutive_highlighted_tokens(tokens: &mut [StyledToken]) {
fn bridgable(candidate: &StyledToken) -> bool {
if candidate.style != Style::Plain {
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].token.len() == 1
&& tokens[i].token != "\n"
&& tokens[i].style == Style::Plain
&& tokens[i - 1].style == Style::Highlighted
&& tokens[i + 1].style == Style::Highlighted
{
tokens[i].style = Style::Highlighted;
if tokens[i - 1].style != Style::Highlighted || tokens[i + 1].style != Style::Highlighted {
continue;
}
if bridgable(&tokens[i - 1]) || !bridgable(&tokens[i]) || bridgable(&tokens[i + 1]) {
continue;
}

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

Expand Down Expand Up @@ -688,44 +700,25 @@ mod tests {
assert_eq!(actual, format!("{OLD}-x\t{NORMAL}"));
}

#[test]
fn test_highlight_space_between_words() {
fn is_char_bridged(before: char, victim: char, after: char) -> bool {
let mut row = [
StyledToken::new("Monkey".to_string(), Style::Highlighted),
StyledToken::new(" ".to_string(), Style::Plain),
StyledToken::new("Dance".to_string(), Style::Highlighted),
StyledToken::new(before.to_string(), Style::Highlighted),
StyledToken::new(victim.to_string(), Style::Plain),
StyledToken::new(after.to_string(), Style::Highlighted),
];

bridge_consecutive_highlighted_tokens(&mut row);

assert_eq!(
row,
[
StyledToken::new("Monkey".to_string(), Style::Highlighted),
StyledToken::new(" ".to_string(), Style::Highlighted),
StyledToken::new("Dance".to_string(), Style::Highlighted),
]
);
return row[1].style == Style::Highlighted;
}

#[test]
fn test_highlight_space_between_random_chars() {
let mut row = [
StyledToken::new(">".to_string(), Style::Highlighted),
StyledToken::new(" ".to_string(), Style::Plain),
StyledToken::new("5".to_string(), Style::Highlighted),
];

bridge_consecutive_highlighted_tokens(&mut row);

assert_eq!(
row,
[
StyledToken::new(">".to_string(), Style::Highlighted),
StyledToken::new(" ".to_string(), Style::Highlighted),
StyledToken::new("5".to_string(), Style::Highlighted),
]
);
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]
Expand Down

0 comments on commit acf99a8

Please sign in to comment.