Search in sources :

Example 1 with ExtensionPoint

use of com.intellij.openapi.extensions.ExtensionPoint in project intellij-community by JetBrains.

the class UsageNodeTreeBuilderTest method buildUsageTree.

private GroupNode buildUsageTree(int[] indices, UsageGroupingRule[] rules) {
    Usage[] usages = new Usage[indices.length];
    for (int i = 0; i < usages.length; i++) {
        usages[i] = createUsage(indices[i]);
    }
    UsageViewPresentation presentation = new UsageViewPresentation();
    presentation.setUsagesString("searching for mock usages");
    ExtensionsArea area = Extensions.getRootArea();
    ExtensionPoint<UsageGroupingRuleProvider> point = area.getExtensionPoint(UsageGroupingRuleProvider.EP_NAME);
    UsageGroupingRuleProvider provider = new UsageGroupingRuleProvider() {

        @NotNull
        @Override
        public UsageGroupingRule[] getActiveRules(Project project) {
            return rules;
        }

        @NotNull
        @Override
        public AnAction[] createGroupingActions(UsageView view) {
            return AnAction.EMPTY_ARRAY;
        }
    };
    point.registerExtension(provider);
    try {
        UsageViewImpl usageView = new UsageViewImpl(getProject(), presentation, UsageTarget.EMPTY_ARRAY, null);
        Disposer.register(getTestRootDisposable(), usageView);
        for (Usage usage : usages) {
            usageView.appendUsage(usage);
        }
        UIUtil.dispatchAllInvocationEvents();
        return usageView.getRoot();
    } finally {
        point.unregisterExtension(provider);
    }
}
Also used : ExtensionsArea(com.intellij.openapi.extensions.ExtensionsArea) Project(com.intellij.openapi.project.Project) UsageGroupingRule(com.intellij.usages.rules.UsageGroupingRule) AnAction(com.intellij.openapi.actionSystem.AnAction) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) UsageGroupingRuleProvider(com.intellij.usages.rules.UsageGroupingRuleProvider)

Example 2 with ExtensionPoint

use of com.intellij.openapi.extensions.ExtensionPoint in project intellij-community by JetBrains.

the class DumpExtensionsAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    List<ExtensionsArea> areas = new ArrayList<>();
    areas.add(Extensions.getRootArea());
    final Project project = e.getData(CommonDataKeys.PROJECT);
    if (project != null) {
        areas.add(Extensions.getArea(project));
        final Module[] modules = ModuleManager.getInstance(project).getModules();
        if (modules.length > 0) {
            areas.add(Extensions.getArea(modules[0]));
        }
    }
    System.out.print(areas.size() + " extension areas: ");
    for (ExtensionsArea area : areas) {
        System.out.print(area.getAreaClass() + " ");
    }
    System.out.println("\n");
    List<ExtensionPoint> points = new ArrayList<>();
    for (ExtensionsArea area : areas) {
        points.addAll(Arrays.asList(area.getExtensionPoints()));
    }
    System.out.println(points.size() + " extension points: ");
    for (ExtensionPoint point : points) {
        System.out.println(" " + point.getName());
    }
    List<Object> extensions = new ArrayList<>();
    for (ExtensionPoint point : points) {
        extensions.addAll(Arrays.asList(point.getExtensions()));
    }
    System.out.println("\n" + extensions.size() + " extensions:");
    for (Object extension : extensions) {
        if (extension instanceof Configurable) {
            System.out.println("!!!! Configurable extension found. Kill it !!!");
        }
        System.out.println(extension);
    }
}
Also used : ExtensionsArea(com.intellij.openapi.extensions.ExtensionsArea) Project(com.intellij.openapi.project.Project) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) ArrayList(java.util.ArrayList) Configurable(com.intellij.openapi.options.Configurable) Module(com.intellij.openapi.module.Module)

Example 3 with ExtensionPoint

use of com.intellij.openapi.extensions.ExtensionPoint in project intellij-community by JetBrains.

the class GutterIconsConfigurable method createComponent.

