Skip to content

Commit 9f6dc78

Browse files
committed
Merge pull request #699 from dart-lang/show-source
display source for method
2 parents 0d24e82 + 651916c commit 9f6dc78

File tree

11 files changed

+135
-35
lines changed

11 files changed

+135
-35
lines changed

lib/resources/styles.css

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,3 +531,35 @@ ol#sidebar li:first-child {
531531
.row-offcanvas-left.active{left:50%;}
532532
.sidebar-offcanvas{position:absolute;top:0;width:50%;}
533533
}
534+
535+
/* toggling source code */
536+
537+
.source-code pre {
538+
margin: 16px 0 16px 0;
539+
}
540+
541+
.source-code h4 {
542+
color: #455A64;
543+
text-transform: uppercase;
544+
font-size: 14px;
545+
}
546+
547+
.source-code h4:hover {
548+
cursor: pointer;
549+
}
550+
551+
.source-code-toggle {
552+
font-size: 80%;
553+
float: left;
554+
width: 1.2em;
555+
padding-top: 2px; /* how to avoid this? how to vertically align? */
556+
opacity: 0.7;
557+
}
558+
559+
.source-code-toggle.closed::before {
560+
content: '\25B6';
561+
}
562+
563+
.source-code-toggle.open::before {
564+
content: '\25BC';
565+
}

lib/src/html_generator.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ class Templates {
8787
'readable_writable',
8888
'documentation',
8989
'name_summary',
90-
'sidebar_for_class'
90+
'sidebar_for_class',
91+
'source_code'
9192
];
9293

9394
for (var partial in partials) {

lib/src/model.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,26 @@ class Enum extends Class {
11061106
}
11071107
}
11081108

