44
55import 'dart:collection' ;
66
7+ import 'src/formatter.dart' as formatter;
78import 'src/gen/google/protobuf/descriptor.pb.dart' ;
89
910/// Specifies code locations where metadata annotations should be attached and
@@ -22,6 +23,9 @@ class NamedLocation {
2223
2324/// A buffer for writing indented source code.
2425class IndentingWriter {
26+ final String ? fileName;
27+ final bool generateMetadata;
28+
2529 final StringBuffer _buffer = StringBuffer ();
2630 final GeneratedCodeInfo sourceLocationInfo = GeneratedCodeInfo ();
2731
@@ -30,12 +34,11 @@ class IndentingWriter {
3034 // After writing any chunk, _previousOffset is the size of everything that was
3135 // written to the buffer before the latest call to print or addBlock.
3236 int _previousOffset = 0 ;
33- final String ? _sourceFile;
3437
3538 // Named text sections to write at the end of the file.
3639 final Map <String , String > _suffixes = SplayTreeMap ();
3740
38- IndentingWriter ({String ? filename}) : _sourceFile = filename ;
41+ IndentingWriter ({this .fileName, this .generateMetadata = false }) ;
3942
4043 /// Appends a string indented to the current level.
4144 /// (Indentation will be added after newline characters where needed.)
@@ -138,18 +141,27 @@ class IndentingWriter {
138141 _suffixes[suffixKey] = text;
139142 }
140143
141- @override
142- String toString () {
144+ /// Emit the generated source.
145+ ///
146+ /// This is safe to call multiple times.
147+ String emitSource ({required bool format}) {
143148 if (_suffixes.isNotEmpty) {
144- // TODO: We may want to introduce the notion of closing the writer.
145149 println ('' );
146150 for (final key in _suffixes.keys) {
147151 println (_suffixes[key]! );
148152 }
149153 _suffixes.clear ();
150154 }
151155
152- return _buffer.toString ();
156+ var source = _buffer.toString ();
157+
158+ // We don't always want to format the source (for example, we don't want to
159+ // format if we're creating annotated locations of source elements).
160+ if (format) {
161+ source = formatter.format (source);
162+ }
163+
164+ return source;
153165 }
154166
155167 /// Writes part of a line of text.
@@ -175,16 +187,15 @@ class IndentingWriter {
175187 /// string that was passed to the previous [print] . Name should be the string
176188 /// that was written to file.
177189 void _addAnnotation (List <int > fieldPath, String name, int start) {
178- if (_sourceFile == null ) {
179- return ;
190+ if (generateMetadata) {
191+ final annotation =
192+ GeneratedCodeInfo_Annotation ()
193+ ..path.addAll (fieldPath)
194+ ..sourceFile = fileName!
195+ ..begin = _previousOffset + start
196+ ..end = _previousOffset + start + name.length;
197+ sourceLocationInfo.annotation.add (annotation);
180198 }
181- final annotation =
182- GeneratedCodeInfo_Annotation ()
183- ..path.addAll (fieldPath)
184- ..sourceFile = _sourceFile
185- ..begin = _previousOffset + start
186- ..end = _previousOffset + start + name.length;
187- sourceLocationInfo.annotation.add (annotation);
188199 }
189200}
190201
0 commit comments