mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
iter live template should be applicable before a type cast
This commit is contained in:
@@ -94,10 +94,10 @@ public abstract class JavaCodeContextType extends TemplateContextType {
|
||||
}
|
||||
|
||||
private static boolean isStatementContext(PsiElement element) {
|
||||
if (!Expression.isExpressionContext(element)) {
|
||||
if (isAfterExpression(element)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PsiStatement statement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
|
||||
return statement != null && statement.getTextRange().getStartOffset() == element.getTextRange().getStartOffset();
|
||||
}
|
||||
@@ -132,17 +132,22 @@ public abstract class JavaCodeContextType extends TemplateContextType {
|
||||
return false;
|
||||
}
|
||||
|
||||
ProcessingContext context = new ProcessingContext();
|
||||
if (psiElement().inside(PsiExpression.class).afterLeaf(psiElement().inside(psiElement(PsiExpression.class).save("prevExpr"))).accepts(element, context)) {
|
||||
PsiExpression prevExpr = (PsiExpression)context.get("prevExpr");
|
||||
if (prevExpr.getTextRange().getEndOffset() <= element.getTextRange().getStartOffset()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return !isAfterExpression(element);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAfterExpression(PsiElement element) {
|
||||
ProcessingContext context = new ProcessingContext();
|
||||
if (psiElement().inside(PsiExpression.class).afterLeaf(psiElement().inside(psiElement(PsiExpression.class).save("prevExpr"))).accepts(element, context)) {
|
||||
PsiExpression prevExpr = (PsiExpression)context.get("prevExpr");
|
||||
if (prevExpr.getTextRange().getEndOffset() <= element.getTextRange().getStartOffset()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static class Declaration extends JavaCodeContextType {
|
||||
public Declaration() {
|
||||
super("JAVA_DECLARATION", "Declaration", Generic.class);
|
||||
|
||||
+23
-13
@@ -17,6 +17,7 @@ package org.jetbrains.plugins.groovy.template;
|
||||
|
||||
import com.intellij.codeInsight.template.EverywhereContextType;
|
||||
import com.intellij.codeInsight.template.TemplateContextType;
|
||||
import com.intellij.openapi.util.Condition;
|
||||
import com.intellij.patterns.PlatformPatterns;
|
||||
import com.intellij.psi.PsiComment;
|
||||
import com.intellij.psi.PsiElement;
|
||||
@@ -80,12 +81,16 @@ public abstract class GroovyTemplateContextType extends TemplateContextType {
|
||||
|
||||
@Override
|
||||
protected boolean isInContext(@NotNull PsiElement element) {
|
||||
return isStatementContext(element);
|
||||
PsiElement stmt = PsiTreeUtil.findFirstParent(element, new Condition<PsiElement>() {
|
||||
@Override
|
||||
public boolean value(PsiElement element11) {
|
||||
return PsiUtil.isExpressionStatement(element11.getParent());
|
||||
}
|
||||
});
|
||||
|
||||
return !isAfterExpression(element) && stmt != null && stmt.getTextRange().getStartOffset() == element.getTextRange().getStartOffset();
|
||||
}
|
||||
|
||||
private static boolean isStatementContext(PsiElement element) {
|
||||
return Expression.isExpressionContext(element) && PsiUtil.isExpressionStatement(element.getParent());
|
||||
}
|
||||
}
|
||||
public static class Expression extends GroovyTemplateContextType {
|
||||
|
||||
@@ -109,17 +114,22 @@ public abstract class GroovyTemplateContextType extends TemplateContextType {
|
||||
if (parent.getParent() instanceof GrCall) {
|
||||
return false;
|
||||
}
|
||||
ProcessingContext context = new ProcessingContext();
|
||||
if (PlatformPatterns.psiElement().inside(GrExpression.class).afterLeaf(
|
||||
PlatformPatterns.psiElement().inside(PlatformPatterns.psiElement(GrExpression.class).save("prevExpr"))).accepts(element, context)) {
|
||||
PsiElement prevExpr = (PsiElement)context.get("prevExpr");
|
||||
if (prevExpr.getTextRange().getEndOffset() <= element.getTextRange().getStartOffset()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return !isAfterExpression(element);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAfterExpression(PsiElement element) {
|
||||
ProcessingContext context = new ProcessingContext();
|
||||
if (PlatformPatterns.psiElement().afterLeaf(
|
||||
PlatformPatterns.psiElement().inside(PlatformPatterns.psiElement(GrExpression.class).save("prevExpr"))).accepts(element, context)) {
|
||||
PsiElement prevExpr = (PsiElement)context.get("prevExpr");
|
||||
if (prevExpr.getTextRange().getEndOffset() <= element.getTextRange().getStartOffset()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static class Declaration extends GroovyTemplateContextType {
|
||||
public Declaration() {
|
||||
super("GROOVY_DECLARATION", "Declaration", Generic.class);
|
||||
|
||||
@@ -63,6 +63,8 @@ public class GroovyLiveTemplatesTest extends LightCodeInsightFixtureTestCase{
|
||||
assertTrue(isApplicable("<caret>inst", template));
|
||||
assertFalse(isApplicable("class Foo {{ return (<caret>inst) }}", template));
|
||||
assertFalse(isApplicable("class Foo {{ return a <caret>inst) }}", template));
|
||||
assertTrue(isApplicable("<caret>a.b()", template));
|
||||
assertTrue(isApplicable("<caret>a()", template));
|
||||
}
|
||||
|
||||
public void testGroovyExpressionContext() throws Exception {
|
||||
|
||||
Reference in New Issue
Block a user