2021-05-14 03:06:32 +00:00
|
|
|
# angel3_code_buffer
|
|
|
|
[![version](https://img.shields.io/badge/pub-v2.12.4-brightgreen)](https://pub.dartlang.org/packages/angel3_code_buffer)
|
|
|
|
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
|
|
|
|
|
|
|
|
[![License](https://img.shields.io/github/license/dukefirehawk/angel)](https://github.com/dukefirehawk/angel/tree/angel3/code_buffer)
|
2021-03-16 00:14:28 +00:00
|
|
|
|
|
|
|
An advanced StringBuffer geared toward generating code, and source maps.
|
|
|
|
|
|
|
|
# Installation
|
|
|
|
In your `pubspec.yaml`:
|
|
|
|
|
|
|
|
```yaml
|
|
|
|
dependencies:
|
2021-05-14 03:06:32 +00:00
|
|
|
angel3_code_buffer: ^2.0.0
|
2021-03-16 00:14:28 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
# Usage
|
|
|
|
Use a `CodeBuffer` just like any regular `StringBuffer`:
|
|
|
|
|
|
|
|
```dart
|
|
|
|
String someFunc() {
|
2021-05-14 03:06:32 +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.
|
|
|
|
|
|
|
|
```dart
|
|
|
|
void someOtherFunc() {
|
2021-05-14 03:06:32 +00:00
|
|
|
var buf = CodeBuffer();
|
2021-03-16 00:14:28 +00:00
|
|
|
// Custom options...
|
2021-05-14 03:06:32 +00:00
|
|
|
var buf = CodeBuffer(newline: '\r\n', space: '\t', trailingNewline: true);
|
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.
|
|
|
|
|
|
|
|
```dart
|
|
|
|
void someFunc(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:
|
|
|
|
|
|
|
|
```dart
|
|
|
|
void yetAnotherFunc(CodeBuffer a, CodeBuffer b) {
|
|
|
|
b.copyInto(a);
|
|
|
|
}
|
|
|
|
```
|