Search in sources :

Example 96 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class ImportFromToImportIntention method doInvoke.

@Override
public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    InfoHolder info = InfoHolder.collect(getElementFromEditor(editor, file));
    try {
        // we don't always qualify with module name
        String qualifier;
        sure(info.myModuleReference);
        sure(info.myModuleName);
        // [0] is remaining import path, [1] is imported module name
        String[] relative_names = null;
        if (info.myRelativeLevel > 0) {
            relative_names = getRelativeNames(true, info);
            if (relative_names == null)
                throw new IncorrectOperationException("failed to get relative names");
            qualifier = relative_names[1];
        } else
            qualifier = info.myModuleName;
        // find all unqualified references that lead to one of our import elements
        final PyImportElement[] ielts = info.myFromImportStatement.getImportElements();
        final PyStarImportElement star_ielt = info.myFromImportStatement.getStarImportElement();
        final Map<PsiReference, PyImportElement> references = new HashMap<>();
        final List<PsiReference> star_references = new ArrayList<>();
        PsiTreeUtil.processElements(file, new PsiElementProcessor() {

            public boolean execute(@NotNull PsiElement element) {
                PyPsiUtils.assertValid(element);
                if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
                    PyReferenceExpression ref = (PyReferenceExpression) element;
                    if (!ref.isQualified()) {
                        ResolveResult[] resolved = ref.getReference().multiResolve(false);
                        for (ResolveResult rr : resolved) {
                            if (rr.isValidResult()) {
                                if (rr.getElement() == star_ielt)
                                    star_references.add(ref.getReference());
                                for (PyImportElement ielt : ielts) {
                                    if (rr.getElement() == ielt)
                                        references.put(ref.getReference(), ielt);
                                }
                            }
                        }
                    }
                }
                return true;
            }
        });
        // check that at every replacement site our topmost qualifier name is visible
        PyQualifiedExpression top_qualifier;
        PyExpression feeler = info.myModuleReference;
        do {
            // if for some crazy reason module name refers to numbers, etc, no point to continue.
            sure(feeler instanceof PyQualifiedExpression);
            top_qualifier = (PyQualifiedExpression) feeler;
            feeler = top_qualifier.getQualifier();
        } while (feeler != null);
        String top_name = top_qualifier.getName();
        Collection<PsiReference> possible_targets = references.keySet();
        if (star_references.size() > 0) {
            possible_targets = new ArrayList<>(references.keySet().size() + star_references.size());
            possible_targets.addAll(references.keySet());
            possible_targets.addAll(star_references);
        }
        final Set<PsiElement> ignored = Sets.<PsiElement>newHashSet(Arrays.asList(info.myFromImportStatement.getImportElements()));
        if (top_name != null && showConflicts(project, findDefinitions(top_name, possible_targets, ignored), top_name, info.myModuleName)) {
            // got conflicts
            return;
        }
        // add qualifiers
        PyElementGenerator generator = PyElementGenerator.getInstance(project);
        for (Map.Entry<PsiReference, PyImportElement> entry : references.entrySet()) {
            PsiElement referring_elt = entry.getKey().getElement();
            // else we won't add it
            assert referring_elt.isValid();
            ASTNode target_node = referring_elt.getNode();
            // else it won't be valid
            assert target_node != null;
            PyImportElement ielt = entry.getValue();
            if (ielt.getAsNameElement() != null) {
                // we have an alias, replace it with real name
                PyReferenceExpression refex = ielt.getImportReferenceExpression();
                // else we won't resolve to this ielt
                assert refex != null;
                String real_name = refex.getReferencedName();
                ASTNode new_qualifier = generator.createExpressionFromText(real_name).getNode();
                assert new_qualifier != null;
                //ASTNode first_under_target = target_node.getFirstChildNode();
                //if (first_under_target != null) new_qualifier.addChildren(first_under_target, null, null); // save the children if any
                target_node.getTreeParent().replaceChild(target_node, new_qualifier);
                target_node = new_qualifier;
            }
            qualifyTarget(target_node, project, qualifier);
        }
        for (PsiReference reference : star_references) {
            PsiElement referring_elt = reference.getElement();
            // else we won't add it
            assert referring_elt.isValid();
            ASTNode target_node = referring_elt.getNode();
            // else it won't be valid
            assert target_node != null;
            qualifyTarget(target_node, project, qualifier);
        }
        // transform the import statement
        PyStatement new_import;
        if (info.myRelativeLevel == 0) {
            new_import = sure(generator.createFromText(LanguageLevel.getDefault(), PyImportStatement.class, "import " + info.myModuleName));
        } else {
            new_import = sure(generator.createFromText(LanguageLevel.getDefault(), PyFromImportStatement.class, "from " + relative_names[0] + " import " + relative_names[1]));
        }
        ASTNode parent = sure(info.myFromImportStatement.getParent().getNode());
        ASTNode old_node = sure(info.myFromImportStatement.getNode());
        parent.replaceChild(old_node, sure(new_import.getNode()));
    //myFromImportStatement.replace(new_import);
    } catch (IncorrectOperationException ignored) {
        PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) HashMap(com.intellij.util.containers.HashMap)

