dart-sass/lib/src/interpolation_buffer.dart

61 lines
1.6 KiB
Dart
Raw Normal View History

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.
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
class InterpolationBuffer implements StringSink {
final _text = new StringBuffer();
final _contents = [];
bool get isEmpty => _contents.isEmpty && _text.isEmpty;
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);
void add(Expression expression) {
2016-05-24 16:50:27 +02:00
_flushText();
2016-05-24 16:00:35 +02:00
_contents.add(expression);
}
void addInterpolation(Interpolation expression) {
2016-05-24 18:25:59 +02:00
if (expression.contents.isEmpty) return;
2016-06-10 01:37:54 +02:00
var toAdd = expression.contents;
2016-05-24 16:50:27 +02:00
var first = expression.contents.first;
if (first is String) {
_text.write(first);
toAdd = expression.contents.skip(1);
}
_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
}
void _flushText() {
if (_text.isEmpty) return;
_contents.add(_text.toString());
_text.clear();
}
Interpolation interpolation([FileSpan span]) {
2016-05-24 16:00:35 +02:00
var contents = _contents.toList();
if (_text.isNotEmpty) contents.add(_text.toString());
return new Interpolation(contents, span: span);
2016-05-24 16:00:35 +02:00
}
2016-08-15 03:49:51 +02:00
String toString() => "${_contents.join('')}$_text";
2016-05-24 16:00:35 +02:00
}