Search in sources :

Example 1 with IScanner

use of org.eclipse.jdt.core.compiler.IScanner in project che 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 == ITerminalSymbols.TokenNameIdentifier) {
                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)

Example 2 with IScanner

use of org.eclipse.jdt.core.compiler.IScanner in project che by eclipse.

the class CodeTemplateContextType method isValidComment.

private boolean isValidComment(String template) {
    IScanner scanner = ToolFactory.createScanner(true, false, false, false);
    scanner.setSource(template.toCharArray());
    try {
        int next = scanner.getNextToken();
        while (TokenScanner.isComment(next)) {
            next = scanner.getNextToken();
        }
        return next == ITerminalSymbols.TokenNameEOF;
    } catch (InvalidInputException e) {
    }
    return false;
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException)

Example 3 with IScanner

use of org.eclipse.jdt.core.compiler.IScanner in project che 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) {
                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 4 with IScanner

use of org.eclipse.jdt.core.compiler.IScanner in project che by eclipse.

the class SelectionAwareSourceRangeComputer method initializeRanges.

private void initializeRanges() throws CoreException {
    fRanges = new HashMap<ASTNode, SourceRange>();
    if (fSelectedNodes.length == 0)
        return;
    fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
    int last = fSelectedNodes.length - 1;
    fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));
    IScanner scanner = ToolFactory.createScanner(true, false, false, false);
    char[] source = fDocumentPortionToScan.toCharArray();
    scanner.setSource(source);
    // initializeRanges() is only called once
    fDocumentPortionToScan = null;
    TokenScanner tokenizer = new TokenScanner(scanner);
    int pos = tokenizer.getNextStartOffset(0, false);
    ASTNode currentNode = fSelectedNodes[0];
    int newStart = Math.min(fSelectionStart + pos, currentNode.getStartPosition());
    SourceRange range = fRanges.get(currentNode);
    fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));
    currentNode = fSelectedNodes[last];
    int scannerStart = currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
    tokenizer.setOffset(scannerStart);
    pos = scannerStart;
    int token = -1;
    try {
        while (true) {
            token = tokenizer.readNext(false);
            pos = tokenizer.getCurrentEndOffset();
        }
    } catch (CoreException e) {
    }
    if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
        int index = pos - 1;
        while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
            pos--;
            index--;
        }
    }
    int newEnd = Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
    range = fRanges.get(currentNode);
    fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) CoreException(org.eclipse.core.runtime.CoreException) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 5 with IScanner

use of org.eclipse.jdt.core.compiler.IScanner in project che by eclipse.

the class TaskMarkerProposal method getUpdatedPosition.

private Position getUpdatedPosition(IDocument document) throws BadLocationException {
    IScanner scanner = ToolFactory.createScanner(true, false, false, false);
    scanner.setSource(document.get().toCharArray());
    int token = getSurroundingComment(scanner);
    if (token == ITerminalSymbols.TokenNameEOF) {
        return null;
    }
    int commentStart = scanner.getCurrentTokenStartPosition();
    int commentEnd = scanner.getCurrentTokenEndPosition() + 1;
    int contentStart = commentStart + 2;
    int contentEnd = commentEnd;
    if (token == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
        contentStart = commentStart + 3;
        contentEnd = commentEnd - 2;
    } else if (token == ITerminalSymbols.TokenNameCOMMENT_BLOCK) {
        contentEnd = commentEnd - 2;
    }
    if (hasContent(document, contentStart, fLocation.getOffset()) || hasContent(document, contentEnd, fLocation.getOffset() + fLocation.getLength())) {
        return new Position(fLocation.getOffset(), fLocation.getLength());
    }
    IRegion startRegion = document.getLineInformationOfOffset(commentStart);
    int start = startRegion.getOffset();
    boolean contentAtBegining = hasContent(document, start, commentStart);
    if (contentAtBegining) {
        start = commentStart;
    }
    int end;
    if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
        if (contentAtBegining) {
            // only to the end of the line
            end = startRegion.getOffset() + startRegion.getLength();
        } else {
            // includes new line
            end = commentEnd;
        }
    } else {
        int endLine = document.getLineOfOffset(commentEnd - 1);
        if (endLine + 1 == document.getNumberOfLines() || contentAtBegining) {
            IRegion endRegion = document.getLineInformation(endLine);
            end = endRegion.getOffset() + endRegion.getLength();
        } else {
            IRegion endRegion = document.getLineInformation(endLine + 1);
            end = endRegion.getOffset();
        }
    }
    if (hasContent(document, commentEnd, end)) {
        end = commentEnd;
        // only remove comment
        start = commentStart;
    }
    return new Position(start, end - start);
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) Position(org.eclipse.jface.text.Position) IRegion(org.eclipse.jface.text.IRegion)

Aggregations

IScanner (org.eclipse.jdt.core.compiler.IScanner)10 InvalidInputException (org.eclipse.jdt.core.compiler.InvalidInputException)7 CoreException (org.eclipse.core.runtime.CoreException)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 IMethod (org.eclipse.jdt.core.IMethod)1 ISourceRange (org.eclipse.jdt.core.ISourceRange)1 SourceRange (org.eclipse.jdt.core.SourceRange)1 MethodDeclarationMatch (org.eclipse.jdt.core.search.MethodDeclarationMatch)1 MethodReferenceMatch (org.eclipse.jdt.core.search.MethodReferenceMatch)1 TokenScanner (org.eclipse.jdt.internal.corext.dom.TokenScanner)1 IRegion (org.eclipse.jface.text.IRegion)1 Position (org.eclipse.jface.text.Position)1