Search in sources :

Example 1 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class GroovyCodeFragmentFactory method externalParameters.

public static Pair<Map<String, String>, GroovyFile> externalParameters(String text, @NotNull final PsiElement context) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(context.getProject());
    final GroovyFile toEval = factory.createGroovyFile(text, false, context);
    final GrClosableBlock closure = PsiTreeUtil.getParentOfType(context, GrClosableBlock.class);
    final Map<String, String> parameters = new THashMap<>();
    final Map<GrExpression, String> replacements = new HashMap<>();
    toEval.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
            super.visitReferenceExpression(referenceExpression);
            if (PsiUtil.isThisReference(referenceExpression) || PsiUtil.isSuperReference(referenceExpression)) {
                replaceWithReference(referenceExpression, "delegate");
                return;
            }
            PsiElement resolved = referenceExpression.resolve();
            if (resolved instanceof PsiMember && (resolved instanceof PsiClass || ((PsiMember) resolved).hasModifierProperty(PsiModifier.STATIC))) {
                String qName = com.intellij.psi.util.PsiUtil.getMemberQualifiedName((PsiMember) resolved);
                if (qName != null && qName.contains(".") && !referenceExpression.isQualified()) {
                    replaceWithReference(referenceExpression, qName);
                    return;
                }
            }
            if (shouldDelegate(referenceExpression, resolved)) {
                replaceWithReference(referenceExpression, "delegate." + referenceExpression.getReferenceName());
                return;
            }
            if (resolved instanceof GrVariable && !(resolved instanceof GrField) && !PsiTreeUtil.isAncestor(toEval, resolved, false)) {
                final String name = ((GrVariable) resolved).getName();
                if (resolved instanceof ClosureSyntheticParameter && PsiTreeUtil.isAncestor(toEval, ((ClosureSyntheticParameter) resolved).getClosure(), false)) {
                    return;
                }
                if (resolved instanceof GrBindingVariable && !PsiTreeUtil.isAncestor(resolved.getContainingFile(), toEval, false)) {
                    return;
                }
                String value;
                if (closure != null && PsiTreeUtil.findCommonParent(resolved, closure) != closure && !(resolved instanceof ClosureSyntheticParameter)) {
                    // Evaluating inside closure for outer variable definitions
                    // All non-local variables are accessed by references
                    value = "this." + name;
                } else {
                    value = name;
                }
                parameters.put(name, value);
                return;
            }
            if (resolved instanceof PsiLocalVariable || resolved instanceof PsiParameter && !(resolved instanceof GrParameter)) {
                String name = referenceExpression.getReferenceName();
                parameters.put(name, name);
            }
        }

        private boolean shouldDelegate(GrReferenceExpression referenceExpression, @Nullable PsiElement resolved) {
            if (referenceExpression.isQualified()) {
                return false;
            }
            if (resolved instanceof GrField) {
                return true;
            }
            if (resolved instanceof PsiMethod) {
                String methodName = ((PsiMethod) resolved).getName();
                if (closure != null && "getDelegate".equals(methodName) || "call".equals(methodName)) {
                    return true;
                }
            }
            return closure != null && resolved instanceof GrLightVariable && "owner".equals(((GrLightVariable) resolved).getName());
        }

        private void replaceWithReference(GrExpression expr, final String exprText) {
            replacements.put(expr, exprText);
        }

        @Override
        public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
            super.visitCodeReferenceElement(refElement);
            if (refElement.getQualifier() == null) {
                PsiElement resolved = refElement.resolve();
                if (resolved instanceof PsiClass) {
                    String qName = ((PsiClass) resolved).getQualifiedName();
                    if (qName != null) {
                        int dotIndex = qName.lastIndexOf(".");
                        if (dotIndex < 0)
                            return;
                        String packageName = qName.substring(0, dotIndex);
                        refElement.setQualifier(factory.createReferenceElementFromText(packageName));
                    }
                }
            }
        }
    });
    for (GrExpression expression : replacements.keySet()) {
        expression.replaceWithExpression(factory.createExpressionFromText(replacements.get(expression)), false);
    }
    return Pair.create(parameters, toEval);
}
Also used : GrField(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField) THashMap(gnu.trove.THashMap) GrParameter(org.jetbrains.plugins.groovy.lang.psi.api.statements.params.GrParameter) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) THashMap(gnu.trove.THashMap) GrBindingVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrBindingVariable) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) ClosureSyntheticParameter(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.ClosureSyntheticParameter) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) GroovyPsiElementFactory(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory) GrCodeReferenceElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement) GrLightVariable(org.jetbrains.plugins.groovy.lang.psi.impl.synthetic.GrLightVariable) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 2 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class MavenArtifactSearcher method processResults.

protected Collection<MavenArtifactSearchResult> processResults(Set<MavenArtifactInfo> infos, String pattern, int maxResult) {
    Map<String, MavenArtifactSearchResult> result = new THashMap<>();
    for (MavenArtifactInfo each : infos) {
        if (!StringUtil.isEmptyOrSpaces(each.getClassifier())) {
            // todo skip for now
            continue;
        }
        String key = makeKey(each);
        MavenArtifactSearchResult searchResult = result.get(key);
        if (searchResult == null) {
            searchResult = new MavenArtifactSearchResult();
            result.put(key, searchResult);
        }
        searchResult.versions.add(each);
    }
    return result.values();
}
Also used : THashMap(gnu.trove.THashMap) MavenArtifactInfo(org.jetbrains.idea.maven.model.MavenArtifactInfo)

