2021-05-14 03:06:32 +00:00
|
|
|
import 'package:angel3_code_buffer/angel3_code_buffer.dart';
|
2021-03-16 00:14:28 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
/// Use a `CodeBuffer` just like any regular `StringBuffer`:
|
|
|
|
String someFunc() {
|
2021-05-18 13:47:54 +00:00
|
|
|
var buf = CodeBuffer();
|
2021-03-16 00:14:28 +00:00
|
|
|
buf
|
|
|
|
..write('hello ')
|
|
|
|
..writeln('world!');
|
|
|
|
return buf.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// However, a `CodeBuffer` supports indentation.
|
|
|
|
void someOtherFunc() {
|
2021-05-18 13:47:54 +00:00
|
|
|
var buf = CodeBuffer();
|
2021-03-16 00:14:28 +00:00
|
|
|
|
|
|
|
// Custom options...
|
|
|
|
// ignore: unused_local_variable
|
2021-05-14 03:06:32 +00:00
|
|
|
var customBuf =
|
2021-05-18 13:47:54 +00:00
|
|
|
CodeBuffer(newline: '\r\n', space: '\t', trailingNewline: true);
|
2021-03-16 00:14:28 +00:00
|
|
|
|
|
|
|
// Without whitespace..
|
|
|
|
// ignore: unused_local_variable
|
2021-05-18 13:47:54 +00:00
|
|
|
var minifyingBuf = CodeBuffer.noWhitespace();
|
2021-03-16 00:14:28 +00:00
|
|
|
|
|
|
|
// Any following lines will have an incremented indentation level...
|
|
|
|
buf.indent();
|
|
|
|
|
|
|
|
// And vice-versa:
|
|
|
|
buf.outdent();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `CodeBuffer` instances keep track of every `SourceSpan` they create.
|
|
|
|
//This makes them useful for codegen tools, or to-JS compilers.
|
|
|
|
void yetAnotherOtherFunc(CodeBuffer buf) {
|
|
|
|
buf.write('hello');
|
|
|
|
expect(buf.lastLine!.text, 'hello');
|
|
|
|
|
|
|
|
buf.writeln('world');
|
|
|
|
expect(buf.lastLine!.lastSpan!.start.column, 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// You can copy a `CodeBuffer` into another, heeding indentation rules:
|
|
|
|
void yetEvenAnotherFunc(CodeBuffer a, CodeBuffer b) {
|
|
|
|
b.copyInto(a);
|
2021-05-14 03:06:32 +00:00
|
|
|
}
|