mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Java: Refactoring - improve readability of ExtractMethodHandler and related classes (IDEA-144957)
This commit is contained in:
+21
-16
@@ -157,31 +157,36 @@ public class ExtractMethodHandler implements RefactoringActionHandler, ContextAw
|
||||
private static boolean invokeOnElements(final Project project, final Editor editor, @NotNull final ExtractMethodProcessor processor, final boolean directTypes) {
|
||||
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, processor.getTargetClass().getContainingFile())) return false;
|
||||
if (processor.showDialog(directTypes)) {
|
||||
run(project, editor, processor);
|
||||
extractMethod(project, processor);
|
||||
DuplicatesImpl.processDuplicates(processor, project, editor);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void run(@NotNull final Project project, final Editor editor, final ExtractMethodProcessor processor) {
|
||||
public static void extractMethod(@NotNull final Project project, final ExtractMethodProcessor processor) {
|
||||
CommandProcessor.getInstance().executeCommand(project,
|
||||
() -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
|
||||
try {
|
||||
final RefactoringEventData beforeData = new RefactoringEventData();
|
||||
beforeData.addElements(processor.myElements);
|
||||
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted("refactoring.extract.method", beforeData);
|
||||
() -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(
|
||||
() -> doRefactoring(project, processor)), REFACTORING_NAME, null);
|
||||
}
|
||||
|
||||
processor.doRefactoring();
|
||||
private static void doRefactoring(@NotNull Project project, ExtractMethodProcessor processor) {
|
||||
try {
|
||||
final RefactoringEventData beforeData = new RefactoringEventData();
|
||||
beforeData.addElements(processor.myElements);
|
||||
project.getMessageBus().syncPublisher(
|
||||
RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted("refactoring.extract.method", beforeData);
|
||||
|
||||
final RefactoringEventData data = new RefactoringEventData();
|
||||
data.addElement(processor.getExtractedMethod());
|
||||
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone("refactoring.extract.method", data);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}), REFACTORING_NAME, null);
|
||||
processor.doRefactoring();
|
||||
|
||||
final RefactoringEventData data = new RefactoringEventData();
|
||||
data.addElement(processor.getExtractedMethod());
|
||||
project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC)
|
||||
.refactoringDone("refactoring.extract.method", data);
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
+1
-1
@@ -755,7 +755,7 @@ public class ExtractMethodProcessor implements MatchProvider {
|
||||
public void testRun() throws IncorrectOperationException {
|
||||
testPrepare();
|
||||
testNullness();
|
||||
ExtractMethodHandler.run(myProject, myEditor, this);
|
||||
ExtractMethodHandler.extractMethod(myProject, this);
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
|
||||
+41
-32
@@ -82,14 +82,14 @@ public class ExtractMethodObjectHandler implements RefactoringActionHandler, Con
|
||||
if (!extractProcessor.prepare()) return;
|
||||
if (!CommonRefactoringUtil.checkReadOnlyStatus(project, extractProcessor.getTargetClass().getContainingFile())) return;
|
||||
if (extractProcessor.showDialog()) {
|
||||
run(project, editor, processor, extractProcessor);
|
||||
extractMethodObject(project, editor, processor, extractProcessor);
|
||||
}
|
||||
}
|
||||
|
||||
public static void run(@NotNull final Project project,
|
||||
final Editor editor,
|
||||
@NotNull final ExtractMethodObjectProcessor processor,
|
||||
@NotNull final ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor) {
|
||||
public static void extractMethodObject(@NotNull final Project project,
|
||||
final Editor editor,
|
||||
@NotNull final ExtractMethodObjectProcessor processor,
|
||||
@NotNull final ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor) {
|
||||
final RangeMarker marker;
|
||||
if (editor != null) {
|
||||
final int offset = editor.getCaretModel().getOffset();
|
||||
@@ -98,33 +98,9 @@ public class ExtractMethodObjectHandler implements RefactoringActionHandler, Con
|
||||
marker = null;
|
||||
}
|
||||
CommandProcessor.getInstance().executeCommand(project,
|
||||
() -> PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
|
||||
try {
|
||||
ApplicationManager.getApplication().runWriteAction(() -> extractProcessor.doRefactoring());
|
||||
processor.run();
|
||||
ApplicationManager.getApplication().runWriteAction(() -> processor.runChangeSignature());
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
if (processor.isCreateInnerClass()) {
|
||||
processor.moveUsedMethodsToInner();
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
if (editor != null) {
|
||||
DuplicatesImpl.processDuplicates(extractProcessor, project, editor);
|
||||
}
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
if (processor.isCreateInnerClass()) {
|
||||
processor.changeInstanceAccess(project);
|
||||
}
|
||||
final PsiElement method = processor.getMethod();
|
||||
LOG.assertTrue(method != null);
|
||||
method.delete();
|
||||
});
|
||||
}), ExtractMethodObjectProcessor.REFACTORING_NAME, ExtractMethodObjectProcessor.REFACTORING_NAME);
|
||||
() -> doRefactoring(project, editor, processor, extractProcessor),
|
||||
ExtractMethodObjectProcessor.REFACTORING_NAME,
|
||||
ExtractMethodObjectProcessor.REFACTORING_NAME);
|
||||
if (editor != null) {
|
||||
editor.getCaretModel().moveToOffset(marker.getStartOffset());
|
||||
marker.dispose();
|
||||
@@ -132,6 +108,39 @@ public class ExtractMethodObjectHandler implements RefactoringActionHandler, Con
|
||||
}
|
||||
}
|
||||
|
||||
private static void doRefactoring(@NotNull Project project,
|
||||
Editor editor,
|
||||
@NotNull ExtractMethodObjectProcessor processor,
|
||||
@NotNull ExtractMethodObjectProcessor.MyExtractMethodProcessor extractProcessor) {
|
||||
PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> {
|
||||
try {
|
||||
ApplicationManager.getApplication().runWriteAction(() -> extractProcessor.doRefactoring());
|
||||
processor.run();
|
||||
ApplicationManager.getApplication().runWriteAction(() -> processor.runChangeSignature());
|
||||
}
|
||||
catch (IncorrectOperationException e) {
|
||||
LOG.error(e);
|
||||
}
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
if (processor.isCreateInnerClass()) {
|
||||
processor.moveUsedMethodsToInner();
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
if (editor != null) {
|
||||
DuplicatesImpl.processDuplicates(extractProcessor, project, editor);
|
||||
}
|
||||
}
|
||||
ApplicationManager.getApplication().runWriteAction(() -> {
|
||||
if (processor.isCreateInnerClass()) {
|
||||
processor.changeInstanceAccess(project);
|
||||
}
|
||||
final PsiElement method = processor.getMethod();
|
||||
LOG.assertTrue(method != null);
|
||||
method.delete();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public void invoke(@NotNull final Project project, @NotNull final PsiElement[] elements, final DataContext dataContext) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class ExtractMethodObjectTest extends LightRefactoringTestCase {
|
||||
extractProcessor.prepare();
|
||||
extractProcessor.testPrepare();
|
||||
|
||||
ExtractMethodObjectHandler.run(getProject(), getEditor(), processor, extractProcessor);
|
||||
ExtractMethodObjectHandler.extractMethodObject(getProject(), getEditor(), processor, extractProcessor);
|
||||
|
||||
checkResultByFile("/refactoring/extractMethodObject/" + testName + ".java" + ".after");
|
||||
}
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ public class ExtractMethodObjectWithMultipleExitPointsTest extends LightRefactor
|
||||
processor.setCreateInnerClass(createInnerClass);
|
||||
|
||||
|
||||
ExtractMethodObjectHandler.run(getProject(), getEditor(), processor, extractProcessor);
|
||||
ExtractMethodObjectHandler.extractMethodObject(getProject(), getEditor(), processor, extractProcessor);
|
||||
|
||||
|
||||
checkResultByFile("/refactoring/extractMethodObject/multipleExitPoints/" + testName + ".java" + ".after");
|
||||
|
||||
@@ -1210,7 +1210,7 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
|
||||
if (newNameOfFirstParam != null) {
|
||||
processor.changeParamName(0, newNameOfFirstParam);
|
||||
}
|
||||
ExtractMethodHandler.run(project, editor, processor);
|
||||
ExtractMethodHandler.extractMethod(project, processor);
|
||||
}
|
||||
|
||||
if (replaceAllDuplicates) {
|
||||
|
||||
Reference in New Issue
Block a user