# analyzer_buffer 0.3.2

A package to help writing code-generators while supporting import prefixes and default values




## Installing
```
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add 'analyzer_buffer:0.3.2'
```
Published: 2026-04-23T06:59:10.369590Z
Expected archive SHA-256: bf559bc54530827a92cc4d9ee340fc76b4f17f386218c2b9e7cd33ed468a7e4d

Available in cache; verification: upstream_checksum
## Direct dependencies
- analyzer: >=10.0.0 <13.0.0
- collection: ^1.19.1
- meta: ^1.15.0
- path: ^1.9.1
- source_gen: ^4.0.0

## Package README (untrusted upstream documentation)
# Welcome to AnalyzerBuffer!

**AnalyzerBuffer** is a package designed for code-generator authors. It helps solving common complex tasks,
such as:

- Gracefully supporting import prefixes (`import ... as prefix`)
- Copy-pasting default values to the generated code, whatever they are
- Writing types while preserving typedefs
- Automatically adding imports (if writing to a file where imports can be added)

All while offering a simple template-based syntax.

TL;DR:

```dart
// A typical "Generator" from source_gen
class MyBuilder extends GeneratorForAnnotation<AnnotationT> {
  @override
  Future<String> generate(
    LibraryReader library,
    BuildStep buildStep,
  ) async {
    // We create a 'AnalyzerBuffer'
    final buffer = AnalyzerBuffer.part(library.element);

    // We write some code in the buffer
    // We use #{{package:packageName/file.dart|Class}} inside the source
    // to reference objects defined in other files
    buffer.write('''
    class Example {
      // Dart package imports
      final #{{dart:async|StreamController}} controller;
      // Third-party package imports:
      final #{{package:riverpod/riverpod.dart|Provider}} provider;
      // Simplified syntax for packages:
      final #{{riverpod|Provider}} provider;
    }
    ''');

    // We convert the buffer into a String
    return buffer.toString();
  }
}
```

Using this syntax, generated code will automatically use import prefixes as
defined by the user.

See [AnalyzerBuffer](https://pub.dev/documentation/analyzer_buffer/latest/analyzer_buffer/AnalyzerBuffer-class.html) for the full list of methods available.

### Dynamic content and loops

`AnalyzerBuffer.write` supports a custom String interpolation, for the sake of
passing non-static content.

The syntax is `#{{name}}` (without a package name), followed by passing
an `args` map:

```dart
buffer.write(args: {
  'DOCS': () {
    for (var i = 0; i < 10; i++)
      buffer.write('// Hello $i\n');
  }
}, '''
class Example {
  #{{DOCS}}
}
''');
```

### Writing DartTypes

`AnalyzerBuffer` offers a [writeType](https://pub.dev/documentation/analyzer_buffer/latest/analyzer_buffer/AnalyzerBuffer/writeType.html) method.
This method enables writing a `DartType` from `analyzer` directly into the generated code:

```dart
ClassElement element;

buffer.writeType(element.thisType);
```

Alternatively, you can convert any `DartType` into its `#{{uri|name}}` representation
using `DartType.toCode`:

```dart
ClassElement element;

buffer.write('''
${element.thisType.toCode()}
''');
```

### Writing default values

A common pattern with code-generators is to have to "copy-paste" default values
from user code into generated code.

For example, a user may define:

```dart
@riverpod
List<Item> fetch(Ref ref, {
  int pageSize = 10,
}) {}
```

And generated code may want to generate copy-paste that `= 10` somewhere
in the generated code, while preserving import prefixes but also
supporting enums and such.

To solve this, AnalyzerBuffer offers `DartObject.toCode()`, to convert
any constant value into its `#{{uri|name}}` representation.

A typical usage is:

```dart
ParameterElement parameter;
// Obtain the constant for the default value of a parameter
final obj = parameter.computeConstantValue();

// We generate code with the same default value
buffer.write('''
void example({
  int pageSize = ${obj.toCode()}
}) {}
''')
```

## Changelog
## 0.3.2 - 2026-04-23

Support analyzer 12.0.0

## 0.3.1 - 2026-02-03

Support analyzer 10.0.0

## 0.3.0 - 2026-01-06

- Removed deprecated `writeType`
- `#{{myPackage|MyClass}}` now matches with classes defined in files such as `package:myPackage/src/file.dart`.
  If you don't want this, explicitly define the expected path in the code, such as `#{{myPackage/src/another|MyClass}}`

## 0.2.0

Support analyzer 9.0.0

## 0.1.12 - 2025-11-03

- Fixed a bug when two imports contain classes with the same name.
- Deprecated `writeType` in favour of `DartType.toCode`+`write`

## 0.1.11 - 2025-09-09

Updated dependencies to support analyzer v8
This allows for compatibility with upgraded generators using analyzer v8 and source_gen v4
(thanks to @TekExplorer)

- `analyzer: '>=7.3.0 <9.0.0'`
- `source_gen: '>=3.0.0 <5.0.0'`

## 0.1.10 - 2025-08-26

- Fix some issue with `functionType.toCode` not including `required`
  (thanks to @TekExplorer)

## 0.1.9 - 2025-08-11

- Fix some bug with indirectly imported objects.

## 0.1.8 - 2025-08-11

- Fix `DartType.toCode` with nullable types

## 0.1.7 - 2025-08-11

- Fixed `DartType.toCode` with typedefs, records, and many other types.

## 0.1.6 - 2025-08-10

- Fixed a bug when importing nested files from other packages

## 0.1.5 - 2025-07-22

- Fix `DartType.toCode` with dynamic/invalid types.

## 0.1.4 - 2025-07-22

- Fix `writeType` with dynamic/invalid types.

## 0.1.3 - 2025-07-21

- Fix a stackoverflow when using cyclic import/export

## 0.1.2 - 2025-07-21

- Fixed auto-import incorrectly using the same prefix for all added imports.

## 0.1.1 - 2025-07-21

Upgrade dependencies

## 0.1.0 - 2025-07-21

- Removed `fromLibrary` in favour of `part`/`part2`.
- Fixed various issues with prefixes

## 0.0.7 - 2025-07-09

chore: Downgraded `meta`

## 0.0.6 - 2025-07-09

fix: `buffer.writeType` no-longer imports the same package multiple times.

## 0.0.5 - 2025-07-07

breaking: `AnalyzerBuffer` constructors now take a mandatory `sourcePath` parameter.
It is necessary for certain edge-cases around types/defaults.  
fix: Fixes an issue where `AnalyzerBuffer` could not be applied to `test` folders.

## 0.0.4

fix: correctly use prefix when an import is re-exporting an element used by generated code.

## 0.0.3

fix: `buffer.write` now correctly respects import prefixes if created using `AnalyzerBuffer.fromLibrary`

## 0.0.2

fix: `buffer.toString` now returns `''` if the buffer is empty.
feat: added `buffer.isEmpty`

## 0.0.1

Initial release








 
