Skip to content

Commit 39e4791

Browse files
authored
More correctly remove indentation from documentation comments. (#1612)
* fix indentation bug * Add end to end test * dartfmt and rebuild docs * review comments
1 parent 3cb8548 commit 39e4791

File tree

92 files changed

+1238
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1238
-5
lines changed

lib/src/utils.dart

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,45 @@
33
// BSD-style license that can be found in the LICENSE file.
44
library dartdoc.utils;
55

6+
final RegExp leadingWhiteSpace = new RegExp(r'^([ \t]*)[^ ]');
7+
8+
String stripCommonWhitespace(String str) {
9+
StringBuffer buf = new StringBuffer();
10+
List<String> lines = str.split('\n');
11+
int minimumSeen;
12+
13+
for (String line in lines) {
14+
if (line.isNotEmpty) {
15+
Match m = leadingWhiteSpace.firstMatch(line);
16+
if (m != null) {
17+
if (minimumSeen == null || m.group(1).length < minimumSeen) {
18+
minimumSeen = m.group(1).length;
19+
}
20+
}
21+
}
22+
}
23+
minimumSeen ??= 0;
24+
int lineno = 1;
25+
for (String line in lines) {
26+
if (line.length >= minimumSeen) {
27+
buf.write('${line.substring(minimumSeen)}\n');
28+
} else {
29+
if (lineno < lines.length) {
30+
buf.write('\n');
31+
}
32+
}
33+
++lineno;
34+
}
35+
return buf.toString();
36+
}
37+
638
String stripComments(String str) {
739
if (str == null) return null;
8-
940
StringBuffer buf = new StringBuffer();
1041

1142
if (str.startsWith('///')) {
43+
str = stripCommonWhitespace(str);
1244
for (String line in str.split('\n')) {
13-
line = line.trimLeft();
1445
if (line.startsWith('/// ')) {
1546
buf.write('${line.substring(4)}\n');
1647
} else if (line.startsWith('///')) {
@@ -26,9 +57,8 @@ String stripComments(String str) {
2657
if (str.endsWith('*/')) {
2758
str = str.substring(0, str.length - 2);
2859
}
29-
str = str.trim();
60+
str = stripCommonWhitespace(str);
3061
for (String line in str.split('\n')) {
31-
line = line.trimLeft();
3262
if (line.startsWith('* ')) {
3363
buf.write('${line.substring(2)}\n');
3464
} else if (line.startsWith('*')) {

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,4 @@ packages:
415415
source: hosted
416416
version: "2.1.13"
417417
sdks:
418-
dart: ">=2.0.0-dev <=2.0.0-dev.24.0"
418+
dart: ">=2.0.0-dev <=2.0.0-dev.30.0"

test/model_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,6 +1502,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
15021502
Field explicitNonDocumentedInBaseClassGetter;
15031503
Field documentedPartialFieldInSubclassOnly;
15041504
Field ExtraSpecialListLength;
1505+
Field aProperty;
15051506

15061507
setUp(() {
15071508
c = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
@@ -1558,6 +1559,27 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
15581559
.firstWhere((c) => c.name == 'SpecialList')
15591560
.allInstanceProperties
15601561
.firstWhere((f) => f.name == 'length');
1562+
aProperty = fakeLibrary.classes
1563+
.firstWhere((c) => c.name == 'AClassWithFancyProperties')
1564+
.allInstanceProperties
1565+
.firstWhere((f) => f.name == 'aProperty');
1566+
});
1567+
1568+
test('indentation is not lost inside indented code samples', () {
1569+
expect(aProperty.documentation, equals(
1570+
'This property is quite fancy, and requires sample code to understand.\n'
1571+
'\n'
1572+
'```dart\n'
1573+
'AClassWithFancyProperties x = new AClassWithFancyProperties();\n'
1574+
'\n'
1575+
'if (x.aProperty.contains(\'Hello\')) {\n'
1576+
' print("I am indented!");\n'
1577+
' if (x.aProperty.contains(\'World\')) {\n'
1578+
' print ("I am indented even more!!!");\n'
1579+
' }\n'
1580+
'}\n'
1581+
'```'
1582+
));
15611583
});
15621584

15631585
test('annotations from getters and setters are accumulated in Fields', () {

test/utils_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,22 @@ void main() {
176176
expect(truncateString('foo bar baz qux', 10), 'foo bar ba…');
177177
});
178178
});
179+
180+
group('leadingWhitespace', () {
181+
test('strip common leading whitespace, but no more', () {
182+
String input = ' 3 space indent\n'
183+
' 4 space indent (one preserved)\n'
184+
' 7 space indent (four preserved)\n'
185+
'\t 2 spaces, one tab (same as 3 space)\n'
186+
' \t4 spaces, one tab (preserve the tab)\n'
187+
' 3 space indent again\n';
188+
String output = '3 space indent\n'
189+
' 4 space indent (one preserved)\n'
190+
' 7 space indent (four preserved)\n'
191+
'2 spaces, one tab (same as 3 space)\n'
192+
' \t4 spaces, one tab (preserve the tab)\n'
193+
'3 space indent again\n';
194+
expect(stripCommonWhitespace(input), equals(output));
195+
});
196+
});
179197
}

testing/test_package/lib/fake.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,22 @@ class _APrivateConstClass {
164164
const _APrivateConstClass();
165165
}
166166

167+
class AClassWithFancyProperties {
168+
/// This property is quite fancy, and requires sample code to understand.
169+
///
170+
/// ```dart
171+
/// AClassWithFancyProperties x = new AClassWithFancyProperties();
172+
///
173+
/// if (x.aProperty.contains('Hello')) {
174+
/// print("I am indented!");
175+
/// if (x.aProperty.contains('World')) {
176+
/// print ("I am indented even more!!!");
177+
/// }
178+
/// }
179+
/// ```
180+
String aProperty;
181+
}
182+
167183
const _APrivateConstClass CUSTOM_CLASS_PRIVATE = const _APrivateConstClass();
168184

169185
// No dart docs on purpose. Also, a non-primitive const class.

testing/test_package_docs/fake/AClassUsingASuperMixin-class.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ <h5>fake library</h5>
4040
<ol>
4141
<li class="section-title"><a href="fake/fake-library.html#classes">Classes</a></li>
4242
<li><a href="fake/AClassUsingASuperMixin-class.html">AClassUsingASuperMixin</a></li>
43+
<li><a href="fake/AClassWithFancyProperties-class.html">AClassWithFancyProperties</a></li>
4344
<li><a href="fake/AMixinCallingSuper-class.html">AMixinCallingSuper</a></li>
4445
<li><a href="fake/Annotation-class.html">Annotation</a></li>
4546
<li><a href="fake/AnotherInterface-class.html">AnotherInterface</a></li>

0 commit comments

Comments
 (0)