Skip to content

Commit

Permalink
GROOVY-6712: Make GroovyDoc aware of Traits (initial spike)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulk-asert committed Apr 28, 2014
1 parent 861990d commit a8fe9b3
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -1053,6 +1053,11 @@ public void visitSuperCtorCall(GroovySourceAST t, int visit) {
while (itr.hasNext()) {((Visitor)itr.next()).visitSuperCtorCall(t,visit);}
}

public void visitTraitDef(GroovySourceAST t, int visit) {
Iterator itr = itr(visit);
while (itr.hasNext()) {((Visitor)itr.next()).visitTraitDef(t,visit);}
}

public void visitTripleDot(GroovySourceAST t, int visit) {
Iterator itr = itr(visit);
while (itr.hasNext()) {((Visitor)itr.next()).visitTripleDot(t,visit);}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -188,6 +188,7 @@ protected void accept(GroovySourceAST currentNode) {
case GroovyTokenTypes.SR: // a >> b
case GroovyTokenTypes.STAR: // a * b or import foo.*
case GroovyTokenTypes.STAR_STAR: // x ** 3
case GroovyTokenTypes.TRAIT_DEF: // trait Foo...
accept_FirstChild_v_RestOfTheChildren(t);
break;

Expand Down
13 changes: 11 additions & 2 deletions src/main/org/codehaus/groovy/antlr/treewalker/SourcePrinter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -907,7 +907,16 @@ private String escape(String literal) {
public void visitSuperCtorCall(GroovySourceAST t,int visit) {
printUpdatingTabLevel(t,visit,"super("," ",")");
}


public void visitTraitDef(GroovySourceAST t,int visit) {
print(t,visit,"trait ",null,null);

if (visit == OPENING_VISIT) {
// store name of class away for use in constructor ident
className = t.childOfType(GroovyTokenTypes.IDENT).getText();
}
}

// visit TripleDot, not used in the AST

public void visitType(GroovySourceAST t,int visit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -259,6 +259,7 @@ protected void visitNode(GroovySourceAST ast, int n) {
case GroovyTokenTypes.STRING_LITERAL : v.visitStringLiteral(ast,n); break;
case GroovyTokenTypes.STRING_NL : v.visitStringNl(ast,n); break;
case GroovyTokenTypes.SUPER_CTOR_CALL : v.visitSuperCtorCall(ast,n); break;
case GroovyTokenTypes.TRAIT_DEF : v.visitTraitDef(ast,n); break;
case GroovyTokenTypes.TRIPLE_DOT : v.visitTripleDot(ast,n); break;
case GroovyTokenTypes.TYPE : v.visitType(ast,n); break;
case GroovyTokenTypes.TYPECAST : v.visitTypecast(ast,n); break;
Expand Down
3 changes: 2 additions & 1 deletion src/main/org/codehaus/groovy/antlr/treewalker/Visitor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2010 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -229,6 +229,7 @@ public interface Visitor {
void visitStringLiteral(GroovySourceAST t, int visit);
void visitStringNl(GroovySourceAST t, int visit);
void visitSuperCtorCall(GroovySourceAST t, int visit);
void visitTraitDef(GroovySourceAST t, int visit);
void visitTripleDot(GroovySourceAST t, int visit);
void visitType(GroovySourceAST t, int visit);
void visitTypecast(GroovySourceAST t, int visit);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2007 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -227,6 +227,7 @@ public void setUp() {}
public void visitStringLiteral(GroovySourceAST t,int visit) {visitDefault(t,visit);}
public void visitStringNl(GroovySourceAST t,int visit) {visitDefault(t,visit);}
public void visitSuperCtorCall(GroovySourceAST t,int visit) {visitDefault(t,visit);}
public void visitTraitDef(GroovySourceAST t,int visit) {visitDefault(t,visit);}
public void visitTripleDot(GroovySourceAST t,int visit) {visitDefault(t,visit);}
public void visitType(GroovySourceAST t,int visit) {visitDefault(t,visit);}
public void visitTypecast(GroovySourceAST t,int visit) {visitDefault(t,visit);}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2012 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -96,6 +96,11 @@ public void visitInterfaceDef(GroovySourceAST t, int visit) {
visitClassDef(t, visit);
}

@Override
public void visitTraitDef(GroovySourceAST t, int visit) {
visitClassDef(t, visit);
}

@Override
public void visitEnumDef(GroovySourceAST t, int visit) {
visitClassDef(t, visit);
Expand Down Expand Up @@ -396,7 +401,7 @@ private boolean isNested() {
private boolean isTopLevelConstruct(GroovySourceAST node) {
if (node == null) return false;
int type = node.getType();
return type == CLASS_DEF || type == INTERFACE_DEF || type == ANNOTATION_DEF || type == ENUM_DEF;
return type == CLASS_DEF || type == INTERFACE_DEF || type == TRAIT_DEF || type == ANNOTATION_DEF || type == ENUM_DEF;
}

private void adjustForAutomaticEnumMethods(SimpleGroovyClassDoc currentClassDoc) {
Expand Down Expand Up @@ -599,7 +604,7 @@ private String getJavaDocCommentsBeforeNode(GroovySourceAST t) {
private boolean isMajorType(GroovySourceAST t) {
if (t == null) return false;
int tt = t.getType();
return tt == CLASS_DEF || tt == INTERFACE_DEF || tt == METHOD_DEF || tt == ANNOTATION_DEF || tt == ENUM_DEF ||
return tt == CLASS_DEF || tt == TRAIT_DEF || tt == INTERFACE_DEF || tt == METHOD_DEF || tt == ANNOTATION_DEF || tt == ENUM_DEF ||
tt == VARIABLE_DEF || tt == ANNOTATION_FIELD_DEF || tt == ENUM_CONSTANT_DEF || tt == CTOR_IDENT;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2011 the original author or authors.
* Copyright 2003-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -135,6 +135,10 @@ public boolean isScript() {
return definitionType == CLASS_DEF && isScript;
}

public boolean isTrait() {
return definitionType == TRAIT_DEF;
}

public boolean isInterface() {
return definitionType == INTERFACE_DEF;
}
Expand All @@ -149,13 +153,15 @@ public boolean isEnum() {

public String getTypeDescription() {
if (isInterface()) return "Interface";
if (isTrait()) return "Trait";
if (isAnnotationType()) return "Annotation Type";
if (isEnum()) return "Enum";
return "Class";
}

public String getTypeSourceDescription() {
if (isInterface()) return "interface";
if (isTrait()) return "trait";
if (isAnnotationType()) return "@interface";
if (isEnum()) return "enum";
return "class";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ <h2 title="${classDesc}" class="title">${classDesc}</h2>
if (interfaces) {
%>
<dl>
<dt>All Implemented Interfaces:</dt>
<dt>All Implemented Interfaces and Traits:</dt>
<dd>${interfaces.collect{ linkable(it) }.join(', ')}</dd>
</dl>
<!-- todo: direct known subclasses -->
Expand Down Expand Up @@ -439,7 +439,7 @@ <h3>Constructor Summary</h3>
classes.addAll(classDoc.interfaces().toList())
} else {
if (classDoc.superclass()) classes += classDoc.superclass()
else classes += new org.codehaus.groovy.tools.groovydoc.ExternalGroovyClassDoc(Object.class)
else if (!classDoc.isTrait()) classes += new org.codehaus.groovy.tools.groovydoc.ExternalGroovyClassDoc(Object.class)
}
Set visited = [classDoc] as Set
while (classes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// TODO enable errors(), enums() etc in SimpleGroovyPackageDoc then replace closures below
def classTypes = [
"Interface" : { it.isInterface() },
"Trait" : { it.isTrait() },
"Class" : { it.isClass() && !it.parentClasses*.qualifiedTypeName().contains('java.lang.Throwable') },
"Enum" : { it.isEnum() },
"Exception" : { it.isClass() && it.parentClasses*.qualifiedTypeName().contains('java.lang.Exception') },
Expand Down Expand Up @@ -40,9 +41,9 @@ <h2>${pluralize(k)}</h2>
<%
for (classDoc in packageDoc.allClasses().findAll{ isVisible(it) && v(it) }) {
%><li><a href="${classDoc.name()}.html" title="${k.toLowerCase()} in ${packageDoc.nameWithDots()}" target="classFrame"><%
if (classDoc.isInterface()) { %><em><% }
if (classDoc.isInterface() || classDoc.isTrait()) { %><em><% }
%>${classDoc.name()}<%
if (classDoc.isInterface()) { %></em><% }
if (classDoc.isInterface() || classDoc.isTrait()) { %></em><% }
%></a></li><%
}
%>
Expand Down

0 comments on commit a8fe9b3

Please sign in to comment.