From 123988d646682bf47e72ad6bc3c6ca426331c514 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 24 Apr 2009 22:28:55 +0400 Subject: [PATCH] IDEADEV-35332 --- .../intelliLang/inject/ConcatenationInjector.java | 45 ++++++++++------------ .../plugins/intelliLang/util/AnnotationUtilEx.java | 4 +- .../util/ContextComputationProcessor.java | 39 +++---------------- .../plugins/intelliLang/util/PsiUtilEx.java | 2 +- 4 files changed, 28 insertions(+), 62 deletions(-) diff --git a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/inject/ConcatenationInjector.java b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/inject/ConcatenationInjector.java index dd2c5cd305..e6c04b47fb 100644 --- a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/inject/ConcatenationInjector.java +++ b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/inject/ConcatenationInjector.java @@ -21,7 +21,6 @@ import org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection; import org.intellij.plugins.intelliLang.util.AnnotationUtilEx; import org.intellij.plugins.intelliLang.util.ContextComputationProcessor; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.*; @@ -39,23 +38,16 @@ public class ConcatenationInjector implements ConcatenationAwareInjector { public void getLanguagesToInject(@NotNull final MultiHostRegistrar registrar, @NotNull PsiElement... operands) { final PsiFile containingFile = operands[0].getContainingFile(); - - PsiLiteralExpression literal = null; - for (PsiElement operand : operands) { - if (CustomLanguageInjector.isStringLiteral(operand)) { - literal = (PsiLiteralExpression)operand; - break; - } - } - processLiteralExpressionInjections(literal, new PairProcessor>>() { + processLiteralExpressionInjections(new PairProcessor>>() { public boolean process(final Language language, List> list) { CustomLanguageInjector.registerInjection(language, list, containingFile, registrar); return true; } - }); + }, operands); } - private boolean processAnnotationInjections(@NotNull PsiLiteralExpression firstLiteral, final PsiModifierListOwner annoElement, final PairProcessor>> processor) { + private boolean processAnnotationInjections(final PsiModifierListOwner annoElement, final PairProcessor>> processor, + final PsiElement... operands) { final PsiAnnotation[] annotations = AnnotationUtilEx.getAnnotationFrom(annoElement, myInjectionConfiguration.getLanguageAnnotationPair(), true); if (annotations.length > 0) { @@ -66,25 +58,27 @@ public class ConcatenationInjector implements ConcatenationAwareInjector { if (prefix != null) injection.setPrefix(prefix); if (suffix != null) injection.setSuffix(suffix); if (id != null) injection.setInjectedLanguageId(id); - processInjectionWithContext(firstLiteral, false, injection, processor); + processInjectionWithContext(false, injection, processor, operands); return true; } return false; } - private void processLiteralExpressionInjections(@Nullable final PsiLiteralExpression firstLiteral, final PairProcessor>> processor) { - if (firstLiteral == null) return; - final PsiElement topBlock = PsiUtil.getTopLevelEnclosingCodeBlock(firstLiteral, null); + private void processLiteralExpressionInjections(final PairProcessor>> processor, + final PsiElement... operands) { + if (operands.length == 0) return; + final PsiElement firstOperand = operands[0]; + final PsiElement topBlock = PsiUtil.getTopLevelEnclosingCodeBlock(firstOperand, null); final LocalSearchScope searchScope = new LocalSearchScope(new PsiElement[]{topBlock instanceof PsiCodeBlock - ? topBlock : firstLiteral.getContainingFile()}, "", true); + ? topBlock : firstOperand.getContainingFile()}, "", true); final THashSet visitedVars = new THashSet(); - final LinkedList places = new LinkedList(); - places.add(firstLiteral); + final LinkedList places = new LinkedList(); + places.add(firstOperand); boolean unparsable = false; while (!places.isEmpty()) { - final PsiExpression curPlace = places.removeFirst(); + final PsiElement curPlace = places.removeFirst(); final PsiModifierListOwner owner = AnnotationUtilEx.getAnnotatedElementFor(curPlace, AnnotationUtilEx.LookupType.PREFER_CONTEXT); if (owner == null) continue; - if (processAnnotationInjections(firstLiteral, owner, processor)) return; // annotated element + if (processAnnotationInjections(owner, processor, operands)) return; // annotated element final PsiMethod psiMethod; final Trinity trin; @@ -122,7 +116,7 @@ public class ConcatenationInjector implements ConcatenationAwareInjector { if (injections == null) return; for (MethodParameterInjection injection : injections) { if (injection.isApplicable(psiMethod)) { - processInjectionWithContext(firstLiteral, unparsable, injection, processor); + processInjectionWithContext(unparsable, injection, processor, operands); if (injection.isTerminal()) { return; } @@ -131,14 +125,15 @@ public class ConcatenationInjector implements ConcatenationAwareInjector { } } - private static void processInjectionWithContext(@NotNull final PsiLiteralExpression place, final boolean unparsable, final MethodParameterInjection injection, - final PairProcessor>> processor) { + private static void processInjectionWithContext(final boolean unparsable, final MethodParameterInjection injection, + final PairProcessor>> processor, + final PsiElement... operands) { final Language language = InjectedLanguage.findLanguageById(injection.getInjectedLanguageId()); if (language == null) return; final boolean separateFiles = !injection.isSingleFile() && StringUtil.isNotEmpty(injection.getValuePattern()); final Ref unparsableRef = Ref.create(unparsable); - final List objects = ContextComputationProcessor.collectOperands(place, injection.getPrefix(), injection.getSuffix(), unparsableRef); + final List objects = ContextComputationProcessor.collectOperands(injection.getPrefix(), injection.getSuffix(), unparsableRef, operands); if (objects.isEmpty()) return; final List> result = new ArrayList>(); final int len = objects.size(); diff --git a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/AnnotationUtilEx.java b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/AnnotationUtilEx.java index ad52bbed5f..cde648c449 100644 --- a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/AnnotationUtilEx.java +++ b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/AnnotationUtilEx.java @@ -54,7 +54,7 @@ public class AnnotationUtilEx { * usage context ("expected type"). */ @Nullable - public static PsiModifierListOwner getAnnotatedElementFor(@Nullable PsiExpression element, LookupType type) { + public static PsiModifierListOwner getAnnotatedElementFor(@Nullable PsiElement element, LookupType type) { if (element == null) return null; if (type == LookupType.PREFER_DECLARATION || type == LookupType.DECLRARATION_ONLY) { @@ -118,7 +118,7 @@ public class AnnotationUtilEx { } @Nullable - private static PsiModifierListOwner getAnnotationMethod(PsiNameValuePair pair, PsiExpression element) { + private static PsiModifierListOwner getAnnotationMethod(PsiNameValuePair pair, PsiElement element) { final PsiAnnotation annotation = PsiTreeUtil.getParentOfType(pair.getParent(), PsiAnnotation.class); assert annotation != null; diff --git a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/ContextComputationProcessor.java b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/ContextComputationProcessor.java index 048d4f0f47..304e603531 100644 --- a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/ContextComputationProcessor.java +++ b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/ContextComputationProcessor.java @@ -19,12 +19,9 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.*; -import com.intellij.psi.util.PsiTreeUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** @@ -42,40 +39,14 @@ public class ContextComputationProcessor { myEvaluationHelper = new SubstitutedExpressionEvaluationHelper(project); } - @Nullable - private static PsiElement getContext(@NotNull PsiElement place) { - final PsiConditionalExpression conditionalExpression = PsiTreeUtil.getParentOfType(place, PsiConditionalExpression.class); - if (conditionalExpression != null && PsiTreeUtil.isAncestor(conditionalExpression.getCondition(), place, false)) return null; - - PsiElement element = place; - PsiElement prev = place; - while (true) { - final PsiElement parent = element.getParent(); - - if (!(parent instanceof PsiBinaryExpression || - parent instanceof PsiParenthesizedExpression || - parent instanceof PsiTypeCastExpression || - parent instanceof PsiConditionalExpression)) { - break; - } - prev = element; - element = parent; - } - if (element instanceof PsiConditionalExpression) { - return prev; - } - - return element; - } - @NotNull - public static List collectOperands(@NotNull final PsiExpression place, final String prefix, final String suffix, final Ref unparsable) { - final PsiElement parent = getContext(place); - if (parent == null) return Collections.emptyList(); - + public static List collectOperands(@NotNull final String prefix, final String suffix, final Ref unparsable, final PsiElement[] operands) { final ArrayList result = new ArrayList(); + final ContextComputationProcessor processor = new ContextComputationProcessor(operands[0].getProject()); addStringFragment(prefix, result); - new ContextComputationProcessor(place.getProject()).collectOperands(parent, result, unparsable); + for (PsiElement operand : operands) { + processor.collectOperands(operand, result, unparsable); + } addStringFragment(suffix, result); return result; } diff --git a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/PsiUtilEx.java b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/PsiUtilEx.java index 18420a56ad..b159ac6e25 100644 --- a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/PsiUtilEx.java +++ b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/util/PsiUtilEx.java @@ -41,7 +41,7 @@ public class PsiUtilEx { } @Nullable - public static PsiParameter getParameterForArgument(PsiExpression element) { + public static PsiParameter getParameterForArgument(PsiElement element) { PsiElement p = element.getParent(); if (!(p instanceof PsiExpressionList)) return null; PsiExpressionList list = (PsiExpressionList)p; -- 2.11.4.GIT