Search in sources :

Example 11 with InvalidInputException

use of org.eclipse.jdt.core.compiler.InvalidInputException in project eclipse.jdt.ls by eclipse.

the class CommentAnalyzer method normalizeReference.

/**
 * Removes comments and whitespace
 *
 * @param reference
 *            the type reference
 * @return the reference only consisting of dots and java identifier
 *         characters
 */
public static String normalizeReference(String reference) {
    IScanner scanner = ToolFactory.createScanner(false, false, false, false);
    scanner.setSource(reference.toCharArray());
    StringBuffer sb = new StringBuffer();
    try {
        int tokenType = scanner.getNextToken();
        while (tokenType != ITerminalSymbols.TokenNameEOF) {
            sb.append(scanner.getRawTokenSource());
            tokenType = scanner.getNextToken();
        }
    } catch (InvalidInputException e) {
        Assert.isTrue(false, reference);
    }
    reference = sb.toString();
    return reference;
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException)

Example 12 with InvalidInputException

use of org.eclipse.jdt.core.compiler.InvalidInputException in project eclipse.jdt.ls by eclipse.

the class TypeOccurrenceCollector method acceptSearchMatch2.

public SearchMatch acceptSearchMatch2(ICompilationUnit unit, SearchMatch match) throws CoreException {
    int start = match.getOffset();
    int length = match.getLength();
    // unqualified:
    String matchText = unit.getBuffer().getText(start, length);
    if (fOldName.equals(matchText)) {
        return match;
    }
    // (partially) qualified:
    if (fOldQualifiedName.endsWith(matchText)) {
        // e.g. rename B and p.A.B ends with match A.B
        int simpleNameLenght = fOldName.length();
        match.setOffset(start + length - simpleNameLenght);
        match.setLength(simpleNameLenght);
        return match;
    }
    // Not a standard reference -- use scanner to find last identifier token:
    IScanner scanner = getScanner(unit);
    scanner.setSource(matchText.toCharArray());
    int simpleNameStart = -1;
    int simpleNameEnd = -1;
    try {
        int token = scanner.getNextToken();
        while (token != ITerminalSymbols.TokenNameEOF) {
            if (token == ITerminalSymbols.TokenNameIdentifier) {
                // type reference can occur in module-info.java and collide with a restricted keyword.
                simpleNameStart = scanner.getCurrentTokenStartPosition();
                simpleNameEnd = scanner.getCurrentTokenEndPosition();
            }
            token = scanner.getNextToken();
        }
    } catch (InvalidInputException e) {
    // ignore
    }
    if (simpleNameStart != -1) {
        match.setOffset(start + simpleNameStart);
        match.setLength(simpleNameEnd + 1 - simpleNameStart);
    }
    return match;
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException)

Example 13 with InvalidInputException

use of org.eclipse.jdt.core.compiler.InvalidInputException in project eclipse.jdt.ls by eclipse.

the class MethodOccurenceCollector method acceptSearchMatch.

@Override
public void acceptSearchMatch(ICompilationUnit unit, SearchMatch match) throws CoreException {
    if (match instanceof MethodReferenceMatch && ((MethodReferenceMatch) match).isSuperInvocation() && match.getAccuracy() == SearchMatch.A_INACCURATE) {
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=156491
        return;
    }
    if (match.isImplicit()) {
        // see bug 94062
        collectMatch(match);
        return;
    }
    int start = match.getOffset();
    int length = match.getLength();
    String matchText = unit.getBuffer().getText(start, length);
    // direct match:
    if (fName.equals(matchText)) {
        collectMatch(match);
        return;
    }
    // lambda expression
    if (match instanceof MethodDeclarationMatch && match.getElement() instanceof IMethod && ((IMethod) match.getElement()).isLambdaMethod()) {
        // don't touch the lambda
        return;
    }
    // Not a standard reference -- use scanner to find last identifier token before left parenthesis:
    IScanner scanner = getScanner(unit);
    scanner.setSource(matchText.toCharArray());
    int simpleNameStart = -1;
    int simpleNameEnd = -1;
    try {
        int token = scanner.getNextToken();
        while (token != ITerminalSymbols.TokenNameEOF && token != ITerminalSymbols.TokenNameLPAREN) {
            // reference in code includes arguments in parentheses
            if (token == InternalTokenNameIdentifier) {
                simpleNameStart = scanner.getCurrentTokenStartPosition();
                simpleNameEnd = scanner.getCurrentTokenEndPosition();
            }
            token = scanner.getNextToken();
        }
    } catch (InvalidInputException e) {
    // ignore
    }
    if (simpleNameStart != -1) {
        match.setOffset(start + simpleNameStart);
        match.setLength(simpleNameEnd + 1 - simpleNameStart);
    }
    collectMatch(match);
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException) MethodReferenceMatch(org.eclipse.jdt.core.search.MethodReferenceMatch) IMethod(org.eclipse.jdt.core.IMethod) MethodDeclarationMatch(org.eclipse.jdt.core.search.MethodDeclarationMatch)

Aggregations

IScanner (org.eclipse.jdt.core.compiler.IScanner)13 InvalidInputException (org.eclipse.jdt.core.compiler.InvalidInputException)13 IMethod (org.eclipse.jdt.core.IMethod)3 MethodDeclarationMatch (org.eclipse.jdt.core.search.MethodDeclarationMatch)3 MethodReferenceMatch (org.eclipse.jdt.core.search.MethodReferenceMatch)3 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)1