TextRange (final ASTNode elementAtOffset) { ASTNode current = elementAtOffset; current = findNearestExpressionParent(current); if (current == null) { if (elementAtOffset.getElementType() == TokenType.WHITE_SPACE) { ASTNode prevElement = elementAtOffset.getTreePrev(); if (prevElement == null) { return elementAtOffset.getTextRange(); } else { ASTNode prevExpressionParent = findNearestExpressionParent(prevElement); if (prevExpressionParent == null) { return elementAtOffset.getTextRange(); } else { return new TextRange(prevExpressionParent.getTextRange().getStartOffset(), elementAtOffset.getTextRange().getEndOffset()); } } } else { // Look at IDEA-65777 for example of situation when it's necessary to expand element range in case of invalid syntax. return combineWithErrorElementIfPossible(elementAtOffset); } } else { return current.getTextRange(); } }