Search in sources :

Example 11 with BaseInjection

use of org.intellij.plugins.intelliLang.inject.config.BaseInjection in project intellij-community by JetBrains.

the class Configuration method loadState.

@Override
public void loadState(final Element element) {
    myInjections.clear();
    List<Element> injectionElements = element.getChildren("injection");
    if (!injectionElements.isEmpty()) {
        final Map<String, LanguageInjectionSupport> supports = new THashMap<>();
        for (LanguageInjectionSupport support : InjectorUtils.getActiveInjectionSupports()) {
            supports.put(support.getId(), support);
        }
        for (Element child : injectionElements) {
            final String key = child.getAttributeValue("injector-id");
            final LanguageInjectionSupport support = supports.get(key);
            final BaseInjection injection = support == null ? new BaseInjection(key) : support.createInjection(child);
            injection.loadState(child);
            InjectionPlace[] places = dropKnownInvalidPlaces(injection.getInjectionPlaces());
            if (places != null) {
                // not all places were removed
                injection.setInjectionPlaces(places);
                myInjections.get(key).add(injection);
            }
        }
    }
    importPlaces(getDefaultInjections());
}
Also used : LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) THashMap(gnu.trove.THashMap) PsiElement(com.intellij.psi.PsiElement) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) Element(org.jdom.Element) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 12 with BaseInjection

use of org.intellij.plugins.intelliLang.inject.config.BaseInjection in project intellij-community by JetBrains.

the class Configuration method loadDefaultInjections.

private static List<BaseInjection> loadDefaultInjections() {
    final List<Configuration> cfgList = new ArrayList<>();
    final Set<Object> visited = new THashSet<>();
    for (LanguageInjectionConfigBean configBean : Extensions.getExtensions(LanguageInjectionSupport.CONFIG_EP_NAME)) {
        PluginDescriptor descriptor = configBean.getPluginDescriptor();
        final ClassLoader loader = descriptor.getPluginClassLoader();
        try {
            final Enumeration<URL> enumeration = loader.getResources(configBean.getConfigUrl());
            if (enumeration == null || !enumeration.hasMoreElements()) {
                LOG.warn(descriptor.getPluginId() + ": " + configBean.getConfigUrl() + " was not found");
            } else {
                while (enumeration.hasMoreElements()) {
                    URL url = enumeration.nextElement();
                    // for DEBUG mode
                    if (!visited.add(url.getFile()))
                        continue;
                    InputStream stream = null;
                    try {
                        stream = url.openStream();
                        cfgList.add(load(stream));
                    } catch (Exception e) {
                        LOG.warn(e);
                    } finally {
                        if (stream != null) {
                            stream.close();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn(e);
        }
    }
    final List<BaseInjection> defaultInjections = new ArrayList<>();
    for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
        for (Configuration cfg : cfgList) {
            final List<BaseInjection> imported = cfg.getInjections(supportId);
            defaultInjections.addAll(imported);
        }
    }
    return defaultInjections;
}
Also used : InputStream(java.io.InputStream) THashSet(gnu.trove.THashSet) URL(java.net.URL) JDOMException(org.jdom.JDOMException) IOException(java.io.IOException) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) PluginDescriptor(com.intellij.openapi.extensions.PluginDescriptor) LanguageInjectionConfigBean(org.intellij.plugins.intelliLang.inject.LanguageInjectionConfigBean)

Example 13 with BaseInjection

use of org.intellij.plugins.intelliLang.inject.config.BaseInjection in project intellij-community by JetBrains.

the class Configuration method getState.

protected Element getState(Element element) {
    Comparator<BaseInjection> comparator = (o1, o2) -> {
        int rc = Comparing.compare(o1.getDisplayName(), o2.getDisplayName());
        if (rc != 0)
            return rc;
        return ContainerUtil.compareLexicographically(Arrays.asList(o1.getInjectionPlaces()), Arrays.asList(o2.getInjectionPlaces()), (o11, o22) -> {
            if (o11.isEnabled() && !o22.isEnabled())
                return -1;
            if (!o11.isEnabled() && o22.isEnabled())
                return 1;
            return Comparing.compare(o11.getElementPattern().toString(), o22.getElementPattern().toString());
        });
    };
    for (String key : ContainerUtil.newTreeSet(myInjections.keySet())) {
        Set<BaseInjection> injections = ContainerUtil.newHashSet(myInjections.get(key));
        injections.removeAll(getDefaultInjections());
        for (BaseInjection injection : ContainerUtil.sorted(injections, comparator)) {
            element.addContent(injection.getState());
        }
    }
    return element;
}
Also used : Language(com.intellij.lang.Language) Storage(com.intellij.openapi.components.Storage) com.intellij.openapi.util(com.intellij.openapi.util) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) com.intellij.util.containers(com.intellij.util.containers) URL(java.net.URL) PersistentStateComponent(com.intellij.openapi.components.PersistentStateComponent) NonNls(org.jetbrains.annotations.NonNls) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) CachedValueProvider(com.intellij.psi.util.CachedValueProvider) PairProcessor(com.intellij.util.PairProcessor) JDOMException(org.jdom.JDOMException) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) State(com.intellij.openapi.components.State) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) UndoConfirmationPolicy(com.intellij.openapi.command.UndoConfirmationPolicy) Logger(com.intellij.openapi.diagnostic.Logger) Extensions(com.intellij.openapi.extensions.Extensions) InjectorUtils(org.intellij.plugins.intelliLang.inject.InjectorUtils) LanguageInjectionConfigBean(org.intellij.plugins.intelliLang.inject.LanguageInjectionConfigBean) CachedValueImpl(com.intellij.util.CachedValueImpl) GlobalUndoableAction(com.intellij.openapi.command.undo.GlobalUndoableAction) FileContentUtil(com.intellij.util.FileContentUtil) UndoableAction(com.intellij.openapi.command.undo.UndoableAction) IOException(java.io.IOException) PsiCompiledElement(com.intellij.psi.PsiCompiledElement) Nullable(org.jetbrains.annotations.Nullable) ServiceManager(com.intellij.openapi.components.ServiceManager) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) CachedValue(com.intellij.psi.util.CachedValue) UndoManager(com.intellij.openapi.command.undo.UndoManager) InjectionPlace(org.intellij.plugins.intelliLang.inject.config.InjectionPlace) LanguageInjectionSupport(org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport) PluginDescriptor(com.intellij.openapi.extensions.PluginDescriptor) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) InputStream(java.io.InputStream) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 14 with BaseInjection

