From b4d4dfbf87221cc648a289a43756bc9e69400e6b Mon Sep 17 00:00:00 2001 From: Max Medvedev Date: Thu, 24 Oct 2013 16:17:18 +0400 Subject: [PATCH] IDEA-114710 Groovy: @Language annotation is not inserted before concatenated string on Alt+Enter --- .../java/JavaLanguageInjectionSupport.java | 38 +++++++++++-------- .../GroovyLanguageInjectionSupport.java | 25 +++++++++++- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java index a45393313a21..99a3ccd21eab 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/JavaLanguageInjectionSupport.java @@ -217,24 +217,32 @@ public class JavaLanguageInjectionSupport extends AbstractLanguageInjectionSuppo final PsiModifierListOwner modifierListOwner, @NotNull PsiLanguageInjectionHost host, final String languageId) { + // todo add languageId comment + return doAddLanguageAnnotation(project, modifierListOwner, host, languageId, new Processor() { + @Override + public boolean process(PsiLanguageInjectionHost host) { + final Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration(); + boolean allowed = configuration.isSourceModificationAllowed(); + configuration.setSourceModificationAllowed(true); + try { + return doInjectInJava(project, host, host, languageId); + } + finally { + configuration.setSourceModificationAllowed(allowed); + } + } + }); + } + + public static boolean doAddLanguageAnnotation(final Project project, + final PsiModifierListOwner modifierListOwner, + @NotNull PsiLanguageInjectionHost host, + final String languageId, + Processor annotationFixer) { if (modifierListOwner.getModifierList() == null || !PsiUtil.isLanguageLevel5OrHigher(modifierListOwner)) return false; final Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration(); if (!configuration.isSourceModificationAllowed()) { - // todo add languageId comment - host.putUserData(InjectLanguageAction.FIX_KEY, new Processor() { - @Override - public boolean process(PsiLanguageInjectionHost host) { - boolean allowed = configuration.isSourceModificationAllowed(); - configuration.setSourceModificationAllowed(true); - try { - return doInjectInJava(project, host, host, languageId); - } - finally { - configuration.setSourceModificationAllowed(allowed); - } - } - }); - + host.putUserData(InjectLanguageAction.FIX_KEY, annotationFixer); return false; } if (!OrderEntryFix.ensureAnnotationsJarInPath(ModuleUtilCore.findModuleForPsiElement(modifierListOwner))) { diff --git a/plugins/groovy/src/org/intellij/plugins/intelliLang/inject/groovy/GroovyLanguageInjectionSupport.java b/plugins/groovy/src/org/intellij/plugins/intelliLang/inject/groovy/GroovyLanguageInjectionSupport.java index d8be45115765..2c958d66f6d6 100644 --- a/plugins/groovy/src/org/intellij/plugins/intelliLang/inject/groovy/GroovyLanguageInjectionSupport.java +++ b/plugins/groovy/src/org/intellij/plugins/intelliLang/inject/groovy/GroovyLanguageInjectionSupport.java @@ -23,6 +23,7 @@ import com.intellij.psi.*; import com.intellij.psi.impl.light.LightElement; import com.intellij.psi.tree.IElementType; import com.intellij.util.NullableFunction; +import com.intellij.util.Processor; import com.intellij.util.containers.ContainerUtil; import org.intellij.plugins.intelliLang.Configuration; import org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport; @@ -187,16 +188,36 @@ public class GroovyLanguageInjectionSupport extends AbstractLanguageInjectionSup } } else { + if (parent instanceof PsiVariable) { - if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner)parent, host, languageId)) return true; + Processor fixer = getAnnotationFixer(project, languageId); + if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner)parent, host, languageId, fixer)) return true; } else if (target instanceof PsiVariable && !(target instanceof LightElement)) { - if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner)target, host, languageId)) return true; + Processor fixer = getAnnotationFixer(project, languageId); + if (JavaLanguageInjectionSupport.doAddLanguageAnnotation(project, (PsiModifierListOwner)target, host, languageId, fixer)) return true; } } return false; } + private static Processor getAnnotationFixer(final Project project, final String languageId) { + return new Processor() { + @Override + public boolean process(PsiLanguageInjectionHost host) { + final Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration(); + boolean allowed = configuration.isSourceModificationAllowed(); + configuration.setSourceModificationAllowed(true); + try { + return doInject(languageId, host, host); + } + finally { + configuration.setSourceModificationAllowed(allowed); + } + } + }; + } + private static boolean isStringLiteral(PsiLanguageInjectionHost element) { if (element instanceof GrStringContent) { return true;