use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-community by JetBrains.
the class PyChangeSignatureTest method changeSignature.
private void changeSignature(@Nullable String newName, @Nullable List<PyParameterInfo> parameters) {
final PyChangeSignatureHandler changeSignatureHandler = new PyChangeSignatureHandler();
final PyFunction function = (PyFunction) changeSignatureHandler.findTargetMember(myFixture.getFile(), myFixture.getEditor());
assertNotNull(function);
final PyFunction newFunction = PyChangeSignatureHandler.getSuperMethod(function);
assertNotNull(newFunction);
final PyMethodDescriptor method = new PyMethodDescriptor(newFunction);
final TestPyChangeSignatureDialog dialog = new TestPyChangeSignatureDialog(newFunction.getProject(), method);
try {
if (newName != null) {
dialog.setNewName(newName);
}
if (parameters != null) {
dialog.setParameterInfos(parameters);
}
final String validationError = dialog.validateAndCommitData();
assertTrue(validationError, validationError == null);
final BaseRefactoringProcessor baseRefactoringProcessor = dialog.createRefactoringProcessor();
assert baseRefactoringProcessor instanceof PyChangeSignatureProcessor;
final PyChangeSignatureProcessor processor = (PyChangeSignatureProcessor) baseRefactoringProcessor;
processor.run();
} finally {
Disposer.dispose(dialog.getDisposable());
}
}
use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-community by JetBrains.
the class PyMoveSymbolDelegate method doMove.
public void doMove(@NotNull Project project, @NotNull List<PyElement> elements) {
final PsiElement firstElement = elements.get(0);
final String initialPath = StringUtil.notNullize(PyPsiUtils.getContainingFilePath(firstElement));
final BaseRefactoringProcessor processor;
if (isMovableLocalFunctionOrMethod(firstElement)) {
final PyFunction function = (PyFunction) firstElement;
final PyMakeFunctionTopLevelDialog dialog = new PyMakeFunctionTopLevelDialog(project, function, initialPath, initialPath);
if (!dialog.showAndGet()) {
return;
}
if (function.getContainingClass() != null) {
processor = new PyMakeMethodTopLevelProcessor(function, dialog.getTargetPath());
} else {
processor = new PyMakeLocalFunctionTopLevelProcessor(function, dialog.getTargetPath());
}
processor.setPreviewUsages(dialog.isPreviewUsages());
} else {
final List<PsiNamedElement> initialElements = Lists.newArrayList();
for (PsiElement element : elements) {
final PsiNamedElement e = PyMoveModuleMembersHelper.extractNamedElement(element);
if (e == null) {
return;
}
initialElements.add(e);
}
final PyMoveModuleMembersDialog dialog = new PyMoveModuleMembersDialog(project, initialElements, initialPath, initialPath);
if (!dialog.showAndGet()) {
return;
}
final PsiNamedElement[] selectedElements = ContainerUtil.findAllAsArray(dialog.getSelectedTopLevelSymbols(), PsiNamedElement.class);
processor = new PyMoveModuleMembersProcessor(selectedElements, dialog.getTargetPath());
processor.setPreviewUsages(dialog.isPreviewUsages());
}
try {
processor.run();
} catch (IncorrectOperationException e) {
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw e;
}
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), null, project);
}
}
use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-community by JetBrains.
the class DetectedJavaChangeInfo method perform.
void perform(final String oldText, Editor editor, boolean silently) {
final PsiMethod method = getSuperMethod();
Project project = getMethod().getProject();
final PsiMethod currentMethod = getMethod();
final TextRange signatureRange = JavaChangeSignatureDetector.getSignatureRange(currentMethod);
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final Document document = documentManager.getDocument(currentMethod.getContainingFile());
if (silently || ApplicationManager.getApplication().isUnitTestMode()) {
final String currentSignature = currentMethod.getContainingFile().getText().substring(signatureRange.getStartOffset(), signatureRange.getEndOffset());
InplaceChangeSignature.temporallyRevertChanges(JavaChangeSignatureDetector.getSignatureRange(currentMethod), document, oldText, project);
PsiMethod prototype;
if (isGenerateDelegate()) {
for (JavaParameterInfo info : getNewParameters()) {
if (info.getOldIndex() == -1) {
//to be replaced with template expr
((ParameterInfoImpl) info).setDefaultValue("null");
}
}
prototype = JavaChangeSignatureUsageProcessor.generateDelegatePrototype(this);
} else {
prototype = null;
}
createChangeSignatureProcessor(method).run();
InplaceChangeSignature.temporallyRevertChanges(JavaChangeSignatureDetector.getSignatureRange(currentMethod), document, currentSignature, project);
if (prototype != null) {
WriteCommandAction.runWriteCommandAction(project, "Delegate", null, () -> {
PsiMethod delegate = currentMethod.getContainingClass().findMethodBySignature(prototype, false);
PsiExpression expression = delegate != null ? LambdaUtil.extractSingleExpressionFromBody(delegate.getBody()) : null;
if (expression instanceof PsiMethodCallExpression) {
PsiExpression[] expressions = ((PsiMethodCallExpression) expression).getArgumentList().getExpressions();
JavaParameterInfo[] parameters = getNewParameters();
PsiExpression[] toBeDefault = Arrays.stream(parameters).filter(param -> param.getOldIndex() == -1).map(info -> {
int i = ArrayUtil.find(parameters, info);
return expressions[i];
}).toArray(PsiExpression[]::new);
DefineParamsDefaultValueAction.startTemplate(project, editor, toBeDefault, delegate);
}
});
}
return;
}
final JavaMethodDescriptor descriptor = new JavaMethodDescriptor(currentMethod) {
@Override
public String getReturnTypeText() {
return getNewReturnType().getTypeText();
}
};
final JavaChangeSignatureDialog dialog = new JavaChangeSignatureDialog(method.getProject(), descriptor, true, method) {
protected BaseRefactoringProcessor createRefactoringProcessor() {
return createChangeSignatureProcessor(method);
}
@Override
protected void invokeRefactoring(final BaseRefactoringProcessor processor) {
CommandProcessor.getInstance().executeCommand(myProject, () -> {
InplaceChangeSignature.temporallyRevertChanges(JavaChangeSignatureDetector.getSignatureRange(currentMethod), document, oldText, project);
doRefactor(processor);
}, RefactoringBundle.message("changing.signature.of.0", DescriptiveNameUtil.getDescriptiveName(currentMethod)), null);
}
private void doRefactor(BaseRefactoringProcessor processor) {
super.invokeRefactoring(processor);
}
};
dialog.showAndGet();
}
use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-plugins by JetBrains.
the class FlexMoveClassDialog method doAction.
@Override
protected void doAction() {
myTargetPackageField.updateRecents();
PsiElement firstElement = myElements.iterator().next();
PsiDirectory baseDir;
if (myTargetContainer instanceof PsiDirectory) {
baseDir = (PsiDirectory) myTargetContainer;
} else {
baseDir = PlatformPackageUtil.getDirectory(firstElement);
}
String nameToCheck = myFileLocal ? myClassNameField.getText() : null;
PsiDirectory targetDirectory = JSRefactoringUtil.chooseOrCreateDirectoryForClass(myProject, ModuleUtilCore.findModuleForPsiElement(firstElement), GlobalSearchScope.projectScope(myProject), myTargetPackageField.getText(), nameToCheck, baseDir, myCbMoveToAnotherSourceFolder.isSelected() ? ThreeState.YES : ThreeState.NO);
if (targetDirectory == null) {
return;
}
// file-local case already checked by JSRefactoringUtil.chooseOrCreateDirectoryForClass (see nameToCheck)
if (!myFileLocal) {
try {
for (PsiElement element : myElements) {
MoveFilesOrDirectoriesUtil.checkMove(element.getContainingFile(), targetDirectory);
}
} catch (IncorrectOperationException e) {
CommonRefactoringUtil.showErrorMessage(RefactoringBundle.message("error.title"), e.getMessage(), getHelpId(), myProject);
return;
}
}
BaseRefactoringProcessor processor;
if (myFileLocal) {
processor = new FlexMoveInnerClassProcessor(myElements.iterator().next(), targetDirectory, myClassNameField.getText(), myTargetPackageField.getText(), myCbSearchInComments.isSelected(), myCbSearchTextOccurences.isSelected(), myCallback);
} else {
processor = new FlexMoveClassProcessor(myElements, targetDirectory, myTargetPackageField.getText(), myCbSearchInComments.isSelected(), myCbSearchTextOccurences.isSelected(), myCallback);
}
invokeRefactoring(processor);
}
use of com.intellij.refactoring.BaseRefactoringProcessor in project intellij-plugins by JetBrains.
the class JSChangeSignatureDetector method performChange.
@Override
public void performChange(JSChangeInfo changeInfo, Editor editor, @NotNull final String oldText) {
final JSChangeInfo jsChangeInfo = changeInfo;
JSMethodDescriptor descriptor = new JSMethodDescriptor(jsChangeInfo.getMethod(), false) {
@Override
public String getName() {
return jsChangeInfo.getNewName();
}
@Override
public List<JSParameterInfo> getParameters() {
return Arrays.asList(jsChangeInfo.getNewParameters());
}
@Override
public int getParametersCount() {
return getParameters().size();
}
@Override
public String getVisibility() {
return jsChangeInfo.getNewVisibility().name();
}
@Override
public JSFunction getMethod() {
return jsChangeInfo.getMethod();
}
};
JSChangeSignatureDialog d = new JSChangeSignatureDialog(descriptor, changeInfo.getMethod()) {
@Override
protected void invokeRefactoring(BaseRefactoringProcessor processor) {
revertChanges(jsChangeInfo.getMethod(), oldText);
super.invokeRefactoring(processor);
}
};
d.showAndGet();
}
Aggregations