@Nullable
@Override
public JComponent createComponent() {
    ExtensionPoint<LineMarkerProvider> point = Extensions.getRootArea().getExtensionPoint(LineMarkerProviders.EP_NAME);
    @SuppressWarnings("unchecked") LanguageExtensionPoint<LineMarkerProvider>[] extensions = (LanguageExtensionPoint<LineMarkerProvider>[]) point.getExtensions();
    NullableFunction<LanguageExtensionPoint<LineMarkerProvider>, PluginDescriptor> function = point1 -> {
        LineMarkerProvider instance = point1.getInstance();
        return instance instanceof LineMarkerProviderDescriptor && ((LineMarkerProviderDescriptor) instance).getName() != null ? point1.getPluginDescriptor() : null;
    };
    MultiMap<PluginDescriptor, LanguageExtensionPoint<LineMarkerProvider>> map = ContainerUtil.groupBy(Arrays.asList(extensions), function);
    Map<GutterIconDescriptor, PluginDescriptor> pluginDescriptorMap = ContainerUtil.newHashMap();
    Set<String> ids = new HashSet<>();
    myDescriptors = new ArrayList<>();
    for (final PluginDescriptor descriptor : map.keySet()) {
        Collection<LanguageExtensionPoint<LineMarkerProvider>> points = map.get(descriptor);
        for (LanguageExtensionPoint<LineMarkerProvider> extensionPoint : points) {
            GutterIconDescriptor instance = (GutterIconDescriptor) extensionPoint.getInstance();
            if (instance.getOptions().length > 0) {
                for (GutterIconDescriptor option : instance.getOptions()) {
                    if (ids.add(option.getId())) {
                        myDescriptors.add(option);
                    }
                    pluginDescriptorMap.put(option, descriptor);
                }
            } else {
                if (ids.add(instance.getId())) {
                    myDescriptors.add(instance);
                }
                pluginDescriptorMap.put(instance, descriptor);
            }
        }
    }
    /*
    List<GutterIconDescriptor> options = new ArrayList<GutterIconDescriptor>();
    for (Iterator<GutterIconDescriptor> iterator = myDescriptors.iterator(); iterator.hasNext(); ) {
      GutterIconDescriptor descriptor = iterator.next();
      if (descriptor.getOptions().length > 0) {
        options.addAll(Arrays.asList(descriptor.getOptions()));
        iterator.remove();
      }
    }
    myDescriptors.addAll(options);
    */
    myDescriptors.sort((o1, o2) -> {
        if (pluginDescriptorMap.get(o1) != pluginDescriptorMap.get(o2))
            return 0;
        return Comparing.compare(o1.getName(), o2.getName());
    });
    PluginDescriptor current = null;
    for (GutterIconDescriptor descriptor : myDescriptors) {
        PluginDescriptor pluginDescriptor = pluginDescriptorMap.get(descriptor);
        if (pluginDescriptor != current) {
            myFirstDescriptors.put(descriptor, pluginDescriptor);
            current = pluginDescriptor;
        }
    }
    myList.setItems(myDescriptors, GutterIconDescriptor::getName);
    myShowGutterIconsJBCheckBox.addChangeListener(e -> myList.setEnabled(myShowGutterIconsJBCheckBox.isSelected()));
    return myPanel;
}
Also used : java.util(java.util) ContainerUtil(com.intellij.util.containers.ContainerUtil) Nls(org.jetbrains.annotations.Nls) ProjectManager(com.intellij.openapi.project.ProjectManager) Comparing(com.intellij.openapi.util.Comparing) CheckBoxList(com.intellij.ui.CheckBoxList) EditorSettingsExternalizable(com.intellij.openapi.editor.ex.EditorSettingsExternalizable) Project(com.intellij.openapi.project.Project) HashSet(com.intellij.util.containers.hash.HashSet) ChangeListener(javax.swing.event.ChangeListener) MultiMap(com.intellij.util.containers.MultiMap) IdeaPluginDescriptor(com.intellij.ide.plugins.IdeaPluginDescriptor) Extensions(com.intellij.openapi.extensions.Extensions) EmptyIcon(com.intellij.util.ui.EmptyIcon) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) JBCheckBox(com.intellij.ui.components.JBCheckBox) NullableFunction(com.intellij.util.NullableFunction) Configurable(com.intellij.openapi.options.Configurable) SeparatorWithText(com.intellij.ui.SeparatorWithText) com.intellij.codeInsight.daemon(com.intellij.codeInsight.daemon) TestOnly(org.jetbrains.annotations.TestOnly) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) LanguageExtensionPoint(com.intellij.lang.LanguageExtensionPoint) PluginDescriptor(com.intellij.openapi.extensions.PluginDescriptor) ConfigurationException(com.intellij.openapi.options.ConfigurationException) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) LanguageExtensionPoint(com.intellij.lang.LanguageExtensionPoint) IdeaPluginDescriptor(com.intellij.ide.plugins.IdeaPluginDescriptor) PluginDescriptor(com.intellij.openapi.extensions.PluginDescriptor) HashSet(com.intellij.util.containers.hash.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ExtensionPoint

use of com.intellij.openapi.extensions.ExtensionPoint in project intellij-community by JetBrains.

the class ExtensionsComplexTest method testPluginInit.

@Test
public void testPluginInit() throws Exception {
    initExtensionPoints(PLUGIN_NAME, "<extensionPoints>\n" + "  <extensionPoint name=\"extensionPoint\" beanClass=\"com.intellij.openapi.extensions.impl.XMLTestBean\" />\n" + "  <extensionPoint name=\"dependentOne\" beanClass=\"com.intellij.openapi.extensions.impl.DependentObjectOne\" />\n" + "</extensionPoints>", null);
    initExtensions("  <extensions xmlns=\"the.test.plugin\">\n" + "    <extensionPoint>\n" + "      <prop1>321</prop1>\n" + "    </extensionPoint>\n" + "    <dependentOne/>\n" + "  </extensions>", null);
    assertTrue(Extensions.getRootArea().hasExtensionPoint("the.test.plugin.extensionPoint"));
    assertEquals(1, Extensions.getExtensions("the.test.plugin.extensionPoint").length);
    ExtensionPoint<XMLTestBean> ep = Extensions.getRootArea().getExtensionPoint("the.test.plugin.extensionPoint");
    XMLTestBean bean = ep.getExtension();
    assertNotNull(bean);
    assertEquals(321, bean.getProp1());
    assertEquals("the.test.plugin", bean.getPluginId().getIdString());
    DependentObjectOne dependentObjectOne = (DependentObjectOne) Extensions.getRootArea().getExtensionPoint("the.test.plugin.dependentOne").getExtension();
    assertNotNull(dependentObjectOne);
    assertEquals(1, dependentObjectOne.getTestBeans().length);
    AreaInstance areaInstance = new MyAreaInstance();
    Extensions.instantiateArea("area", areaInstance, null);
    initExtensionPoints(PLUGIN_NAME, "<extensionPoints>\n" + "  <extensionPoint name=\"dependentTwo\" beanClass=\"com.intellij.openapi.extensions.impl.DependentObjectTwo\" area=\"area\"/>\n" + "  <extensionPoint name=\"extensionPoint4area\" beanClass=\"com.intellij.openapi.extensions.impl.XMLTestBean\" area=\"area\" />\n" + "</extensionPoints>", areaInstance);
    initExtensions("  <extensions xmlns=\"the.test.plugin\">\n" + "    <extensionPoint4area area=\"area\"/>\n" + "    <dependentTwo area=\"area\"/>\n" + "  </extensions>", areaInstance);
    ExtensionPoint extensionPoint = Extensions.getArea(areaInstance).getExtensionPoint("the.test.plugin.extensionPoint4area");
    assertNotNull(extensionPoint);
    assertSame(areaInstance, extensionPoint.getArea());
    assertNotNull(extensionPoint.getExtension());
    DependentObjectTwo dependentObjectTwo = (DependentObjectTwo) Extensions.getArea(areaInstance).getExtensionPoint("the.test.plugin.dependentTwo").getExtension();
    assertNotNull(dependentObjectTwo);
    assertSame(dependentObjectOne, dependentObjectTwo.getOne());
}
Also used : ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) AreaInstance(com.intellij.openapi.extensions.AreaInstance) Test(org.junit.Test)

