-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from rtfeldman/format-records
Add formatting for records
- Loading branch information
Showing
5 changed files
with
306 additions
and
153 deletions.
There are no files selected for viewing
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,82 @@ | ||
use parse::ast::{AssignedField, Expr, Pattern}; | ||
|
||
pub fn is_multiline_expr<'a>(expr: &'a Expr<'a>) -> bool { | ||
use parse::ast::Expr::*; | ||
// TODO cache these answers using a Map<Pointer, bool>, so | ||
// we don't have to traverse subexpressions repeatedly | ||
|
||
match expr { | ||
// Return whether these spaces contain any Newlines | ||
SpaceBefore(_, spaces) | SpaceAfter(_, spaces) => { | ||
spaces.iter().any(|space| space.contains_newline()) | ||
} | ||
|
||
// These expressions never have newlines | ||
Float(_) | ||
| Int(_) | ||
| HexInt(_) | ||
| OctalInt(_) | ||
| BinaryInt(_) | ||
| Str(_) | ||
| Field(_, _) | ||
| QualifiedField(_, _) | ||
| AccessorFunction(_) | ||
| Var(_, _) | ||
| MalformedIdent(_) | ||
| MalformedClosure | ||
| Variant(_, _) => false, | ||
|
||
// These expressions always have newlines | ||
Defs(_, _) | Case(_, _) => true, | ||
|
||
List(elems) => elems | ||
.iter() | ||
.any(|loc_expr| is_multiline_expr(&loc_expr.value)), | ||
|
||
BlockStr(lines) => lines.len() > 1, | ||
Apply(loc_expr, args, _) => { | ||
is_multiline_expr(&loc_expr.value) | ||
|| args.iter().any(|loc_arg| is_multiline_expr(&loc_arg.value)) | ||
} | ||
|
||
If((loc_cond, loc_if_true, loc_if_false)) => { | ||
is_multiline_expr(&loc_cond.value) | ||
|| is_multiline_expr(&loc_if_true.value) | ||
|| is_multiline_expr(&loc_if_false.value) | ||
} | ||
|
||
BinOp((loc_left, _, loc_right)) => { | ||
is_multiline_expr(&loc_left.value) || is_multiline_expr(&loc_right.value) | ||
} | ||
|
||
UnaryOp(loc_subexpr, _) => is_multiline_expr(&loc_subexpr.value), | ||
|
||
PrecedenceConflict(_, _, loc_expr) => is_multiline_expr(&loc_expr.value), | ||
|
||
Closure(loc_patterns, loc_body) => { | ||
// check the body first because it's more likely to be multiline | ||
is_multiline_expr(&loc_body.value) | ||
|| loc_patterns | ||
.iter() | ||
.any(|loc_pattern| is_multiline_pattern(&loc_pattern.value)) | ||
} | ||
|
||
Record(loc_fields) => loc_fields | ||
.iter() | ||
.any(|loc_field| is_multiline_field(&loc_field.value)), | ||
} | ||
} | ||
|
||
pub fn is_multiline_field<'a, Val>(field: &'a AssignedField<'a, Val>) -> bool { | ||
use self::AssignedField::*; | ||
|
||
match field { | ||
LabeledValue(_, spaces, _) | LabelOnly(_, spaces) => !spaces.is_empty(), | ||
AssignedField::SpaceBefore(_, _) | AssignedField::SpaceAfter(_, _) => true, | ||
Malformed(text) => text.chars().any(|c| c == '\n'), | ||
} | ||
} | ||
|
||
pub fn is_multiline_pattern<'a>(_pattern: &'a Pattern<'a>) -> bool { | ||
panic!("TODO return iff there are any newlines") | ||
} |
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
Oops, something went wrong.