Example 97 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class RngNsDescriptor method convertElementDescriptors.

XmlElementDescriptor[] convertElementDescriptors(List<DElementPattern> patterns) {
    patterns = ContainerUtil.findAll(patterns, NamedPatternFilter.INSTANCE);
    final Map<QName, List<DElementPattern>> name2patterns = new HashMap<>();
    for (DElementPattern pattern : patterns) {
        for (QName qName : pattern.getName().listNames()) {
            List<DElementPattern> dPatterns = name2patterns.get(qName);
            if (dPatterns == null) {
                dPatterns = new ArrayList<>();
                name2patterns.put(qName, dPatterns);
            }
            if (!dPatterns.contains(pattern))
                dPatterns.add(pattern);
        }
    }
    final List<XmlElementDescriptor> result = new ArrayList<>();
    for (QName qName : name2patterns.keySet()) {
        final List<DElementPattern> patternList = name2patterns.get(qName);
        final XmlElementDescriptor descriptor = findDescriptor(qName, patternList);
        if (descriptor != null) {
            result.add(descriptor);
        }
    }
    return result.toArray(new XmlElementDescriptor[result.size()]);
}
Also used : HashMap(com.intellij.util.containers.HashMap) QName(javax.xml.namespace.QName) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) AnyXmlElementDescriptor(com.intellij.xml.impl.schema.AnyXmlElementDescriptor) DElementPattern(org.kohsuke.rngom.digested.DElementPattern)

Example 98 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AndroidDxWrapper method execute.

@SuppressWarnings({ "IOResourceOpenedButNotSafelyClosed" })
public static Map<AndroidCompilerMessageKind, List<String>> execute(@NotNull Module module, @NotNull IAndroidTarget target, @NotNull String outputDir, @NotNull String[] compileTargets, @NotNull String additionalVmParams, int maxHeapSize, boolean optimize) {
    BuildToolInfo buildToolInfo = target.getBuildToolInfo();
    if (buildToolInfo == null) {
        return Collections.singletonMap(AndroidCompilerMessageKind.ERROR, Collections.singletonList("No Build Tools in the Android SDK."));
    }
    String outFile = outputDir + File.separatorChar + AndroidCommonUtils.CLASSES_FILE_NAME;
    final Map<AndroidCompilerMessageKind, List<String>> messages = new HashMap<AndroidCompilerMessageKind, List<String>>(2);
    messages.put(AndroidCompilerMessageKind.ERROR, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.INFORMATION, new ArrayList<String>());
    messages.put(AndroidCompilerMessageKind.WARNING, new ArrayList<String>());
    String dxJarPath = buildToolInfo.getPath(BuildToolInfo.PathId.DX_JAR);
    File dxJar = new File(dxJarPath);
    if (!dxJar.isFile()) {
        messages.get(AndroidCompilerMessageKind.ERROR).add(AndroidBundle.message("android.file.not.exist.error", dxJarPath));
        return messages;
    }
    JavaParameters parameters = new JavaParameters();
    Sdk sdk = ModuleRootManager.getInstance(module).getSdk();
    // dex runs after simple java compilation, so JDK must be specified
    assert sdk != null;
    parameters.setJdk(sdk);
    parameters.setMainClass(AndroidDxRunner.class.getName());
    ParametersList programParamList = parameters.getProgramParametersList();
    programParamList.add(dxJarPath);
    programParamList.add(outFile);
    programParamList.add("--optimize", Boolean.toString(optimize));
    programParamList.addAll(compileTargets);
    programParamList.add("--exclude");
    ParametersList vmParamList = parameters.getVMParametersList();
    if (additionalVmParams.length() > 0) {
        vmParamList.addParametersString(additionalVmParams);
    }
    if (!AndroidCommonUtils.hasXmxParam(vmParamList.getParameters())) {
        vmParamList.add("-Xmx" + maxHeapSize + "M");
    }
    final PathsList classPath = parameters.getClassPath();
    classPath.add(PathUtil.getJarPathForClass(AndroidDxRunner.class));
    classPath.add(PathUtil.getJarPathForClass(FileUtilRt.class));
    // delete file to check if it will exist after dex compilation
    if (!new File(outFile).delete()) {
        LOG.info("Cannot delete file " + outFile);
    }
    Process process;
    try {
        parameters.setUseDynamicClasspath(true);
        GeneralCommandLine commandLine = parameters.toCommandLine();
        LOG.info(commandLine.getCommandLineString());
        process = commandLine.createProcess();
        AndroidCommonUtils.handleDexCompilationResult(process, commandLine.getCommandLineString(), outFile, messages, false);
    } catch (ExecutionException e) {
        messages.get(AndroidCompilerMessageKind.ERROR).add("ExecutionException: " + e.getMessage());
        LOG.info(e);
        return messages;
    }
    return messages;
}
Also used : BuildToolInfo(com.android.sdklib.BuildToolInfo) HashMap(com.intellij.util.containers.HashMap) AndroidCompilerMessageKind(org.jetbrains.android.util.AndroidCompilerMessageKind) PathsList(com.intellij.util.PathsList) PathsList(com.intellij.util.PathsList) ArrayList(java.util.ArrayList) List(java.util.List) FileUtilRt(com.intellij.openapi.util.io.FileUtilRt) Sdk(com.intellij.openapi.projectRoots.Sdk) ExecutionException(com.intellij.execution.ExecutionException) File(java.io.File)