Example 5 with ExtensionPoint

use of com.intellij.openapi.extensions.ExtensionPoint in project intellij-community by JetBrains.

the class MyTestInjector method injectVariousStuffEverywhere.

private static void injectVariousStuffEverywhere(Disposable parent, final PsiManager psiManager) {
    final Language ql = Language.findLanguageByID("JPAQL");
    final Language js = Language.findLanguageByID("JavaScript 1.6");
    final Language html = Language.findLanguageByID("HTML");
    if (ql == null || js == null)
        return;
    final Language ecma4 = Language.findLanguageByID("ECMA Script Level 4");
    final MultiHostInjector myMultiHostInjector = new MultiHostInjector() {

        @Override
        public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
            XmlAttributeValue value = (XmlAttributeValue) context;
            PsiElement parent = value.getParent();
            if (parent instanceof XmlAttribute) {
                @NonNls String attrName = ((XmlAttribute) parent).getLocalName();
                if ("jsInBraces".equals(attrName)) {
                    registrar.startInjecting(js);
                    String text = value.getText();
                    int index = 0;
                    while (text.indexOf('{', index) != -1) {
                        int lbrace = text.indexOf('{', index);
                        int rbrace = text.indexOf('}', index);
                        registrar.addPlace("", "", (PsiLanguageInjectionHost) value, new TextRange(lbrace + 1, rbrace));
                        index = rbrace + 1;
                    }
                    registrar.doneInjecting();
                }
            }
        }

        @Override
        @NotNull
        public List<? extends Class<? extends PsiElement>> elementsToInjectIn() {
            return Arrays.asList(XmlAttributeValue.class);
        }
    };
    InjectedLanguageManager.getInstance(psiManager.getProject()).registerMultiHostInjector(myMultiHostInjector);
    Disposer.register(parent, new Disposable() {

        @Override
        public void dispose() {
            boolean b = InjectedLanguageManager.getInstance(psiManager.getProject()).unregisterMultiHostInjector(myMultiHostInjector);
            assert b;
        }
    });
    final LanguageInjector myInjector = new LanguageInjector() {

        @Override
        public void getLanguagesToInject(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces placesToInject) {
            if (host instanceof XmlAttributeValue) {
                XmlAttributeValue value = (XmlAttributeValue) host;
                PsiElement parent = value.getParent();
                if (parent instanceof XmlAttribute) {
                    @NonNls String attrName = ((XmlAttribute) parent).getLocalName();
                    if ("ql".equals(attrName)) {
                        inject(host, placesToInject, ql);
                        return;
                    }
                    if ("js".equals(attrName)) {
                        inject(host, placesToInject, js);
                        return;
                    }
                    if ("jsprefix".equals(attrName)) {
                        inject(host, placesToInject, js, "function foo(doc, window){", "}");
                        return;
                    }
                }
            }
            if (host instanceof XmlText) {
                // inject to xml tags named 'ql'
                final XmlText xmlText = (XmlText) host;
                XmlTag tag = xmlText.getParentTag();
                if (tag == null)
                    return;
                if ("ql".equals(tag.getLocalName())) {
                    inject(host, placesToInject, ql);
                    return;
                }
                if ("js".equals(tag.getLocalName())) {
                    inject(host, placesToInject, js);
                    return;
                }
                if ("htmlInject".equals(tag.getLocalName())) {
                    inject(host, placesToInject, html);
                    return;
                }
                if (ecma4 != null && "ecma4".equals(tag.getLocalName())) {
                    inject(host, placesToInject, ecma4);
                    return;
                }
                if ("jsprefix".equals(tag.getLocalName())) {
                    inject(host, placesToInject, js, "function foo(doc, window){", "}");
                    return;
                }
                if ("jsInHash".equals(tag.getLocalName())) {
                    String text = xmlText.getText();
                    if (text.contains("#")) {
                        int start = text.indexOf('#');
                        int end = text.lastIndexOf('#');
                        if (start != end && start != -1) {
                            placesToInject.addPlace(js, new TextRange(start + 1, end), null, null);
                            return;
                        }
                    }
                }
            }
            if (host instanceof PsiComment && ((PsiComment) host).getTokenType() == JavaTokenType.C_STYLE_COMMENT) {
                /* {{{
           *   js code
           *   js code
           * }}}
           */
                String text = host.getText();
                String prefix = "/*\n * {{{\n";
                String suffix = " }}}\n */";
                if (text.startsWith(prefix) && text.endsWith(suffix)) {
                    String s = StringUtil.trimEnd(StringUtil.trimStart(text, prefix), suffix);
                    int off = 0;
                    while (!s.isEmpty()) {
                        String t = s.trim();
                        if (t.startsWith("*"))
                            t = t.substring(1).trim();
                        int i = s.length() - t.length();
                        off += i;
                        int endOfLine = t.indexOf('\n');
                        if (endOfLine == -1)
                            endOfLine = t.length();
                        placesToInject.addPlace(js, TextRange.from(prefix.length() + off, endOfLine), "", "\n");
                        off += endOfLine;
                        s = s.substring(i + endOfLine);
                    }
                    return;
                }
            }
            if (host instanceof PsiCommentImpl) {
                String text = host.getText();
                if (text.startsWith("/*--{") && text.endsWith("}--*/")) {
                    TextRange textRange = new TextRange(4, text.length() - 4);
                    if (!(host.getParent() instanceof PsiMethod))
                        return;
                    PsiMethod method = (PsiMethod) host.getParent();
                    if (!method.hasModifierProperty(PsiModifier.NATIVE) || !method.hasModifierProperty(PsiModifier.PUBLIC))
                        return;
                    String paramList = "";
                    for (PsiParameter parameter : method.getParameterList().getParameters()) {
                        if (!paramList.isEmpty())
                            paramList += ",";
                        paramList += parameter.getName();
                    }
                    @NonNls String header = "function " + method.getName() + "(" + paramList + ") {";
                    Language gwt = Language.findLanguageByID("GWT JavaScript");
                    placesToInject.addPlace(gwt, textRange, header, "}");
                    return;
                }
                PsiElement parent = host.getParent();
                if (parent instanceof PsiMethod && ((PsiMethod) parent).getName().equals("xml")) {
                    placesToInject.addPlace(StdLanguages.XML, new TextRange(2, host.getTextLength() - 2), null, null);
                    return;
                }
            }
            // inject to all string literal initializers of variables named 'ql'
            if (host instanceof PsiLiteralExpression && ((PsiLiteralExpression) host).getValue() instanceof String) {
                PsiVariable variable = PsiTreeUtil.getParentOfType(host, PsiVariable.class);
                if (variable == null)
                    return;
                if (host.getParent() instanceof PsiPolyadicExpression)
                    return;
                if ("ql".equals(variable.getName())) {
                    placesToInject.addPlace(ql, textRangeToInject(host), null, null);
                }
                if ("xml".equals(variable.getName())) {
                    placesToInject.addPlace(StdLanguages.XML, textRangeToInject(host), null, null);
                }
                if ("js".equals(variable.getName())) {
                    // with prefix/suffix
                    placesToInject.addPlace(js, textRangeToInject(host), "function foo(doc,window) {", "}");
                }
                if ("lang".equals(variable.getName())) {
                    // various lang depending on field "languageID" content
                    PsiClass aClass = PsiTreeUtil.getParentOfType(variable, PsiClass.class);
                    aClass = aClass.findInnerClassByName("Language", false);
                    String text = aClass.getInitializers()[0].getBody().getFirstBodyElement().getNextSibling().getText().substring(2);
                    Language language = Language.findLanguageByID(text);
                    if (language != null) {
                        placesToInject.addPlace(language, textRangeToInject(host), "", "");
                    }
                }
            }
        }
    };
    final ExtensionPoint<LanguageInjector> extensionPoint = Extensions.getRootArea().getExtensionPoint(LanguageInjector.EXTENSION_POINT_NAME);
    extensionPoint.registerExtension(myInjector);
    Disposer.register(parent, new Disposable() {

        @Override
        public void dispose() {
            extensionPoint.unregisterExtension(myInjector);
        }
    });
}
Also used : MultiHostInjector(com.intellij.lang.injection.MultiHostInjector) NotNull(org.jetbrains.annotations.NotNull) Language(com.intellij.lang.Language) JavaLanguage(com.intellij.lang.java.JavaLanguage) PsiCommentImpl(com.intellij.psi.impl.source.tree.PsiCommentImpl) Disposable(com.intellij.openapi.Disposable) NonNls(org.jetbrains.annotations.NonNls) ProperTextRange(com.intellij.openapi.util.ProperTextRange) TextRange(com.intellij.openapi.util.TextRange) ExtensionPoint(com.intellij.openapi.extensions.ExtensionPoint) MultiHostRegistrar(com.intellij.lang.injection.MultiHostRegistrar)

