Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

support empty line comments #439

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.8.1

* Emit empty line comments as empty lines.

## 4.8.0

* Add `Expression.operatorSubtract`
Expand Down
4 changes: 3 additions & 1 deletion lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,9 @@ class DartEmitter extends Object
output ??= StringBuffer();

if (spec.comments.isNotEmpty) {
spec.comments.map((line) => '// $line').forEach(output.writeln);
spec.comments
.map((line) => line.isEmpty ? '' : '// $line')
.forEach(output.writeln);
output.writeln();
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_builder
version: 4.8.0
version: 4.8.1
description: >-
A fluent, builder-based library for generating valid Dart code
repository: https://github.com/dart-lang/code_builder
Expand Down
2 changes: 1 addition & 1 deletion test/specs/library_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main() {
),
equalsDart(r'''
// Generated by foo!
//
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like there should still be a way to get this output. Not sure how though... comments could be a list of nullable strings, and then null means something different from a blank string. Or a string that is only whitespace vs blank? I can't think of anything great...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that it's an important use case to support // lines? But the easiest and most clear way would likely be by supporting null strings.

An alternative approach would be to support line wrapping. So, multiple lines would mean multiple groups of comments, with each line automatically wrapped at ~80 cols.

I may implement the 1st solution as that seems like the smallest API diff.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I don't have any examples, but I can imagine that it's more common to write a multi-paragraph comment (with blank // lines) than adjacent comments. Like if you have two paragraphs, or an example, like a code block separated from test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality is just affecting leading library line comments; mostly this is used for the ~3 line copyright headers for files.

The use case I have here is that I also want a // Generated by ... message at the top of the file. Currently this is emitted as part of the file copyright header, which is really not intended or desired. So, I want to go from:

// Copyright (c) 2023, 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.
//
// Generated from Web IDL definitions.

to:

// Copyright (c) 2023, 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.

// Generated from Web IDL definitions.

(see https://github.com/dart-lang/web/blob/main/lib/src/dom/accelerometer.dart)


// Avoid editing by hand.

class Foo { }
Expand Down