-
Notifications
You must be signed in to change notification settings - Fork 198
update the generator to emit formatted files #1020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
61af2f4
45b87bf
09eaaa8
8323194
7c46e16
8c71f4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import 'dart:collection'; | ||
|
|
||
| import 'src/formatter.dart' as formatter; | ||
| import 'src/gen/google/protobuf/descriptor.pb.dart'; | ||
|
|
||
| /// Specifies code locations where metadata annotations should be attached and | ||
|
|
@@ -13,14 +14,18 @@ class NamedLocation { | |
| final List<int> fieldPathSegment; | ||
| final int start; | ||
|
|
||
| NamedLocation( | ||
| {required this.name, | ||
| required this.fieldPathSegment, | ||
| required this.start}); | ||
| NamedLocation({ | ||
| required this.name, | ||
| required this.fieldPathSegment, | ||
| required this.start, | ||
| }); | ||
| } | ||
|
|
||
| /// A buffer for writing indented source code. | ||
| class IndentingWriter { | ||
| final String? fileName; | ||
| final bool generateMetadata; | ||
|
|
||
| final StringBuffer _buffer = StringBuffer(); | ||
| final GeneratedCodeInfo sourceLocationInfo = GeneratedCodeInfo(); | ||
|
|
||
|
|
@@ -29,12 +34,14 @@ class IndentingWriter { | |
| // After writing any chunk, _previousOffset is the size of everything that was | ||
| // written to the buffer before the latest call to print or addBlock. | ||
| int _previousOffset = 0; | ||
| final String? _sourceFile; | ||
|
|
||
| // Named text sections to write at the end of the file. | ||
| final Map<String, String> _suffixes = SplayTreeMap(); | ||
|
|
||
| IndentingWriter({String? filename}) : _sourceFile = filename; | ||
| IndentingWriter({ | ||
| this.fileName, | ||
| this.generateMetadata = false, | ||
| }); | ||
|
|
||
| /// Appends a string indented to the current level. | ||
| /// (Indentation will be added after newline characters where needed.) | ||
|
|
@@ -118,18 +125,27 @@ class IndentingWriter { | |
| _suffixes[suffixKey] = text; | ||
| } | ||
|
|
||
| @override | ||
| String toString() { | ||
| /// Emit the generated source. | ||
| /// | ||
| /// This is safe to call multiple times. | ||
| String emitSource({required bool format}) { | ||
| if (_suffixes.isNotEmpty) { | ||
| // TODO: We may want to introduce the notion of closing the writer. | ||
| println(''); | ||
| for (final key in _suffixes.keys) { | ||
| println(_suffixes[key]!); | ||
| } | ||
| _suffixes.clear(); | ||
| } | ||
|
|
||
| return _buffer.toString(); | ||
| var source = _buffer.toString(); | ||
|
|
||
| // We don't always want to format the source (for example, we don't want to | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an interesting problem. It would be cool if we had some way of "anchoring" the annotated locations in the source. Something like https://gcc.gnu.org/onlinedocs/cpp/Line-Control.html#Line-Control-1 ... @lrhn wdyt?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I think that would be interesting. I have in the past (when absolutely necessary) 'recovered' locations post format by:
Separately, I think this location annotation code only exists for generating files used by kythe. I don't know if those are used anymore? Kythe has a dart parser, so not sure why we need to generate separate symbol location files for it. 🤷
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe these are for references from .proto files to the generated code. Not sure a dart parser works directly here. (but perhaps via symbol names...) |
||
| // format if we're creating annotated locations of source elements). | ||
| if (format) { | ||
| source = formatter.format(source); | ||
| } | ||
|
|
||
| return source; | ||
| } | ||
|
|
||
| /// Writes part of a line of text. | ||
|
|
@@ -155,15 +171,14 @@ class IndentingWriter { | |
| /// string that was passed to the previous [print]. Name should be the string | ||
| /// that was written to file. | ||
| void _addAnnotation(List<int> fieldPath, String name, int start) { | ||
| if (_sourceFile == null) { | ||
| return; | ||
| if (generateMetadata) { | ||
| final annotation = GeneratedCodeInfo_Annotation() | ||
| ..path.addAll(fieldPath) | ||
| ..sourceFile = fileName! | ||
| ..begin = _previousOffset + start | ||
| ..end = _previousOffset + start + name.length; | ||
| sourceLocationInfo.annotation.add(annotation); | ||
| } | ||
| final annotation = GeneratedCodeInfo_Annotation() | ||
| ..path.addAll(fieldPath) | ||
| ..sourceFile = _sourceFile | ||
| ..begin = _previousOffset + start | ||
| ..end = _previousOffset + start + name.length; | ||
| sourceLocationInfo.annotation.add(annotation); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:dart_style/dart_style.dart'; | ||
| import 'package:pub_semver/pub_semver.dart'; | ||
|
|
||
| // Note: keep this in sync with the SDK constraint in pubspec.yaml. | ||
| final Version formatUsingVersion = Version(3, 6, 0); | ||
|
|
||
| final DartFormatter _formatter = | ||
| DartFormatter(languageVersion: formatUsingVersion); | ||
|
|
||
| /// Return the Dart formatted version of the given source. | ||
| String format(String source) => _formatter.format(source); |
Uh oh!
There was an error while loading. Please reload this page.