void (final @NotNull Editor editor, final PsiFile file, final @NlsContexts.DialogTitle String refactoringName, final String helpId, final Project project, final ElementsProcessor<? super ElementToWorkOn> processor) { PsiLocalVariable localVar = null; PsiExpression expr = null; if (!editor.getSelectionModel().hasSelection()) { PsiElement element = TargetElementUtil.findTargetElement(editor, TargetElementUtil .ELEMENT_NAME_ACCEPTED | TargetElementUtil .REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil .LOOKUP_ITEM_ACCEPTED); if (element instanceof PsiLocalVariable) { localVar = (PsiLocalVariable)element; PsiElement elementAt = file.findElementAt(editor.getCaretModel().getOffset()); if (elementAt instanceof PsiIdentifier && elementAt.getParent() instanceof PsiReferenceExpression) { expr = (PsiExpression)elementAt.getParent(); } else { final PsiReference reference = TargetElementUtil.findReference(editor); if (reference != null) { final PsiElement refElement = reference.getElement(); if (refElement instanceof PsiReferenceExpression) { expr = (PsiReferenceExpression)refElement; } } } } else { final PsiLocalVariable variable = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PsiLocalVariable.class); final int offset = editor.getCaretModel().getOffset(); final PsiElement[] statementsInRange = IntroduceVariableUtil.findStatementsAtOffset(editor, file, offset); if (statementsInRange.length == 1 && IntroduceVariableUtil.selectLineAtCaret(offset, statementsInRange)) { editor.getSelectionModel().selectLineAtCaret(); final ElementToWorkOn elementToWorkOn = getElementToWorkOn(editor, file, refactoringName, helpId, project, null, null); if (elementToWorkOn == null || elementToWorkOn.getLocalVariable() == null && elementToWorkOn.getExpression() == null || !processor.accept(elementToWorkOn)) { editor.getSelectionModel().removeSelection(); } } if (!editor.getSelectionModel().hasSelection()) { final List<PsiExpression> expressions = CommonJavaRefactoringUtil.collectExpressions(file, editor, offset); for (Iterator<PsiExpression> iterator = expressions.iterator(); iterator.hasNext(); ) { PsiExpression expression = iterator.next(); if (!processor.accept(new ElementToWorkOn(null, expression))) { iterator.remove(); } } if (expressions.isEmpty()) { editor.getSelectionModel().selectLineAtCaret(); } else if (!IntroduceVariableUtil.isChooserNeeded(expressions)) { expr = expressions.get(0); } else { final int selection = IntroduceVariableUtil.preferredSelection(statementsInRange, expressions); IntroduceTargetChooser.showChooser(editor, expressions, new Pass<>() { @Override public void pass(final PsiExpression selectedValue) { PsiLocalVariable var = null; //replace var if selected expression == var initializer if (variable != null && variable.getInitializer() == selectedValue) { var = variable; } processor.pass(getElementToWorkOn(editor, file, refactoringName, helpId, project, var, selectedValue)); } }, new PsiExpressionTrimRenderer.RenderFunction(), RefactoringBundle.message("introduce.target.chooser.expressions.title"), selection, ScopeHighlighter.NATURAL_RANGER); return; } } } } processor.pass(getElementToWorkOn(editor, file, refactoringName, helpId, project, localVar, expr)); }