/
AddFunctionParametersFix.java
197 lines (172 loc) · 8.54 KB
/
AddFunctionParametersFix.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright 2010-2015 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.kotlin.idea.quickfix;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiReference;
import com.intellij.psi.search.searches.ReferencesSearch;
import com.intellij.psi.util.PsiTreeUtil;
import kotlin.ExtensionFunction0;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor;
import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor;
import org.jetbrains.kotlin.idea.JetBundle;
import org.jetbrains.kotlin.idea.caches.resolve.ResolvePackage;
import org.jetbrains.kotlin.idea.core.refactoring.JetNameValidator;
import org.jetbrains.kotlin.idea.core.refactoring.SimpleCollectingValidator;
import org.jetbrains.kotlin.idea.refactoring.changeSignature.*;
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers;
import org.jetbrains.kotlin.psi.JetCallElement;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.psi.ValueArgument;
import org.jetbrains.kotlin.resolve.BindingContext;
import org.jetbrains.kotlin.types.JetType;
import org.jetbrains.kotlin.types.checker.JetTypeChecker;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.jetbrains.kotlin.idea.refactoring.changeSignature.ChangeSignaturePackage.runChangeSignature;
public class AddFunctionParametersFix extends ChangeFunctionSignatureFix {
private final JetCallElement callElement;
private final boolean hasTypeMismatches;
private final List<JetType> typesToShorten = new ArrayList<JetType>();
public AddFunctionParametersFix(
@NotNull JetCallElement callElement,
@NotNull FunctionDescriptor functionDescriptor,
boolean hasTypeMismatches
) {
super(callElement, functionDescriptor);
this.callElement = callElement;
this.hasTypeMismatches = hasTypeMismatches;
}
@NotNull
@Override
public String getText() {
List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters();
List<? extends ValueArgument> arguments = callElement.getValueArguments();
int newParametersCnt = arguments.size() - parameters.size();
assert newParametersCnt > 0;
String subjectSuffix = newParametersCnt > 1 ? "s" : "";
if (isConstructor()) {
String className = functionDescriptor.getContainingDeclaration().getName().asString();
if (hasTypeMismatches)
return JetBundle.message("change.constructor.signature", className);
else
return JetBundle.message("add.parameters.to.constructor", subjectSuffix, className);
}
else {
String functionName = functionDescriptor.getName().asString();
if (hasTypeMismatches)
return JetBundle.message("change.function.signature", functionName);
else
return JetBundle.message("add.parameters.to.function", subjectSuffix, functionName);
}
}
@Override
public boolean isAvailable(
@NotNull Project project, Editor editor, PsiFile file
) {
if (!super.isAvailable(project, editor, file)) {
return false;
}
int newParametersCnt = callElement.getValueArguments().size() - functionDescriptor.getValueParameters().size();
if (newParametersCnt <= 0) {
// psi for this quickfix is no longer valid
return false;
}
return true;
}
@Override
public boolean startInWriteAction() {
return true;
}
@Override
protected void invoke(@NotNull Project project, Editor editor, JetFile file) {
BindingContext bindingContext = ResolvePackage.analyzeFully((JetFile) callElement.getContainingFile());
runChangeSignature(project, functionDescriptor, addParameterConfiguration(), bindingContext, callElement, getText());
}
private JetChangeSignatureConfiguration addParameterConfiguration() {
return new JetChangeSignatureConfiguration() {
@NotNull
@Override
public JetMethodDescriptor configure(@NotNull final JetMethodDescriptor originalDescriptor, @NotNull final BindingContext bindingContext) {
return ChangeSignaturePackage.modify(
originalDescriptor,
new ExtensionFunction0<JetMutableMethodDescriptor, Unit>() {
@Override
public Unit invoke(JetMutableMethodDescriptor descriptor) {
List<ValueParameterDescriptor> parameters = functionDescriptor.getValueParameters();
List<? extends ValueArgument> arguments = callElement.getValueArguments();
JetNameValidator validator = new SimpleCollectingValidator();
for (int i = 0; i < arguments.size(); i ++) {
ValueArgument argument = arguments.get(i);
JetExpression expression = argument.getArgumentExpression();
if (i < parameters.size()) {
validator.validateName(parameters.get(i).getName().asString());
JetType argumentType = expression != null ? bindingContext.getType(expression) : null;
JetType parameterType = parameters.get(i).getType();
if (argumentType != null && !JetTypeChecker.DEFAULT.isSubtypeOf(argumentType, parameterType)) {
descriptor.getParameters().get(i).setCurrentTypeText(IdeDescriptorRenderers.SOURCE_CODE.renderType(argumentType));
typesToShorten.add(argumentType);
}
}
else {
JetParameterInfo parameterInfo =
getNewParameterInfo(originalDescriptor.getBaseDescriptor(), bindingContext, argument, validator);
typesToShorten.add(parameterInfo.getOriginalType());
if (expression != null) {
parameterInfo.setDefaultValueForCall(expression);
}
descriptor.addParameter(parameterInfo);
}
}
return null;
}
}
);
}
@Override
public boolean performSilently(@NotNull Collection<? extends PsiElement> affectedFunctions) {
if (affectedFunctions.size() != 1) {
return false;
}
PsiElement onlyFunction = affectedFunctions.iterator().next();
return !hasTypeMismatches && !isConstructor() && !hasOtherUsages(onlyFunction);
}
@Override
public boolean forcePerformForSelectedFunctionOnly() {
return false;
}
};
}
private boolean hasOtherUsages(@NotNull PsiElement function) {
for (PsiReference reference : ReferencesSearch.search(function)) {
JetCallElement call = PsiTreeUtil.getParentOfType(reference.getElement(), JetCallElement.class);
if (call != null && !callElement.equals(call)) {
return true;
}
}
return false;
}
private boolean isConstructor() {
return functionDescriptor instanceof ConstructorDescriptor;
}
}