Create from usage: Removed extraneous project parameters.

This commit is contained in:
Jack Zhou
2013-05-02 11:17:04 -04:00
parent 1a34c71763
commit e0201bf8f3
@@ -614,7 +614,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
assert !ownerTypeCandidates.isEmpty(); assert !ownerTypeCandidates.isEmpty();
if (ownerTypeCandidates.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) { if (ownerTypeCandidates.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
selectedReceiverType = ownerTypeCandidates.get(0); selectedReceiverType = ownerTypeCandidates.get(0);
addFunctionToSelectedOwner(project); addFunctionToSelectedOwner();
} else { } else {
// class selection // class selection
List<ClassCandidate> options = new ArrayList<ClassCandidate>(); List<ClassCandidate> options = new ArrayList<ClassCandidate>();
@@ -638,7 +638,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
CommandProcessor.getInstance().executeCommand(project, new Runnable() { CommandProcessor.getInstance().executeCommand(project, new Runnable() {
@Override @Override
public void run() { public void run() {
addFunctionToSelectedOwner(project); addFunctionToSelectedOwner();
} }
}, getText(), null); }, getText(), null);
} }
@@ -651,7 +651,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
} }
} }
private void addFunctionToSelectedOwner(@NotNull final Project project) { private void addFunctionToSelectedOwner() {
// gather relevant information // gather relevant information
ownerClassDescriptor = DescriptorUtils.getClassDescriptorForType(selectedReceiverType.getType()); ownerClassDescriptor = DescriptorUtils.getClassDescriptorForType(selectedReceiverType.getType());
JetType receiverType = ownerClassDescriptor.getDefaultType(); JetType receiverType = ownerClassDescriptor.getDefaultType();
@@ -705,13 +705,14 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
ApplicationManager.getApplication().runWriteAction(new Runnable() { ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override @Override
public void run() { public void run() {
JetNamedFunction func = createFunctionSkeleton(project); JetNamedFunction func = createFunctionSkeleton();
buildAndRunTemplate(project, func); buildAndRunTemplate(func);
} }
}); });
} }
private JetNamedFunction createFunctionSkeleton(@NotNull Project project) { private JetNamedFunction createFunctionSkeleton() {
Project project = currentFile.getProject();
JetNamedFunction func; JetNamedFunction func;
String[] parameterStrings = new String[parameters.size()]; String[] parameterStrings = new String[parameters.size()];
for (int i = 0; i < parameterStrings.length; i++) { for (int i = 0; i < parameterStrings.length; i++) {
@@ -751,7 +752,8 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
return func; return func;
} }
private void buildAndRunTemplate(@NotNull final Project project, @NotNull JetNamedFunction func) { private void buildAndRunTemplate(@NotNull JetNamedFunction func) {
Project project = func.getProject();
JetParameterList parameterList = func.getValueParameterList(); JetParameterList parameterList = func.getValueParameterList();
assert parameterList != null; assert parameterList != null;
@@ -764,7 +766,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
TemplateBuilderImpl builder = new TemplateBuilderImpl(containingFile); TemplateBuilderImpl builder = new TemplateBuilderImpl(containingFile);
final TypeExpression returnTypeExpression = isUnit ? null : setupReturnTypeTemplate(builder, func); final TypeExpression returnTypeExpression = isUnit ? null : setupReturnTypeTemplate(builder, func);
final TypeExpression[] parameterTypeExpressions = setupParameterTypeTemplates(project, builder, parameterList); final TypeExpression[] parameterTypeExpressions = setupParameterTypeTemplates(builder, parameterList);
// add a segment for the parameter list // add a segment for the parameter list
// Note: because TemplateBuilderImpl does not have a replaceElement overload that takes in both a TextRange and alwaysStopAt, we // Note: because TemplateBuilderImpl does not have a replaceElement overload that takes in both a TextRange and alwaysStopAt, we
@@ -797,10 +799,10 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
@Override @Override
public void run() { public void run() {
// file templates // file templates
setupFunctionBody(project, func); setupFunctionBody(func);
// change short type names to fully qualified ones (to be shortened below) // change short type names to fully qualified ones (to be shortened below)
setupTypeReferencesForShortening(project, func, typeRefsToShorten, parameterTypeExpressions, returnTypeExpression); setupTypeReferencesForShortening(func, typeRefsToShorten, parameterTypeExpressions, returnTypeExpression);
} }
}); });
@@ -841,7 +843,6 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
} }
private void setupTypeReferencesForShortening( private void setupTypeReferencesForShortening(
@NotNull Project project,
@NotNull JetNamedFunction func, @NotNull JetNamedFunction func,
@NotNull List<JetTypeReference> typeRefsToShorten, @NotNull List<JetTypeReference> typeRefsToShorten,
@NotNull TypeExpression[] parameterTypeExpressions, @NotNull TypeExpression[] parameterTypeExpressions,
@@ -849,8 +850,8 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
) { ) {
if (isExtension) { if (isExtension) {
JetTypeReference receiverTypeRef = JetTypeReference receiverTypeRef =
JetPsiFactory.createType(project, renderTypeLong(selectedReceiverType.getType(), typeParameterNameMap)); JetPsiFactory.createType(func.getProject(), renderTypeLong(selectedReceiverType.getType(), typeParameterNameMap));
replaceWithLongerName(project, receiverTypeRef, selectedReceiverType.getType()); replaceWithLongerName(receiverTypeRef, selectedReceiverType.getType());
receiverTypeRef = func.getReceiverTypeRef(); receiverTypeRef = func.getReceiverTypeRef();
if (receiverTypeRef != null) { if (receiverTypeRef != null) {
@@ -864,7 +865,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
if (returnTypeRef != null) { if (returnTypeRef != null) {
JetType returnType = returnTypeExpression.getTypeFromSelection(returnTypeRef.getText()); JetType returnType = returnTypeExpression.getTypeFromSelection(returnTypeRef.getText());
if (returnType != null) { // user selected a given type if (returnType != null) { // user selected a given type
replaceWithLongerName(project, returnTypeRef, returnType); replaceWithLongerName(returnTypeRef, returnType);
returnTypeRef = func.getReturnTypeRef(); returnTypeRef = func.getReturnTypeRef();
assert returnTypeRef != null; assert returnTypeRef != null;
typeRefsToShorten.add(returnTypeRef); typeRefsToShorten.add(returnTypeRef);
@@ -881,7 +882,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
if (parameterTypeRef != null) { if (parameterTypeRef != null) {
JetType parameterType = parameterTypeExpressions[i].getTypeFromSelection(parameterTypeRef.getText()); JetType parameterType = parameterTypeExpressions[i].getTypeFromSelection(parameterTypeRef.getText());
if (parameterType != null) { if (parameterType != null) {
replaceWithLongerName(project, parameterTypeRef, parameterType); replaceWithLongerName(parameterTypeRef, parameterType);
parameterIndicesToShorten.add(i); parameterIndicesToShorten.add(i);
} }
} }
@@ -895,7 +896,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
} }
} }
private void setupFunctionBody(@NotNull Project project, @NotNull JetNamedFunction func) { private void setupFunctionBody(@NotNull JetNamedFunction func) {
FileTemplate fileTemplate = FileTemplateManager.getInstance().getCodeTemplate(TEMPLATE_FROM_USAGE_FUNCTION_BODY); FileTemplate fileTemplate = FileTemplateManager.getInstance().getCodeTemplate(TEMPLATE_FROM_USAGE_FUNCTION_BODY);
Properties properties = new Properties(); Properties properties = new Properties();
if (isUnit) { if (isUnit) {
@@ -917,7 +918,7 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
} catch (Exception e) { } catch (Exception e) {
throw new IncorrectOperationException("Failed to parse file template", e); throw new IncorrectOperationException("Failed to parse file template", e);
} }
JetExpression newBodyExpression = JetPsiFactory.createFunctionBody(project, bodyText); JetExpression newBodyExpression = JetPsiFactory.createFunctionBody(func.getProject(), bodyText);
JetExpression oldBodyExpression = func.getBodyExpression(); JetExpression oldBodyExpression = func.getBodyExpression();
assert oldBodyExpression != null; assert oldBodyExpression != null;
oldBodyExpression.replace(newBodyExpression); oldBodyExpression.replace(newBodyExpression);
@@ -954,12 +955,11 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
return new TypeParameterListExpression(receiverTypeParameterNames, typeParameterMap); return new TypeParameterListExpression(receiverTypeParameterNames, typeParameterMap);
} }
private TypeExpression[] setupParameterTypeTemplates(@NotNull Project project, @NotNull TemplateBuilder builder, private TypeExpression[] setupParameterTypeTemplates(@NotNull TemplateBuilder builder, @NotNull JetParameterList parameterList) {
@NotNull JetParameterList parameterList) {
List<JetParameter> jetParameters = parameterList.getParameters(); List<JetParameter> jetParameters = parameterList.getParameters();
assert jetParameters.size() == parameters.size(); assert jetParameters.size() == parameters.size();
TypeExpression[] parameterTypeExpressions = new TypeExpression[parameters.size()]; TypeExpression[] parameterTypeExpressions = new TypeExpression[parameters.size()];
JetNameValidator dummyValidator = JetNameValidator.getEmptyValidator(project); JetNameValidator dummyValidator = JetNameValidator.getEmptyValidator(parameterList.getProject());
for (int i = 0; i < parameters.size(); i++) { for (int i = 0; i < parameters.size(); i++) {
Parameter parameter = parameters.get(i); Parameter parameter = parameters.get(i);
JetParameter jetParameter = jetParameters.get(i); JetParameter jetParameter = jetParameters.get(i);
@@ -998,7 +998,8 @@ public class CreateFunctionFromUsageFix extends CreateFromUsageFixBase {
return parameterTypeExpressions; return parameterTypeExpressions;
} }
private void replaceWithLongerName(@NotNull Project project, @NotNull JetTypeReference typeRef, @NotNull JetType type) { private void replaceWithLongerName(@NotNull JetTypeReference typeRef, @NotNull JetType type) {
Project project = typeRef.getProject();
JetTypeReference fullyQualifiedReceiverTypeRef = JetPsiFactory.createType(project, renderTypeLong(type, typeParameterNameMap)); JetTypeReference fullyQualifiedReceiverTypeRef = JetPsiFactory.createType(project, renderTypeLong(type, typeParameterNameMap));
typeRef.replace(fullyQualifiedReceiverTypeRef); typeRef.replace(fullyQualifiedReceiverTypeRef);
} }