Example usage for org.eclipse.jdt.internal.compiler.parser Scanner getNextCharAsJavaIdentifierPart

List of usage examples for org.eclipse.jdt.internal.compiler.parser Scanner getNextCharAsJavaIdentifierPart

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.parser Scanner getNextCharAsJavaIdentifierPart.

Prototype

public boolean getNextCharAsJavaIdentifierPart() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.codeassist.SelectionEngine.java

License:Open Source License

private boolean checkSelection(char[] source, int selectionStart, int selectionEnd) {

    Scanner scanner = new Scanner(false /*comment*/, false /*whitespace*/, false /*nls*/,
            this.compilerOptions.sourceLevel, this.compilerOptions.complianceLevel, null/*taskTag*/,
            null/*taskPriorities*/, true /*taskCaseSensitive*/);
    scanner.setSource(source);/*  w w w .  j  a va 2s  . c  o  m*/

    int lastIdentifierStart = -1;
    int lastIdentifierEnd = -1;
    char[] lastIdentifier = null;
    int token;

    if (selectionStart > selectionEnd) {
        int end = source.length - 1;

        // compute start position of current line
        int currentPosition = selectionStart - 1;
        int nextCharacterPosition = selectionStart;
        char currentCharacter = ' ';
        try {
            lineLoop: while (currentPosition > 0) {

                if (source[currentPosition] == '\\' && source[currentPosition + 1] == 'u') {
                    int pos = currentPosition + 2;
                    int c1 = 0, c2 = 0, c3 = 0, c4 = 0;
                    while (source[pos] == 'u') {
                        pos++;
                    }

                    int endOfUnicode = pos + 3;
                    if (end < endOfUnicode) {
                        if (endOfUnicode < source.length) {
                            end = endOfUnicode;
                        } else {
                            return false; // not enough characters to decode an unicode
                        }
                    }

                    if ((c1 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c1 < 0
                            || (c2 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c2 < 0
                            || (c3 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c3 < 0
                            || (c4 = ScannerHelper.getHexadecimalValue(source[pos++])) > 15 || c4 < 0) {
                        return false;
                    } else {
                        currentCharacter = (char) (((c1 * 16 + c2) * 16 + c3) * 16 + c4);
                        nextCharacterPosition = pos;
                    }
                } else {
                    currentCharacter = source[currentPosition];
                    nextCharacterPosition = currentPosition + 1;
                }

                switch (currentCharacter) {
                case '\r':
                case '\n':
                case '/':
                case '"':
                case '\'':
                    break lineLoop;
                }
                currentPosition--;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            return false;
        }

        // compute start and end of the last token
        scanner.resetTo(nextCharacterPosition, end);
        isolateLastName: do {
            try {
                token = scanner.getNextToken();
            } catch (InvalidInputException e) {
                return false;
            }
            switch (token) {
            case TerminalTokens.TokenNamethis:
            case TerminalTokens.TokenNamesuper:
            case TerminalTokens.TokenNamenew:
            case TerminalTokens.TokenNameIdentifier:
                if (scanner.startPosition <= selectionStart && selectionStart <= scanner.currentPosition) {
                    if (scanner.currentPosition == scanner.eofPosition) {
                        int temp = scanner.eofPosition;
                        scanner.eofPosition = scanner.source.length;
                        while (scanner.getNextCharAsJavaIdentifierPart()) {
                            /*empty*/}
                        scanner.eofPosition = temp;
                    }
                    lastIdentifierStart = scanner.startPosition;
                    lastIdentifierEnd = scanner.currentPosition - 1;
                    lastIdentifier = scanner.getCurrentTokenSource();
                    break isolateLastName;
                }
                break;
            }
        } while (token != TerminalTokens.TokenNameEOF);
    } else {
        if (selectionStart == selectionEnd) { // Widen the selection to scan -> || :: if needed. No unicode handling for now.
            if (selectionStart > 0 && selectionEnd < source.length - 1) {
                if ((source[selectionStart] == '>' && source[selectionStart - 1] == '-')
                        || source[selectionStart] == ':' && source[selectionStart - 1] == ':') {
                    selectionStart--;
                } else {
                    if ((source[selectionStart] == '-' && source[selectionEnd + 1] == '>')
                            || source[selectionStart] == ':' && source[selectionEnd + 1] == ':') {
                        selectionEnd++;
                    }
                }
            }
        } // there could be some innocuous widening, shouldn't matter.
        scanner.resetTo(selectionStart, selectionEnd);

        boolean expectingIdentifier = true;
        do {
            try {
                token = scanner.getNextToken();
            } catch (InvalidInputException e) {
                return false;
            }
            switch (token) {
            case TerminalTokens.TokenNamethis:
            case TerminalTokens.TokenNamesuper:
            case TerminalTokens.TokenNamenew:
            case TerminalTokens.TokenNameIdentifier:
                if (!expectingIdentifier)
                    return false;
                lastIdentifier = scanner.getCurrentTokenSource();
                lastIdentifierStart = scanner.startPosition;
                lastIdentifierEnd = scanner.currentPosition - 1;
                if (lastIdentifierEnd > selectionEnd) {
                    lastIdentifierEnd = selectionEnd;
                    lastIdentifier = CharOperation.subarray(lastIdentifier, 0,
                            lastIdentifierEnd - lastIdentifierStart + 1);
                }
                expectingIdentifier = false;
                break;
            case TerminalTokens.TokenNameCOLON_COLON:
                if (selectionStart >= scanner.startPosition && selectionEnd < scanner.currentPosition) {
                    this.actualSelectionStart = selectionStart;
                    this.actualSelectionEnd = selectionEnd;
                    this.selectedIdentifier = CharOperation.NO_CHAR;
                    return true;
                }
                //$FALL-THROUGH$
            case TerminalTokens.TokenNameDOT:
                if (expectingIdentifier)
                    return false;
                expectingIdentifier = true;
                break;
            case TerminalTokens.TokenNameEOF:
                if (expectingIdentifier)
                    return false;
                break;
            case TerminalTokens.TokenNameLESS:
                if (!checkTypeArgument(scanner))
                    return false;
                break;
            case TerminalTokens.TokenNameAT:
                if (scanner.startPosition != scanner.initialPosition)
                    return false;
                break;
            case TerminalTokens.TokenNameARROW:
                if (selectionStart >= scanner.startPosition && selectionEnd < scanner.currentPosition) {
                    this.actualSelectionStart = selectionStart;
                    this.actualSelectionEnd = selectionEnd;
                    this.selectedIdentifier = CharOperation.NO_CHAR;
                    return true;
                }
                return false;
            default:
                return false;
            }
        } while (token != TerminalTokens.TokenNameEOF);
    }
    if (lastIdentifierStart > 0) {
        this.actualSelectionStart = lastIdentifierStart;
        this.actualSelectionEnd = lastIdentifierEnd;
        this.selectedIdentifier = lastIdentifier;
        return true;
    }
    return false;
}