Minor refactorings in ChangeMemberFunctionSignatureFix

This commit is contained in:
Valentin Kipyatkov
2014-10-06 22:16:47 +04:00
parent b26e6665b0
commit c42b7b1780
@@ -60,6 +60,12 @@ import java.util.*;
* Fix that changes member function's signature to match one of super functions' signatures. * Fix that changes member function's signature to match one of super functions' signatures.
*/ */
public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunction> { public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunction> {
private static final DescriptorRenderer SIGNATURE_RENDERER = new DescriptorRendererBuilder()
.setWithDefinedIn(false)
.setModifiers()
.setShortNames(true)
.setUnitReturnType(false).build();
private final List<FunctionDescriptor> possibleSignatures; private final List<FunctionDescriptor> possibleSignatures;
public ChangeMemberFunctionSignatureFix(@NotNull JetNamedFunction element) { public ChangeMemberFunctionSignatureFix(@NotNull JetNamedFunction element) {
@@ -75,11 +81,12 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
@NotNull @NotNull
@Override @Override
public String getText() { public String getText() {
if (possibleSignatures.size() == 1) if (possibleSignatures.size() == 1) {
return JetBundle.message("change.function.signature.action.single", return JetBundle.message("change.function.signature.action.single", SIGNATURE_RENDERER.render(possibleSignatures.get(0)));
SIGNATURE_RENDERER.render(possibleSignatures.get(0))); }
else else {
return JetBundle.message("change.function.signature.action.multiple"); return JetBundle.message("change.function.signature.action.multiple");
}
} }
@NotNull @NotNull
@@ -89,21 +96,15 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
} }
@Override @Override
protected void invoke(@NotNull final Project project, @NotNull final Editor editor, JetFile file) protected void invoke(@NotNull final Project project, final Editor editor, JetFile file) throws IncorrectOperationException {
throws IncorrectOperationException {
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() { CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override @Override
public void run() { public void run() {
createAction(project, editor).execute(); new MyAction(project, editor, element, possibleSignatures).execute();
} }
}); });
} }
@NotNull
private JetChangeFunctionSignatureAction createAction(@NotNull Project project, @NotNull Editor editor) {
return new JetChangeFunctionSignatureAction(project, editor, element, possibleSignatures);
}
/** /**
* Computes all the signatures a 'functionElement' could be changed to in order to remove NOTHING_TO_OVERRIDE error. * Computes all the signatures a 'functionElement' could be changed to in order to remove NOTHING_TO_OVERRIDE error.
*/ */
@@ -294,46 +295,31 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
return true; return true;
} }
private static final DescriptorRenderer SIGNATURE_RENDERER = new DescriptorRendererBuilder() private static class MyAction {
.setWithDefinedIn(false)
.setModifiers()
.setShortNames(true)
.setUnitReturnType(false).build();
private static class JetChangeFunctionSignatureAction {
private final Project project; private final Project project;
private final Editor editor; private final Editor editor;
private final JetNamedFunction element; private final JetNamedFunction function;
private final List<FunctionDescriptor> signatures; private final List<FunctionDescriptor> signatures;
/** public MyAction(
* @param project Project where action takes place.
* @param editor Editor where modification should be done.
* @param element Function element which signature should be changed.
* @param signatures Variants for new function signature.
*/
public JetChangeFunctionSignatureAction(
@NotNull Project project, @NotNull Project project,
@NotNull Editor editor, @NotNull Editor editor,
@NotNull JetNamedFunction element, @NotNull JetNamedFunction function,
@NotNull Collection<FunctionDescriptor> signatures @NotNull List<FunctionDescriptor> signatures
) { ) {
this.project = project; this.project = project;
this.editor = editor; this.editor = editor;
this.element = element; this.function = function;
this.signatures = new ArrayList<FunctionDescriptor>(signatures); this.signatures = signatures;
} }
public boolean execute() { public boolean execute() {
PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiDocumentManager.getInstance(project).commitAllDocuments();
if (!element.isValid() || signatures.isEmpty()) { if (!function.isValid() || signatures.isEmpty()) return false;
return false;
}
if (signatures.size() == 1 || !editor.getComponent().isShowing()) { if (signatures.size() == 1 || !editor.getComponent().isShowing()) {
changeSignature(element, project, signatures.get(0)); changeSignature(signatures.get(0));
} }
else { else {
chooseSignatureAndChange(); chooseSignatureAndChange();
@@ -353,7 +339,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
@Override @Override
public PopupStep onChosen(FunctionDescriptor selectedValue, boolean finalChoice) { public PopupStep onChosen(FunctionDescriptor selectedValue, boolean finalChoice) {
if (finalChoice) { if (finalChoice) {
changeSignature(element, project, selectedValue); changeSignature(selectedValue);
} }
return FINAL_CHOICE; return FINAL_CHOICE;
} }
@@ -371,7 +357,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
}; };
} }
private static void changeSignature(final JetNamedFunction function, Project project, FunctionDescriptor patternDescriptor) { private void changeSignature(FunctionDescriptor patternDescriptor) {
final String signatureString = DescriptorRenderer.SOURCE_CODE.render(patternDescriptor); final String signatureString = DescriptorRenderer.SOURCE_CODE.render(patternDescriptor);
PsiDocumentManager.getInstance(project).commitAllDocuments(); PsiDocumentManager.getInstance(project).commitAllDocuments();