2016-05-24 16:00:35 +02:00
|
|
|
// Copyright 2016 Google Inc. Use of this source code is governed by an
|
|
|
|
// MIT-style license that can be found in the LICENSE file or at
|
|
|
|
// https://opensource.org/licenses/MIT.
|
|
|
|
|
2017-01-07 01:45:17 +01:00
|
|
|
import 'package:charcode/charcode.dart';
|
2016-05-24 16:00:35 +02:00
|
|
|
import 'package:source_span/source_span.dart';
|
|
|
|
|
2016-08-28 01:12:17 +02:00
|
|
|
import 'ast/sass.dart';
|
2016-05-24 16:00:35 +02:00
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// A buffer that iteratively builds up an [Interpolation].
|
|
|
|
///
|
|
|
|
/// Add text using [write] and related methods, and [Expression]s using [add].
|
|
|
|
/// Once that's done, call [interpolation] to build the result.
|
2016-05-24 16:00:35 +02:00
|
|
|
class InterpolationBuffer implements StringSink {
|
2016-10-10 08:51:20 +02:00
|
|
|
/// The buffer that accumulates plain text.
|
2016-05-24 16:00:35 +02:00
|
|
|
final _text = new StringBuffer();
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// The contents of the [Interpolation] so far.
|
|
|
|
///
|
|
|
|
/// This contains [String]s and [Expression]s.
|
2016-05-24 16:00:35 +02:00
|
|
|
final _contents = [];
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// Returns whether this buffer has no contents.
|
2016-05-24 16:00:35 +02:00
|
|
|
bool get isEmpty => _contents.isEmpty && _text.isEmpty;
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// Empties this buffer.
|
2016-05-24 16:00:35 +02:00
|
|
|
void clear() {
|
|
|
|
_contents.clear();
|
|
|
|
_text.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(Object obj) => _text.write(obj);
|
|
|
|
void writeAll(Iterable<Object> objects, [String separator = '']) =>
|
|
|
|
_text.writeAll(objects, separator);
|
|
|
|
void writeCharCode(int character) => _text.writeCharCode(character);
|
|
|
|
void writeln([Object obj = '']) => _text.writeln(obj);
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// Adds [expression] to this buffer.
|
2016-05-24 16:00:35 +02:00
|
|
|
void add(Expression expression) {
|
2016-05-24 16:50:27 +02:00
|
|
|
_flushText();
|
2016-05-24 16:00:35 +02:00
|
|
|
_contents.add(expression);
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// Adds the contents of [interpolation] to this buffer.
|
|
|
|
void addInterpolation(Interpolation interpolation) {
|
|
|
|
if (interpolation.contents.isEmpty) return;
|
2016-05-24 18:25:59 +02:00
|
|
|
|
2016-10-20 02:56:48 +02:00
|
|
|
Iterable toAdd = interpolation.contents;
|
2016-10-10 08:51:20 +02:00
|
|
|
var first = interpolation.contents.first;
|
2016-05-24 16:50:27 +02:00
|
|
|
if (first is String) {
|
|
|
|
_text.write(first);
|
2016-10-10 08:51:20 +02:00
|
|
|
toAdd = interpolation.contents.skip(1);
|
2016-05-24 16:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_flushText();
|
|
|
|
_contents.addAll(toAdd);
|
2016-06-10 01:37:54 +02:00
|
|
|
if (_contents.last is String) _text.write(_contents.removeLast());
|
2016-05-24 16:50:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// Flushes [_text] to [_contents] if necessary.
|
2016-05-24 16:50:27 +02:00
|
|
|
void _flushText() {
|
|
|
|
if (_text.isEmpty) return;
|
|
|
|
_contents.add(_text.toString());
|
|
|
|
_text.clear();
|
|
|
|
}
|
|
|
|
|
2016-10-10 08:51:20 +02:00
|
|
|
/// Creates an [Interpolation] with the given [span] from the contents of this
|
|
|
|
/// buffer.
|
|
|
|
Interpolation interpolation(FileSpan span) {
|
2016-05-24 16:00:35 +02:00
|
|
|
var contents = _contents.toList();
|
|
|
|
if (_text.isNotEmpty) contents.add(_text.toString());
|
2016-08-28 23:53:20 +02:00
|
|
|
return new Interpolation(contents, span);
|
2016-05-24 16:00:35 +02:00
|
|
|
}
|
2016-08-15 03:49:51 +02:00
|
|
|
|
2017-01-07 01:45:17 +01:00
|
|
|
String toString() {
|
|
|
|
var buffer = new StringBuffer();
|
|
|
|
for (var element in _contents) {
|
|
|
|
if (element is String) {
|
|
|
|
buffer.write(element);
|
|
|
|
} else {
|
|
|
|
buffer.write("#{");
|
|
|
|
buffer.write(element);
|
|
|
|
buffer.writeCharCode($rbrace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
buffer.write(_text);
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
2016-05-24 16:00:35 +02:00
|
|
|
}
|