-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Qickfixes for add/remove typeTags for fields and methods
- Loading branch information
Showing
9 changed files
with
356 additions
and
1 deletion.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/main/java/com/intellij/plugins/haxe/ide/intention/AddReturnTypeTagIntention.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.intellij.plugins.haxe.ide.intention; | ||
|
||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.plugins.haxe.HaxeBundle; | ||
import com.intellij.plugins.haxe.HaxeLanguage; | ||
import com.intellij.plugins.haxe.lang.psi.*; | ||
import com.intellij.plugins.haxe.model.type.HaxeGenericResolver; | ||
import com.intellij.plugins.haxe.model.type.ResultHolder; | ||
import com.intellij.plugins.haxe.model.type.SpecificTypeReference; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.PsiParameterList; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static com.intellij.plugins.haxe.util.HaxeElementGenerator.createTypeTag; | ||
import static com.intellij.plugins.haxe.util.UsefulPsiTreeUtil.findParentOfTypeButStopIfTypeIs; | ||
|
||
public class AddReturnTypeTagIntention extends BaseIntentionAction { | ||
|
||
private HaxeMethod myMethod; | ||
private ResultHolder returnType; | ||
|
||
public AddReturnTypeTagIntention() { | ||
} | ||
|
||
@Nls | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return HaxeBundle.message("quick.fixes.family"); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getText() { | ||
return HaxeBundle.message("haxe.quickfix.add.return.type"); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (file.getLanguage() != HaxeLanguage.INSTANCE) return false; | ||
|
||
attemptToFindMethod(editor, file); | ||
|
||
boolean isMissingTypeTag = myMethod != null | ||
&& !myMethod.isConstructor() | ||
&& myMethod instanceof HaxeMethodDeclaration declaration | ||
&& declaration.getTypeTag() == null; | ||
if (isMissingTypeTag) { | ||
HaxeGenericResolver resolver = myMethod.getModel().getGenericResolver(null); | ||
resolver = resolver.withTypeParametersAsType(myMethod.getModel().getGenericParams()); | ||
returnType = myMethod.getModel().getReturnType(resolver); | ||
return !(returnType == null); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
@Override | ||
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
|
||
String typeText = returnType.isUnknown() ? SpecificTypeReference.VOID : returnType.getType().toPresentationString(); | ||
HaxeTypeTag tag = createTypeTag(project, typeText); | ||
PsiParameterList list = myMethod.getParameterList(); | ||
PsiElement element = PsiTreeUtil.nextVisibleLeaf(list); | ||
myMethod.addAfter(tag, element); | ||
|
||
} | ||
|
||
|
||
private void attemptToFindMethod(Editor editor, PsiFile file) { | ||
PsiElement place = file.findElementAt(editor.getCaretModel().getOffset()); | ||
if (place instanceof HaxeMethod method) { | ||
myMethod = method; | ||
}else { | ||
myMethod = findParentOfTypeButStopIfTypeIs(place, HaxeMethod.class, HaxeBlockStatement.class); | ||
} | ||
} | ||
|
||
|
||
|
||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/intellij/plugins/haxe/ide/intention/AddTypeTagToFieldIntention.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.intellij.plugins.haxe.ide.intention; | ||
|
||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.plugins.haxe.HaxeBundle; | ||
import com.intellij.plugins.haxe.HaxeLanguage; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeLocalVarDeclaration; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeLocalVarDeclarationList; | ||
import com.intellij.plugins.haxe.lang.psi.HaxePsiField; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeTypeTag; | ||
import com.intellij.plugins.haxe.model.type.HaxeExpressionEvaluator; | ||
import com.intellij.plugins.haxe.model.type.HaxeExpressionEvaluatorContext; | ||
import com.intellij.plugins.haxe.model.type.ResultHolder; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
import static com.intellij.plugins.haxe.util.HaxeElementGenerator.createTypeTag; | ||
|
||
public class AddTypeTagToFieldIntention extends BaseIntentionAction { | ||
|
||
private HaxePsiField myField; | ||
private ResultHolder type; | ||
|
||
public AddTypeTagToFieldIntention() { | ||
} | ||
|
||
@Nls | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return HaxeBundle.message("quick.fixes.family"); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public String getText() { | ||
return HaxeBundle.message("haxe.quickfix.add.type.tag"); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (file.getLanguage() != HaxeLanguage.INSTANCE) return false; | ||
|
||
attemptToFindField(editor, file); | ||
|
||
boolean isMissingTypeTag = myField != null && myField.getTypeTag() == null; | ||
if (isMissingTypeTag) { | ||
type = HaxeExpressionEvaluator.evaluate(myField, new HaxeExpressionEvaluatorContext(myField), null).result; | ||
return !(type == null || type.isUnknown()); | ||
} | ||
return false; | ||
} | ||
|
||
|
||
@Override | ||
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
|
||
HaxeTypeTag tag = createTypeTag(project, type.getType().toString()); | ||
myField.addAfter(tag, myField.getComponentName()); | ||
|
||
} | ||
|
||
|
||
private void attemptToFindField(Editor editor, PsiFile file) { | ||
PsiElement place = file.findElementAt(editor.getCaretModel().getOffset()); | ||
HaxeLocalVarDeclarationList varDeclarationList = PsiTreeUtil.getParentOfType(place, HaxeLocalVarDeclarationList.class); | ||
if (varDeclarationList != null) { | ||
List<HaxeLocalVarDeclaration> list = varDeclarationList.getLocalVarDeclarationList(); | ||
if (!list.isEmpty())myField = list.get(list.size() - 1); | ||
} else if (place instanceof HaxePsiField psiField) { | ||
myField = psiField; | ||
}else { | ||
myField = PsiTreeUtil.getParentOfType(place, HaxePsiField.class); | ||
} | ||
} | ||
|
||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/intellij/plugins/haxe/ide/intention/RemoveReturnTypeTagIntention.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.intellij.plugins.haxe.ide.intention; | ||
|
||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.plugins.haxe.HaxeBundle; | ||
import com.intellij.plugins.haxe.HaxeLanguage; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeBlockStatement; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeMethod; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeMethodDeclaration; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import static com.intellij.plugins.haxe.util.UsefulPsiTreeUtil.findParentOfTypeButStopIfTypeIs; | ||
|
||
public class RemoveReturnTypeTagIntention extends BaseIntentionAction { | ||
|
||
private HaxeMethod myMethod; | ||
|
||
public RemoveReturnTypeTagIntention() { | ||
} | ||
|
||
@Nls | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return HaxeBundle.message("quick.fixes.family"); | ||
} | ||
@NotNull | ||
@Override | ||
public String getText() { | ||
return HaxeBundle.message("haxe.quickfix.remove.return.type"); | ||
} | ||
|
||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (file.getLanguage() != HaxeLanguage.INSTANCE) return false; | ||
|
||
attemptToFindMethod(editor, file); | ||
|
||
return myMethod instanceof HaxeMethodDeclaration declaration && declaration.getTypeTag() != null; | ||
} | ||
|
||
|
||
@Override | ||
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
|
||
HaxeMethodDeclaration declaration = (HaxeMethodDeclaration)myMethod; | ||
declaration.getTypeTag().delete(); | ||
} | ||
|
||
|
||
private void attemptToFindMethod(Editor editor, PsiFile file) { | ||
PsiElement place = file.findElementAt(editor.getCaretModel().getOffset()); | ||
if (place instanceof HaxeMethod method) { | ||
myMethod = method; | ||
} | ||
else { | ||
myMethod = findParentOfTypeButStopIfTypeIs(place, HaxeMethod.class, HaxeBlockStatement.class); | ||
} | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/intellij/plugins/haxe/ide/intention/RemoveTypeTagFromFieldIntention.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.intellij.plugins.haxe.ide.intention; | ||
|
||
import com.intellij.codeInsight.intention.impl.BaseIntentionAction; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.plugins.haxe.HaxeBundle; | ||
import com.intellij.plugins.haxe.HaxeLanguage; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeLocalVarDeclaration; | ||
import com.intellij.plugins.haxe.lang.psi.HaxeLocalVarDeclarationList; | ||
import com.intellij.plugins.haxe.lang.psi.HaxePsiField; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import com.intellij.psi.util.PsiTreeUtil; | ||
import com.intellij.util.IncorrectOperationException; | ||
import org.jetbrains.annotations.Nls; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.List; | ||
|
||
public class RemoveTypeTagFromFieldIntention extends BaseIntentionAction { | ||
|
||
private HaxePsiField myField; | ||
|
||
|
||
@Nls | ||
@NotNull | ||
@Override | ||
public String getFamilyName() { | ||
return HaxeBundle.message("quick.fixes.family"); | ||
} | ||
@NotNull | ||
@Override | ||
public String getText() { | ||
return HaxeBundle.message("haxe.quickfix.remove.type.tag"); | ||
} | ||
|
||
@Override | ||
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { | ||
if (file.getLanguage() != HaxeLanguage.INSTANCE) return false; | ||
|
||
attemptToFindField(editor, file); | ||
|
||
return myField != null && myField.getTypeTag() != null; | ||
} | ||
|
||
|
||
@Override | ||
public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { | ||
myField.getTypeTag().delete(); | ||
} | ||
|
||
|
||
private void attemptToFindField(Editor editor, PsiFile file) { | ||
PsiElement place = file.findElementAt(editor.getCaretModel().getOffset()); | ||
HaxeLocalVarDeclarationList varDeclarationList = PsiTreeUtil.getParentOfType(place, HaxeLocalVarDeclarationList.class); | ||
if (varDeclarationList != null) { | ||
List<HaxeLocalVarDeclaration> list = varDeclarationList.getLocalVarDeclarationList(); | ||
if (!list.isEmpty())myField = list.get(list.size() - 1); | ||
} else if (place instanceof HaxePsiField psiField) { | ||
myField = psiField; | ||
}else { | ||
myField = PsiTreeUtil.getParentOfType(place, HaxePsiField.class); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters