Search in sources :

Example 1 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class RemoveSemiColonRefactoring method removeSuperfluousSemiColons.

private boolean removeSuperfluousSemiColons(ASTNode node, int start, int end) {
    if (end <= start) {
        return VISIT_SUBTREE;
    }
    boolean result = VISIT_SUBTREE;
    final Map<String, SourceLocation> nonCommentsStrings = getNonCommentsStrings(node, start, end);
    for (Entry<String, SourceLocation> entry : nonCommentsStrings.entrySet()) {
        final String s = entry.getKey();
        final Matcher m = Pattern.compile("\\s*(;+)\\s*").matcher(s);
        while (m.find()) {
            int startPos = entry.getValue().getStartPosition();
            SourceLocation toRemove = fromPositions(startPos + m.start(1), startPos + m.end(1));
            this.ctx.getRefactorings().remove(toRemove);
            result = DO_NOT_VISIT_SUBTREE;
        }
    }
    return result;
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) Matcher(java.util.regex.Matcher)

Example 2 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class RemoveSemiColonRefactoring method putResult.

private void putResult(String source, int start, int end, final LinkedHashMap<String, SourceLocation> results) {
    final SourceLocation sourceLoc = fromPositions(start, end);
    final String s = sourceLoc.substring(source);
    results.put(s, sourceLoc);
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation)

Example 3 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class RemoveEmptyLinesRefactoring method maybeRemoveEmptyLines.

private boolean maybeRemoveEmptyLines(String source, int endOfLineIndex, int newLineIndex) {
    if (endOfLineIndex < newLineIndex) {
        Matcher matcher = NEWLINE_PATTERN.matcher(source).region(endOfLineIndex, newLineIndex);
        boolean isEqualToNewline = matcher.matches();
        if (!isEqualToNewline && matcher.find() && matcher.end() < newLineIndex) {
            final SourceLocation toRemove = SourceLocation.fromPositions(matcher.end(), newLineIndex);
            this.ctx.getRefactorings().remove(toRemove);
            return true;
        }
    }
    return false;
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) Matcher(java.util.regex.Matcher)

Example 4 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method getIndentForJavadoc.

private SourceLocation getIndentForJavadoc(LineComment lineComment, String source, TreeSet<Integer> lineStarts) {
    final SourceLocation indentLoc = getIndent(lineComment, lineStarts);
    final Matcher matcher = INDENT.matcher(source).region(indentLoc.getStartPosition(), indentLoc.getEndPosition());
    if (matcher.matches()) {
        return indentLoc;
    }
    return SourceLocation.fromPositions(0, 0);
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) Matcher(java.util.regex.Matcher)

Example 5 with SourceLocation

use of org.autorefactor.refactoring.SourceLocation in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method overlaps.

private boolean overlaps(TextEdit edits, TextEdit edit2) {
    final SourceLocation range = toSourceLoc(edit2);
    final AtomicBoolean overlaps = new AtomicBoolean();
    edits.accept(new TextEditVisitor() {

        @Override
        public boolean visit(MultiTextEdit edit) {
            // move on there is nothing to check here
            return VISIT_SUBTREE;
        }

        @Override
        public boolean visitNode(TextEdit edit) {
            if (!overlaps.get()) {
                overlaps.set(range.overlapsWith(toSourceLoc(edit)));
            }
            return !overlaps.get();
        }
    });
    return overlaps.get();
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

SourceLocation (org.autorefactor.refactoring.SourceLocation)10 Matcher (java.util.regex.Matcher)3 Comment (org.eclipse.jdt.core.dom.Comment)3 LineComment (org.eclipse.jdt.core.dom.LineComment)3 BlockComment (org.eclipse.jdt.core.dom.BlockComment)2 InsertEdit (org.eclipse.text.edits.InsertEdit)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 LinkedHashMap (java.util.LinkedHashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NotImplementedException (org.autorefactor.util.NotImplementedException)1 Pair (org.autorefactor.util.Pair)1 Javadoc (org.eclipse.jdt.core.dom.Javadoc)1 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)1 TextEdit (org.eclipse.text.edits.TextEdit)1 TextEditVisitor (org.eclipse.text.edits.TextEditVisitor)1