-
Notifications
You must be signed in to change notification settings - Fork 929
Format all patterns (well, except macros) #820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -15,20 +15,32 @@ use lists::{format_item_list, itemize_list}; | |
use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple}; | ||
use types::rewrite_path; | ||
|
||
use syntax::ast::{BindingMode, Pat, Pat_}; | ||
use syntax::ast::{BindingMode, Pat, Pat_, FieldPat}; | ||
|
||
// FIXME(#18): implement pattern formatting. | ||
impl Rewrite for Pat { | ||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { | ||
match self.node { | ||
Pat_::PatBox(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset), | ||
Pat_::PatIdent(binding_mode, ident, None) => { | ||
Pat_::PatIdent(binding_mode, ident, ref sub_pat) => { | ||
let (prefix, mutability) = match binding_mode { | ||
BindingMode::BindByRef(mutability) => ("ref ", mutability), | ||
BindingMode::BindByValue(mutability) => ("", mutability), | ||
}; | ||
let mut_infix = format_mutability(mutability); | ||
let result = format!("{}{}{}", prefix, mut_infix, ident.node); | ||
let id_str = ident.node.to_string(); | ||
|
||
let sub_pat = match *sub_pat { | ||
Some(ref p) => { | ||
// 3 - ` @ `. | ||
let width = try_opt!(width.checked_sub(prefix.len() + mut_infix.len() + | ||
id_str.len() + | ||
3)); | ||
format!(" @ {}", try_opt!(p.rewrite(context, width, offset))) | ||
} | ||
None => "".to_owned(), | ||
}; | ||
|
||
let result = format!("{}{}{}{}", prefix, mut_infix, id_str, sub_pat); | ||
wrap_str(result, context.config.max_width, width, offset) | ||
} | ||
Pat_::PatWild => { | ||
|
@@ -55,39 +67,106 @@ impl Rewrite for Pat { | |
width, | ||
offset) | ||
} | ||
Pat_::PatEnum(ref path, Some(ref pat_vec)) => { | ||
let path_str = try_opt!(::types::rewrite_path(context, | ||
true, | ||
None, | ||
path, | ||
width, | ||
offset)); | ||
Pat_::PatEnum(ref path, ref pat_vec) => { | ||
let path_str = try_opt!(rewrite_path(context, true, None, path, width, offset)); | ||
|
||
if pat_vec.is_empty() { | ||
Some(path_str) | ||
} else { | ||
// 1 = ( | ||
let width = try_opt!(width.checked_sub(path_str.len() + 1)); | ||
let offset = offset + path_str.len() + 1; | ||
let items = itemize_list(context.codemap, | ||
pat_vec.iter(), | ||
")", | ||
|item| item.span.lo, | ||
|item| item.span.hi, | ||
|item| item.rewrite(context, width, offset), | ||
span_after(self.span, "(", context.codemap), | ||
self.span.hi); | ||
Some(format!("{}({})", | ||
path_str, | ||
try_opt!(format_item_list(items, width, offset, context.config)))) | ||
match *pat_vec { | ||
Some(ref pat_vec) => { | ||
if pat_vec.is_empty() { | ||
Some(path_str) | ||
} else { | ||
// 1 = ( | ||
let width = try_opt!(width.checked_sub(path_str.len() + 1)); | ||
let offset = offset + path_str.len() + 1; | ||
let items = itemize_list(context.codemap, | ||
pat_vec.iter(), | ||
")", | ||
|item| item.span.lo, | ||
|item| item.span.hi, | ||
|item| item.rewrite(context, width, offset), | ||
span_after(self.span, "(", context.codemap), | ||
self.span.hi); | ||
Some(format!("{}({})", | ||
path_str, | ||
try_opt!(format_item_list(items, | ||
width, | ||
offset, | ||
context.config)))) | ||
} | ||
} | ||
None => Some(format!("{}(..)", path_str)), | ||
} | ||
} | ||
Pat_::PatLit(ref expr) => expr.rewrite(context, width, offset), | ||
// FIXME(#8): format remaining pattern variants. | ||
Pat_::PatIdent(_, _, Some(..)) | | ||
Pat_::PatEnum(_, None) | | ||
Pat_::PatStruct(..) | | ||
Pat_::PatVec(..) | | ||
Pat_::PatVec(ref prefix, ref slice_pat, ref suffix) => { | ||
// Rewrite all the sub-patterns. | ||
let prefix = prefix.iter().map(|p| p.rewrite(context, width, offset)); | ||
let slice_pat = slice_pat.as_ref().map(|p| { | ||
Some(format!("{}..", try_opt!(p.rewrite(context, width, offset)))) | ||
}); | ||
let suffix = suffix.iter().map(|p| p.rewrite(context, width, offset)); | ||
|
||
// Munge them together. | ||
let pats: Option<Vec<String>> = prefix.chain(slice_pat.into_iter()) | ||
.chain(suffix) | ||
.collect(); | ||
|
||
// Check that all the rewrites succeeded, and if not return None. | ||
let pats = try_opt!(pats); | ||
|
||
// Unwrap all the sub-strings and join them with commas. | ||
let result = format!("[{}]", pats.join(", ")); | ||
wrap_str(result, context.config.max_width, width, offset) | ||
} | ||
Pat_::PatStruct(ref path, ref fields, elipses) => { | ||
let path = try_opt!(rewrite_path(context, true, None, path, width, offset)); | ||
|
||
let (elipses_str, terminator) = if elipses { | ||
(", ..", "..") | ||
} else { | ||
("", "}") | ||
}; | ||
|
||
// 5 = `{` plus space before and after plus `}` plus space before. | ||
let budget = try_opt!(width.checked_sub(path.len() + 5 + elipses_str.len())); | ||
// FIXME Using visual indenting, should use block or visual to match | ||
// struct lit preference (however, in practice I think it is rare | ||
// for struct patterns to be multi-line). | ||
// 3 = `{` plus space before and after. | ||
let offset = offset + path.len() + 3; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment explaining the origin of the 3 and 5? |
||
|
||
let items = itemize_list(context.codemap, | ||
fields.iter(), | ||
terminator, | ||
|f| f.span.lo, | ||
|f| f.span.hi, | ||
|f| f.node.rewrite(context, budget, offset), | ||
span_after(self.span, "{", context.codemap), | ||
self.span.hi); | ||
let mut field_string = try_opt!(format_item_list(items, | ||
budget, | ||
offset, | ||
context.config)); | ||
if elipses { | ||
if field_string.contains('\n') { | ||
field_string.push_str(",\n"); | ||
field_string.push_str(&offset.to_string(context.config)); | ||
field_string.push_str(".."); | ||
} else { | ||
if field_string.len() > 0 { | ||
field_string.push_str(", "); | ||
} | ||
field_string.push_str(".."); | ||
} | ||
} | ||
|
||
if field_string.is_empty() { | ||
Some(format!("{} {{}}", path)) | ||
} else { | ||
Some(format!("{} {{ {} }}", path, field_string)) | ||
} | ||
} | ||
// FIXME(#819) format pattern macros. | ||
Pat_::PatMac(..) => { | ||
wrap_str(context.snippet(self.span), | ||
context.config.max_width, | ||
|
@@ -97,3 +176,17 @@ impl Rewrite for Pat { | |
} | ||
} | ||
} | ||
|
||
impl Rewrite for FieldPat { | ||
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { | ||
let pat = self.pat.rewrite(context, width, offset); | ||
if self.is_shorthand { | ||
pat | ||
} else { | ||
wrap_str(format!("{}: {}", self.ident.to_string(), try_opt!(pat)), | ||
context.config.max_width, | ||
width, | ||
offset) | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
3
for" @ "
?