Skip to content

Commit

Permalink
Inherited fields (#175)
Browse files Browse the repository at this point in the history
* view source is button with icon

* Add inherited fields

* fields and methods are ordered by inherited type
* added a specific function in the api, to end up with simple template markup
* to use `map.get` in the template we *need* `dce=no` in run.hxml
* the type in printFieldSignature is needed for correct links of inherited members
* all is invisible when there is no inheritance
  • Loading branch information
markknol authored and Simn committed Apr 24, 2016
1 parent c8ce22e commit 04d0c08
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 12 deletions.
2 changes: 1 addition & 1 deletion run.hxml
Expand Up @@ -5,4 +5,4 @@
-cp src
-neko run.n
-main dox.Dox
-dce full
-dce no
63 changes: 62 additions & 1 deletion src/dox/Api.hx
Expand Up @@ -366,6 +366,60 @@ class Api {
return allFields;
}

/**
Returns inherited fields/methods of `c`. Sorted by field-name ordered by type in a map.
**/
public function getInheritedFields(c:Classdef):InheritedFields {
var oc = c;

var inheritedFields:InheritedFields = {
methods: new Map<Classdef,Array<ClassField>>(),
fields:new Map<Classdef,Array<ClassField>>(),
types: [],
}

var allFields = [];
var fieldMap = new Map();
function loop(c:Classdef) {
for (cf in c.fields) {
if (!fieldMap.exists(cf.name) || cf.overloads != null) {
if (c != oc) allFields.push({ field: cf, definedBy: c});
fieldMap[cf.name] = true;
}
}
if (c.superClass != null) {
var cSuper:Classdef = cast infos.typeMap[c.superClass.path];
if (cSuper != null) { // class is not part of documentation
inheritedFields.types.push(cSuper);
inheritedFields.methods.set(cSuper, []);
inheritedFields.fields.set(cSuper, []);
loop(cSuper);
}
}
}
loop(c);

function addFieldTo(f, map) {
var fields = map.exists(f.definedBy) ? map.get(f.definedBy) : [];
fields.push(f.field);
map.set(f.definedBy, fields);
}
for (f in allFields) {
if (isMethod(f.field))
addFieldTo(f, inheritedFields.methods);
else
addFieldTo(f, inheritedFields.fields);
}
for (fields in inheritedFields.methods) {
fields.sort(function(f1, f2) return Reflect.compare(f1.name, f2.name));
}
for (fields in inheritedFields.fields) {
fields.sort(function(f1, f2) return Reflect.compare(f1.name, f2.name));
}

return inheritedFields;
}

private function sanitizePath(path:String) {
return ~/Index$/.replace(path, "$Index");
}
Expand Down Expand Up @@ -423,4 +477,11 @@ typedef FieldModifiers = {
typedef MemberField = {
field: ClassField,
definedBy: Classdef
}
}

typedef InheritedFields = {
methods: Map<Classdef,Array<ClassField>>,
fields: Map<Classdef,Array<ClassField>>,
// defines order of the types since keys in maps arent ordered
types:Array<Classdef>,
}
9 changes: 9 additions & 0 deletions themes/default/resources/index.js
Expand Up @@ -126,6 +126,15 @@ $(document).ready(function(){
$(this.parentElement).addClass("active");
}
});

$("a.expand-button").click(function (e) {
var container = $(this).parent().next();
container.toggle();
$("i", this).removeClass("fa-arrow-circle-o-down")
.removeClass("fa-arrow-circle-o-right")
.addClass(container.is(":visible") ? "fa-arrow-circle-o-down" : "fa-arrow-circle-o-right");
return false;
});

// Because there is no CSS parent selector
$("code.prettyprint").parents("pre").addClass("example");
Expand Down
6 changes: 6 additions & 0 deletions themes/default/resources/styles.css
Expand Up @@ -320,6 +320,12 @@ pre.example:before {
border: 1px solid rgba(0,0,0,0.15);
}

.inherited-fields {
padding: 5px 20px;
margin-top:1em;
background:#fcfcfc;
}

