p-hatan/ mirror

pub repository

mirror.p-hatan.ir

analyzer_buffer

0.1.2

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

Download archive

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 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

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add analyzer_buffer
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

VersionPublishedArchiveSHA-256
0.0.12025-07-04T11:13:19.474987ZDownloadd32c7465406f78e9207b6d75844eaf302b33f29c502ddf96ecb4491640e8f91d
0.0.22025-07-04T12:46:13.311452ZDownload24f734daf16f3b00fe2f95b87eab84d3f9fb0545611e0797aa518352d5185c8e
0.0.32025-07-04T15:05:04.451765ZDownload0f13c41d9e17dbd3bf5168af46cba0fc6c26653291c1100a99e76df6075f8229
0.0.42025-07-04T16:59:16.133766ZDownload6ae60db0ac174e3789b1bb36a1ff5412edd3fe4d4466a3dcce22cd941bbe25b6
0.0.52025-07-07T09:36:27.027849ZDownloadf839e03fffcef1f305776863d2b7f50210cf82d7be6691b75b37bbd4a92495d9
0.0.62025-07-09T07:17:23.616580ZDownload9b97cc8d55c3fda0cee2b1314d2b53e142dd1bcaed3fb74104cb968e29b4b11b
0.0.72025-07-09T07:40:33.178474ZDownload24af961dc96d980d284e3fa18dc9a277df7ab0cc5f8682a57937a45e12195d36
0.1.02025-07-21T11:18:24.550378ZDownload7b85859d761d926c6aab35aff6c3aadecbfd4b3a586b93b83adc554d08731179
0.1.12025-07-21T11:22:16.290676ZDownload721d411da309e401c1bf023a1230937f3a8be42c46d16f427d8b96358c682dbf
0.1.22025-07-21T12:27:24.401259ZDownload37c3a450df2b95869001d8737181304db2e68f9ba831fc0bba884abb20547a99
0.1.32025-07-21T16:55:39.230420ZDownloadd39e2f427b68949654ae7233686fc7d1bf52987d24e12464c206b530f784c4ca
0.1.42025-07-22T17:42:34.889489ZDownload84e2f45038087835309a406b3db92f7ee3fe4a40464b68b96c6f2954c9bfb591
0.1.52025-07-22T18:02:41.252425ZDownload6d9a3c970a9fbf673470cfa6ec76bbfd11bf5eb0bc2b8ab00fea304d6d985d8f
0.1.62025-08-10T20:05:21.650677ZDownload946c01d21a7fc26a77f8cfb9fde6c521c82c7a7bf56cb6643fb858816b846025
0.1.72025-08-11T10:00:43.739860ZDownloada83cacea4c88fbbefc331a58eee91393e87ca1debec1a910a29316b0f408d7f7
0.1.82025-08-11T11:49:33.993954ZDownloade1b43c6b23bd8ea1ff9bb17a13f5cb20f5a891a4bd324492b9cd0434ffa9a4f5
0.1.92025-08-11T17:49:59.291546ZDownload5c99913875693c566e402384885ee43bb1b6a68a2f54dd5901d3d28c397e9ffe
0.1.102025-08-26T13:30:25.719419ZDownloadf7833bee67c03c37241c67f8741b17cc501b69d9758df7a5a4a13ed6c947be43
0.1.112025-09-09T12:57:34.106711ZDownloadaba2f75e63b3135fd1efaa8b6abefe1aa6e41b6bd9806221620fa48f98156033
0.1.122025-11-03T14:31:10.050456ZDownloadbfc88ddd7b0c66c30149f2afedfae59dc3c209990e0800b7551470caa271c90a
0.2.02025-12-30T19:28:52.276806ZDownloadbee9ac548ea67d4dbe5a13498c4a19365281adabf0a0b71ada19e6792c137178
0.3.02026-01-06T15:29:16.520549ZDownloadff4bd291778c7417fe53fe24ee0d0a1f1ffe281a2d4ea887e7094f16e36eace7
0.3.12026-02-03T14:45:09.341969ZDownload5fcd06b0715ebeee99f03e3f437b3412249969d8d12b191ea8a1d76e42a4e4a1
0.3.22026-04-23T06:59:10.369590ZDownloadbf559bc54530827a92cc4d9ee340fc76b4f17f386218c2b9e7cd33ed468a7e4d
0.3.32026-06-11T13:12:22.375425ZDownload445b77e2054fa3e8c8a8ef1b5e9e6b23bb8028fffd34b5e60eaef315b7750674
0.3.42026-08-29T17:39:56.729461ZDownloadf890a2e708e38fbf4238ad2f9fd3d506538acdddb0cd8eb2c3e8c7bd17784b57

Dependencies

analyzer "^7.3.0"

collection "^1.19.1"

meta "^1.15.0"

path "^1.9.1"

source_gen "^3.0.0"