Aggregations

ExtensionPoint (com.intellij.openapi.extensions.ExtensionPoint)5 Project (com.intellij.openapi.project.Project)3 ExtensionsArea (com.intellij.openapi.extensions.ExtensionsArea)2 Configurable (com.intellij.openapi.options.Configurable)2 NotNull (org.jetbrains.annotations.NotNull)2 com.intellij.codeInsight.daemon (com.intellij.codeInsight.daemon)1 IdeaPluginDescriptor (com.intellij.ide.plugins.IdeaPluginDescriptor)1 Language (com.intellij.lang.Language)1 LanguageExtensionPoint (com.intellij.lang.LanguageExtensionPoint)1 MultiHostInjector (com.intellij.lang.injection.MultiHostInjector)1 MultiHostRegistrar (com.intellij.lang.injection.MultiHostRegistrar)1 JavaLanguage (com.intellij.lang.java.JavaLanguage)1 Disposable (com.intellij.openapi.Disposable)1 AnAction (com.intellij.openapi.actionSystem.AnAction)1 EditorSettingsExternalizable (com.intellij.openapi.editor.ex.EditorSettingsExternalizable)1 AreaInstance (com.intellij.openapi.extensions.AreaInstance)1 Extensions (com.intellij.openapi.extensions.Extensions)1 PluginDescriptor (com.intellij.openapi.extensions.PluginDescriptor)1 Module (com.intellij.openapi.module.Module)1 ConfigurationException (com.intellij.openapi.options.ConfigurationException)1