More quickfixes to use API for setting type ref

This commit is contained in:
Valentin Kipyatkov
2014-10-04 00:33:05 +04:00
parent d876e85eb2
commit 89ea4d5a85
3 changed files with 10 additions and 27 deletions
@@ -20,7 +20,6 @@ import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
@@ -108,26 +107,15 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
changeFunctionLiteralReturnTypeFix.invoke(project, editor, file);
}
else {
element.setReturnTypeRef(null);
if (!(KotlinBuiltIns.getInstance().isUnit(type) && element.hasBlockBody())) {
addReturnTypeAnnotation(element, renderedType);
element.setReturnTypeRef(JetPsiFactory(project).createType(renderedType));
}
else {
element.setReturnTypeRef(null);
}
}
}
public static void addReturnTypeAnnotation(JetFunction function, String typeText) {
PsiElement elementToPrecedeType = function.getValueParameterList();
if (elementToPrecedeType == null) elementToPrecedeType = function.getNameIdentifier();
assert elementToPrecedeType != null : "Return type of function without name can't mismatch anything";
if (elementToPrecedeType.getNextSibling() instanceof PsiErrorElement) {
// if a function doesn't have a value parameter list, a syntax error is raised, and it should follow the function name
elementToPrecedeType = elementToPrecedeType.getNextSibling();
}
JetPsiFactory psiFactory = JetPsiFactory(function);
function.addAfter(psiFactory.createType(typeText), elementToPrecedeType);
function.addAfter(psiFactory.createColon(), elementToPrecedeType);
}
@NotNull
public static JetMultiDeclarationEntry getMultiDeclarationEntryThatTypeMismatchComponentFunction(Diagnostic diagnostic) {
Name componentName = COMPONENT_FUNCTION_RETURN_TYPE_MISMATCH.cast(diagnostic).getA();
@@ -65,8 +65,6 @@ public class ChangeParameterTypeFix extends JetIntentionAction<JetParameter> {
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetTypeReference typeReference = element.getTypeReference();
assert typeReference != null : "Parameter without type annotation cannot cause type mismatch";
typeReference.replace(JetPsiFactory(file).createType(renderedType));
element.setTypeRef(JetPsiFactory(file).createType(renderedType));
}
}
@@ -24,7 +24,6 @@ import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import org.jetbrains.jet.lexer.JetKeywordToken;
import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.plugin.quickfix.AddModifierFix;
import org.jetbrains.jet.plugin.quickfix.ChangeFunctionReturnTypeFix;
import org.jetbrains.jet.plugin.quickfix.ChangeVisibilityModifierFix;
import org.jetbrains.jet.plugin.refactoring.JetRefactoringUtil;
import org.jetbrains.jet.plugin.refactoring.changeSignature.JetChangeInfo;
@@ -62,8 +61,9 @@ public class JetFunctionDefinitionUsage extends JetUsageInfo<PsiElement> {
String returnTypeText = changeInfo.getNewReturnTypeText();
//TODO use ChangeFunctionReturnTypeFix.invoke when JetTypeCodeFragment.getType() is ready
if (!KotlinBuiltIns.getInstance().getUnitType().toString().equals(returnTypeText))
ChangeFunctionReturnTypeFix.addReturnTypeAnnotation(function, returnTypeText);
if (!KotlinBuiltIns.getInstance().getUnitType().toString().equals(returnTypeText)) {
function.setReturnTypeRef(JetPsiFactory(function).createType(returnTypeText));
}
}
}
else
@@ -138,11 +138,8 @@ public class JetFunctionDefinitionUsage extends JetUsageInfo<PsiElement> {
}
if (parameterInfo.isTypeChanged()) {
JetTypeReference newType = psiFactory.createType(parameterInfo.getTypeText());
JetTypeReference typeReference = parameter.getTypeReference();
if (typeReference != null)
typeReference.replace(newType);
JetTypeReference newTypeRef = psiFactory.createType(parameterInfo.getTypeText());
parameter.setTypeRef(newTypeRef);
}
PsiElement identifier = parameter.getNameIdentifier();