Search in sources :

Example 1 with Configuration

use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.

the class JavaLanguageInjectionSupport method doAddLanguageAnnotation.

public static boolean doAddLanguageAnnotation(Project project, @Nullable final PsiModifierListOwner modifierListOwner, @NotNull final PsiLanguageInjectionHost host, final String languageId, Processor<PsiLanguageInjectionHost> annotationFixer) {
    final boolean addAnnotation = isAnnotationsJarInPath(ModuleUtilCore.findModuleForPsiElement(modifierListOwner)) && PsiUtil.isLanguageLevel5OrHigher(modifierListOwner) && modifierListOwner.getModifierList() != null;
    final PsiStatement statement = PsiTreeUtil.getParentOfType(host, PsiStatement.class);
    if (!addAnnotation && statement == null)
        return false;
    Configuration.AdvancedConfiguration configuration = Configuration.getProjectInstance(project).getAdvancedConfiguration();
    if (!configuration.isSourceModificationAllowed()) {
        host.putUserData(InjectLanguageAction.FIX_KEY, annotationFixer);
        return false;
    }
    new WriteCommandAction(modifierListOwner.getProject(), modifierListOwner.getContainingFile()) {

        protected void run(@NotNull Result result) throws Throwable {
            PsiElementFactory javaFacade = JavaPsiFacade.getElementFactory(getProject());
            if (addAnnotation) {
                JVMElementFactory factory = ObjectUtils.chooseNotNull(JVMElementFactories.getFactory(modifierListOwner.getLanguage(), getProject()), javaFacade);
                PsiAnnotation annotation = factory.createAnnotationFromText("@" + AnnotationUtil.LANGUAGE + "(\"" + languageId + "\")", modifierListOwner);
                PsiModifierList list = ObjectUtils.assertNotNull(modifierListOwner.getModifierList());
                final PsiAnnotation existingAnnotation = list.findAnnotation(AnnotationUtil.LANGUAGE);
                if (existingAnnotation != null) {
                    existingAnnotation.replace(annotation);
                } else {
                    list.addAfter(annotation, null);
                }
                JavaCodeStyleManager.getInstance(getProject()).shortenClassReferences(list);
            } else {
                statement.getParent().addBefore(javaFacade.createCommentFromText("//language=" + languageId, host), statement);
            }
        }
    }.execute();
    return true;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Configuration(org.intellij.plugins.intelliLang.Configuration) Result(com.intellij.openapi.application.Result)

Example 2 with Configuration

use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.

the class JavaLanguageInjectionSupport method doEditInjection.

private static void doEditInjection(final Project project, final MethodParameterInjection template, final PsiMethod contextMethod) {
    final Configuration configuration = InjectorUtils.getEditableInstance(project);
    final BaseInjection baseTemplate = new BaseInjection(template.getSupportId()).copyFrom(template);
    final MethodParameterInjection allMethodParameterInjection = createFrom(project, baseTemplate, contextMethod, true);
    // find existing injection for this class.
    final BaseInjection originalInjection = configuration.findExistingInjection(allMethodParameterInjection);
    final MethodParameterInjection methodParameterInjection;
    if (originalInjection == null) {
        methodParameterInjection = template;
    } else {
        final BaseInjection originalCopy = originalInjection.copy();
        final InjectionPlace currentPlace = template.getInjectionPlaces()[0];
        originalCopy.mergeOriginalPlacesFrom(template, true);
        originalCopy.setPlaceEnabled(currentPlace.getText(), true);
        methodParameterInjection = createFrom(project, originalCopy, contextMethod, false);
    }
    mergePlacesAndAddToConfiguration(project, configuration, methodParameterInjection, originalInjection);
}
Also used : Configuration(org.intellij.plugins.intelliLang.Configuration) MethodParameterInjection(org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 3 with Configuration

use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.

the class XmlLanguageInjectionSupport method doEditInjection.

private static void doEditInjection(final Project project, final XmlAttributeInjection template) {
    final Configuration configuration = InjectorUtils.getEditableInstance(project);
    final BaseInjection originalInjection = configuration.findExistingInjection(template);
    final BaseInjection newInjection = originalInjection == null ? template : originalInjection.copy();
    configuration.replaceInjectionsWithUndo(project, Collections.singletonList(newInjection), ContainerUtil.createMaybeSingletonList(originalInjection), Collections.<PsiElement>emptyList());
}
Also used : Configuration(org.intellij.plugins.intelliLang.Configuration)

Example 4 with Configuration

use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.

the class XmlLanguageInjectionSupport method editInjectionInPlace.

public boolean editInjectionInPlace(final PsiLanguageInjectionHost host) {
    if (!isMine(host))
        return false;
    final Project project = host.getProject();
    final Configuration configuration = Configuration.getProjectInstance(project);
    final ArrayList<BaseInjection> injections = collectInjections(host, configuration);
    if (injections.isEmpty())
        return false;
    final BaseInjection originalInjection = injections.get(0);
    final BaseInjection xmlInjection = createFrom(originalInjection);
    final BaseInjection newInjection = xmlInjection == null ? showDefaultInjectionUI(project, originalInjection.copy()) : showInjectionUI(project, xmlInjection);
    if (newInjection != null) {
        configuration.replaceInjectionsWithUndo(project, Collections.singletonList(newInjection), Collections.singletonList(originalInjection), Collections.<PsiElement>emptyList());
    }
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) Configuration(org.intellij.plugins.intelliLang.Configuration)

Example 5 with Configuration

use of org.intellij.plugins.intelliLang.Configuration in project intellij-community by JetBrains.

the class JavaLanguageInjectionSupport method editInjectionInPlace.

public boolean editInjectionInPlace(final PsiLanguageInjectionHost psiElement) {
    if (!isMine(psiElement))
        return false;
    final HashMap<BaseInjection, Pair<PsiMethod, Integer>> injectionsMap = new HashMap<>();
    final ArrayList<PsiElement> annotations = new ArrayList<>();
    final PsiLiteralExpression host = (PsiLiteralExpression) psiElement;
    final Project project = host.getProject();
    final Configuration configuration = Configuration.getProjectInstance(project);
    collectInjections(host, configuration, this, injectionsMap, annotations);
    if (injectionsMap.isEmpty() || !annotations.isEmpty())
        return false;
    final BaseInjection originalInjection = injectionsMap.keySet().iterator().next();
    final MethodParameterInjection methodParameterInjection = createFrom(psiElement.getProject(), originalInjection, injectionsMap.get(originalInjection).first, false);
    final MethodParameterInjection copy = methodParameterInjection.copy();
    final BaseInjection newInjection = showInjectionUI(project, methodParameterInjection);
    if (newInjection != null) {
        newInjection.mergeOriginalPlacesFrom(copy, false);
        newInjection.mergeOriginalPlacesFrom(originalInjection, true);
        configuration.replaceInjectionsWithUndo(project, Collections.singletonList(newInjection), Collections.singletonList(originalInjection), Collections.<PsiAnnotation>emptyList());
    }
    return true;
}
Also used : Project(com.intellij.openapi.project.Project) Configuration(org.intellij.plugins.intelliLang.Configuration) MethodParameterInjection(org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) Pair(com.intellij.openapi.util.Pair)

Aggregations

Configuration (org.intellij.plugins.intelliLang.Configuration)11 Project (com.intellij.openapi.project.Project)5 BaseInjection (org.intellij.plugins.intelliLang.inject.config.BaseInjection)4 NotNull (org.jetbrains.annotations.NotNull)4 Pair (com.intellij.openapi.util.Pair)3 MethodParameterInjection (org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection)3 Language (com.intellij.lang.Language)2 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 com.intellij.psi (com.intellij.psi)2 ProcessingContext (com.intellij.util.ProcessingContext)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 ArrayList (java.util.ArrayList)2 AbstractLanguageInjectionSupport (org.intellij.plugins.intelliLang.inject.AbstractLanguageInjectionSupport)2 InjectorUtils (org.intellij.plugins.intelliLang.inject.InjectorUtils)2 NonNls (org.jetbrains.annotations.NonNls)2 Nullable (org.jetbrains.annotations.Nullable)2 AnnotationUtil (com.intellij.codeInsight.AnnotationUtil)1 MultiHostRegistrar (com.intellij.lang.injection.MultiHostRegistrar)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1