Example 99 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class SubtagsProcessingUtil method registerClassNameSubtags.

private static void registerClassNameSubtags(XmlTag tag, Map<String, PsiClass> classMap, Type type, SubtagProcessor subtagProcessor) {
    final Set<String> allAllowedTags = new HashSet<>();
    final Map<String, String> class2Name = new HashMap<>();
    for (Map.Entry<String, PsiClass> entry : classMap.entrySet()) {
        final String tagName = entry.getKey();
        final PsiClass aClass = entry.getValue();
        if (!AndroidUtils.isAbstract(aClass)) {
            allAllowedTags.add(tagName);
            final String qName = aClass.getQualifiedName();
            final String prevTagName = class2Name.get(qName);
            if (prevTagName == null || tagName.indexOf('.') == -1) {
                class2Name.put(qName, tagName);
            }
        }
    }
    registerSubtags(tag, allAllowedTags, class2Name.values(), type, subtagProcessor);
}
Also used : HashMap(com.intellij.util.containers.HashMap) PsiClass(com.intellij.psi.PsiClass) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 100 with HashMap

use of com.intellij.util.containers.HashMap in project android by JetBrains.

the class AttributeDefinitionsImpl method addAttrsFromFile.

private void addAttrsFromFile(XmlFile file) {
    Map<StyleableDefinitionImpl, String[]> parentMap = new HashMap<>();
    final XmlDocument document = file.getDocument();
    if (document == null)
        return;
    final XmlTag rootTag = document.getRootTag();
    if (rootTag == null || !TAG_RESOURCES.equals(rootTag.getName()))
        return;
    String attrGroup = null;
    for (XmlTag tag : rootTag.getSubTags()) {
        String tagName = tag.getName();
        if (TAG_ATTR.equals(tagName)) {
            AttributeDefinition def = parseAttrTag(tag, null);
            // Sets group for attribute, for example: sets "Button Styles" group for "buttonStyleSmall" attribute
            if (def != null) {
                def.setAttrGroup(attrGroup);
            }
        } else if (TAG_DECLARE_STYLEABLE.equals(tagName)) {
            StyleableDefinitionImpl def = parseDeclareStyleableTag(tag, parentMap);
            // Only "Theme" Styleable has attribute groups
            if (def != null && def.getName().equals("Theme")) {
                parseAndAddAttrGroups(tag);
            }
        } else if (TAG_EAT_COMMENT.equals(tagName)) {
            // The framework attribute file follows a special convention where related attributes are grouped together,
            // and there is always a set of comments that indicate these sections which look like this:
            //     <!-- =========== -->
            //     <!-- Text styles -->
            //     <!-- =========== -->
            //     <eat-comment />
            // These section headers are always immediately followed by an <eat-comment>,
            // so to identify these we just look for <eat-comments>, and then we look for the comment within the block that isn't ascii art.
            String newAttrGroup = getCommentBeforeEatComment(tag);
            // We identify these by looking at the line length; category comments are short, and descriptive comments are longer
            if (newAttrGroup != null && newAttrGroup.length() <= ATTR_GROUP_MAX_CHARACTERS) {
                attrGroup = newAttrGroup;
            }
        }
    }
    for (Map.Entry<StyleableDefinitionImpl, String[]> entry : parentMap.entrySet()) {
        StyleableDefinitionImpl definition = entry.getKey();
        String[] parentNames = entry.getValue();
        for (String parentName : parentNames) {
            StyleableDefinitionImpl parent = getStyleableByName(parentName);
            if (parent != null) {
                definition.addParent(parent);
                parent.addChild(definition);
            } else {
                LOG.info("Found tag with unknown parent: " + parentName);
            }
        }
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) XmlDocument(com.intellij.psi.xml.XmlDocument) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

HashMap (com.intellij.util.containers.HashMap)118 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 File (java.io.File)22 Nullable (org.jetbrains.annotations.Nullable)18 Map (java.util.Map)17 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)15 ArrayList (java.util.ArrayList)14 PsiElement (com.intellij.psi.PsiElement)11 HashSet (com.intellij.util.containers.HashSet)10 List (java.util.List)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 IOException (java.io.IOException)8 Pair (com.intellij.openapi.util.Pair)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 SearchScope (com.intellij.psi.search.SearchScope)6 Logger (com.intellij.openapi.diagnostic.Logger)5 PsiFile (com.intellij.psi.PsiFile)5 UsageInfo (com.intellij.usageView.UsageInfo)5