void (@Nullable JavaReplaceChoice choice) { if (!anchorStatement.isValid()) { return; } final Editor topLevelEditor; if (!InjectedLanguageManager.getInstance(project).isInjectedFragment(anchorStatement.getContainingFile())) { topLevelEditor = InjectedLanguageUtil.getTopLevelEditor(editor); } else { topLevelEditor = editor; } PsiVariable variable = null; try { boolean hasWriteAccess = occurrencesInfo.myHasWriteAccess; final InputValidator validator = new InputValidator(IntroduceVariableBase.this, project, occurrenceManager); final TypeSelectorManagerImpl typeSelectorManager = new TypeSelectorManagerImpl(project, originalType, expr, occurrences); boolean inFinalContext = occurrenceManager.isInFinalContext(); final IntroduceVariableSettings settings = getSettings(project, topLevelEditor, expr, occurrences, typeSelectorManager, inFinalContext, hasWriteAccess, validator, anchorStatement, choice); if (!settings.isOK()) { wasSucceed = false; return; } JavaReplaceChoice finalChoice = settings.getReplaceChoice(); PsiExpression[] selectedOccurrences = finalChoice.filter(occurrenceManager); if (selectedOccurrences.length == 0) { showErrorMessage(project, editor, JavaRefactoringBundle.message("introduce.variable.no.matching.occurrences")); wasSucceed = false; return; } final PsiElement chosenAnchor = getAnchor(selectedOccurrences); if (chosenAnchor == null) { String text = file.getText(); String textWithOccurrences = StreamEx.of(selectedOccurrences) .map(e -> getPhysicalElement(e).getTextRange()) .flatMapToEntry(range -> EntryStream.of(range.getStartOffset(), "[", range.getEndOffset(), "]").toMap()) .sortedBy(Map.Entry::getKey) .prepend(0, "") .append(text.length(), "") .map(Function.identity()) .pairMap((prev, next) -> text.substring(prev.getKey(), next.getKey()) + next.getValue()) .joining(); IntroduceVariableUtil.LOG.error("Unable to find anchor for a new variable; selectedOccurrences.length = " + selectedOccurrences.length, new Attachment("source.java", textWithOccurrences)); return; } final RefactoringEventData beforeData = new RefactoringEventData(); beforeData.addElement(expr); project.getMessageBus() .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, beforeData); variable = VariableExtractor.introduce(project, expr, topLevelEditor, chosenAnchor, selectedOccurrences, settings); } finally { final RefactoringEventData afterData = new RefactoringEventData(); afterData.addElement(variable); project.getMessageBus() .syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, afterData); } }