platform-common-utilities/packages/code_buffer
2024-05-21 23:51:17 +08:00
..
example Added code_buffer, combinator, pub_sub 2021-09-11 21:46:31 +08:00
lib Added code_buffer, combinator, pub_sub 2021-09-11 21:46:31 +08:00
test Added code_buffer, combinator, pub_sub 2021-09-11 21:46:31 +08:00
analysis_options.yaml Added code_buffer, combinator, pub_sub 2021-09-11 21:46:31 +08:00
AUTHORS.md Added code_buffer, combinator, pub_sub 2021-09-11 21:46:31 +08:00
CHANGELOG.md Updated Changelog 2024-05-21 23:51:17 +08:00
LICENSE Added code_buffer, combinator, pub_sub 2021-09-11 21:46:31 +08:00
melos_belatuk_code_buffer.iml Upgraded to support sdk 2.17 2022-07-06 22:04:09 +08:00
pubspec.yaml Updated to min dart sdk 3.3 2024-05-21 23:08:07 +08:00
README.md Updated to use lints 3.0.0 2023-12-12 10:10:10 +08:00

Belatuk Code Buffer

Pub Version (including pre-releases) Null Safety License

Replacement of package:code_buffer with breaking changes to support NNBD.

An advanced StringBuffer geared toward generating code, and source maps.

Installation

In your pubspec.yaml:

dependencies:
  belatuk_code_buffer: ^5.1.0

Usage

Use a CodeBuffer just like any regular StringBuffer:

String someFunc() {
    var buf = CodeBuffer();
    buf
      ..write('hello ')
      ..writeln('world!');
    return buf.toString();
}

However, a CodeBuffer supports indentation.

void someOtherFunc() {
  var buf = CodeBuffer();
  // Custom options...
  var buf = CodeBuffer(newline: '\r\n', space: '\t', trailingNewline: true);
  
  // 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 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:

void yetAnotherFunc(CodeBuffer a, CodeBuffer b) {
  b.copyInto(a);
}