use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.
the class AbstractInplaceIntroduceTest method doTest.
protected void doTest(final Pass<AbstractInplaceIntroducer> pass) {
String name = getTestName(true);
configureByFile(getBasePath() + name + getExtension());
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
try {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
final AbstractInplaceIntroducer introducer = invokeRefactoring();
pass.pass(introducer);
TemplateState state = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(getEditor()));
assert state != null;
state.gotoEnd(false);
checkResultByFile(getBasePath() + name + "_after" + getExtension());
} finally {
getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.
the class IntroduceParameterHandler method introduceStrategy.
@VisibleForTesting
public boolean introduceStrategy(final Project project, final Editor editor, PsiFile file, final PsiElement[] elements) {
if (elements.length > 0) {
final AbstractInplaceIntroducer inplaceIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
if (inplaceIntroducer instanceof InplaceIntroduceParameterPopup) {
return false;
}
final PsiMethod containingMethod = Util.getContainingMethod(elements[0]);
if (containingMethod == null) {
return false;
}
final List<PsiMethod> enclosingMethods = getEnclosingMethods(containingMethod);
if (enclosingMethods.isEmpty()) {
return false;
}
final PsiElement[] elementsCopy;
if (!elements[0].isPhysical()) {
elementsCopy = elements;
} else {
final PsiFile copy = PsiFileFactory.getInstance(project).createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);
final TextRange range = new TextRange(elements[0].getTextRange().getStartOffset(), elements[elements.length - 1].getTextRange().getEndOffset());
final PsiExpression exprInRange = CodeInsightUtil.findExpressionInRange(copy, range.getStartOffset(), range.getEndOffset());
elementsCopy = exprInRange != null ? new PsiElement[] { exprInRange } : CodeInsightUtil.findStatementsInRange(copy, range.getStartOffset(), range.getEndOffset());
}
final PsiMethod containingMethodCopy = Util.getContainingMethod(elementsCopy[0]);
LOG.assertTrue(containingMethodCopy != null);
final List<PsiMethod> enclosingMethodsInCopy = getEnclosingMethods(containingMethodCopy);
final MyExtractMethodProcessor processor = new MyExtractMethodProcessor(project, editor, elementsCopy, enclosingMethodsInCopy.get(enclosingMethodsInCopy.size() - 1));
try {
if (!processor.prepare())
return false;
processor.showDialog();
//provide context for generated method to check exceptions compatibility
final PsiMethod emptyMethod = JavaPsiFacade.getElementFactory(project).createMethodFromText(processor.generateEmptyMethod("name").getText(), elements[0]);
final Collection<? extends PsiType> types = FunctionalInterfaceSuggester.suggestFunctionalInterfaces(emptyMethod);
if (types.isEmpty()) {
return false;
}
if (types.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
final PsiType next = types.iterator().next();
functionalInterfaceSelected(next, enclosingMethods, project, editor, processor, elements);
} else {
final Map<PsiClass, PsiType> classes = new LinkedHashMap<>();
for (PsiType type : types) {
classes.put(PsiUtil.resolveClassInType(type), type);
}
final PsiClass[] psiClasses = classes.keySet().toArray(new PsiClass[classes.size()]);
final String methodSignature = PsiFormatUtil.formatMethod(emptyMethod, PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE);
final PsiType returnType = emptyMethod.getReturnType();
LOG.assertTrue(returnType != null);
final String title = "Choose Applicable Functional Interface: " + methodSignature + " -> " + returnType.getPresentableText();
NavigationUtil.getPsiElementPopup(psiClasses, new PsiClassListCellRenderer(), title, new PsiElementProcessor<PsiClass>() {
@Override
public boolean execute(@NotNull PsiClass psiClass) {
functionalInterfaceSelected(classes.get(psiClass), enclosingMethods, project, editor, processor, elements);
return true;
}
}).showInBestPositionFor(editor);
return true;
}
return true;
} catch (IncorrectOperationException | PrepareFailedException ignore) {
}
}
return false;
}
use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.
the class InplaceIntroduceVariableTest method doTestTypeChange.
private void doTestTypeChange(final String newType) {
final Pass<AbstractInplaceIntroducer> typeChanger = new Pass<AbstractInplaceIntroducer>() {
@Override
public void pass(AbstractInplaceIntroducer inplaceIntroduceFieldPopup) {
type(newType);
}
};
String name = getTestName(true);
configureByFile(getBasePath() + name + getExtension());
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
try {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
final AbstractInplaceIntroducer introducer = invokeRefactoring();
TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
assert state != null;
state.previousTab();
typeChanger.pass(introducer);
state.gotoEnd(false);
checkResultByFile(getBasePath() + name + "_after" + getExtension());
} finally {
getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.
the class BaseExpressionToFieldHandler method invokeImpl.
protected boolean invokeImpl(final Project project, @NotNull final PsiExpression selectedExpr, final Editor editor) {
final PsiElement element = getPhysicalElement(selectedExpr);
final PsiFile file = element.getContainingFile();
LOG.assertTrue(file != null, "expr.getContainingFile() == null");
if (LOG.isDebugEnabled()) {
LOG.debug("expression:" + selectedExpr);
}
final PsiType tempType = getTypeByExpression(selectedExpr);
if (tempType == null || LambdaUtil.notInferredType(tempType)) {
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("unknown.expression.type"));
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
return false;
}
if (PsiType.VOID.equals(tempType)) {
String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("selected.expression.has.void.type"));
CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), getHelpID());
return false;
}
myParentClass = getParentClass(selectedExpr);
final List<PsiClass> classes = new ArrayList<>();
PsiClass aClass = myParentClass;
while (aClass != null) {
classes.add(aClass);
final PsiField psiField = ConvertToFieldRunnable.checkForwardRefs(selectedExpr, aClass);
if (psiField != null && psiField.getParent() == aClass)
break;
aClass = PsiTreeUtil.getParentOfType(aClass, PsiClass.class, true);
}
final AbstractInplaceIntroducer activeIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor);
final boolean shouldSuggestDialog = activeIntroducer instanceof InplaceIntroduceConstantPopup && activeIntroducer.startsOnTheSameElement(selectedExpr, null);
if (classes.size() == 1 || editor == null || ApplicationManager.getApplication().isUnitTestMode() || shouldSuggestDialog) {
return !convertExpressionToField(selectedExpr, editor, file, project, tempType);
} else if (!classes.isEmpty()) {
PsiClass selection = AnonymousTargetClassPreselectionUtil.getPreselection(classes, myParentClass);
NavigationUtil.getPsiElementPopup(classes.toArray(new PsiClass[classes.size()]), new PsiClassListCellRenderer(), "Choose class to introduce " + (myIsConstant ? "constant" : "field"), new PsiElementProcessor<PsiClass>() {
@Override
public boolean execute(@NotNull PsiClass aClass) {
AnonymousTargetClassPreselectionUtil.rememberSelection(aClass, myParentClass);
myParentClass = aClass;
convertExpressionToField(selectedExpr, editor, file, project, tempType);
return false;
}
}, selection).showInBestPositionFor(editor);
}
return true;
}
use of com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer in project intellij-community by JetBrains.
the class AbstractInplaceIntroduceTest method doTestEscape.
protected void doTestEscape(Pass<AbstractInplaceIntroducer> pass) {
String name = getTestName(true);
configureByFile(getBasePath() + name + getExtension());
final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled();
try {
TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable());
getEditor().getSettings().setVariableInplaceRenameEnabled(true);
final AbstractInplaceIntroducer introducer = invokeRefactoring();
if (pass != null) {
pass.pass(introducer);
}
TemplateState state = TemplateManagerImpl.getTemplateState(getEditor());
assert state != null;
state.gotoEnd(true);
checkResultByFile(getBasePath() + name + "_after" + getExtension());
} finally {
getEditor().getSettings().setVariableInplaceRenameEnabled(enabled);
}
}
Aggregations