Search in sources :

Example 41 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class MissingTranslationsInspectionProvider method check.

@Override
public void check(BidirectionalMap<PropertiesFile, PropertiesFile> parents, List<PropertiesFile> files, Map<PropertiesFile, Set<String>> keysUpToParent, Map<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps, InspectionManager manager, RefManager refManager, ProblemDescriptionsProcessor processor) {
    for (PropertiesFile file : files) {
        PropertiesFile parent = parents.get(file);
        if (parent == null)
            continue;
        List<PropertiesFile> children = parents.getKeysByValue(file);
        boolean isLeaf = children == null || children.isEmpty();
        if (!isLeaf)
            continue;
        Set<String> keys = propertiesFilesNamesMaps.get(file).keySet();
        Set<String> parentKeys = new THashSet<>(keysUpToParent.get(parent));
        if (parent.getLocale().getLanguage().equals(file.getLocale().getLanguage())) {
            // properties can be left untranslated in the dialect files
            keys = new THashSet<>(keys);
            keys.addAll(propertiesFilesNamesMaps.get(parent).keySet());
            parent = parents.get(parent);
            if (parent == null)
                continue;
            parentKeys = new THashSet<>(keysUpToParent.get(parent));
        }
        parentKeys.removeAll(keys);
        for (String untranslatedKey : parentKeys) {
            IProperty untranslatedProperty = null;
            PropertiesFile untranslatedFile = parent;
            while (untranslatedFile != null) {
                untranslatedProperty = untranslatedFile.findPropertyByKey(untranslatedKey);
                if (untranslatedProperty != null)
                    break;
                untranslatedFile = parents.get(untranslatedFile);
            }
            assert untranslatedProperty != null;
            String message = InspectionsBundle.message("inconsistent.bundle.untranslated.property.error", untranslatedKey, file.getName());
            ProblemDescriptor descriptor = manager.createProblemDescriptor(untranslatedProperty.getPsiElement(), message, false, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            processor.addProblemElement(refManager.getReference(untranslatedFile.getContainingFile()), descriptor);
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashSet(gnu.trove.THashSet)

Example 42 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class PropertiesKeysConsistencyInspectionProvider method check.

@Override
public void check(BidirectionalMap<PropertiesFile, PropertiesFile> parents, List<PropertiesFile> files, Map<PropertiesFile, Set<String>> keysUpToParent, Map<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps, InspectionManager manager, RefManager refManager, ProblemDescriptionsProcessor processor) {
    for (PropertiesFile file : files) {
        PropertiesFile parent = parents.get(file);
        Set<String> parentKeys = keysUpToParent.get(parent);
        if (parent == null) {
            parentKeys = new THashSet<>();
            for (PropertiesFile otherTopLevelFile : files) {
                if (otherTopLevelFile != file && parents.get(otherTopLevelFile) == null) {
                    parent = otherTopLevelFile;
                    parentKeys.addAll(propertiesFilesNamesMaps.get(otherTopLevelFile).keySet());
                }
            }
            if (parent == null)
                continue;
        }
        Set<String> keys = new THashSet<>(propertiesFilesNamesMaps.get(file).keySet());
        keys.removeAll(parentKeys);
        for (String inconsistentKey : keys) {
            IProperty property = file.findPropertyByKey(inconsistentKey);
            assert property != null;
            String message = InspectionsBundle.message("inconsistent.bundle.property.error", inconsistentKey, parent.getName());
            ProblemDescriptor descriptor = manager.createProblemDescriptor(property.getPsiElement(), message, false, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            processor.addProblemElement(refManager.getReference(file.getContainingFile()), descriptor);
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashSet(gnu.trove.THashSet)

Example 43 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class PropertiesPlaceholdersInspectionProvider method check.

@Override
public void check(BidirectionalMap<PropertiesFile, PropertiesFile> parents, List<PropertiesFile> files, Map<PropertiesFile, Set<String>> keysUpToParent, Map<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps, InspectionManager manager, RefManager refManager, ProblemDescriptionsProcessor processor) {
    for (PropertiesFile file : files) {
        final Set<String> filePropertyKeys = new THashSet<>(propertiesFilesNamesMaps.get(file).keySet());
        PropertiesFile parent = parents.get(file);
        while (parent != null) {
            final Collection<String> commonKeys = ContainerUtil.intersection(propertiesFilesNamesMaps.get(parent).keySet(), filePropertyKeys);
            for (final String commonKey : commonKeys) {
                final IProperty property = file.findPropertyByKey(commonKey);
                assert property != null;
                final String propertyValue = property.getValue();
                if (propertyValue == null) {
                    continue;
                }
                final String parentPropertyValue = propertiesFilesNamesMaps.get(parent).get(commonKey);
                if (parentPropertyValue == null) {
                    continue;
                }
                final int occurrences = JavaI18nUtil.getPropertyValuePlaceholdersCount(propertyValue);
                final int parentOccurrences = JavaI18nUtil.getPropertyValuePlaceholdersCount(parentPropertyValue);
                if (occurrences != parentOccurrences) {
                    final String problemDescriptorString = InspectionsBundle.message("inconsistent.bundle.property.inconsistent.placeholders", parentOccurrences, parent.getName());
                    final PsiElement propertyPsiElement = property.getPsiElement();
                    processor.addProblemElement(refManager.getReference(file.getContainingFile()), manager.createProblemDescriptor(propertyPsiElement, problemDescriptorString, true, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                }
            }
            filePropertyKeys.removeAll(commonKeys);
            parent = parents.get(parent);
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashSet(gnu.trove.THashSet) PsiElement(com.intellij.psi.PsiElement)

Example 44 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class PropertyFoldingBuilder method getI18nProperty.

@Nullable
public static IProperty getI18nProperty(PsiLiteralExpression literal) {
    final Property property = (Property) literal.getUserData(CACHE);
    if (property == NULL)
        return null;
    if (property != null && isValid(property, literal))
        return property;
    if (isI18nProperty(literal)) {
        final PsiReference[] references = literal.getReferences();
        for (PsiReference reference : references) {
            if (reference instanceof PsiPolyVariantReference) {
                final ResolveResult[] results = ((PsiPolyVariantReference) reference).multiResolve(false);
                for (ResolveResult result : results) {
                    final PsiElement element = result.getElement();
                    if (element instanceof IProperty) {
                        IProperty p = (IProperty) element;
                        literal.putUserData(CACHE, p);
                        return p;
                    }
                }
            } else {
                final PsiElement element = reference.resolve();
                if (element instanceof IProperty) {
                    IProperty p = (IProperty) element;
                    literal.putUserData(CACHE, p);
                    return p;
                }
            }
        }
    }
    return null;
}
Also used : IProperty(com.intellij.lang.properties.IProperty) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with IProperty

use of com.intellij.lang.properties.IProperty in project intellij-community by JetBrains.

the class DuplicatedPropertiesInspectionProvider method check.

@Override
public void check(BidirectionalMap<PropertiesFile, PropertiesFile> parents, List<PropertiesFile> files, Map<PropertiesFile, Set<String>> keysUpToParent, Map<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps, InspectionManager manager, RefManager refManager, ProblemDescriptionsProcessor processor) {
    for (PropertiesFile file : files) {
        PropertiesFile parent = parents.get(file);
        if (parent == null)
            continue;
        Set<String> parentKeys = keysUpToParent.get(parent);
        Set<String> overriddenKeys = new THashSet<>(propertiesFilesNamesMaps.get(file).keySet());
        overriddenKeys.retainAll(parentKeys);
        for (String overriddenKey : overriddenKeys) {
            IProperty property = file.findPropertyByKey(overriddenKey);
            assert property != null;
            while (parent != null) {
                IProperty parentProperty = parent.findPropertyByKey(overriddenKey);
                if (parentProperty != null && Comparing.strEqual(property.getValue(), parentProperty.getValue())) {
                    String message = InspectionsBundle.message("inconsistent.bundle.property.inherited.with.the.same.value", parent.getName());
                    ProblemDescriptor descriptor = manager.createProblemDescriptor(property.getPsiElement(), message, RemovePropertyLocalFix.INSTANCE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, false);
                    processor.addProblemElement(refManager.getReference(file.getContainingFile()), descriptor);
                }
                parent = parents.get(parent);
            }
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) THashSet(gnu.trove.THashSet)

Aggregations

IProperty (com.intellij.lang.properties.IProperty)75 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)45 PsiElement (com.intellij.psi.PsiElement)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)13 PsiFile (com.intellij.psi.PsiFile)13 NotNull (org.jetbrains.annotations.NotNull)13 Property (com.intellij.lang.properties.psi.Property)11 Project (com.intellij.openapi.project.Project)7 ArrayList (java.util.ArrayList)7 Nullable (org.jetbrains.annotations.Nullable)7 ResourceBundle (com.intellij.lang.properties.ResourceBundle)6 THashSet (gnu.trove.THashSet)6 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)4 Module (com.intellij.openapi.module.Module)4 Collections (java.util.Collections)4 StructureViewTreeElement (com.intellij.ide.structureView.StructureViewTreeElement)3 PropertiesImplUtil (com.intellij.lang.properties.PropertiesImplUtil)3 TreeElement (com.intellij.ide.util.treeView.smartTree.TreeElement)2 XmlProperty (com.intellij.lang.properties.xml.XmlProperty)2 Logger (com.intellij.openapi.diagnostic.Logger)2