Converted JetIntentionAction to Kotlin

This commit is contained in:
Valentin Kipyatkov
2015-10-13 22:48:24 +03:00
parent 800e629614
commit ab87471177
31 changed files with 131 additions and 152 deletions
@@ -51,12 +51,12 @@ public class AddFunctionBodyFix extends JetIntentionAction<JetFunction> {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && !element.hasBody();
return super.isAvailable(project, editor, file) && !getElement().hasBody();
}
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetFunction newElement = (JetFunction) element.copy();
JetFunction newElement = (JetFunction) getElement().copy();
JetPsiFactory psiFactory = JetPsiFactoryKt.JetPsiFactory(file);
if (!(newElement.getLastChild() instanceof PsiWhiteSpace)) {
newElement.add(psiFactory.createWhiteSpace());
@@ -64,7 +64,7 @@ public class AddFunctionBodyFix extends JetIntentionAction<JetFunction> {
if (!newElement.hasBody()) {
newElement.add(psiFactory.createEmptyBody());
}
element.replace(newElement);
getElement().replace(newElement);
}
public static JetSingleIntentionActionFactory createFactory() {
@@ -67,7 +67,7 @@ public class AddFunctionParametersFix(
"Add parameter$subjectSuffix to $callableDescription"
}
override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean {
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
if (!super.isAvailable(project, editor, file)) return false
// newParametersCnt <= 0: psi for this quickfix is no longer valid
@@ -24,13 +24,12 @@ import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.psi.psiUtil.parents
import java.util.*
public class AddLoopLabelFix(loop: JetLoopExpression, val jumpExpression: JetElement): JetIntentionAction<JetLoopExpression>(loop) {
override fun getText() = "Add label to loop"
override fun getFamilyName() = getText()
override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean {
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
return super.isAvailable(project, editor, file)
}
@@ -65,7 +65,7 @@ public class AddModifierFix extends JetIntentionAction<JetModifierListOwner> {
@Override
public String getText() {
if (modifier == ABSTRACT_KEYWORD || modifier == JetTokens.OPEN_KEYWORD) {
return JetBundle.message("make.element.modifier", getElementName(element), modifier.getValue());
return JetBundle.message("make.element.modifier", getElementName(getElement()), modifier.getValue());
}
return JetBundle.message("add.modifier", modifier.getValue());
}
@@ -78,7 +78,7 @@ public class AddModifierFix extends JetIntentionAction<JetModifierListOwner> {
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
element.addModifier(modifier);
getElement().addModifier(modifier);
}
@Override
@@ -46,7 +46,7 @@ public class AddOpenModifierToClassDeclarationFix extends JetIntentionAction<Jet
return false;
}
JetSimpleNameExpression referenceExpression = PsiTreeUtil.findChildOfType(element, JetSimpleNameExpression.class);
JetSimpleNameExpression referenceExpression = PsiTreeUtil.findChildOfType(getElement(), JetSimpleNameExpression.class);
if (referenceExpression == null) {
return false;
}
@@ -55,7 +55,7 @@ public class AddOverrideToEqualsHashCodeToStringFix extends JetIntentionAction<P
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && isEqualsHashCodeOrToString(element);
return super.isAvailable(project, editor, file) && isEqualsHashCodeOrToString(getElement());
}
private static boolean isEqualsHashCodeOrToString(@Nullable PsiElement element) {
@@ -55,13 +55,13 @@ public abstract class AddStarProjectionsFix extends JetIntentionAction<JetUserTy
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
assert element.getTypeArguments().isEmpty();
assert getElement().getTypeArguments().isEmpty();
String typeString = TypeReconstructionUtil.getTypeNameAndStarProjectionsString(element.getText(), argumentCount);
String typeString = TypeReconstructionUtil.getTypeNameAndStarProjectionsString(getElement().getText(), argumentCount);
JetTypeElement replacement = JetPsiFactoryKt.JetPsiFactory(file).createType(typeString).getTypeElement();
assert replacement != null : "No type element after parsing " + typeString;
element.replace(replacement);
getElement().replace(replacement);
}
@Override
@@ -107,11 +107,11 @@ public abstract class AddStarProjectionsFix extends JetIntentionAction<JetUserTy
}
private boolean isZeroTypeArguments() {
return element.getTypeArguments().isEmpty();
return getElement().getTypeArguments().isEmpty();
}
private boolean isInsideJavaClassCall() {
PsiElement parent = element.getParent().getParent().getParent().getParent();
PsiElement parent = getElement().getParent().getParent().getParent().getParent();
if (parent instanceof JetCallExpression) {
JetExpression calleeExpression = ((JetCallExpression) parent).getCalleeExpression();
if (calleeExpression instanceof JetSimpleNameExpression) {
@@ -52,19 +52,19 @@ public class AddWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && element.getCloseBrace() != null;
return super.isAvailable(project, editor, file) && getElement().getCloseBrace() != null;
}
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
PsiElement whenCloseBrace = element.getCloseBrace();
PsiElement whenCloseBrace = getElement().getCloseBrace();
assert (whenCloseBrace != null) : "isAvailable should check if close brace exist";
JetPsiFactory psiFactory = JetPsiFactoryKt.JetPsiFactory(file);
JetWhenEntry entry = psiFactory.createWhenEntry(ELSE_ENTRY_TEXT);
PsiElement insertedBranch = element.addBefore(entry, whenCloseBrace);
element.addAfter(psiFactory.createNewLine(), insertedBranch);
PsiElement insertedBranch = getElement().addBefore(entry, whenCloseBrace);
getElement().addAfter(psiFactory.createNewLine(), insertedBranch);
JetWhenEntry insertedWhenEntry = (JetWhenEntry) CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(insertedBranch);
TextRange textRange = insertedWhenEntry.getTextRange();
@@ -84,7 +84,7 @@ public class AutoImportFix(element: JetSimpleNameExpression) : JetHintAction<Jet
override fun getFamilyName() = JetBundle.message("import.fix")
override fun isAvailable(project: Project, editor: Editor, file: PsiFile)
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile)
= (super.isAvailable(project, editor, file)) && (anySuggestionFound ?: !suggestions.isEmpty())
override fun invoke(project: Project, editor: Editor?, file: JetFile) {
@@ -49,7 +49,7 @@ public class CastExpressionFix extends JetIntentionAction<JetExpression> {
public String getText() {
return JetBundle.message(
"cast.expression.to.type",
element.getText(),
getElement().getText(),
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
);
}
@@ -63,7 +63,7 @@ public class CastExpressionFix extends JetIntentionAction<JetExpression> {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!super.isAvailable(project, editor, file)) return false;
JetType expressionType = ResolutionUtils.analyze(element).getType(element);
JetType expressionType = ResolutionUtils.analyze(getElement()).getType(getElement());
return expressionType != null
&& (
JetTypeChecker.DEFAULT.isSubtypeOf(type, expressionType) // downcast
@@ -77,13 +77,13 @@ public class CastExpressionFix extends JetIntentionAction<JetExpression> {
JetPsiFactory psiFactory = JetPsiFactoryKt.JetPsiFactory(file);
JetBinaryExpressionWithTypeRHS castExpression =
(JetBinaryExpressionWithTypeRHS) psiFactory.createExpression("(" + element.getText() + ") as " + renderedType);
(JetBinaryExpressionWithTypeRHS) psiFactory.createExpression("(" + getElement().getText() + ") as " + renderedType);
if (JetPsiUtil.areParenthesesUseless((JetParenthesizedExpression) castExpression.getLeft())) {
castExpression = (JetBinaryExpressionWithTypeRHS) psiFactory.createExpression(element.getText() + " as " + renderedType);
castExpression = (JetBinaryExpressionWithTypeRHS) psiFactory.createExpression(getElement().getText() + " as " + renderedType);
}
JetParenthesizedExpression castExpressionInParentheses =
(JetParenthesizedExpression) element.replace(psiFactory.createExpression("(" + castExpression.getText() + ")"));
(JetParenthesizedExpression) getElement().replace(psiFactory.createExpression("(" + castExpression.getText() + ")"));
if (JetPsiUtil.areParenthesesUseless(castExpressionInParentheses)) {
castExpression = (JetBinaryExpressionWithTypeRHS) castExpressionInParentheses.replace(castExpression);
@@ -39,7 +39,7 @@ public class ChangeAccessorTypeFix extends JetIntentionAction<JetPropertyAccesso
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
JetProperty property = PsiTreeUtil.getParentOfType(element, JetProperty.class);
JetProperty property = PsiTreeUtil.getParentOfType(getElement(), JetProperty.class);
if (property == null) return false;
JetType type = QuickFixUtil.getDeclarationReturnType(property);
if (super.isAvailable(project, editor, file) && type != null && !type.isError()) {
@@ -53,7 +53,7 @@ public class ChangeAccessorTypeFix extends JetIntentionAction<JetPropertyAccesso
@Override
public String getText() {
return JetBundle.message(
element.isGetter() ? "change.getter.type" : "change.setter.type",
getElement().isGetter() ? "change.getter.type" : "change.setter.type",
IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
);
}
@@ -70,11 +70,11 @@ public class ChangeAccessorTypeFix extends JetIntentionAction<JetPropertyAccesso
.JetPsiFactory(file).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type));
JetTypeReference typeReference;
if (element.isGetter()) {
typeReference = element.getReturnTypeReference();
if (getElement().isGetter()) {
typeReference = getElement().getReturnTypeReference();
}
else {
JetParameter parameter = element.getParameter();
JetParameter parameter = getElement().getParameter();
assert parameter != null;
typeReference = parameter.getTypeReference();
}
@@ -79,11 +79,11 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
return changeFunctionLiteralReturnTypeFix.getText();
}
String functionName = element.getName();
FqName fqName = element.getFqName();
String functionName = getElement().getName();
FqName fqName = getElement().getFqName();
if (fqName != null) functionName = fqName.asString();
if (KotlinBuiltIns.isUnit(type) && element.hasBlockBody()) {
if (KotlinBuiltIns.isUnit(type) && getElement().hasBlockBody()) {
return functionName == null ?
JetBundle.message("remove.no.name.function.return.type") :
JetBundle.message("remove.function.return.type", functionName);
@@ -111,15 +111,14 @@ public class ChangeFunctionReturnTypeFix extends JetIntentionAction<JetFunction>
changeFunctionLiteralReturnTypeFix.invoke(project, editor, file);
}
else {
if (!(KotlinBuiltIns.isUnit(type) && element.hasBlockBody())) {
JetTypeReference newTypeRef = JetPsiFactoryKt
.JetPsiFactory(project).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type));
newTypeRef = element.setTypeReference(newTypeRef);
if (!(KotlinBuiltIns.isUnit(type) && getElement().hasBlockBody())) {
JetTypeReference newTypeRef = JetPsiFactoryKt.JetPsiFactory(project).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type));
newTypeRef = getElement().setTypeReference(newTypeRef);
assert newTypeRef != null;
ShortenReferences.DEFAULT.process(newTypeRef);
}
else {
element.setTypeReference(null);
getElement().setTypeReference(null);
}
}
}
@@ -125,7 +125,7 @@ public class ChangeMemberFunctionSignatureFix extends JetHintAction<JetNamedFunc
CommandProcessor.getInstance().runUndoTransparentAction(new Runnable() {
@Override
public void run() {
new MyAction(project, editor, element, possibleSignatures).execute();
new MyAction(project, editor, getElement(), possibleSignatures).execute();
}
});
}
@@ -53,8 +53,8 @@ public class ChangeParameterTypeFix extends JetIntentionAction<JetParameter> {
public String getText() {
String renderedType = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type);
return isPrimaryConstructorParameter ?
JetBundle.message("change.primary.constructor.parameter.type", element.getName(), containingDeclarationName, renderedType) :
JetBundle.message("change.function.parameter.type", element.getName(), containingDeclarationName, renderedType);
JetBundle.message("change.primary.constructor.parameter.type", getElement().getName(), containingDeclarationName, renderedType) :
JetBundle.message("change.function.parameter.type", getElement().getName(), containingDeclarationName, renderedType);
}
@NotNull
@@ -66,7 +66,7 @@ public class ChangeParameterTypeFix extends JetIntentionAction<JetParameter> {
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetTypeReference newTypeRef = JetPsiFactoryKt.JetPsiFactory(file).createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type));
newTypeRef = element.setTypeReference(newTypeRef);
newTypeRef = getElement().setTypeReference(newTypeRef);
assert newTypeRef != null;
ShortenReferences.DEFAULT.process(newTypeRef);
}
@@ -46,8 +46,8 @@ public class ChangeToFunctionInvocationFix extends JetIntentionAction<JetExpress
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetExpression reference = (JetExpression) element.copy();
element.replace(JetPsiFactoryKt.JetPsiFactory(file).createExpression(reference.getText() + "()"));
JetExpression reference = (JetExpression) getElement().copy();
getElement().replace(JetPsiFactoryKt.JetPsiFactory(file).createExpression(reference.getText() + "()"));
}
public static JetSingleIntentionActionFactory createFactory() {
@@ -33,7 +33,7 @@ public class ChangeToPropertyNameFix extends JetIntentionAction<JetSimpleNameExp
private String getBackingFieldName() {
return element.getText();
return getElement().getText();
}
private String getPropertyName() {
@@ -54,9 +54,8 @@ public class ChangeToPropertyNameFix extends JetIntentionAction<JetSimpleNameExp
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetSimpleNameExpression propertyName = (JetSimpleNameExpression) JetPsiFactoryKt
.JetPsiFactory(file).createExpression(getPropertyName());
element.replace(propertyName);
JetSimpleNameExpression propertyName = (JetSimpleNameExpression) JetPsiFactoryKt.JetPsiFactory(file).createExpression(getPropertyName());
getElement().replace(propertyName);
}
public static JetSingleIntentionActionFactory createFactory() {
@@ -35,7 +35,7 @@ public class ChangeToStarProjectionFix extends JetIntentionAction<JetTypeElement
@NotNull
@Override
public String getText() {
String stars = TypeReconstructionUtil.getTypeNameAndStarProjectionsString("", element.getTypeArgumentsAsTypes().size());
String stars = TypeReconstructionUtil.getTypeNameAndStarProjectionsString("", getElement().getTypeArgumentsAsTypes().size());
return JetBundle.message("change.to.star.projection", stars);
}
@@ -47,7 +47,7 @@ public class ChangeToStarProjectionFix extends JetIntentionAction<JetTypeElement
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
for (JetTypeReference typeReference : element.getTypeArgumentsAsTypes()) {
for (JetTypeReference typeReference : getElement().getTypeArgumentsAsTypes()) {
if (typeReference != null) {
typeReference.replace(JetPsiFactoryKt.JetPsiFactory(file).createStar());
}
@@ -48,7 +48,7 @@ public class ChangeTypeFix extends JetIntentionAction<JetTypeReference> {
@NotNull
@Override
public String getText() {
String currentTypeText = element.getText();
String currentTypeText = getElement().getText();
return JetBundle.message("change.type", currentTypeText, QuickFixUtil.renderTypeWithFqNameOnClash(type, currentTypeText));
}
@@ -60,7 +60,7 @@ public class ChangeTypeFix extends JetIntentionAction<JetTypeReference> {
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetTypeReference newTypeRef = (JetTypeReference) element.replace(JetPsiFactoryKt.JetPsiFactory(file).createType(renderedType));
JetTypeReference newTypeRef = (JetTypeReference) getElement().replace(JetPsiFactoryKt.JetPsiFactory(file).createType(renderedType));
ShortenReferences.DEFAULT.process(newTypeRef);
}
@@ -60,8 +60,8 @@ public class ChangeVariableTypeFix extends JetIntentionAction<JetVariableDeclara
@NotNull
@Override
public String getText() {
String propertyName = element.getName();
FqName fqName = element.getFqName();
String propertyName = getElement().getName();
FqName fqName = getElement().getFqName();
if (fqName != null) propertyName = fqName.asString();
return JetBundle.message("change.element.type", propertyName, IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type));
@@ -82,21 +82,21 @@ public class ChangeVariableTypeFix extends JetIntentionAction<JetVariableDeclara
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetPsiFactory psiFactory = JetPsiFactoryKt.JetPsiFactory(file);
PsiElement nameIdentifier = element.getNameIdentifier();
PsiElement nameIdentifier = getElement().getNameIdentifier();
assert nameIdentifier != null : "ChangeVariableTypeFix applied to variable without name";
JetTypeReference replacingTypeReference = psiFactory.createType(IdeDescriptorRenderers.SOURCE_CODE.renderType(type));
ArrayList<JetTypeReference> toShorten = new ArrayList<JetTypeReference>();
toShorten.add(element.setTypeReference(replacingTypeReference));
toShorten.add(getElement().setTypeReference(replacingTypeReference));
if (element instanceof JetProperty) {
JetPropertyAccessor getter = ((JetProperty) element).getGetter();
if (getElement() instanceof JetProperty) {
JetPropertyAccessor getter = ((JetProperty) getElement()).getGetter();
JetTypeReference getterReturnTypeRef = getter == null ? null : getter.getReturnTypeReference();
if (getterReturnTypeRef != null) {
toShorten.add((JetTypeReference) getterReturnTypeRef.replace(replacingTypeReference));
}
JetPropertyAccessor setter = ((JetProperty) element).getSetter();
JetPropertyAccessor setter = ((JetProperty) getElement()).getSetter();
JetParameter setterParameter = setter == null ? null : setter.getParameter();
JetTypeReference setterParameterTypeRef = setterParameter == null ? null : setterParameter.getTypeReference();
if (setterParameterTypeRef != null) {
@@ -65,18 +65,18 @@ public class ChangeVisibilityModifierFix extends JetIntentionAction<JetDeclarati
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetModifierKeywordToken modifier = findVisibilityChangeTo(file);
assert modifier != null;
PsiModificationUtilsKt.setVisibility(element, modifier);
PsiModificationUtilsKt.setVisibility(getElement(), modifier);
}
@Nullable
private JetModifierKeywordToken findVisibilityChangeTo(JetFile file) {
BindingContext bindingContext = ResolutionUtils.analyze(element);
BindingContext bindingContext = ResolutionUtils.analyze(getElement());
DeclarationDescriptor descriptor;
if (element instanceof JetParameter) {
descriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, element);
if (getElement() instanceof JetParameter) {
descriptor = bindingContext.get(BindingContext.PRIMARY_CONSTRUCTOR_PARAMETER, getElement());
}
else {
descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, element);
descriptor = bindingContext.get(BindingContext.DECLARATION_TO_DESCRIPTOR, getElement());
}
if (!(descriptor instanceof CallableMemberDescriptor)) return null;
@@ -32,7 +32,7 @@ public abstract class JetHintAction<T extends PsiElement> extends JetIntentionAc
}
protected boolean isCaretNearRef(Editor editor, T ref) {
TextRange range = element.getTextRange();
TextRange range = getElement().getTextRange();
int offset = editor.getCaretModel().getOffset();
return offset == range.getEndOffset();
@@ -1,60 +0,0 @@
/*
* 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.codeInsight.FileModificationService;
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.PsiFile;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.psi.JetCodeFragment;
import org.jetbrains.kotlin.psi.JetFile;
public abstract class JetIntentionAction<T extends PsiElement> implements IntentionAction {
protected @NotNull T element;
public JetIntentionAction(@NotNull T element) {
this.element = element;
}
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiFile file) {
return element.isValid() && (file.getManager().isInProject(file) || file instanceof JetCodeFragment) && (file instanceof JetFile);
}
//Don't override this method. Use the method with JetFile instead.
@Deprecated
@Override
public void invoke(@NotNull Project project, @Nullable Editor editor, @NotNull PsiFile file) throws IncorrectOperationException {
if (file instanceof JetFile) {
if (FileModificationService.getInstance().prepareFileForWrite(element.getContainingFile())) {
invoke(project, editor, (JetFile) file);
}
}
}
protected abstract void invoke(@NotNull Project project, @Nullable Editor editor, @NotNull JetFile file) throws IncorrectOperationException;
@Override
public boolean startInWriteAction() {
return true;
}
}
@@ -0,0 +1,43 @@
/*
* 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.codeInsight.FileModificationService
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.PsiFile
import org.jetbrains.kotlin.psi.JetCodeFragment
import org.jetbrains.kotlin.psi.JetFile
abstract class JetIntentionAction<T : PsiElement>(protected val element: T) : IntentionAction {
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
return element.isValid && (file.manager.isInProject(file) || file is JetCodeFragment) && (file is JetFile)
}
override final fun invoke(project: Project, editor: Editor?, file: PsiFile) {
if (file is JetFile && FileModificationService.getInstance().prepareFileForWrite(element.containingFile)) {
invoke(project, editor, file)
}
}
protected abstract fun invoke(project: Project, editor: Editor?, file: JetFile)
override fun startInWriteAction() = true
}
@@ -40,7 +40,7 @@ public class MakeOverriddenMemberOpenFix(declaration: JetDeclaration) : JetInten
private val overriddenNonOverridableMembers = ArrayList<JetCallableDeclaration>()
private val containingDeclarationsNames = ArrayList<String>()
override fun isAvailable(project: Project, editor: Editor, file: PsiFile): Boolean {
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean {
if (!super.isAvailable(project, editor, file) || file !is JetFile) {
return false
}
@@ -51,14 +51,14 @@ public class MoveWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
if (!super.isAvailable(project, editor, file)) {
return false;
}
return JetPsiUtil.checkWhenExpressionHasSingleElse(element);
return JetPsiUtil.checkWhenExpressionHasSingleElse(getElement());
}
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetWhenEntry elseEntry = null;
JetWhenEntry lastEntry = null;
for (JetWhenEntry entry : element.getEntries()) {
for (JetWhenEntry entry : getElement().getEntries()) {
if (entry.isElse()) {
elseEntry = entry;
}
@@ -67,9 +67,9 @@ public class MoveWhenElseBranchFix extends JetIntentionAction<JetWhenExpression>
assert (elseEntry != null) : "isAvailable should check whether there is only one else branch";
int cursorOffset = editor.getCaretModel().getOffset() - elseEntry.getTextOffset();
PsiElement insertedBranch = element.addAfter(elseEntry, lastEntry);
element.addAfter(JetPsiFactoryKt.JetPsiFactory(file).createNewLine(), lastEntry);
element.deleteChildRange(elseEntry, elseEntry);
PsiElement insertedBranch = getElement().addAfter(elseEntry, lastEntry);
getElement().addAfter(JetPsiFactoryKt.JetPsiFactory(file).createNewLine(), lastEntry);
getElement().deleteChildRange(elseEntry, elseEntry);
JetWhenEntry insertedWhenEntry = (JetWhenEntry) CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(insertedBranch);
editor.getCaretModel().moveToOffset(insertedWhenEntry.getTextOffset() + cursorOffset);
@@ -53,12 +53,12 @@ public class RemoveFunctionBodyFix extends JetIntentionAction<JetFunction> {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
return super.isAvailable(project, editor, file) && element.hasBody();
return super.isAvailable(project, editor, file) && getElement().hasBody();
}
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetFunction function = (JetFunction) element.copy();
JetFunction function = (JetFunction) getElement().copy();
assert function instanceof ASTDelegatePsiElement;
ASTDelegatePsiElement functionElementWithAst = (ASTDelegatePsiElement) function;
JetExpression bodyExpression = function.getBodyExpression();
@@ -76,7 +76,7 @@ public class RemoveFunctionBodyFix extends JetIntentionAction<JetFunction> {
removePossiblyEquationSign(functionElementWithAst, prevPrevElement);
functionElementWithAst.deleteChildInternal(bodyExpression.getNode());
}
element.replace(function);
getElement().replace(function);
}
private static boolean removePossiblyEquationSign(@NotNull ASTDelegatePsiElement element, @Nullable PsiElement possiblyEq) {
@@ -59,9 +59,9 @@ public class RemoveNullableFix extends JetIntentionAction<JetNullableType> {
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetTypeElement type = super.element.getInnerType();
assert type != null : "No inner type " + element.getText() + ", should have been rejected in createFactory()";
super.element.replace(type);
JetTypeElement type = super.getElement().getInnerType();
assert type != null : "No inner type " + getElement().getText() + ", should have been rejected in createFactory()";
super.getElement().replace(type);
}
public static JetSingleIntentionActionFactory createFactory(final NullableKind typeOfError) {
@@ -85,14 +85,14 @@ public class RemovePartsFromPropertyFix extends JetIntentionAction<JetProperty>
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
JetType type = QuickFixUtil.getDeclarationReturnType(element);
JetType type = QuickFixUtil.getDeclarationReturnType(getElement());
return super.isAvailable(project, editor, file) && type != null && !type.isError();
}
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
JetType type = QuickFixUtil.getDeclarationReturnType(element);
JetProperty newElement = (JetProperty) element.copy();
JetType type = QuickFixUtil.getDeclarationReturnType(getElement());
JetProperty newElement = (JetProperty) getElement().copy();
JetPropertyAccessor getter = newElement.getGetter();
if (removeGetter && getter != null) {
newElement.deleteChildInternal(getter.getNode());
@@ -114,9 +114,9 @@ public class RemovePartsFromPropertyFix extends JetIntentionAction<JetProperty>
typeToAdd = type;
}
}
element = (JetProperty) element.replace(newElement);
newElement = (JetProperty) getElement().replace(newElement);
if (typeToAdd != null) {
SpecifyTypeExplicitlyIntention.Companion.addTypeAnnotation(editor, element, typeToAdd);
SpecifyTypeExplicitlyIntention.Companion.addTypeAnnotation(editor, newElement, typeToAdd);
}
}
@@ -21,7 +21,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.diagnostics.Diagnostic;
import org.jetbrains.kotlin.idea.JetBundle;
import org.jetbrains.kotlin.idea.core.quickfix.QuickFixUtil;
@@ -55,12 +54,12 @@ public class RemoveRightPartOfBinaryExpressionFix<T extends JetExpression> exten
public JetExpression invoke() throws IncorrectOperationException {
JetExpression newExpression = null;
if (element instanceof JetBinaryExpression) {
if (getElement() instanceof JetBinaryExpression) {
//noinspection ConstantConditions
newExpression = (JetExpression) element.replace(((JetBinaryExpression) element.copy()).getLeft());
newExpression = (JetExpression) getElement().replace(((JetBinaryExpression) getElement().copy()).getLeft());
}
else if (element instanceof JetBinaryExpressionWithTypeRHS) {
newExpression = (JetExpression) element.replace(((JetBinaryExpressionWithTypeRHS) element.copy()).getLeft());
else if (getElement() instanceof JetBinaryExpressionWithTypeRHS) {
newExpression = (JetExpression) getElement().replace(((JetBinaryExpressionWithTypeRHS) getElement().copy()).getLeft());
}
PsiElement parent = newExpression != null ? newExpression.getParent() : null;
@@ -53,7 +53,7 @@ public class RemoveValVarFromParameterFix extends JetIntentionAction<JetParamete
@Override
protected void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
PsiElement keyword = element.getValOrVarKeyword();
PsiElement keyword = getElement().getValOrVarKeyword();
if (keyword == null) return;
keyword.delete();
}
@@ -39,12 +39,12 @@ public abstract class ReplaceOperationInBinaryExpressionFix<T extends JetExpress
@Override
public void invoke(@NotNull Project project, Editor editor, JetFile file) throws IncorrectOperationException {
if (element instanceof JetBinaryExpressionWithTypeRHS) {
JetExpression left = ((JetBinaryExpressionWithTypeRHS) element).getLeft();
JetTypeReference right = ((JetBinaryExpressionWithTypeRHS) element).getRight();
if (getElement() instanceof JetBinaryExpressionWithTypeRHS) {
JetExpression left = ((JetBinaryExpressionWithTypeRHS) getElement()).getLeft();
JetTypeReference right = ((JetBinaryExpressionWithTypeRHS) getElement()).getRight();
if (right != null) {
JetExpression expression = JetPsiFactoryKt.JetPsiFactory(file).createExpression(left.getText() + operation + right.getText());
element.replace(expression);
getElement().replace(expression);
}
}
}