|
| 1 | +//! Functions and tests that determine and apply the user defined [NewlineStyle]. |
| 2 | +use crate::{ |
| 3 | + config::whitespace::{NewlineStyle, NewlineSystemType}, |
| 4 | + constants::{CARRIAGE_RETURN, LINE_FEED, UNIX_NEWLINE, WINDOWS_NEWLINE}, |
| 5 | +}; |
| 6 | + |
| 7 | +/// Apply this newline style to the formatted text. When the style is set |
| 8 | +/// to `Auto`, the `raw_input_text` is used to detect the existing line |
| 9 | +/// endings. |
| 10 | +/// |
| 11 | +/// If the style is set to `Auto` and `raw_input_text` contains no |
| 12 | +/// newlines, the `Native` style will be used. |
| 13 | +pub(crate) fn apply_newline_style( |
| 14 | + newline_style: NewlineStyle, |
| 15 | + formatted_text: &mut String, |
| 16 | + raw_input_text: &str, |
| 17 | +) { |
| 18 | + *formatted_text = match NewlineSystemType::get_newline_style(newline_style, raw_input_text) { |
| 19 | + NewlineSystemType::Windows => convert_to_windows_newlines(formatted_text), |
| 20 | + NewlineSystemType::Unix => convert_to_unix_newlines(formatted_text), |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +fn convert_to_windows_newlines(formatted_text: &String) -> String { |
| 25 | + let mut transformed = String::with_capacity(2 * formatted_text.capacity()); |
| 26 | + let mut chars = formatted_text.chars().peekable(); |
| 27 | + while let Some(current_char) = chars.next() { |
| 28 | + let next_char = chars.peek(); |
| 29 | + match current_char { |
| 30 | + LINE_FEED => transformed.push_str(WINDOWS_NEWLINE), |
| 31 | + CARRIAGE_RETURN if next_char == Some(&LINE_FEED) => {} |
| 32 | + current_char => transformed.push(current_char), |
| 33 | + } |
| 34 | + } |
| 35 | + transformed |
| 36 | +} |
| 37 | + |
| 38 | +fn convert_to_unix_newlines(formatted_text: &str) -> String { |
| 39 | + formatted_text.replace(WINDOWS_NEWLINE, UNIX_NEWLINE) |
| 40 | +} |
| 41 | + |
| 42 | +#[cfg(test)] |
| 43 | +mod tests { |
| 44 | + use super::*; |
| 45 | + |
| 46 | + #[test] |
| 47 | + fn auto_detects_unix_newlines() { |
| 48 | + assert_eq!( |
| 49 | + NewlineSystemType::Unix, |
| 50 | + NewlineSystemType::auto_detect_newline_style("One\nTwo\nThree") |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + #[test] |
| 55 | + fn auto_detects_windows_newlines() { |
| 56 | + assert_eq!( |
| 57 | + NewlineSystemType::Windows, |
| 58 | + NewlineSystemType::auto_detect_newline_style("One\r\nTwo\r\nThree") |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + #[test] |
| 63 | + fn auto_detects_windows_newlines_with_multibyte_char_on_first_line() { |
| 64 | + assert_eq!( |
| 65 | + NewlineSystemType::Windows, |
| 66 | + NewlineSystemType::auto_detect_newline_style("A 🎢 of a first line\r\nTwo\r\nThree") |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn falls_back_to_native_newlines_if_no_newlines_are_found() { |
| 72 | + let expected_newline_style = if cfg!(windows) { |
| 73 | + NewlineSystemType::Windows |
| 74 | + } else { |
| 75 | + NewlineSystemType::Unix |
| 76 | + }; |
| 77 | + assert_eq!( |
| 78 | + expected_newline_style, |
| 79 | + NewlineSystemType::auto_detect_newline_style("One Two Three") |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn auto_detects_and_applies_unix_newlines() { |
| 85 | + let formatted_text = "One\nTwo\nThree"; |
| 86 | + let raw_input_text = "One\nTwo\nThree"; |
| 87 | + |
| 88 | + let mut out = String::from(formatted_text); |
| 89 | + apply_newline_style(NewlineStyle::Auto, &mut out, raw_input_text); |
| 90 | + assert_eq!("One\nTwo\nThree", &out, "auto should detect 'lf'"); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn auto_detects_and_applies_windows_newlines() { |
| 95 | + let formatted_text = "One\nTwo\nThree"; |
| 96 | + let raw_input_text = "One\r\nTwo\r\nThree"; |
| 97 | + |
| 98 | + let mut out = String::from(formatted_text); |
| 99 | + apply_newline_style(NewlineStyle::Auto, &mut out, raw_input_text); |
| 100 | + assert_eq!("One\r\nTwo\r\nThree", &out, "auto should detect 'crlf'"); |
| 101 | + } |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn auto_detects_and_applies_native_newlines() { |
| 105 | + let formatted_text = "One\nTwo\nThree"; |
| 106 | + let raw_input_text = "One Two Three"; |
| 107 | + |
| 108 | + let mut out = String::from(formatted_text); |
| 109 | + apply_newline_style(NewlineStyle::Auto, &mut out, raw_input_text); |
| 110 | + |
| 111 | + if cfg!(windows) { |
| 112 | + assert_eq!( |
| 113 | + "One\r\nTwo\r\nThree", &out, |
| 114 | + "auto-native-windows should detect 'crlf'" |
| 115 | + ); |
| 116 | + } else { |
| 117 | + assert_eq!( |
| 118 | + "One\nTwo\nThree", &out, |
| 119 | + "auto-native-unix should detect 'lf'" |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn applies_unix_newlines() { |
| 126 | + test_newlines_are_applied_correctly( |
| 127 | + "One\r\nTwo\nThree", |
| 128 | + "One\nTwo\nThree", |
| 129 | + NewlineStyle::Unix, |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn applying_unix_newlines_changes_nothing_for_unix_newlines() { |
| 135 | + let formatted_text = "One\nTwo\nThree"; |
| 136 | + test_newlines_are_applied_correctly(formatted_text, formatted_text, NewlineStyle::Unix); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn applies_unix_newlines_to_string_with_unix_and_windows_newlines() { |
| 141 | + test_newlines_are_applied_correctly( |
| 142 | + "One\r\nTwo\r\nThree\nFour", |
| 143 | + "One\nTwo\nThree\nFour", |
| 144 | + NewlineStyle::Unix, |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + #[test] |
| 149 | + fn applies_windows_newlines_to_string_with_unix_and_windows_newlines() { |
| 150 | + test_newlines_are_applied_correctly( |
| 151 | + "One\nTwo\nThree\r\nFour", |
| 152 | + "One\r\nTwo\r\nThree\r\nFour", |
| 153 | + NewlineStyle::Windows, |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn applying_windows_newlines_changes_nothing_for_windows_newlines() { |
| 159 | + let formatted_text = "One\r\nTwo\r\nThree"; |
| 160 | + test_newlines_are_applied_correctly(formatted_text, formatted_text, NewlineStyle::Windows); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn keeps_carriage_returns_when_applying_windows_newlines_to_str_with_unix_newlines() { |
| 165 | + test_newlines_are_applied_correctly( |
| 166 | + "One\nTwo\nThree\rDrei", |
| 167 | + "One\r\nTwo\r\nThree\rDrei", |
| 168 | + NewlineStyle::Windows, |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + #[test] |
| 173 | + fn keeps_carriage_returns_when_applying_unix_newlines_to_str_with_unix_newlines() { |
| 174 | + test_newlines_are_applied_correctly( |
| 175 | + "One\nTwo\nThree\rDrei", |
| 176 | + "One\nTwo\nThree\rDrei", |
| 177 | + NewlineStyle::Unix, |
| 178 | + ); |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn keeps_carriage_returns_when_applying_windows_newlines_to_str_with_windows_newlines() { |
| 183 | + test_newlines_are_applied_correctly( |
| 184 | + "One\r\nTwo\r\nThree\rDrei", |
| 185 | + "One\r\nTwo\r\nThree\rDrei", |
| 186 | + NewlineStyle::Windows, |
| 187 | + ); |
| 188 | + } |
| 189 | + |
| 190 | + #[test] |
| 191 | + fn keeps_carriage_returns_when_applying_unix_newlines_to_str_with_windows_newlines() { |
| 192 | + test_newlines_are_applied_correctly( |
| 193 | + "One\r\nTwo\r\nThree\rDrei", |
| 194 | + "One\nTwo\nThree\rDrei", |
| 195 | + NewlineStyle::Unix, |
| 196 | + ); |
| 197 | + } |
| 198 | + |
| 199 | + fn test_newlines_are_applied_correctly( |
| 200 | + input: &str, |
| 201 | + expected: &str, |
| 202 | + newline_style: NewlineStyle, |
| 203 | + ) { |
| 204 | + let mut out = String::from(input); |
| 205 | + apply_newline_style(newline_style, &mut out, input); |
| 206 | + assert_eq!(expected, &out); |
| 207 | + } |
| 208 | +} |
0 commit comments