Skip to content

Commit

Permalink
Merge pull request #270 from dart-lang/const-constructors
Browse files Browse the repository at this point in the history
Const constructors
  • Loading branch information
sethladd committed Mar 15, 2015
2 parents 71aa629 + d2f5efc commit ea246ef
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 17 deletions.
42 changes: 28 additions & 14 deletions lib/src/html_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class HtmlGenerator extends Generator {

final String generatedOn;

// TODO: fix this to be a single map, just like _partialTemplates
static final String indexTemplate = _loadTemplate('templates/index.html');
static final String libraryTemplate =
_loadTemplate('templates/library.html');
Expand All @@ -37,13 +38,14 @@ class HtmlGenerator extends Generator {
_loadTemplate('templates/function.html');
static final String methodTemplate =
_loadTemplate('templates/method.html');
static final String constructorTemplate =
_loadTemplate('templates/constructor.html');

static final Map partials = {
'footer': _loadTemplate('templates/_footer.html'),
'head': _loadTemplate('templates/_head.html'),
'styles_and_scripts':
_loadTemplate('templates/_styles_and_scripts.html')
};
static final Map<String, String> _partialTemplates = {};

static String _partial(String name) {
return _partialTemplates.putIfAbsent(name, () => _loadTemplate('templates/_$name.html'));
}

HtmlGenerator(this._url)
: generatedOn = new DateFormat('MMMM dd yyyy').format(new DateTime.now());
Expand All @@ -60,6 +62,10 @@ class HtmlGenerator extends Generator {
lib.getClasses().forEach((Class clazz) {
generateClass(package, lib, clazz);

clazz.constructors.forEach((constructor) {
generateConstructor(package, lib, clazz, constructor);
});

clazz.instanceMethods.forEach((method) {
generateMethod(package, lib, clazz, method);
});
Expand Down Expand Up @@ -126,6 +132,21 @@ class HtmlGenerator extends Generator {
_writeFile(path.joinAll(clazz.href.split('/')), classTemplate, data);
}

void generateConstructor(Package package, Library lib, Class clazz,
Constructor constructor) {
Map data = {
'package': package,
'generatedOn': generatedOn,
'markdown': renderMarkdown,
'oneLiner': oneLiner,
'library': lib,
'class': clazz,
'constructor': constructor
};

_writeFile(path.joinAll(constructor.href.split('/')), constructorTemplate, data);
}

void generateEnum(Package package, Library lib, Class eNum) {
Map data = {
'package': package,
Expand Down Expand Up @@ -186,7 +207,7 @@ class HtmlGenerator extends Generator {
void _writeFile(String filename, String template, Map data) {
File f = _createOutputFile(filename);
String content = render(template, data,
partial: _partials,
partial: _partial,
assumeNullNonExistingProperty: false,
errorOnMissingProperty: true);
f.writeAsStringSync(content);
Expand All @@ -200,13 +221,6 @@ class HtmlGenerator extends Generator {
return contents;
}

static String _partials(String name) {
String partial = partials[name];
if (partial == null) {
throw "Could not find partial '$name'";
}
return partial;
}
}

// TODO: parse the custom dartdoc formatting brackets
Expand Down
4 changes: 3 additions & 1 deletion lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,9 @@ class Constructor extends ModelElement {

@override
String get _href =>
'${library.name}/${_constructor.enclosingElement.name}.html#$name';
'${library.name}/${_constructor.enclosingElement.name}/$name.html';

bool get isConst => _constructor.isConst;

@override
String get name {
Expand Down
9 changes: 7 additions & 2 deletions templates/class.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,17 @@ <h4>Implementors</h4>
<section class="summary" id="constructors">
<h2>Constructors</h2>

<dl class="dl-horizontal">
<dl class="constructor-summary-list">
{{#class.constructors}}
<dt id="{{htmlId}}">
{{{linkedName}}}
<code>{{{linkedName}}}({{{linkedParams}}})</code>
</dt>
<dd>
{{#isConst}}
<div class="constructor-modifier">
const
</div>
{{/isConst}}
{{#oneLiner}}{{ documentation }}{{/oneLiner}}
</dd>
{{/class.constructors}}
Expand Down
58 changes: 58 additions & 0 deletions templates/constructor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{{>head}}
<title>{{constructor.name}} constructor - {{class.name}} class - {{ library.name }} library - Dart API</title>

<!-- required because all the links are pseudo-absolute -->
<base href="../..">

{{>styles_and_scripts}}

<meta name="description" content="API docs for the {{constructor.name}} constructor from the {{class.name}} class {{ library.name }} library, for the Dart programming language.">

</head>

<body>

<header class="container-fluid" id="title">
<!-- TODO: align this -->
<div class="container">
<ul class="breadcrumbs gt-separated">
<li><a href="{{package.href}}">{{package.name}} package</a></li>
<li><a href="{{library.href}}">{{library.name}} library</a></li>
<li><a href="{{class.href}}">{{class.name}} class</a></li>
</ul>
<h1 class="title">
{{ constructor.name }}
<span class="type">
{{#constructor.isConst}}const {{/constructor.isConst}}constructor
</span>
</h1>
</div>
</header>

<div class="container">

<section class="multi-line-signature">
<code>
{{#constructor}}
{{#isConst}}const{{/isConst}}
<span class="name">{{ name }}</span>({{#hasParameters}}
<br>
<div class="parameters">
{{{linkedParamsLines}}}
</div>
{{/hasParameters}})
{{/constructor}}
</code>
</section>

<section class="desc markdown">

{{#markdown}}
{{{ constructor.documentation }}}
{{/markdown}}

</section>

</div>

{{>footer}}
9 changes: 9 additions & 0 deletions templates/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,12 @@ section {
#declared-in {
font-style: italic;
}

dl.constructor-summary-list dd {
margin-left: 18px;
color: gray;
}

.constructor-modifier {
font-style: italic;
}
14 changes: 14 additions & 0 deletions test/fake_package/lib/fake.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,23 @@ class Annotation {
const Annotation(this.value);
}

/// For make-better testing of constants.
///
/// Make one of these neato classes like this:
///
/// `var constant = const ConstantClass('neat')`
class ConstantClass {
final String value;

/// Make compile-time constants with this constructor!
/// Go ahead, it's fun.
const ConstantClass(this.value);

/// A named compile-time constant constructor.
const ConstantClass.isVeryConstant(this.value);

/// Not actually constant.
ConstantClass.notConstant(this.value);
}

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

0 comments on commit ea246ef

Please sign in to comment.