use of org.intellij.plugins.intelliLang.inject.config.BaseInjection in project intellij-community by JetBrains.

the class InjectionsSettingsUI method doImportAction.

private void doImportAction(final DataContext dataContext) {
    final FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, true, false, true, false) {

        @Override
        public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
            return super.isFileVisible(file, showHiddenFiles) && (file.isDirectory() || "xml".equals(file.getExtension()) || file.getFileType() == FileTypes.ARCHIVE);
        }

        @Override
        public boolean isFileSelectable(VirtualFile file) {
            return file.getFileType() == StdFileTypes.XML;
        }
    };
    descriptor.setDescription("Please select the configuration file (usually named IntelliLang.xml) to import.");
    descriptor.setTitle("Import Configuration");
    descriptor.putUserData(LangDataKeys.MODULE_CONTEXT, LangDataKeys.MODULE.getData(dataContext));
    final SplitterProportionsData splitterData = new SplitterProportionsDataImpl();
    splitterData.externalizeFromDimensionService("IntelliLang.ImportSettingsKey.SplitterProportions");
    final VirtualFile file = FileChooser.chooseFile(descriptor, myProject, null);
    if (file == null)
        return;
    try {
        final Configuration cfg = Configuration.load(file.getInputStream());
        if (cfg == null) {
            Messages.showWarningDialog(myProject, "The selected file does not contain any importable configuration.", "Nothing to Import");
            return;
        }
        final CfgInfo info = getDefaultCfgInfo();
        final Map<String, Set<InjInfo>> currentMap = ContainerUtil.classify(info.injectionInfos.iterator(), new Convertor<InjInfo, String>() {

            public String convert(final InjInfo o) {
                return o.injection.getSupportId();
            }
        });
        final List<BaseInjection> originalInjections = new ArrayList<>();
        final List<BaseInjection> newInjections = new ArrayList<>();
        for (String supportId : InjectorUtils.getActiveInjectionSupportIds()) {
            ArrayList<InjInfo> list = new ArrayList<>(ObjectUtils.notNull(currentMap.get(supportId), Collections.<InjInfo>emptyList()));
            final List<BaseInjection> currentInjections = getInjectionList(list);
            final List<BaseInjection> importingInjections = cfg.getInjections(supportId);
            if (currentInjections == null) {
                newInjections.addAll(importingInjections);
            } else {
                Configuration.importInjections(currentInjections, importingInjections, originalInjections, newInjections);
            }
        }
        info.replace(originalInjections, newInjections);
        myInjectionsTable.getListTableModel().setItems(getInjInfoList(myInfos));
        final int n = newInjections.size();
        if (n > 1) {
            Messages.showInfoMessage(myProject, n + " entries have been successfully imported", "Import Successful");
        } else if (n == 1) {
            Messages.showInfoMessage(myProject, "One entry has been successfully imported", "Import Successful");
        } else {
            Messages.showInfoMessage(myProject, "No new entries have been imported", "Import");
        }
    } catch (Exception ex) {
        Configuration.LOG.error(ex);
        final String msg = ex.getLocalizedMessage();
        Messages.showErrorDialog(myProject, msg != null && msg.length() > 0 ? msg : ex.toString(), "Import Failed");
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SplitterProportionsData(com.intellij.openapi.ui.SplitterProportionsData) THashSet(gnu.trove.THashSet) FileChooserDescriptor(com.intellij.openapi.fileChooser.FileChooserDescriptor) SplitterProportionsDataImpl(com.intellij.ide.ui.SplitterProportionsDataImpl) IOException(java.io.IOException) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Example 15 with BaseInjection

use of org.intellij.plugins.intelliLang.inject.config.BaseInjection in project intellij-community by JetBrains.

the class AbstractLanguageInjectionSupport method createDefaultEditAction.

public static AnAction createDefaultEditAction(final Project project, final Factory<BaseInjection> producer) {
    return new AnAction() {

        @Override
        public void actionPerformed(AnActionEvent e) {
            final BaseInjection originalInjection = producer.create();
            final BaseInjection newInjection = showDefaultInjectionUI(project, originalInjection.copy());
            if (newInjection != null) {
                originalInjection.copyFrom(newInjection);
            }
        }
    };
}
Also used : AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) AnAction(com.intellij.openapi.actionSystem.AnAction) BaseInjection(org.intellij.plugins.intelliLang.inject.config.BaseInjection)

