Skip to content

Commit 4bc5911

Browse files
authored
Merge pull request rust-lang#3526 from bash/refactor-apply-newline-style
Refactor apply newline style
2 parents 509f4c2 + 821a370 commit 4bc5911

7 files changed

+267
-115
lines changed

src/config/options.rs

-112
Original file line numberDiff line numberDiff line change
@@ -22,62 +22,6 @@ pub enum NewlineStyle {
2222
Native,
2323
}
2424

25-
impl NewlineStyle {
26-
fn auto_detect(raw_input_text: &str) -> NewlineStyle {
27-
if let Some(pos) = raw_input_text.find('\n') {
28-
let pos = pos.saturating_sub(1);
29-
if let Some('\r') = raw_input_text.chars().nth(pos) {
30-
NewlineStyle::Windows
31-
} else {
32-
NewlineStyle::Unix
33-
}
34-
} else {
35-
NewlineStyle::Native
36-
}
37-
}
38-
39-
fn native() -> NewlineStyle {
40-
if cfg!(windows) {
41-
NewlineStyle::Windows
42-
} else {
43-
NewlineStyle::Unix
44-
}
45-
}
46-
47-
/// Apply this newline style to the formatted text. When the style is set
48-
/// to `Auto`, the `raw_input_text` is used to detect the existing line
49-
/// endings.
50-
///
51-
/// If the style is set to `Auto` and `raw_input_text` contains no
52-
/// newlines, the `Native` style will be used.
53-
pub(crate) fn apply(self, formatted_text: &mut String, raw_input_text: &str) {
54-
use crate::NewlineStyle::*;
55-
let mut style = self;
56-
if style == Auto {
57-
style = Self::auto_detect(raw_input_text);
58-
}
59-
if style == Native {
60-
style = Self::native();
61-
}
62-
match style {
63-
Windows => {
64-
let mut transformed = String::with_capacity(2 * formatted_text.capacity());
65-
for c in formatted_text.chars() {
66-
match c {
67-
'\n' => transformed.push_str("\r\n"),
68-
'\r' => continue,
69-
c => transformed.push(c),
70-
}
71-
}
72-
*formatted_text = transformed;
73-
}
74-
Unix => return,
75-
Native => unreachable!("NewlineStyle::Native"),
76-
Auto => unreachable!("NewlineStyle::Auto"),
77-
}
78-
}
79-
}
80-
8125
#[config_type]
8226
/// Where to put the opening brace of items (`fn`, `impl`, etc.).
8327
pub enum BraceStyle {
@@ -412,59 +356,3 @@ impl Edition {
412356
}
413357
}
414358
}
415-
416-
#[test]
417-
fn test_newline_style_auto_detect() {
418-
let lf = "One\nTwo\nThree";
419-
let crlf = "One\r\nTwo\r\nThree";
420-
let none = "One Two Three";
421-
422-
assert_eq!(NewlineStyle::Unix, NewlineStyle::auto_detect(lf));
423-
assert_eq!(NewlineStyle::Windows, NewlineStyle::auto_detect(crlf));
424-
assert_eq!(NewlineStyle::Native, NewlineStyle::auto_detect(none));
425-
}
426-
427-
#[test]
428-
fn test_newline_style_auto_apply() {
429-
let auto = NewlineStyle::Auto;
430-
431-
let formatted_text = "One\nTwo\nThree";
432-
let raw_input_text = "One\nTwo\nThree";
433-
434-
let mut out = String::from(formatted_text);
435-
auto.apply(&mut out, raw_input_text);
436-
assert_eq!("One\nTwo\nThree", &out, "auto should detect 'lf'");
437-
438-
let formatted_text = "One\nTwo\nThree";
439-
let raw_input_text = "One\r\nTwo\r\nThree";
440-
441-
let mut out = String::from(formatted_text);
442-
auto.apply(&mut out, raw_input_text);
443-
assert_eq!("One\r\nTwo\r\nThree", &out, "auto should detect 'crlf'");
444-
445-
#[cfg(not(windows))]
446-
{
447-
let formatted_text = "One\nTwo\nThree";
448-
let raw_input_text = "One Two Three";
449-
450-
let mut out = String::from(formatted_text);
451-
auto.apply(&mut out, raw_input_text);
452-
assert_eq!(
453-
"One\nTwo\nThree", &out,
454-
"auto-native-unix should detect 'lf'"
455-
);
456-
}
457-
458-
#[cfg(windows)]
459-
{
460-
let formatted_text = "One\nTwo\nThree";
461-
let raw_input_text = "One Two Three";
462-
463-
let mut out = String::from(formatted_text);
464-
auto.apply(&mut out, raw_input_text);
465-
assert_eq!(
466-
"One\r\nTwo\r\nThree", &out,
467-
"auto-native-windows should detect 'crlf'"
468-
);
469-
}
470-
}

src/formatting.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use syntax::errors::{DiagnosticBuilder, Handler};
1212
use syntax::parse::{self, ParseSess};
1313
use syntax::source_map::{FilePathMapping, SourceMap, Span, DUMMY_SP};
1414

15+
use self::newline_style::apply_newline_style;
1516
use crate::comment::{CharClasses, FullCodeCharKind};
1617
use crate::config::{Config, FileName, Verbosity};
1718
use crate::ignore_path::IgnorePathSet;
@@ -20,6 +21,8 @@ use crate::utils::{count_newlines, get_skip_macro_names};
2021
use crate::visitor::{FmtVisitor, SnippetProvider};
2122
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
2223

24+
mod newline_style;
25+
2326
// A map of the files of a crate, with their new content
2427
pub(crate) type SourceFile = Vec<FileRecord>;
2528
pub(crate) type FileRecord = (FileName, String);
@@ -191,9 +194,12 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
191194
&self.config,
192195
&self.report,
193196
);
194-
self.config
195-
.newline_style()
196-
.apply(&mut visitor.buffer, &big_snippet);
197+
198+
apply_newline_style(
199+
self.config.newline_style(),
200+
&mut visitor.buffer,
201+
&big_snippet,
202+
);
197203

198204
if visitor.macro_rewrite_failure {
199205
self.report.add_macro_format_failure();

0 commit comments

Comments
 (0)