Example 3 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class GetCompositeCollectionInvocation method invoke.

@Override
public Object invoke(final DomInvocationHandler<?, ?> handler, final Object[] args) throws Throwable {
    Map<XmlTag, DomElement> map = new THashMap<>();
    for (final CollectionChildDescriptionImpl qname : myQnames) {
        for (DomElement element : handler.getCollectionChildren(qname)) {
            map.put(element.getXmlTag(), element);
        }
    }
    final XmlTag tag = handler.getXmlTag();
    if (tag == null)
        return Collections.emptyList();
    final List<DomElement> list = new ArrayList<>();
    for (final XmlTag subTag : tag.getSubTags()) {
        ContainerUtil.addIfNotNull(list, map.get(subTag));
    }
    return list;
}
Also used : DomElement(com.intellij.util.xml.DomElement) THashMap(gnu.trove.THashMap) XmlTag(com.intellij.psi.xml.XmlTag)

Example 4 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class DomElementsProblemsHolderImpl method getProblemsMap.

@NotNull
private Map<Class<? extends DomElementsInspection>, List<DomElementProblemDescriptor>> getProblemsMap(final DomElement domElement) {
    final Map<Class<? extends DomElementsInspection>, List<DomElementProblemDescriptor>> map = myCachedChildrenErrors.get(domElement);
    if (map != null) {
        return map;
    }
    final Map<Class<? extends DomElementsInspection>, List<DomElementProblemDescriptor>> problems = new THashMap<>();
    if (domElement == myElement) {
        for (Map<Class<? extends DomElementsInspection>, List<DomElementProblemDescriptor>> listMap : myCachedErrors.values()) {
            mergeMaps(problems, listMap);
        }
    } else {
        mergeMaps(problems, myCachedErrors.get(domElement));
        if (DomUtil.hasXml(domElement)) {
            domElement.acceptChildren(new DomElementVisitor() {

                @Override
                public void visitDomElement(DomElement element) {
                    mergeMaps(problems, getProblemsMap(element));
                }
            });
        }
    }
    myCachedChildrenErrors.put(domElement, problems);
    return problems;
}
Also used : DomElement(com.intellij.util.xml.DomElement) THashMap(gnu.trove.THashMap) DomElementVisitor(com.intellij.util.xml.DomElementVisitor) SmartList(com.intellij.util.SmartList) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with THashMap

use of gnu.trove.THashMap in project intellij-community by JetBrains.

the class JavaI18nizeQuickFixDialog method getI18nizedText.

public String getI18nizedText() {
    String propertyKey = StringUtil.escapeStringCharacters(getKey());
    I18nizedTextGenerator textGenerator = myResourceBundleManager.getI18nizedTextGenerator();
    if (textGenerator != null) {
        return generateText(textGenerator, propertyKey, getPropertiesFile(), myLiteralExpression);
    }
    String templateName = getTemplateName();
    LOG.assertTrue(templateName != null);
    FileTemplate template = FileTemplateManager.getInstance(myProject).getCodeTemplate(templateName);
    Map<String, String> attributes = new THashMap<>();
    attributes.put(PROPERTY_KEY_OPTION_KEY, propertyKey);
    attributes.put(RESOURCE_BUNDLE_OPTION_KEY, getResourceBundleText());
    attributes.put(PROPERTY_VALUE_ATTR, StringUtil.escapeStringCharacters(myDefaultPropertyValue));
    addAdditionalAttributes(attributes);
    String text = null;
    try {
        text = template.getText(attributes);
    } catch (IOException e) {
        LOG.error(e);
    }
    return text;
}
Also used : I18nizedTextGenerator(com.intellij.lang.properties.psi.I18nizedTextGenerator) THashMap(gnu.trove.THashMap) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) IOException(java.io.IOException)

Aggregations

THashMap (gnu.trove.THashMap)132 NotNull (org.jetbrains.annotations.NotNull)33 THashSet (gnu.trove.THashSet)27 VirtualFile (com.intellij.openapi.vfs.VirtualFile)19 Element (org.jdom.Element)18 PsiElement (com.intellij.psi.PsiElement)17 IOException (java.io.IOException)17 File (java.io.File)15 Map (java.util.Map)11 Nullable (org.jetbrains.annotations.Nullable)10 Project (com.intellij.openapi.project.Project)9 List (java.util.List)9 Module (com.intellij.openapi.module.Module)8 Pair (com.intellij.openapi.util.Pair)7 PsiFile (com.intellij.psi.PsiFile)7 ArrayList (java.util.ArrayList)7 java.util (java.util)6 ApplicationManager (com.intellij.openapi.application.ApplicationManager)5 Document (com.intellij.openapi.editor.Document)5 ModifiableRootModel (com.intellij.openapi.roots.ModifiableRootModel)5