-
-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fix #2104: fmt incorrectly using 1-indexing for columns * Clippy...
- Loading branch information
1 parent
2dc6cec
commit d8942a2
Showing
17 changed files
with
756 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use prettyplease::unparse; | ||
use syn::{Expr, File, Item}; | ||
|
||
/// Unparse an expression back into a string | ||
/// | ||
/// This creates a new temporary file, parses the expression into it, and then formats the file. | ||
/// This is a bit of a hack, but dtonlay doesn't want to support this very simple usecase, forcing us to clone the expr | ||
pub fn unparse_expr(expr: &Expr) -> String { | ||
let file = wrapped(expr); | ||
let wrapped = unparse(&file); | ||
unwrapped(wrapped) | ||
} | ||
|
||
// Split off the fn main and then cut the tabs off the front | ||
fn unwrapped(raw: String) -> String { | ||
raw.strip_prefix("fn main() {\n") | ||
.unwrap() | ||
.strip_suffix("}\n") | ||
.unwrap() | ||
.lines() | ||
.map(|line| line.strip_prefix(" ").unwrap()) // todo: set this to tab level | ||
.collect::<Vec<_>>() | ||
.join("\n") | ||
} | ||
|
||
fn wrapped(expr: &Expr) -> File { | ||
File { | ||
shebang: None, | ||
attrs: vec![], | ||
items: vec![ | ||
// | ||
Item::Verbatim(quote::quote! { | ||
fn main() { | ||
#expr | ||
} | ||
}), | ||
], | ||
} | ||
} | ||
|
||
#[test] | ||
fn unparses_raw() { | ||
let expr = syn::parse_str("1 + 1").unwrap(); | ||
let unparsed = unparse(&wrapped(&expr)); | ||
assert_eq!(unparsed, "fn main() {\n 1 + 1\n}\n"); | ||
} | ||
|
||
#[test] | ||
fn unparses_completely() { | ||
let expr = syn::parse_str("1 + 1").unwrap(); | ||
let unparsed = unparse_expr(&expr); | ||
assert_eq!(unparsed, "1 + 1"); | ||
} | ||
|
||
#[test] | ||
fn weird_ifcase() { | ||
let contents = r##" | ||
fn main() { | ||
move |_| timer.with_mut(|t| if t.started_at.is_none() { Some(Instant::now()) } else { None }) | ||
} | ||
"##; | ||
|
||
let expr: File = syn::parse_file(contents).unwrap(); | ||
let out = unparse(&expr); | ||
println!("{}", out); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,5 @@ twoway![ | |
tiny, | ||
tinynoopt, | ||
trailing_expr, | ||
many_exprs, | ||
]; |
Oops, something went wrong.