use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class OcniExtractor method findBlockComments.
/**
* Finds all block comments and associates them with their containing type.
* This is trickier than you might expect because of inner types.
*/
private static ListMultimap<TreeNode, Comment> findBlockComments(CompilationUnit unit) {
ListMultimap<TreeNode, Comment> blockComments = MultimapBuilder.hashKeys().arrayListValues().build();
for (Comment comment : unit.getCommentList()) {
if (!comment.isBlockComment()) {
continue;
}
int commentPos = comment.getStartPosition();
AbstractTypeDeclaration containingType = null;
int containingTypePos = -1;
for (AbstractTypeDeclaration type : unit.getTypes()) {
int typePos = type.getStartPosition();
if (typePos < 0) {
continue;
}
int typeEnd = typePos + type.getLength();
if (commentPos > typePos && commentPos < typeEnd && typePos > containingTypePos) {
containingType = type;
containingTypePos = typePos;
}
}
blockComments.put(containingType != null ? containingType : unit, comment);
}
return blockComments;
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class JavadocConverter method visitText.
@Override
public Void visitText(TextTree node, TagElement tag) {
String[] lines = node.getBody().split("\n");
int linePos = pos(node);
for (String line : lines) {
if (line.length() > 0) {
linePos = source.indexOf(line, linePos);
int endPos = linePos + line.length();
TreeNode newNode = setPos(new TextElement().setText(line), linePos, endPos);
tag.addFragment(newNode);
}
}
return null;
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class JavadocConverter method visitReference.
@Override
public Void visitReference(ReferenceTree node, TagElement tag) {
DCTree.DCReference ref = (DCTree.DCReference) node;
JCTree qualifier = ref.qualifierExpression;
TreeNode newNode;
if (qualifier != null && qualifier.getKind() == com.sun.source.tree.Tree.Kind.MEMBER_SELECT) {
newNode = convertQualifiedName(qualifier);
} else {
newNode = new TextElement().setText(node.getSignature());
}
tag.addFragment(setPos(node, newNode));
return null;
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class JavadocConverter method convertJavadoc.
/**
* Returns an AST node for the javadoc comment of a specified class,
* method, or field element.
*/
static Javadoc convertJavadoc(Element element, String source, JavacEnvironment env, boolean reportWarnings) {
DocTrees docTrees = DocTrees.instance(env.task());
TreePath path = docTrees.getPath(element);
if (path == null) {
throw new AssertionError("could not find tree path for element");
}
DCTree.DCDocComment docComment = (DCTree.DCDocComment) docTrees.getDocCommentTree(path);
if (docComment == null) {
// Declaration does not have a javadoc comment.
return null;
}
JavadocConverter converter = new JavadocConverter(element, docComment, source, docTrees, path.getCompilationUnit(), reportWarnings);
Javadoc result = new Javadoc();
// First tag has no name.
TagElement newTag = new TagElement();
converter.scan(docComment.getFirstSentence(), newTag);
converter.scan(docComment.getBody(), newTag);
if (!newTag.getFragments().isEmpty()) {
List<TreeNode> fragments = newTag.getFragments();
int start = fragments.get(0).getStartPosition();
TreeNode lastFragment = fragments.get(fragments.size() - 1);
int end = start + lastFragment.getLength();
converter.setPos(newTag, start, end);
result.addTag(newTag);
}
for (DocTree tag : docComment.getBlockTags()) {
if (tag.getKind() != DocTree.Kind.ERRONEOUS) {
newTag = new TagElement();
converter.scan(tag, newTag);
result.addTag(newTag);
}
}
return result;
}
use of com.google.devtools.j2objc.ast.TreeNode in project j2objc by google.
the class JavadocGenerator method getSourceIndent.
/**
* Fetch the leading whitespace from the comment line. Since the JDT
* strips leading and trailing whitespace from lines, the original
* source is fetched and is walked backwards from the fragment's start
* until the previous new line, then moved forward if there is a leading
* "* ".
*/
private String getSourceIndent(TreeNode fragment) {
int index = fragment.getStartPosition();
if (index < 1) {
return "";
}
TreeNode node = fragment.getParent();
while (node != null && node.getKind() != TreeNode.Kind.COMPILATION_UNIT) {
node = node.getParent();
}
if (node instanceof CompilationUnit) {
String source = ((CompilationUnit) node).getSource();
int i = index - 1;
char c;
while (i >= 0 && (c = source.charAt(i)) != '\n') {
if (c != '*' && !Character.isWhitespace(c)) {
// Pre tag embedded in other text, so no indent.
return "";
}
--i;
}
String lineStart = source.substring(i + 1, index);
i = lineStart.indexOf('*');
if (i == -1) {
return lineStart;
}
// Indent could end with '*' instead of "* ", if there's no text after it.
return (++i + 1) < lineStart.length() ? lineStart.substring(i + 1) : lineStart.substring(i);
}
return "";
}
Aggregations