From 6fa9a87a4f84a4b963a45cfc78db459ff0f064ab Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Thu, 2 Aug 2012 23:15:58 +0400 Subject: [PATCH] explicit "allow modificaiton" option and //lang comment initial --- .../daemon/impl/quickfix/OrderEntryFix.java | 12 +++++--- .../intelliLang/AdvancedSettingsUI.form | 14 ++++++++-- .../intelliLang/AdvancedSettingsUI.java | 6 ++++ .../inject/java/ConcatenationInjector.java | 28 +++++++++++++++++-- .../java/JavaLanguageInjectionSupport.java | 10 +++++-- .../plugins/intelliLang/Configuration.java | 16 +++++++++++ 6 files changed, 74 insertions(+), 12 deletions(-) diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java index 9ddc0a667214..7573d0bf239e 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/OrderEntryFix.java @@ -368,11 +368,9 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix { }); } - public static boolean ensureAnnotationsJarInPath(final Module module, String annotationName) { + public static boolean ensureAnnotationsJarInPath(final Module module) { + if (isAnnotationsJarInPath(module)) return true; if (module == null) return false; - final PsiClass psiClass = JavaPsiFacade.getInstance(module.getProject()) - .findClass(annotationName, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)); - if (psiClass != null) return true; final LocateLibraryDialog dialog = new LocateLibraryDialog( module, PathManager.getLibPath(), "annotations.jar", QuickFixBundle.message("add.library.annotations.description")); @@ -388,4 +386,10 @@ public abstract class OrderEntryFix implements IntentionAction, LocalQuickFix { } return false; } + + public static boolean isAnnotationsJarInPath(Module module) { + if (module == null) return false; + return JavaPsiFacade.getInstance(module.getProject()) + .findClass(AnnotationUtil.LANGUAGE, GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(module)) != null; + } } diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.form b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.form index 660726ca673b..9aba68ac9b46 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.form +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.form @@ -1,9 +1,9 @@
- + - + @@ -167,9 +167,17 @@ + + + + + + + + - + diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.java index abedb57aad75..ab8c7a918ac2 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.java +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/AdvancedSettingsUI.java @@ -59,6 +59,7 @@ public class AdvancedSettingsUI implements Configurable { private JRadioButton myUseDfa; private JRadioButton myLookForAssignments; private JCheckBox myIncludeUncomputableOperandsAsCheckBox; + private JCheckBox mySourceModificationAllowedCheckBox; private final ReferenceEditorWithBrowseButton myAnnotationField; private final ReferenceEditorWithBrowseButton myPatternField; @@ -138,6 +139,9 @@ public class AdvancedSettingsUI implements Configurable { if (myConfiguration.isIncludeUncomputablesAsLiterals() != myIncludeUncomputableOperandsAsCheckBox.isSelected()) { return true; } + if (myConfiguration.isSourceModificationAllowed() != mySourceModificationAllowedCheckBox.isSelected()) { + return true; + } return false; } @@ -159,6 +163,7 @@ public class AdvancedSettingsUI implements Configurable { myConfiguration.setDfaOption(getDfaOption()); myConfiguration.setIncludeUncomputablesAsLiterals(myIncludeUncomputableOperandsAsCheckBox.isSelected()); + myConfiguration.setSourceModificationAllowed(mySourceModificationAllowedCheckBox.isSelected()); } @NotNull @@ -181,6 +186,7 @@ public class AdvancedSettingsUI implements Configurable { setDfaOption(myConfiguration.getDfaOption()); myIncludeUncomputableOperandsAsCheckBox.setSelected(myConfiguration.isIncludeUncomputablesAsLiterals()); + mySourceModificationAllowedCheckBox.setSelected(myConfiguration.isSourceModificationAllowed()); } private void setDfaOption(@NotNull final Configuration.DfaOption dfaOption) { diff --git a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/ConcatenationInjector.java b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/ConcatenationInjector.java index 10008e8aab33..b02eab77cdaf 100644 --- a/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/ConcatenationInjector.java +++ b/plugins/IntelliLang/java-support/org/intellij/plugins/intelliLang/inject/java/ConcatenationInjector.java @@ -19,7 +19,9 @@ import com.intellij.lang.Language; import com.intellij.lang.injection.ConcatenationAwareInjector; import com.intellij.lang.injection.MultiHostRegistrar; import com.intellij.openapi.project.Project; -import com.intellij.openapi.util.*; +import com.intellij.openapi.util.Ref; +import com.intellij.openapi.util.TextRange; +import com.intellij.openapi.util.Trinity; import com.intellij.openapi.util.text.StringUtil; import com.intellij.patterns.ElementPattern; import com.intellij.psi.*; @@ -225,7 +227,7 @@ public class ConcatenationInjector implements ConcatenationAwareInjector { } }); } - if (areThereInjectionsWithName(variable.getName(), false)) { + if (!processCommentInjections(variable) && areThereInjectionsWithName(variable.getName(), false)) { process(variable, null, -1); } return false; @@ -272,6 +274,28 @@ public class ConcatenationInjector implements ConcatenationAwareInjector { } } + private boolean processCommentInjections(PsiVariable owner) { + PsiElement prev = owner.getFirstChild(); + if (prev instanceof PsiComment) { + String text = ElementManipulators.getValueText(prev).trim(); + Language language = null; + for (int idx = 0, len = text.length(); idx != -1 && language == null; idx = StringUtil.indexOfAny(text, " \t\r\n,", idx + 1, len)) { + String id = idx > 0 ? text.substring(0, idx).trim() : text; + language = Language.findLanguageByID(id); + } + if (language != null) { + final BaseInjection injection = new BaseInjection(LanguageInjectionSupport.JAVA_SUPPORT_ID); + //if (prefix != null) injection.setPrefix(prefix); + //if (suffix != null) injection.setSuffix(suffix); + injection.setInjectedLanguageId(language.getID()); + processInjectionWithContext(myUnparsable, injection, false); + return true; + } + + } + return false; + } + private void process(final PsiModifierListOwner owner, PsiMethod method, int paramIndex) { if (!processAnnotationInjections(owner)) { myShouldStop = true; 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 bb5556d2bba2..6cfa7b10fea0 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 @@ -201,13 +201,17 @@ public class JavaLanguageInjectionSupport extends AbstractLanguageInjectionSuppo } static boolean doAddLanguageAnnotation(final Project project, final PsiModifierListOwner modifierListOwner, - final String languageId) { + final String languageId) { + if (!Configuration.getProjectInstance(project).getAdvancedConfiguration().isSourceModificationAllowed()) return false; if (modifierListOwner.getModifierList() == null || !PsiUtil.isLanguageLevel5OrHigher(modifierListOwner)) return false; - if (!OrderEntryFix.ensureAnnotationsJarInPath(ModuleUtil.findModuleForPsiElement(modifierListOwner), AnnotationUtil.LANGUAGE)) return false; + if (!OrderEntryFix.isAnnotationsJarInPath(ModuleUtil.findModuleForPsiElement(modifierListOwner))) { + // todo add languageId comment + return false; + } new WriteCommandAction(project, modifierListOwner.getContainingFile()) { protected void run(final Result result) throws Throwable { final PsiAnnotation annotation = JavaPsiFacade.getInstance(project).getElementFactory() - .createAnnotationFromText("@" + AnnotationUtil.LANGUAGE + "(\"" + languageId + "\")", modifierListOwner); + .createAnnotationFromText("@" + AnnotationUtil.LANGUAGE + "(\"" + languageId + "\")", modifierListOwner); final PsiModifierList list = modifierListOwner.getModifierList(); assert list != null; final PsiAnnotation existingAnnotation = list.findAnnotation(AnnotationUtil.LANGUAGE); diff --git a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java index 60105777538c..ad0b6cda2930 100644 --- a/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java +++ b/plugins/IntelliLang/src/org/intellij/plugins/intelliLang/Configuration.java @@ -178,6 +178,7 @@ public class Configuration implements PersistentStateComponent, Modific @NonNls private static final String LOOK_FOR_VAR_ASSIGNMENTS = "LOOK_FOR_VAR_ASSIGNMENTS"; @NonNls private static final String USE_DFA_IF_AVAILABLE = "USE_DFA_IF_AVAILABLE"; @NonNls private static final String INCLUDE_UNCOMPUTABLES_AS_LITERALS = "INCLUDE_UNCOMPUTABLES_AS_LITERALS"; + @NonNls private static final String SOURCE_MODIFICATION_ALLOWED = "SOURCE_MODIFICATION_ALLOWED"; private final Map> myInjections = new ConcurrentFactoryMap>() { @Override @@ -566,6 +567,7 @@ public class Configuration implements PersistentStateComponent, Modific private boolean myIncludeUncomputablesAsLiterals; private DfaOption myDfaOption = DfaOption.RESOLVE; + private boolean mySourceModificationAllowed; // cached annotation name pairs private Pair> myLanguageAnnotationPair; @@ -648,6 +650,13 @@ public class Configuration implements PersistentStateComponent, Modific myDfaOption = dfaOption; } + public boolean isSourceModificationAllowed() { + return mySourceModificationAllowed; + } + + public void setSourceModificationAllowed(boolean sourceModificationAllowed) { + mySourceModificationAllowed = sourceModificationAllowed; + } public InstrumentationType getInstrumentation() { return myInstrumentationType; @@ -658,6 +667,12 @@ public class Configuration implements PersistentStateComponent, Modific JDOMExternalizerUtil.writeField(element, LANGUAGE_ANNOTATION_NAME, myLanguageAnnotation); JDOMExternalizerUtil.writeField(element, PATTERN_ANNOTATION_NAME, myPatternAnnotation); JDOMExternalizerUtil.writeField(element, SUBST_ANNOTATION_NAME, mySubstAnnotation); + if (myIncludeUncomputablesAsLiterals) { + JDOMExternalizerUtil.writeField(element, INCLUDE_UNCOMPUTABLES_AS_LITERALS, "true"); + } + if (mySourceModificationAllowed) { + JDOMExternalizerUtil.writeField(element, SOURCE_MODIFICATION_ALLOWED, "true"); + } switch (myDfaOption) { case OFF: break; @@ -688,6 +703,7 @@ public class Configuration implements PersistentStateComponent, Modific setDfaOption(DfaOption.DFA); } setIncludeUncomputablesAsLiterals(readBoolean(element, INCLUDE_UNCOMPUTABLES_AS_LITERALS, false)); + setSourceModificationAllowed(readBoolean(element, SOURCE_MODIFICATION_ALLOWED, false)); } } }