Search in sources :

Example 11 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class MavenDuplicatePluginInspection method checkFileElement.

@Override
public void checkFileElement(DomFileElement<MavenDomProjectModel> domFileElement, DomElementAnnotationHolder holder) {
    MavenDomProjectModel projectModel = domFileElement.getRootElement();
    MultiMap<Pair<String, String>, MavenDomPlugin> duplicates = MultiMap.createSet();
    for (MavenDomPlugin plugin : projectModel.getBuild().getPlugins().getPlugins()) {
        String groupId = plugin.getGroupId().getStringValue();
        String artifactId = plugin.getArtifactId().getStringValue();
        if (StringUtil.isEmptyOrSpaces(artifactId))
            continue;
        if ("".equals(groupId) || "org.apache.maven.plugins".equals(groupId) || "org.codehaus.mojo".equals(groupId)) {
            groupId = null;
        }
        duplicates.putValue(Pair.create(groupId, artifactId), plugin);
    }
    for (Map.Entry<Pair<String, String>, Collection<MavenDomPlugin>> entry : duplicates.entrySet()) {
        Collection<MavenDomPlugin> set = entry.getValue();
        if (set.size() <= 1)
            continue;
        for (MavenDomPlugin dependency : set) {
            holder.createProblem(dependency, HighlightSeverity.WARNING, "Duplicated plugin declaration");
        }
    }
}
Also used : MavenDomProjectModel(org.jetbrains.idea.maven.dom.model.MavenDomProjectModel) MavenDomPlugin(org.jetbrains.idea.maven.dom.model.MavenDomPlugin) Collection(java.util.Collection) Map(java.util.Map) MultiMap(com.intellij.util.containers.MultiMap) Pair(com.intellij.openapi.util.Pair)

Example 12 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class XsltCodeInsightUtil method getRangeInsideHost.

@NotNull
public static TextRange getRangeInsideHost(XPathElement expr) {
    final PsiLanguageInjectionHost host = PsiTreeUtil.getContextOfType(expr, PsiLanguageInjectionHost.class, true);
    assert host != null;
    final List<Pair<PsiElement, TextRange>> psi = InjectedLanguageManager.getInstance(host.getProject()).getInjectedPsiFiles(host);
    assert psi != null;
    for (Pair<PsiElement, TextRange> pair : psi) {
        if (PsiTreeUtil.isAncestor(pair.first, expr, false)) {
            return pair.second;
        }
    }
    assert false;
    return null;
}
Also used : PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class DeclarationConflictChecker method findDefinitions.

/**
   * For each reference in the collection, finds a definition of name visible from the point of the reference. Returns a list of
   * such definitions.
   * @param name what to look for.
   * @param references references to check.
   * @param ignored if an element defining the name is also listed here, ignore it.
   * @return a list of pairs (referring element, element that defines name).
   */
@NotNull
public static List<Pair<PsiElement, PsiElement>> findDefinitions(@NotNull String name, @NotNull Collection<PsiReference> references, @NotNull Set<PsiElement> ignored) {
    final List<Pair<PsiElement, PsiElement>> conflicts = new ArrayList<>();
    for (PsiReference ref : references) {
        final PsiElement refElement = ref.getElement();
        final ScopeOwner owner = ScopeUtil.getScopeOwner(refElement);
        if (owner != null) {
            for (PsiElement element : PyResolveUtil.resolveLocally(owner, name)) {
                if (!ignored.contains(element)) {
                    conflicts.add(Pair.create(refElement, element));
                }
            }
        }
    }
    return conflicts;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class ImportToggleAliasIntention method doInvoke.

@Override
public void doInvoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    // sanity check: isAvailable must have set it.
    final IntentionState state = IntentionState.fromContext(editor, file);
    //
    // we set in in the source
    final String target_name;
    // we replace it in the source
    final String remove_name;
    PyReferenceExpression reference = sure(state.myImportElement.getImportReferenceExpression());
    // search for references to us with the right name
    try {
        String imported_name = PyPsiUtils.toPath(reference);
        if (state.myAlias != null) {
            // have to remove alias, rename everything to original
            target_name = imported_name;
            remove_name = state.myAlias;
        } else {
            // ask for and add alias
            Application application = ApplicationManager.getApplication();
            if (application != null && !application.isUnitTestMode()) {
                String alias = Messages.showInputDialog(project, PyBundle.message("INTN.alias.for.$0.dialog.title", imported_name), "Add Alias", Messages.getQuestionIcon(), "", new InputValidator() {

                    @Override
                    public boolean checkInput(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }

                    @Override
                    public boolean canClose(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }
                });
                if (alias == null) {
                    return;
                }
                target_name = alias;
            } else {
                // test mode
                target_name = "alias";
            }
            remove_name = imported_name;
        }
        final PsiElement referee = reference.getReference().resolve();
        if (referee != null && imported_name != null) {
            final Collection<PsiReference> references = new ArrayList<>();
            final ScopeOwner scope = PsiTreeUtil.getParentOfType(state.myImportElement, ScopeOwner.class);
            PsiTreeUtil.processElements(scope, new PsiElementProcessor() {

                public boolean execute(@NotNull PsiElement element) {
                    getReferences(element);
                    if (element instanceof PyStringLiteralExpression) {
                        final PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) element;
                        final List<Pair<PsiElement, TextRange>> files = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(host);
                        if (files != null) {
                            for (Pair<PsiElement, TextRange> pair : files) {
                                final PsiElement first = pair.getFirst();
                                if (first instanceof ScopeOwner) {
                                    final ScopeOwner scopeOwner = (ScopeOwner) first;
                                    PsiTreeUtil.processElements(scopeOwner, new PsiElementProcessor() {

                                        public boolean execute(@NotNull PsiElement element) {
                                            getReferences(element);
                                            return true;
                                        }
                                    });
                                }
                            }
                        }
                    }
                    return true;
                }

                private void getReferences(PsiElement element) {
                    if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
                        PyReferenceExpression ref = (PyReferenceExpression) element;
                        if (remove_name.equals(PyPsiUtils.toPath(ref))) {
                            // filter out other names that might resolve to our target
                            PsiElement resolved = ref.getReference().resolve();
                            if (resolved == referee)
                                references.add(ref.getReference());
                        }
                    }
                }
            });
            // no references here is OK by us.
            if (showConflicts(project, findDefinitions(target_name, references, Collections.<PsiElement>emptySet()), target_name, null)) {
                // got conflicts
                return;
            }
            // alter the import element
            PyElementGenerator generator = PyElementGenerator.getInstance(project);
            final LanguageLevel languageLevel = LanguageLevel.forElement(state.myImportElement);
            if (state.myAlias != null) {
                // remove alias
                ASTNode node = sure(state.myImportElement.getNode());
                ASTNode parent = sure(node.getTreeParent());
                // this is the reference
                node = sure(node.getFirstChildNode());
                // things past the reference: space, 'as', and alias
                node = sure(node.getTreeNext());
                parent.removeRange(node, null);
            } else {
                // add alias
                ASTNode my_ielt_node = sure(state.myImportElement.getNode());
                PyImportElement fountain = generator.createFromText(languageLevel, PyImportElement.class, "import foo as " + target_name, new int[] { 0, 2 });
                // at import elt
                ASTNode graft_node = sure(fountain.getNode());
                // at ref
                graft_node = sure(graft_node.getFirstChildNode());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // 'as'
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // alias
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
            }
            // alter references
            for (PsiReference ref : references) {
                ASTNode ref_name_node = sure(sure(ref.getElement()).getNode());
                ASTNode parent = sure(ref_name_node.getTreeParent());
                ASTNode new_name_node = generator.createExpressionFromText(languageLevel, target_name).getNode();
                assert new_name_node != null;
                parent.replaceChild(ref_name_node, new_name_node);
            }
        }
    } catch (IncorrectOperationException ignored) {
        PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
    }
}
Also used : ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) InputValidator(com.intellij.openapi.ui.InputValidator) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Application(com.intellij.openapi.application.Application) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 15 with Pair

