Skip to content

Commit

Permalink
Fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro authored and bradleypmartin committed May 25, 2020
1 parent b68f06b commit 991ab17
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 123 deletions.
3 changes: 2 additions & 1 deletion rustfmt-core/rustfmt-lib/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io;
use std::time::{Duration, Instant};

use rustc_ast::ast;
use rustc_span::symbol;

pub(crate) use syntux::session::ParseSess;

Expand Down Expand Up @@ -499,7 +500,7 @@ impl Input {
let file_stem = file.file_stem()?;
if file.parent()?.to_path_buf().join(file_stem).is_dir() {
Some(DirectoryOwnership::Owned {
relative: file_stem.to_str().map(ast::Ident::from_str),
relative: file_stem.to_str().map(symbol::Ident::from_str),
})
} else {
None
Expand Down
122 changes: 0 additions & 122 deletions rustfmt-core/rustfmt-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,125 +70,3 @@ pub enum Input {
/// A UTF-8 string, in many cases from stdin.
Text(String),
}

impl Input {
fn file_name(&self) -> FileName {
match *self {
Input::File(ref file) => FileName::Real(file.clone()),
Input::Text(..) => FileName::Stdin,
}
}

fn to_directory_ownership(&self) -> Option<DirectoryOwnership> {
match self {
Input::File(ref file) => {
// If there exists a directory with the same name as an input,
// then the input should be parsed as a sub module.
let file_stem = file.file_stem()?;
if file.parent()?.to_path_buf().join(file_stem).is_dir() {
Some(DirectoryOwnership::Owned {
relative: file_stem.to_str().map(symbol::Ident::from_str),
})
} else {
None
}
}
_ => None,
}
}
}

#[cfg(test)]
mod unit_tests {
use super::*;

#[test]
fn test_no_panic_on_format_snippet_and_format_code_block() {
// `format_snippet()` and `format_code_block()` should not panic
// even when we cannot parse the given snippet.
let snippet = "let";
assert!(format_snippet(snippet, &Config::default()).is_none());
assert!(format_code_block(snippet, &Config::default()).is_none());
}

fn test_format_inner<F>(formatter: F, input: &str, expected: &str) -> bool
where
F: Fn(&str, &Config) -> Option<FormattedSnippet>,
{
let output = formatter(input, &Config::default());
output.is_some() && output.unwrap().snippet == expected
}

#[test]
fn test_format_snippet() {
let snippet = "fn main() { println!(\"hello, world\"); }";
#[cfg(not(windows))]
let expected = "fn main() {\n \
println!(\"hello, world\");\n\
}\n";
#[cfg(windows)]
let expected = "fn main() {\r\n \
println!(\"hello, world\");\r\n\
}\r\n";
assert!(test_format_inner(format_snippet, snippet, expected));
}

#[test]
fn test_format_code_block_fail() {
#[rustfmt::skip]
let code_block = "this_line_is_100_characters_long_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx(x, y, z);";
assert!(format_code_block(code_block, &Config::default()).is_none());
}

#[test]
fn test_format_code_block() {
// simple code block
let code_block = "let x=3;";
let expected = "let x = 3;";
assert!(test_format_inner(format_code_block, code_block, expected));

// more complex code block, taken from chains.rs.
let code_block =
"let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
(
chain_indent(context, shape.add_offset(parent_rewrite.len())),
context.config.indent_style() == IndentStyle::Visual || is_small_parent,
)
} else if is_block_expr(context, &parent, &parent_rewrite) {
match context.config.indent_style() {
// Try to put the first child on the same line with parent's last line
IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
// The parent is a block, so align the rest of the chain with the closing
// brace.
IndentStyle::Visual => (parent_shape, false),
}
} else {
(
chain_indent(context, shape.add_offset(parent_rewrite.len())),
false,
)
};
";
let expected =
"let (nested_shape, extend) = if !parent_rewrite_contains_newline && is_continuable(&parent) {
(
chain_indent(context, shape.add_offset(parent_rewrite.len())),
context.config.indent_style() == IndentStyle::Visual || is_small_parent,
)
} else if is_block_expr(context, &parent, &parent_rewrite) {
match context.config.indent_style() {
// Try to put the first child on the same line with parent's last line
IndentStyle::Block => (parent_shape.block_indent(context.config.tab_spaces()), true),
// The parent is a block, so align the rest of the chain with the closing
// brace.
IndentStyle::Visual => (parent_shape, false),
}
} else {
(
chain_indent(context, shape.add_offset(parent_rewrite.len())),
false,
)
};";
assert!(test_format_inner(format_code_block, code_block, expected));
}
}

0 comments on commit 991ab17

Please sign in to comment.