Search in sources :

Example 66 with IProperty

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

the class GroupByWordPrefixes method group.

@Override
@NotNull
public Collection<Group> group(@NotNull final AbstractTreeNode parent, @NotNull Collection<TreeElement> children) {
    List<Key> keys = new ArrayList<>();
    String parentPrefix;
    int parentPrefixLength;
    if (parent.getValue() instanceof PropertiesPrefixGroup) {
        parentPrefix = ((PropertiesPrefixGroup) parent.getValue()).getPrefix();
        parentPrefixLength = StringUtil.split(parentPrefix, mySeparator).size();
    } else {
        parentPrefix = "";
        parentPrefixLength = 0;
    }
    for (TreeElement element : children) {
        if (!(element instanceof StructureViewTreeElement)) {
            continue;
        }
        Object value = ((StructureViewTreeElement) element).getValue();
        if (!(value instanceof IProperty)) {
            continue;
        }
        final String text = ((IProperty) value).getUnescapedKey();
        if (text == null)
            continue;
        LOG.assertTrue(text.startsWith(parentPrefix) || text.startsWith(mySeparator));
        List<String> words = StringUtil.split(text, mySeparator);
        keys.add(new Key(words, element));
    }
    Collections.sort(keys, (k1, k2) -> {
        List<String> o1 = k1.words;
        List<String> o2 = k2.words;
        for (int i = 0; i < Math.max(o1.size(), o2.size()); i++) {
            if (i == o1.size())
                return 1;
            if (i == o2.size())
                return -1;
            String s1 = o1.get(i);
            String s2 = o2.get(i);
            int res = s1.compareTo(s2);
            if (res != 0)
                return res;
        }
        return 0;
    });
    List<Group> groups = new ArrayList<>();
    int groupStart = 0;
    for (int i = 0; i <= keys.size(); i++) {
        if (!isEndOfGroup(i, keys, parentPrefixLength)) {
            continue;
        }
        // find longest group prefix
        List<String> firstKey = groupStart == keys.size() ? Collections.<String>emptyList() : keys.get(groupStart).words;
        int prefixLen = firstKey.size();
        for (int j = groupStart + 1; j < i; j++) {
            List<String> prevKey = keys.get(j - 1).words;
            List<String> nextKey = keys.get(j).words;
            for (int k = parentPrefixLength; k < prefixLen; k++) {
                String word = k < nextKey.size() ? nextKey.get(k) : null;
                String wordInPrevKey = k < prevKey.size() ? prevKey.get(k) : null;
                if (!Comparing.strEqual(word, wordInPrevKey)) {
                    prefixLen = k;
                    break;
                }
            }
        }
        String[] strings = firstKey.subList(0, prefixLen).toArray(new String[prefixLen]);
        String prefix = StringUtil.join(strings, mySeparator);
        String presentableName = prefix.substring(parentPrefix.length());
        presentableName = StringUtil.trimStart(presentableName, mySeparator);
        if (i - groupStart > 1) {
            groups.add(new PropertiesPrefixGroup(children, prefix, presentableName, mySeparator));
        } else if (groupStart != keys.size()) {
            TreeElement node = keys.get(groupStart).node;
            if (node instanceof PropertiesStructureViewElement) {
                ((PropertiesStructureViewElement) node).setPresentableName(presentableName);
            } else {
                ((ResourceBundlePropertyStructureViewElement) node).setPresentableName(presentableName);
            }
        }
        groupStart = i;
    }
    return groups;
}
Also used : StructureViewTreeElement(com.intellij.ide.structureView.StructureViewTreeElement) IProperty(com.intellij.lang.properties.IProperty) StructureViewTreeElement(com.intellij.ide.structureView.StructureViewTreeElement) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with IProperty

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

the class PropertiesFileStructureViewElement method getChildrenBase.

@NotNull
public Collection<StructureViewTreeElement> getChildrenBase() {
    List<? extends IProperty> properties = getElement().getProperties();
    Collection<StructureViewTreeElement> elements = new ArrayList<>(properties.size());
    for (IProperty property : properties) {
        elements.add(new PropertiesStructureViewElement((Property) property));
    }
    return elements;
}
Also used : IProperty(com.intellij.lang.properties.IProperty) ArrayList(java.util.ArrayList) StructureViewTreeElement(com.intellij.ide.structureView.StructureViewTreeElement) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) NotNull(org.jetbrains.annotations.NotNull)

Example 68 with IProperty

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

the class TrailingSpacesInPropertyInspection method checkFile.

