JetChangeFunctionSignatureAction made nested class of quickfix class (and it does not need to be QuestionAction!)
This commit is contained in:
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2013 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.jet.plugin.actions;
|
||||
|
||||
import com.intellij.codeInsight.hint.QuestionAction;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
||||
import com.intellij.openapi.ui.popup.PopupStep;
|
||||
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
||||
import org.jetbrains.jet.lang.psi.JetParameterList;
|
||||
import org.jetbrains.jet.lang.psi.JetPsiFactory;
|
||||
import org.jetbrains.jet.lang.psi.JetTypeReference;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.jet.renderer.DescriptorRendererBuilder;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import static org.jetbrains.jet.lang.psi.PsiPackage.JetPsiFactory;
|
||||
|
||||
/**
|
||||
* Changes method signature to one of provided signatures.
|
||||
* Based on {@link JetAddImportAction}
|
||||
*/
|
||||
public class JetChangeFunctionSignatureAction implements QuestionAction {
|
||||
public static final DescriptorRenderer SIGNATURE_RENDERER = new DescriptorRendererBuilder()
|
||||
.setWithDefinedIn(false)
|
||||
.setModifiers()
|
||||
.setShortNames(true)
|
||||
.setUnitReturnType(false).build();
|
||||
|
||||
private final Project project;
|
||||
private final Editor editor;
|
||||
private final JetNamedFunction element;
|
||||
private final List<FunctionDescriptor> signatures;
|
||||
|
||||
/**
|
||||
* @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 Editor editor,
|
||||
@NotNull JetNamedFunction element,
|
||||
@NotNull Collection<FunctionDescriptor> signatures
|
||||
) {
|
||||
this.project = project;
|
||||
this.editor = editor;
|
||||
this.element = element;
|
||||
this.signatures = new ArrayList<FunctionDescriptor>(signatures);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute() {
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
if (!element.isValid() || signatures.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (signatures.size() == 1 || !editor.getComponent().isShowing()) {
|
||||
changeSignature(element, project, signatures.get(0));
|
||||
}
|
||||
else {
|
||||
chooseSignatureAndChange();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private BaseListPopupStep getSignaturePopup() {
|
||||
return new BaseListPopupStep<FunctionDescriptor>(
|
||||
JetBundle.message("change.function.signature.chooser.title"), signatures) {
|
||||
@Override
|
||||
public boolean isAutoSelectionEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PopupStep onChosen(FunctionDescriptor selectedValue, boolean finalChoice) {
|
||||
if (finalChoice) {
|
||||
changeSignature(element, project, selectedValue);
|
||||
}
|
||||
return FINAL_CHOICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Icon getIconFor(FunctionDescriptor aValue) {
|
||||
return PlatformIcons.FUNCTION_ICON;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getTextFor(FunctionDescriptor aValue) {
|
||||
return SIGNATURE_RENDERER.render(aValue);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void changeSignature(final JetNamedFunction function, Project project, FunctionDescriptor patternDescriptor) {
|
||||
final String signatureString = DescriptorRenderer.SOURCE_CODE.render(patternDescriptor);
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
final JetPsiFactory psiFactory = JetPsiFactory(project);
|
||||
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JetNamedFunction patternFunction = psiFactory.createFunction(signatureString);
|
||||
|
||||
JetTypeReference newTypeRef = function.setReturnTypeRef(patternFunction.getReturnTypeRef());
|
||||
if (newTypeRef != null) {
|
||||
ShortenReferences.INSTANCE$.process(newTypeRef);
|
||||
}
|
||||
|
||||
JetParameterList newParameterList = (JetParameterList) function.getValueParameterList().replace(patternFunction.getValueParameterList());
|
||||
ShortenReferences.INSTANCE$.process(newParameterList);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, JetBundle.message("change.function.signature.action"), null);
|
||||
}
|
||||
|
||||
private void chooseSignatureAndChange() {
|
||||
JBPopupFactory.getInstance().createListPopup(getSignaturePopup()).showInBestPositionFor(editor);
|
||||
}
|
||||
}
|
||||
@@ -23,17 +23,22 @@ import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Queues;
|
||||
import com.intellij.codeInsight.hint.HintManager;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.command.CommandProcessor;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.popup.JBPopupFactory;
|
||||
import com.intellij.openapi.ui.popup.PopupStep;
|
||||
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.jet.lang.descriptors.*;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamedFunction;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.FunctionDescriptorUtil;
|
||||
import org.jetbrains.jet.lang.resolve.VisibilityUtil;
|
||||
@@ -43,9 +48,12 @@ import org.jetbrains.jet.lang.types.JetType;
|
||||
import org.jetbrains.jet.lang.types.TypeUtils;
|
||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||
import org.jetbrains.jet.plugin.JetBundle;
|
||||
import org.jetbrains.jet.plugin.actions.JetChangeFunctionSignatureAction;
|
||||
import org.jetbrains.jet.plugin.caches.resolve.ResolvePackage;
|
||||
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences;
|
||||
import org.jetbrains.jet.renderer.DescriptorRenderer;
|
||||
import org.jetbrains.jet.renderer.DescriptorRendererBuilder;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
@@ -69,16 +77,11 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
|
||||
public String getText() {
|
||||
if (possibleSignatures.size() == 1)
|
||||
return JetBundle.message("change.function.signature.action.single",
|
||||
getFunctionSignatureString(possibleSignatures.get(0)));
|
||||
SIGNATURE_RENDERER.render(possibleSignatures.get(0)));
|
||||
else
|
||||
return JetBundle.message("change.function.signature.action.multiple");
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getFunctionSignatureString(@NotNull FunctionDescriptor functionSignature) {
|
||||
return JetChangeFunctionSignatureAction.SIGNATURE_RENDERER.render(functionSignature);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getFamilyName() {
|
||||
@@ -118,7 +121,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
|
||||
for (FunctionDescriptor superFunction : superFunctions) {
|
||||
if (!superFunction.getKind().isReal()) continue;
|
||||
FunctionDescriptor signature = changeSignatureToMatch(functionDescriptor, superFunction);
|
||||
possibleSignatures.put(getFunctionSignatureString(signature), signature);
|
||||
possibleSignatures.put(SIGNATURE_RENDERER.render(signature), signature);
|
||||
}
|
||||
List<String> keys = new ArrayList<String>(possibleSignatures.keySet());
|
||||
Collections.sort(keys);
|
||||
@@ -290,4 +293,113 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static final DescriptorRenderer SIGNATURE_RENDERER = new DescriptorRendererBuilder()
|
||||
.setWithDefinedIn(false)
|
||||
.setModifiers()
|
||||
.setShortNames(true)
|
||||
.setUnitReturnType(false).build();
|
||||
|
||||
private static class JetChangeFunctionSignatureAction {
|
||||
|
||||
private final Project project;
|
||||
private final Editor editor;
|
||||
private final JetNamedFunction element;
|
||||
private final List<FunctionDescriptor> signatures;
|
||||
|
||||
/**
|
||||
* @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 Editor editor,
|
||||
@NotNull JetNamedFunction element,
|
||||
@NotNull Collection<FunctionDescriptor> signatures
|
||||
) {
|
||||
this.project = project;
|
||||
this.editor = editor;
|
||||
this.element = element;
|
||||
this.signatures = new ArrayList<FunctionDescriptor>(signatures);
|
||||
}
|
||||
|
||||
public boolean execute() {
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
if (!element.isValid() || signatures.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (signatures.size() == 1 || !editor.getComponent().isShowing()) {
|
||||
changeSignature(element, project, signatures.get(0));
|
||||
}
|
||||
else {
|
||||
chooseSignatureAndChange();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private BaseListPopupStep getSignaturePopup() {
|
||||
return new BaseListPopupStep<FunctionDescriptor>(
|
||||
JetBundle.message("change.function.signature.chooser.title"), signatures) {
|
||||
@Override
|
||||
public boolean isAutoSelectionEnabled() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PopupStep onChosen(FunctionDescriptor selectedValue, boolean finalChoice) {
|
||||
if (finalChoice) {
|
||||
changeSignature(element, project, selectedValue);
|
||||
}
|
||||
return FINAL_CHOICE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Icon getIconFor(FunctionDescriptor aValue) {
|
||||
return PlatformIcons.FUNCTION_ICON;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public String getTextFor(FunctionDescriptor aValue) {
|
||||
return SIGNATURE_RENDERER.render(aValue);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void changeSignature(final JetNamedFunction function, Project project, FunctionDescriptor patternDescriptor) {
|
||||
final String signatureString = DescriptorRenderer.SOURCE_CODE.render(patternDescriptor);
|
||||
|
||||
PsiDocumentManager.getInstance(project).commitAllDocuments();
|
||||
|
||||
final JetPsiFactory psiFactory = PsiPackage.JetPsiFactory(project);
|
||||
CommandProcessor.getInstance().executeCommand(project, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
ApplicationManager.getApplication().runWriteAction(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
JetNamedFunction patternFunction = psiFactory.createFunction(signatureString);
|
||||
|
||||
JetTypeReference newTypeRef = function.setReturnTypeRef(patternFunction.getReturnTypeRef());
|
||||
if (newTypeRef != null) {
|
||||
ShortenReferences.INSTANCE$.process(newTypeRef);
|
||||
}
|
||||
|
||||
JetParameterList newParameterList = (JetParameterList) function.getValueParameterList().replace(patternFunction.getValueParameterList());
|
||||
ShortenReferences.INSTANCE$.process(newParameterList);
|
||||
}
|
||||
});
|
||||
}
|
||||
}, JetBundle.message("change.function.signature.action"), null);
|
||||
}
|
||||
|
||||
private void chooseSignatureAndChange() {
|
||||
JBPopupFactory.getInstance().createListPopup(getSignaturePopup()).showInBestPositionFor(editor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user