Add nested list parenthsization.

This commit is contained in:
Natalie Weizenbaum 2016-08-14 23:19:18 -07:00
parent 9f2f32c7e4
commit 0b7ea7d8c9
2 changed files with 39 additions and 4 deletions

View File

@ -31,12 +31,34 @@ class ListExpression implements Expression {
/*=T*/ accept/*<T>*/(ExpressionVisitor/*<T>*/ visitor) =>
visitor.visitListExpression(this);
// TODO: parenthesize nested lists if necessary
String toString() {
var buffer = new StringBuffer();
if (isBracketed) buffer.writeCharCode($lbracket);
buffer.write(contents.join(separator == ListSeparator.comma ? ", " : " "));
buffer.write(contents
.map((element) =>
_elementNeedsParens(element) ? "($element)" : element.toString())
.join(separator == ListSeparator.comma ? ", " : " "));
if (isBracketed) buffer.writeCharCode($rbracket);
return buffer.toString();
}
bool _elementNeedsParens(Expression expression) {
if (expression is ListExpression) {
if (expression.contents.length < 2) return false;
if (expression.isBracketed) return false;
return separator == ListSeparator.comma
? separator == ListSeparator.comma
: separator != ListSeparator.unknown;
}
if (separator != ListSeparator.space) return false;
if (element is UnaryOperatorExpression) {
return element.operator == UnaryOperator.plus ||
element.operator == UnaryOperator.minus;
}
// TODO: handle binary operations.
return false;
}
}

View File

@ -32,14 +32,27 @@ class SassList extends Value {
int get hashCode => listHash(contents);
// TODO: parenthesize nested lists if necessary
String toString() {
var buffer = new StringBuffer();
if (isBracketed) buffer.writeCharCode($lbracket);
buffer.write(contents.join(separator == ListSeparator.comma ? ", " : " "));
buffer.write(contents
.map((element) =>
_elementNeedsParens(element) ? "($element)" : element.toString())
.join(separator == ListSeparator.comma ? ", " : " "));
if (isBracketed) buffer.writeCharCode($rbracket);
return buffer.toString();
}
bool _elementNeedsParens(Value value) {
if (value is SassList) {
if (expression.contents.length < 2) return false;
if (expression.isBracketed) return false;
return separator == ListSeparator.comma
? separator == ListSeparator.comma
: separator != ListSeparator.unknown;
}
return false;
}
}
class ListSeparator {