1109-
class ModelFunction extends ModelElement {
1109+
abstract class SourceCodeMixin {
1110+
String get sourceCode {
1111+
String contents = element.source.contents.data;
1112+
var node = element.node; // TODO: computeNode once we go to 0.25.2
1113+
// find the start of the line, so that we can line up all the indents
1114+
int i = node.offset;
1115+
while (i > 0) {
1116+
i -= 1;
1117+
if (contents[i] == '\n' || contents[i] == '\r') {
1118+
i += 1;
1119+
break;
1120+
}
1121+
}
1122+
return contents.substring(node.offset - (node.offset - i), node.end);
1123+
}
1124+
1125+
Element get element;
1126+
}
1127+
1128+
class ModelFunction extends ModelElement with SourceCodeMixin {
11101129
ModelFunction(FunctionElement element, Library library)
11111130
: super(element, library) {
11121131
_modelType = new ElementType(_func.type, this);
@@ -1301,7 +1320,7 @@ class Constructor extends ModelElement {
13011320
}
13021321
}
13031322

1304-
class Method extends ModelElement {
1323+
class Method extends ModelElement with SourceCodeMixin {
13051324
bool _isInherited = false;
13061325

13071326
MethodElement get _method => (element as MethodElement);

lib/templates/_footer.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,26 @@
5858
scrollBy(0, -68);
5959
}
6060

61+
function initSourceToggle() {
62+
var toggle = document.querySelector('.source-code h4');
63+
var toggleArrow = document.querySelector('.source-code-toggle');
64+
var code = document.querySelector('.source-code pre');
65+
66+
if (toggle && toggleArrow && code) {
67+
toggle.addEventListener('click', function(e) {
68+
var closed = toggleArrow.classList.contains('closed');
69+
toggleArrow.classList.toggle('closed', !closed);
70+
toggleArrow.classList.toggle('open', closed);
71+
code.style.display = closed ? 'block' : 'none';
72+
});
73+
}
74+
}
75+
6176
document.addEventListener("DOMContentLoaded", function() {
6277
prettyPrint();
6378
initScroller();
6479
initSideNav();
80+
initSourceToggle();
6581

6682
// Make sure the anchors scroll past the fixed page header (#648).
6783
if (location.hash) shiftWindow();

lib/templates/_source_code.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<section class="source-code">
2+
<h4><span class="source-code-toggle closed"></span> Source</h4>
3+
<pre style="display:none"><code class="prettyprint lang-dart">{{ sourceCode }}</code></pre>
4+
</section>

lib/templates/function.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ <h5>{{{library.linkedName}}}</h5>
6969
{{>documentation}}
7070
{{/function}}
7171

72+
{{#function}}
73+
{{>source_code}}
74+
{{/function}}
75+
7276
</div> <!-- /.col-xs-12.col-sm-9 -->
7377

7478
</div> <!-- /.row-offcanvas -->

lib/templates/method.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ <h5>{{{class.linkedName}}}</h5>
2222
{{>documentation}}
2323
{{/method}}
2424

25+
{{#method}}
26+
{{>source_code}}
27+
{{/method}}
28+
2529
</div> <!-- /.col-xs-12.col-sm-9 -->
2630

2731
</div> <!-- /.row-offcanvas -->

pubspec.lock

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ packages:
44
analyzer:
55
description: analyzer
66
source: hosted
7-
version: "0.25.1"
7+
version: "0.25.0"
88
ansicolor:
99
description: ansicolor
1010
source: hosted
@@ -13,14 +13,14 @@ packages:
1313
description: args
1414
source: hosted
1515
version: "0.13.2"
16+
async:
17+
description: async
18+
source: hosted
19+
version: "1.2.0"
1620
barback:
1721
description: barback
1822
source: hosted
19-
version: "0.15.2+4"
20-
charcode:
21-
description: charcode
22-
source: hosted
23-
version: "1.1.0"
23+
version: "0.15.2+5"
2424
cli_util:
2525
description: cli_util
2626
source: hosted
@@ -40,39 +40,39 @@ packages:
4040
csslib:
4141
description: csslib
4242
source: hosted
43-
version: "0.12.0+1"
43+
version: "0.12.1"
4444
den_api:
4545
description: den_api
4646
source: hosted
4747
version: "0.1.0"
4848
github:
4949
description: github
5050
source: hosted
51-
version: "2.2.2"
51+
version: "2.2.3+1"
5252
glob:
5353
description: glob
5454
source: hosted
55-
version: "1.0.4"
55+
version: "1.0.5"
5656
grinder:
5757
description: grinder
5858
source: hosted
5959
version: "0.7.2"
6060
html:
6161
description: html
6262
source: hosted
63-
version: "0.12.1+1"
63+
version: "0.12.1+2"
6464
http:
6565
description: http
6666
source: hosted
67-
version: "0.11.2"
67+
version: "0.11.3"
6868
http_multi_server:
6969
description: http_multi_server
7070
source: hosted
7171
version: "1.3.2"
7272
http_parser:
7373
description: http_parser
7474
source: hosted
75-
version: "0.0.2+7"
75+
version: "0.0.2+8"
7676
librato:
7777
description: librato
7878
source: hosted
@@ -101,26 +101,18 @@ packages:
101101
description: mustache4dart
102102
source: hosted
103103
version: "1.0.10"
104-
package_config:
105-
description: package_config
106-
source: hosted
107-
version: "0.1.1"
108104
path:
109105
description: path
110106
source: hosted
111-
version: "1.3.5"
107+
version: "1.3.6"
112108
petitparser:
113109
description: petitparser
114110
source: hosted
115111
version: "1.4.3"
116-
plugin:
117-
description: plugin
118-
source: hosted
119-
version: "0.1.0"
120112
pool:
121113
description: pool
122114
source: hosted
123-
version: "1.0.2"
115+
version: "1.1.0"
124116
pub_cache:
125117
description: pub_cache
126118
source: hosted
@@ -140,7 +132,7 @@ packages:
140132
shelf:
141133
description: shelf
142134
source: hosted
143-
version: "0.6.1+2"
135+
version: "0.6.2"
144136
shelf_static:
145137
description: shelf_static
146138
source: hosted
@@ -164,7 +156,7 @@ packages:
164156
stack_trace:
165157
description: stack_trace
166158
source: hosted
167-
version: "1.3.3"
159+
version: "1.3.4"
168160
string_scanner:
169161
description: string_scanner
170162
source: hosted
@@ -176,19 +168,19 @@ packages:
176168
test:
177169
description: test
178170
source: hosted
179-
version: "0.12.3"
171+
version: "0.12.3+7"
180172
unscripted:
181173
description: unscripted
182174
source: hosted
183-
version: "0.6.1+1"
175+
version: "0.6.2"
184176
utf:
185177
description: utf
186178
source: hosted
187179
version: "0.9.0+2"
188180
watcher:
189181
description: watcher
190182
source: hosted
191-
version: "0.9.6"
183+
version: "0.9.7"
192184
when:
193185
description: when
194186
source: hosted

pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ author: Dart Team <[email protected]>
55
description: A documentation generator for Dart.
66
homepage: https://github.com/dart-lang/dartdoc
77
environment:
8-
sdk: '>=1.9.0 <2.0.0'
8+
sdk: '>=1.9.0 <2.0.0' # when we go to 1.12, bump analyzer version
99
dependencies:
10-
analyzer: '>=0.22.0 <0.26.0'
10+
analyzer: '0.25.0' # > 0.25 has new APIs, but only works in 1.12
1111
args: ^0.13.0
1212
cli_util: ^0.0.1
1313
html: ^0.12.1

test/model_test.dart

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,22 +408,36 @@ void main() {
408408
test('docs do not lose brackets in code blocks', () {
409409
expect(topLevelFunction.documentation, contains("['hello from dart']"));
410410
});
411+
412+
test('has source code', () {
413+
expect(topLevelFunction.sourceCode, startsWith(
414+
'/// Top-level function 3 params and 1 optional positional param.'));
415+
expect(topLevelFunction.sourceCode, endsWith('''
416+
String topLevelFunction(int param1, bool param2, Cool coolBeans,
417+
[double optionalPositional = 0.0]) {
418+
return null;
419+
}'''));
420+
});
411421
});
412422

413423
group('Method', () {
414-
Class classB, klass;
415-
Method m, isGreaterThan, m4, m5, m6;
424+
Class classB, klass, HasGenerics;
425+
Method m, isGreaterThan, m4, m5, m6, convertToMap;
416426

417427
setUp(() {
418428
klass = exLibrary.classes.singleWhere((c) => c.name == 'Klass');
419429
classB = exLibrary.classes.singleWhere((c) => c.name == 'B');
430+
HasGenerics =
431+
fakeLibrary.classes.singleWhere((c) => c.name == 'HasGenerics');
420432
m = classB.instanceMethods.first;
421433
isGreaterThan = exLibrary.classes
422434
.singleWhere((c) => c.name == 'Apple').instanceMethods
423435
.singleWhere((m) => m.name == 'isGreaterThan');
424436
m4 = classB.instanceMethods[1];
425437
m5 = klass.instanceMethods.singleWhere((m) => m.name == 'another');
426438
m6 = klass.instanceMethods.singleWhere((m) => m.name == 'toString');
439+
convertToMap = HasGenerics.instanceMethods
440+
.singleWhere((m) => m.name == 'convertToMap');
427441
});
428442

429443
test('overriden method', () {
@@ -457,6 +471,11 @@ void main() {
457471
expect(comment, equals('Another method'));
458472
expect(comment2, equals('A shadowed method'));
459473
});
474+
475+
test('method source code indents correctly', () {
476+
expect(convertToMap.sourceCode,
477+
startsWith(' /// Converts itself to a map.'));
478+
});
460479
});
461480

462481
group('Field', () {

0 commit comments

Comments
 (0)