Aggregations

BaseInjection (org.intellij.plugins.intelliLang.inject.config.BaseInjection)23 InjectionPlace (org.intellij.plugins.intelliLang.inject.config.InjectionPlace)7 Nullable (org.jetbrains.annotations.Nullable)7 Language (com.intellij.lang.Language)6 Project (com.intellij.openapi.project.Project)6 LanguageInjectionSupport (org.intellij.plugins.intelliLang.inject.LanguageInjectionSupport)6 InjectorUtils (org.intellij.plugins.intelliLang.inject.InjectorUtils)5 NotNull (org.jetbrains.annotations.NotNull)5 AnAction (com.intellij.openapi.actionSystem.AnAction)4 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)4 ContainerUtil (com.intellij.util.containers.ContainerUtil)4 THashSet (gnu.trove.THashSet)4 Pair (com.intellij.openapi.util.Pair)3 PsiElement (com.intellij.psi.PsiElement)3 IOException (java.io.IOException)3 Configuration (org.intellij.plugins.intelliLang.Configuration)3 MethodParameterInjection (org.intellij.plugins.intelliLang.inject.config.MethodParameterInjection)3 Element (org.jdom.Element)3 NonNls (org.jetbrains.annotations.NonNls)3 SplitterProportionsDataImpl (com.intellij.ide.ui.SplitterProportionsDataImpl)2