void (@NotNull CompletionInitializationContext context) { if (!context.getEditor().getSelectionModel().hasSelection()) { final PsiFile file = context.getFile(); PsiElement element = file.findElementAt(context.getStartOffset()); if (element instanceof PsiIdentifier) { element = element.getParent(); while (element instanceof PsiJavaCodeReferenceElement || element instanceof PsiCall || element instanceof PsiThisExpression || element instanceof PsiSuperExpression || element instanceof PsiTypeElement || element instanceof PsiClassObjectAccessExpression) { int newEnd = element.getTextRange().getEndOffset(); if (element instanceof PsiMethodCallExpression) { newEnd = ((PsiMethodCallExpression)element).getMethodExpression().getTextRange().getEndOffset(); } else if (element instanceof PsiNewExpression) { final PsiJavaCodeReferenceElement classReference = ((PsiNewExpression)element).getClassReference(); if (classReference != null) { newEnd = classReference.getTextRange().getEndOffset(); } } context.setReplacementOffset(newEnd); element = element.getParent(); } } } PsiElement lastElement = context.getFile().findElementAt(context.getStartOffset() - 1); if (lastElement != null && lastElement.getText().equals("(") && lastElement.getParent() instanceof PsiParenthesizedExpression) { // don't trim dummy identifier or we won't be able to determine the type of the expression after '(' // which is needed to insert correct cast return; } context.setDummyIdentifier(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED); }