public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull final InspectionManager manager, final boolean isOnTheFly) {
    if (!(file instanceof PropertiesFile))
        return null;
    final List<IProperty> properties = ((PropertiesFile) file).getProperties();
    final List<ProblemDescriptor> descriptors = new SmartList<>();
    for (IProperty property : properties) {
        ProgressManager.checkCanceled();
        final PropertyImpl propertyImpl = (PropertyImpl) property;
        for (ASTNode node : ContainerUtil.ar(propertyImpl.getKeyNode(), propertyImpl.getValueNode())) {
            if (node != null) {
                PsiElement key = node.getPsi();
                TextRange textRange = getTrailingSpaces(key, myIgnoreVisibleSpaces);
                if (textRange != null) {
                    descriptors.add(manager.createProblemDescriptor(key, textRange, "Trailing spaces", ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true, new RemoveTrailingSpacesFix(myIgnoreVisibleSpaces)));
                }
            }
        }
    }
    return descriptors.toArray(new ProblemDescriptor[descriptors.size()]);
}
Also used : IProperty(com.intellij.lang.properties.IProperty) ASTNode(com.intellij.lang.ASTNode) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertyImpl(com.intellij.lang.properties.psi.impl.PropertyImpl) TextRange(com.intellij.openapi.util.TextRange) SmartList(com.intellij.util.SmartList) PsiElement(com.intellij.psi.PsiElement)

Example 69 with IProperty

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

the class DuplicatePropertyInspection method processDuplicateKeysWithDifferentValues.

private static void processDuplicateKeysWithDifferentValues(final Map<String, Set<String>> keyToDifferentValues, final Map<String, Set<PsiFile>> keyToFiles, final List<ProblemDescriptor> problemDescriptors, final InspectionManager manager, final PsiFile psiFile, final ProgressIndicator progress) {
    for (String key : keyToDifferentValues.keySet()) {
        if (progress != null) {
            progress.setText2(InspectionsBundle.message("duplicate.property.diff.key.progress.indicator.text", key));
            if (progress.isCanceled())
                throw new ProcessCanceledException();
        }
        final Set<String> values = keyToDifferentValues.get(key);
        if (values == null || values.size() < 2) {
            keyToFiles.remove(key);
        } else {
            StringBuffer message = new StringBuffer();
            final Set<PsiFile> psiFiles = keyToFiles.get(key);
            boolean firstUsage = true;
            for (PsiFile file : psiFiles) {
                if (!(file instanceof PropertiesFile))
                    continue;
                PropertiesFile propertiesFile = (PropertiesFile) file;
                final List<IProperty> propertiesByKey = propertiesFile.findPropertiesByKey(key);
                for (IProperty property : propertiesByKey) {
                    if (firstUsage) {
                        message.append(InspectionsBundle.message("duplicate.property.diff.key.problem.descriptor", key));
                        firstUsage = false;
                    }
                    surroundWithHref(message, property.getPsiElement().getFirstChild(), false);
                }
            }
            problemDescriptors.add(manager.createProblemDescriptor(psiFile, message.toString(), false, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 70 with IProperty

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

the class AlphaUnsortedPropertiesFileInspection method sortPropertiesFile.

private static void sortPropertiesFile(final PropertiesFile file) {
    final List<IProperty> properties = new ArrayList<>(file.getProperties());
    Collections.sort(properties, (p1, p2) -> Comparing.compare(p1.getKey(), p2.getKey(), String.CASE_INSENSITIVE_ORDER));
    final char delimiter = PropertiesCodeStyleSettings.getInstance(file.getProject()).getDelimiter();
    final StringBuilder rawText = new StringBuilder();
    for (int i = 0; i < properties.size(); i++) {
        IProperty property = properties.get(i);
        final String value = property.getValue();
        final String commentAboveProperty = property.getDocCommentText();
        if (commentAboveProperty != null) {
            rawText.append(commentAboveProperty).append("\n");
        }
        final String key = property.getKey();
        final String propertyText;
        if (key != null) {
            propertyText = PropertiesElementFactory.getPropertyText(key, value != null ? value : "", delimiter, null, false);
            rawText.append(propertyText);
            if (i != properties.size() - 1) {
                rawText.append("\n");
            }
        }
    }
    final PropertiesFile fakeFile = PropertiesElementFactory.createPropertiesFile(file.getProject(), rawText.toString());
    final PropertiesList propertiesList = PsiTreeUtil.findChildOfType(file.getContainingFile(), PropertiesList.class);
    LOG.assertTrue(propertiesList != null);
    final PropertiesList fakePropertiesList = PsiTreeUtil.findChildOfType(fakeFile.getContainingFile(), PropertiesList.class);
    LOG.assertTrue(fakePropertiesList != null);
    propertiesList.replace(fakePropertiesList);
}
Also used : IProperty(com.intellij.lang.properties.IProperty) ArrayList(java.util.ArrayList) PropertiesList(com.intellij.lang.properties.psi.PropertiesList) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

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