@media (max-width: 767px) {
.navbar-fixed-top, .navbar-fixed-bottom, .navbar-static-top {
margin-right: 0;
Expand Down
11 changes: 6 additions & 5 deletions themes/default/templates/class.mtt
Expand Up @@ -25,20 +25,19 @@
$$printModule(::type.module::)

<h4 ::cond subClasses !=null::>
<small>
<small>
::set title = "extended by "::
::set infos = subClasses::
::use "related_types.mtt"::::end::
</small>
</small>
</h4>
<h4 ::cond implementors !=null::>
<small>
<small>
::set title = "implemented by "::
::set infos = implementors::
::use "related_types.mtt"::::end::
</small>
</small>
</h4>

$$printPlatforms(::type.platforms::,::true::)
</div>
<div class="body">
Expand All @@ -48,5 +47,7 @@
</div>
</div>
$$printClassBody(::type::)

$$printInheritedFields(::type::)
</div>
::end::
4 changes: 2 additions & 2 deletions themes/default/templates/class_field.mtt
Expand Up @@ -9,10 +9,10 @@

<a ::attr name field.name::></a>
<h3>
<p>$$printFieldSignature(::field::,::isStatic::)</p>
<p>$$printFieldSignature(::field::,::isStatic::,::type::)</p>
::if field.overloads != null::
::foreach field field.overloads::
<p>$$printFieldSignature(::field::,::isStatic::)</p>
<p>$$printFieldSignature(::field::,::isStatic::,::type::)</p>
::end::
::end::
</h3>
Expand Down
48 changes: 45 additions & 3 deletions themes/default/templates/macros.mtt
Expand Up @@ -67,11 +67,11 @@

<macro name="printModule(module)">
<p ::cond module != null && module != "StdTypes"::>
<h4><small>import <a ::attr href api.pathToUrl(module)::>::module::</a></small></h4>
<h4><small>import <a ::attr href api.pathToUrl(module)::>::module::</a></small></h4>
</p>
</macro>

<macro name="printFieldSignature(field, isStatic)">
<macro name="printFieldSignature(field, isStatic, type)">
<code>
::if !field.isPublic::
<span class="label">private</span>
Expand Down Expand Up @@ -101,7 +101,7 @@
<span class="label">write only</span>
::end::

<a href="#::field.name::"><span class="identifier">::field.name::</span></a>:$$printLinkedType(::field.type::)$$printInitExpr(::field.expr::)
<a href="::api.pathToUrl(type.path)::#::field.name::"><span class="identifier">::field.name::</span></a>:$$printLinkedType(::field.type::)$$printInitExpr(::field.expr::)

::case 2::
<a href="#::field.name::"><span class="identifier">::field.name::</span></a>
Expand Down Expand Up @@ -221,6 +221,48 @@
::end::
</macro>

<macro name="printInheritedFields(type)">
::if type.superClass != null::
::set inheritedFields = api.getInheritedFields(type)::
::set hasFields = inheritedFields.fields.keys().hasNext()::
::set hasMethods = inheritedFields.methods.keys().hasNext()::

<div class="inherited-fields well" ::cond hasFields || hasMethods::>
<h3 class="section" ::cond hasFields::>Inherited Variables</h3>
<div class="fields" ::cond hasFields::>
::set fields=inheritedFields.fields::
::foreach cl inheritedFields.types::
<h4 ::cond fields.get(cl).length>0::><a href="#" class="expand-button"><i class="fa fa-arrow-circle-o-right"></i></a> Defined by $$printLinkedPath(::cl.path::,::null::)</h4>
<div style="display:none">
::foreach field fields.get(cl)::
::set isStatic = false::
::set type=cl::
::use "class_field.mtt"::::end::
::end::
</div>
::end::
</div>

<h3 class="section" ::cond hasMethods::>Inherited Methods</h3>
<div class="fields" ::cond hasMethods::>
::set fields=inheritedFields.methods::
::foreach cl inheritedFields.types::
<h4 ::cond fields.get(cl).length>0::><a href="#" class="expand-button"><i class="fa fa-arrow-circle-o-right"></i></a> Defined by $$printLinkedPath(::cl.path::,::null::)</h4>
<div style="display:none">
::foreach field fields.get(cl)::
::set isStatic = false::
::if field.name != "new" ::
::set type=cl::
::use "class_field.mtt"::::end::
::end::
::end::
</div>
::end::
</div>
</div>
::end::
</macro>

<macro name="printPackage(type)">
::if type.path.indexOf(".") >= 0::
::set packagePath = type.path.split(".").slice(0, -1).join(".")::
Expand Down

0 comments on commit 04d0c08

Please sign in to comment.