Search in sources :

Example 6 with IScanner

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

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

the class NewTypeWizardPage 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 8 with IScanner

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

the class StatementAnalyzer method checkSelectedNodes.

protected void checkSelectedNodes() {
    ASTNode[] nodes = getSelectedNodes();
    if (nodes.length == 0)
        return;
    ASTNode node = nodes[0];
    int selectionOffset = getSelection().getOffset();
    try {
        int start = fScanner.getNextStartOffset(selectionOffset, true);
        if (start == node.getStartPosition()) {
            int lastNodeEnd = ASTNodes.getExclusiveEnd(nodes[nodes.length - 1]);
            int pos = fScanner.getNextStartOffset(lastNodeEnd, true);
            int selectionEnd = getSelection().getInclusiveEnd();
            if (pos <= selectionEnd) {
                IScanner scanner = fScanner.getScanner();
                //see https://bugs.eclipse.org/324237
                char[] token = scanner.getCurrentTokenSource();
                if (start < lastNodeEnd && token.length == 1 && (token[0] == ';' || token[0] == ',')) {
                    setSelection(Selection.createFromStartEnd(start, lastNodeEnd - 1));
                } else {
                    ISourceRange range = new SourceRange(lastNodeEnd, pos - lastNodeEnd);
                    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_end_of_selection, JavaStatusContext.create(fCUnit, range));
                }
            }
            // success
            return;
        }
    } catch (CoreException e) {
    // fall through
    }
    ISourceRange range = new SourceRange(selectionOffset, node.getStartPosition() - selectionOffset + 1);
    invalidSelection(RefactoringCoreMessages.StatementAnalyzer_beginning_of_selection, JavaStatusContext.create(fCUnit, range));
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) CoreException(org.eclipse.core.runtime.CoreException) ASTNode(org.eclipse.jdt.core.dom.ASTNode) SourceRange(org.eclipse.jdt.core.SourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 9 with IScanner

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

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

the class Util method isJustWhitespaceOrComment.

private static boolean isJustWhitespaceOrComment(int start, int end, IBuffer buffer) {
    if (start == end)
        return true;
    Assert.isTrue(start <= end);
    String trimmedText = buffer.getText(start, end - start).trim();
    if (0 == trimmedText.length()) {
        return true;
    } else {
        IScanner scanner = ToolFactory.createScanner(false, false, false, null);
        scanner.setSource(trimmedText.toCharArray());
        try {
            return scanner.getNextToken() == ITerminalSymbols.TokenNameEOF;
        } catch (InvalidInputException e) {
            return false;
        }
    }
}
Also used : IScanner(org.eclipse.jdt.core.compiler.IScanner) InvalidInputException(org.eclipse.jdt.core.compiler.InvalidInputException)

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