Skip to content

Very minor comment and space util cleanup #2542

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

Merged
merged 2 commits into from
Feb 25, 2021
Merged
Changes from all commits
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
41 changes: 22 additions & 19 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,59 +6,62 @@ library dartdoc.utils;
final RegExp leadingWhiteSpace = RegExp(r'^([ \t]*)[^ ]');

Iterable<String> stripCommonWhitespace(String str) sync* {
var lines = str.split('\n');
int minimumSeen;
if (str.isEmpty) return;
final lines = str.split('\n');
int /*?*/ minimumSeen;

for (var line in lines) {
for (final line in lines) {
if (line.isNotEmpty) {
Match m = leadingWhiteSpace.firstMatch(line);
if (m != null) {
if (minimumSeen == null || m.group(1).length < minimumSeen) {
minimumSeen = m.group(1).length;
final match = leadingWhiteSpace.firstMatch(line);
if (match != null) {
if (minimumSeen == null || match.group(1).length < minimumSeen) {
minimumSeen = match.group(1).length;
}
}
}
}
minimumSeen ??= 0;
for (var line in lines) {
for (final line in lines) {
if (line.length >= minimumSeen) {
yield '${line.substring(minimumSeen)}';
yield line.substring(minimumSeen);
} else {
yield '';
}
}
}

String stripComments(String str) {
var cStyle = false;
// TODO(parlough): Once we migrate to null safety, prohibit null here
if (str == null) return null;
var buf = StringBuffer();
if (str.isEmpty) return '';
final buf = StringBuffer();

if (str.startsWith('///')) {
for (var line in stripCommonWhitespace(str)) {
for (final line in stripCommonWhitespace(str)) {
if (line.startsWith('/// ')) {
buf.write('${line.substring(4)}\n');
buf.writeln(line.substring(4));
} else if (line.startsWith('///')) {
buf.write('${line.substring(3)}\n');
buf.writeln(line.substring(3));
} else {
buf.write('$line\n');
buf.writeln(line);
}
}
} else {
var cStyle = false;
if (str.startsWith('/**')) {
str = str.substring(3);
cStyle = true;
}
if (str.endsWith('*/')) {
str = str.substring(0, str.length - 2);
}
for (var line in stripCommonWhitespace(str)) {
for (final line in stripCommonWhitespace(str)) {
if (cStyle && line.startsWith('* ')) {
buf.write('${line.substring(2)}\n');
buf.writeln(line.substring(2));
} else if (cStyle && line.startsWith('*')) {
buf.write('${line.substring(1)}\n');
buf.writeln(line.substring(1));
} else {
buf.write('$line\n');
buf.writeln(line);
}
}
}
Expand Down