use of com.intellij.openapi.util.Pair in project intellij-community by JetBrains.

the class XmlUnwrapDescriptor method collectUnwrappers.

@Override
public List<Pair<PsiElement, Unwrapper>> collectUnwrappers(Project project, Editor editor, PsiFile file) {
    int offset = editor.getCaretModel().getOffset();
    PsiElement e1 = file.findElementAt(offset);
    if (e1 != null) {
        Language language = e1.getParent().getLanguage();
        if (language != file.getLanguage()) {
            UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
            if (unwrapDescriptor != null && !(unwrapDescriptor instanceof XmlUnwrapDescriptor)) {
                return unwrapDescriptor.collectUnwrappers(project, editor, file);
            }
        }
    }
    List<Pair<PsiElement, Unwrapper>> result = new ArrayList<>();
    FileViewProvider viewProvider = file.getViewProvider();
    for (Language language : viewProvider.getLanguages()) {
        UnwrapDescriptor unwrapDescriptor = LanguageUnwrappers.INSTANCE.forLanguage(language);
        if (unwrapDescriptor instanceof XmlUnwrapDescriptor) {
            PsiElement e = viewProvider.findElementAt(offset, language);
            PsiElement tag = PsiTreeUtil.getParentOfType(e, XmlTag.class);
            while (tag != null) {
                if (XmlChildRole.START_TAG_NAME_FINDER.findChild(tag.getNode()) != null) {
                    // Exclude implicit tags suck as 'jsp:root'
                    result.add(new Pair<>(tag, new XmlEnclosingTagUnwrapper()));
                }
                tag = PsiTreeUtil.getParentOfType(tag, XmlTag.class);
            }
        }
    }
    Collections.sort(result, (o1, o2) -> o2.first.getTextOffset() - o1.first.getTextOffset());
    return result;
}
Also used : Language(com.intellij.lang.Language) FileViewProvider(com.intellij.psi.FileViewProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) UnwrapDescriptor(com.intellij.codeInsight.unwrap.UnwrapDescriptor) Pair(com.intellij.openapi.util.Pair) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

Pair (com.intellij.openapi.util.Pair)391 NotNull (org.jetbrains.annotations.NotNull)131 ArrayList (java.util.ArrayList)83 VirtualFile (com.intellij.openapi.vfs.VirtualFile)68 Project (com.intellij.openapi.project.Project)60 Nullable (org.jetbrains.annotations.Nullable)59 PsiElement (com.intellij.psi.PsiElement)47 TextRange (com.intellij.openapi.util.TextRange)43 File (java.io.File)42 List (java.util.List)37 Module (com.intellij.openapi.module.Module)34 ContainerUtil (com.intellij.util.containers.ContainerUtil)26 PsiFile (com.intellij.psi.PsiFile)25 StringUtil (com.intellij.openapi.util.text.StringUtil)20 IOException (java.io.IOException)19 java.util (java.util)19 ApplicationManager (com.intellij.openapi.application.ApplicationManager)18 THashSet (gnu.trove.THashSet)17 Map (java.util.Map)17 Document (com.intellij.openapi.editor.Document)15