use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.
the class RemoveSemiColonRefactoring method getNonCommentsStrings.
private Map<String, SourceLocation> getNonCommentsStrings(ASTNode node, int start, int end) {
final List<Comment> comments = filterCommentsInRange(start, end, node.getRoot());
final String source = ctx.getSource(node);
final LinkedHashMap<String, SourceLocation> results = new LinkedHashMap<String, SourceLocation>();
if (comments.isEmpty()) {
putResult(source, start, end, results);
} else {
int nextStart = start;
for (Comment comment : comments) {
if (nextStart < comment.getStartPosition()) {
putResult(source, nextStart, comment.getStartPosition(), results);
}
nextStart = getEndPosition(comment);
}
}
return results;
}
use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.
the class CommentsRefactoring method visit.
@Override
public boolean visit(CompilationUnit node) {
this.astRoot = node;
for (Comment comment : getCommentList(astRoot)) {
comments.add(Pair.of(new SourceLocation(comment), comment));
}
for (Comment comment : getCommentList(astRoot)) {
if (comment.isBlockComment()) {
final BlockComment bc = (BlockComment) comment;
bc.accept(this);
} else if (comment.isLineComment()) {
final LineComment lc = (LineComment) comment;
lc.accept(this);
} else if (comment.isDocComment()) {
final Javadoc jc = (Javadoc) comment;
jc.accept(this);
} else {
throw new NotImplementedException(comment);
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.
the class CommentsRefactoring method betterCommentExist.
private boolean betterCommentExist(Comment comment, ASTNode nodeWhereToAddJavadoc) {
if (hasJavadoc(nodeWhereToAddJavadoc)) {
return true;
}
final SourceLocation nodeLoc = new SourceLocation(nodeWhereToAddJavadoc);
SourceLocation bestLoc = new SourceLocation(comment);
Comment bestComment = comment;
for (Iterator<Pair<SourceLocation, Comment>> iter = this.comments.iterator(); iter.hasNext(); ) {
final Pair<SourceLocation, Comment> pair = iter.next();
final SourceLocation newLoc = pair.getFirst();
final Comment newComment = pair.getSecond();
if (newLoc.compareTo(bestLoc) < 0) {
// since comments are visited in ascending order,
// we can forget this comment.
iter.remove();
continue;
}
if (nodeLoc.compareTo(newLoc) < 0) {
break;
}
if (bestLoc.compareTo(newLoc) < 0) {
if (!(newComment instanceof LineComment)) {
// new comment is a BlockComment or a Javadoc
bestLoc = newLoc;
bestComment = newComment;
continue;
} else if (!(bestComment instanceof LineComment)) {
// new comment is a line comment and best comment is not
bestLoc = newLoc;
bestComment = newComment;
continue;
}
}
}
return bestComment != null && bestComment != comment;
}
use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method replaceLineCommentBeforeJavaElement.
private void replaceLineCommentBeforeJavaElement(List<TextEdit> commentEdits, LineComment lineComment, List<LineComment> lineComments, int i, String source, TreeSet<Integer> lineStarts) {
final int replaceLength = "//".length();
final boolean isFirst = i == 0;
String replacementText;
final SourceLocation indentLoc = getIndentForJavadoc(lineComment, source, lineStarts);
if (isFirst) {
// TODO JNR how to obey configured indentation?
replacementText = "/**" + lineSeparator + indentLoc.substring(source) + " *";
} else {
replacementText = " *";
}
final boolean commentStartsWithSlash = source.charAt(lineComment.getStartPosition() + replaceLength) == '/';
if (commentStartsWithSlash) {
replacementText += " ";
}
commentEdits.add(new ReplaceEdit(lineComment.getStartPosition(), replaceLength, replacementText));
replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
final boolean isLast = i == lineComments.size() - 1;
if (isLast) {
// TODO JNR how to obey configured indentation?
final int position = getEndPosition(lineComment);
commentEdits.add(new InsertEdit(position, lineSeparator + indentLoc.substring(source) + " */"));
}
}
use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method addSingleLineCommentToJavadocEdits.
private void addSingleLineCommentToJavadocEdits(List<TextEdit> commentEdits, ASTNode nextNode, List<LineComment> lineComments, String source, TreeSet<Integer> lineStarts) {
final int nodeStart = nextNode.getStartPosition();
final LineComment lineComment = lineComments.get(0);
// TODO JNR how to obey configured indentation?
// TODO JNR how to obey configured line length?
final int commentStart = lineComment.getStartPosition();
if (commentStart < nodeStart) {
// assume comment is situated exactly before target node for javadoc
final String spaceAtStart = getSpaceAtStart(source, lineComment);
commentEdits.add(new ReplaceEdit(commentStart, "//".length(), "/**" + spaceAtStart));
commentEdits.add(new InsertEdit(getEndPosition(lineComment), getSpaceAtEnd(source, lineComment) + "*/"));
replaceEndsOfBlockCommentFromCommentText(commentEdits, lineComment, source);
} else {
// assume comment is situated exactly after target node for javadoc
final StringBuilder newJavadoc = new StringBuilder().append("/**").append(getSpaceAtStart(source, lineComment));
appendCommentTextReplaceEndsOfBlockComment(newJavadoc, lineComment, source);
SourceLocation indent = getIndent(nextNode, lineStarts);
newJavadoc.append(getSpaceAtEnd(source, lineComment)).append("*/").append(lineSeparator).append(source, indent.getStartPosition(), indent.getEndPosition());
commentEdits.add(new InsertEdit(nodeStart, newJavadoc.toString()));
deleteLineCommentAfterNode(commentEdits, source, lineComment);
}
}
Aggregations