Viewing version 0.1.2. Open the latest package page.
analyzer_buffer
0.1.2A package to help writing code-generators while supporting import prefixes and default values
Readme
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:
// 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 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:
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 method.
This method enables writing a DartType from analyzer directly into the generated code:
ClassElement element;
buffer.writeType(element.thisType);
Alternatively, you can convert any DartType into its #{{uri|name}} representation
using DartType.toCode:
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:
@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:
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.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
fromLibraryin favour ofpart/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
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.com
flutter pub add 'analyzer_buffer:0.1.2'pubspec.yaml
name: analyzer_buffer
version: 0.1.2
description: A package to help writing code-generators while supporting import prefixes and default values
repository: https://github.com/rrousselGit/analyzer_buffer
environment:
sdk: ^3.6.0
resolution: workspace
dependencies:
analyzer: ^7.3.0
collection: ^1.19.1
meta: ^1.15.0
path: ^1.9.1
source_gen: ^3.0.0
dev_dependencies:
test: ^1.25.15
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | Cached · Upstream SHA-256 matched | Download | d32c7465…e8f91d |
| 0.0.2 | Cached · Upstream SHA-256 matched | Download | 24f734da…185c8e |
| 0.0.3 | Cached · Upstream SHA-256 matched | Download | 0f13c41d…5f8229 |
| 0.0.4 | Cached · Upstream SHA-256 matched | Download | 6ae60db0…be25b6 |
| 0.0.5 | Cached · Upstream SHA-256 matched | Download | f839e03f…2495d9 |
| 0.0.6 | Cached · Upstream SHA-256 matched | Download | 9b97cc8d…b4b11b |
| 0.0.7 | Cached · Upstream SHA-256 matched | Download | 24af961d…195d36 |
| 0.1.0 | Cached · Upstream SHA-256 matched | Download | 7b85859d…731179 |
| 0.1.1 | Cached · Upstream SHA-256 matched | Download | 721d411d…682dbf |
| 0.1.2 | Cached · Upstream SHA-256 matched | Download | 37c3a450…547a99 |
| 0.1.3 | Cached · Upstream SHA-256 matched | Download | d39e2f42…84c4ca |
| 0.1.4 | Cached · Upstream SHA-256 matched | Download | 84e2f450…bfb591 |
| 0.1.5 | Cached · Upstream SHA-256 matched | Download | 6d9a3c97…985d8f |
| 0.1.6 | Cached · Upstream SHA-256 matched | Download | 946c01d2…846025 |
| 0.1.7 | Cached · Upstream SHA-256 matched | Download | a83cacea…08d7f7 |
| 0.1.8 | Cached · Upstream SHA-256 matched | Download | e1b43c6b…a9a4f5 |
| 0.1.9 | Cached · Upstream SHA-256 matched | Download | 5c999138…7e9ffe |
| 0.1.10 | Cached · Upstream SHA-256 matched | Download | f7833bee…47be43 |
| 0.1.11 | Cached · Upstream SHA-256 matched | Download | aba2f75e…156033 |
| 0.1.12 | Cached · Upstream SHA-256 matched | Download | bfc88ddd…71c90a |
| 0.2.0 | Cached · Upstream SHA-256 matched | Download | bee9ac54…137178 |
| 0.3.0 | Cached · Upstream SHA-256 matched | Download | ff4bd291…6eace7 |
| 0.3.1 | Cached · Upstream SHA-256 matched | Download | 5fcd06b0…a4e4a1 |
| 0.3.2 | Cached · Upstream SHA-256 matched | Download | bf559bc5…8a7e4d |
| 0.3.3 | Cached · Upstream SHA-256 matched | Download | 445b77e2…750674 |
| 0.3.4 | Cached · Upstream SHA-256 matched | Download | f890a2e7…784b57 |
Dependencies
analyzer "^7.3.0"
collection "^1.19.1"
meta "^1.15.0"
path "^1.9.1"
source_gen "^3.0.0"