Python-specific intentions: do not perform isAvailable on non-python files

This commit is contained in:
Ekaterina Tuzova
2013-03-05 19:29:30 +04:00
parent 4598b6c46e
commit e8460dff9d
21 changed files with 79 additions and 2 deletions
@@ -202,6 +202,10 @@ public class ConvertFormatOperatorToMethodIntention extends BaseIntentionAction
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyBinaryExpression binaryExpression =
PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class, false);
if (binaryExpression == null) {
@@ -44,6 +44,10 @@ public class ConvertVariadicParamIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyFunction function =
PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyFunction.class);
if (function != null) {
@@ -128,6 +128,10 @@ public class ImportFromToImportIntention implements IntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
InfoHolder info = InfoHolder.collect(getElementFromEditor(editor, file));
info.myModuleReference = null;
final PsiElement position = file.findElementAt(editor.getCaretModel().getOffset());
@@ -214,6 +214,10 @@ public class ImportToImportFromIntention implements IntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
IntentionState state = new IntentionState(editor, file);
if (state.isAvailable()) {
myText = state.getText();
@@ -103,6 +103,10 @@ public class ImportToggleAliasIntention implements IntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
IntentionState state = IntentionState.fromContext(editor, file);
myLastText = state.getText();
return state.isAvailable();
@@ -40,6 +40,10 @@ public class PyConvertLambdaToFunctionIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyLambdaExpression.class);
if (lambdaExpression != null) {
if (lambdaExpression.getBody() != null) {
@@ -42,6 +42,10 @@ public class PyConvertTripleQuotedStringIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
if (string != null) {
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(string, PyDocStringOwner.class);
@@ -30,6 +30,10 @@ public class PyDemorganIntention extends BaseIntentionAction {
@Override
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyBinaryExpression expression = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class);
if (expression != null) {
PyElementType op = expression.getOperator();
@@ -33,6 +33,10 @@ public class PyDictConstructorToLiteralFormIntention extends BaseIntentionAction
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyCallExpression expression =
PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyCallExpression.class);
@@ -33,6 +33,9 @@ public class PyDictLiteralFormToConstructorIntention extends BaseIntentionAction
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyDictLiteralExpression dictExpression =
PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyDictLiteralExpression.class);
@@ -13,6 +13,7 @@ import com.jetbrains.python.PyTokenTypes;
import com.jetbrains.python.psi.PyBinaryExpression;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyElementType;
import com.jetbrains.python.psi.PyFile;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@@ -42,6 +43,10 @@ public class PyFlipComparisonIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(element, PyBinaryExpression.class, false);
while (binaryExpression != null) {
@@ -13,6 +13,7 @@ import com.jetbrains.python.debugger.PySignature;
import com.jetbrains.python.debugger.PySignatureCacheManager;
import com.jetbrains.python.documentation.PyDocstringGenerator;
import com.jetbrains.python.documentation.doctest.PyDocstringFile;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyUtil;
import org.jetbrains.annotations.NotNull;
@@ -37,7 +38,7 @@ public class PyGenerateDocstringIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (file instanceof PyDocstringFile) return false;
if (!(file instanceof PyFile) || file instanceof PyDocstringFile) return false;
PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
if (elementAt == null) {
return false;
@@ -39,6 +39,10 @@ public class PyJoinIfIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyIfStatement expression =
PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyIfStatement.class);
@@ -49,6 +49,9 @@ public class PyNegateComparisonIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PsiElement element = file.findElementAt(editor.getCaretModel().getOffset());
PyBinaryExpression binaryExpression = PsiTreeUtil.getParentOfType(element, PyBinaryExpression.class, false);
@@ -9,6 +9,7 @@ import com.intellij.util.IncorrectOperationException;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.PyDocStringOwner;
import com.jetbrains.python.psi.PyElementGenerator;
import com.jetbrains.python.psi.PyFile;
import com.jetbrains.python.psi.PyStringLiteralExpression;
import com.jetbrains.python.psi.impl.PyStringLiteralExpressionImpl;
import org.jetbrains.annotations.NotNull;
@@ -25,6 +26,10 @@ public class PyQuotedStringIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyStringLiteralExpression string = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyStringLiteralExpression.class);
if (string != null) {
final PyDocStringOwner docStringOwner = PsiTreeUtil.getParentOfType(string, PyDocStringOwner.class);
@@ -26,6 +26,10 @@ public class PySplitIfIntention extends BaseIntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PsiElement elementAtOffset = file.findElementAt(editor.getCaretModel().getOffset());
if (elementAtOffset == null || elementAtOffset.getNode() == null) {
return false;
@@ -35,6 +35,10 @@ public class PyStringConcatenationToFormatIntention extends BaseIntentionAction
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PsiElement element = PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class, false);
if (element == null) {
@@ -36,6 +36,10 @@ public class PyTransformConditionalExpressionIntention extends BaseIntentionActi
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyAssignmentStatement expression =
PsiTreeUtil.getParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyAssignmentStatement.class);
if (expression != null && expression.getAssignedValue() instanceof PyConditionalExpression) {
@@ -29,6 +29,10 @@ public class ReplaceListComprehensionWithForIntention implements IntentionAction
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PyListCompExpression expression =
PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyListCompExpression.class);
if (expression == null) {
@@ -41,6 +41,10 @@ public class TypeAssertionIntention implements IntentionAction {
}
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (!(file instanceof PyFile)) {
return false;
}
PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());
PyExpression problemElement = PsiTreeUtil.getParentOfType(elementAt, PyReferenceExpression.class);
if (problemElement == null) return false;
@@ -24,7 +24,7 @@ import org.jetbrains.annotations.Nullable;
public abstract class TypeIntention implements IntentionAction {
public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
if (file instanceof PyDocstringFile) return false;
if (!(file instanceof PyFile) || file instanceof PyDocstringFile) return false;
updateText(false);
final PsiElement elementAt = PyUtil.findNonWhitespaceAtOffset(file, editor.getCaretModel().getOffset());