Search in sources :

Example 1 with PropertiesList

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

the class PropertiesInspectionSuppressor method isSuppressedFor.

@Override
public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String toolId) {
    Property property = PsiTreeUtil.getParentOfType(element, Property.class, false);
    PropertiesFile file;
    if (property == null) {
        PsiFile containingFile = element.getContainingFile();
        if (containingFile instanceof PropertiesFile) {
            file = (PropertiesFile) containingFile;
        } else {
            return false;
        }
    } else {
        PsiElement prev = property.getPrevSibling();
        while (prev instanceof PsiWhiteSpace || prev instanceof PsiComment) {
            if (prev instanceof PsiComment) {
                @NonNls String text = prev.getText();
                if (text.contains("suppress") && text.contains("\"" + toolId + "\""))
                    return true;
            }
            prev = prev.getPrevSibling();
        }
        file = property.getPropertiesFile();
    }
    PsiElement leaf = file.getContainingFile().findElementAt(0);
    while (leaf instanceof PsiWhiteSpace) leaf = leaf.getNextSibling();
    while (leaf instanceof PsiComment) {
        @NonNls String text = leaf.getText();
        if (text.contains("suppress") && text.contains("\"" + toolId + "\"") && text.contains("file")) {
            return true;
        }
        leaf = leaf.getNextSibling();
        if (leaf instanceof PsiWhiteSpace)
            leaf = leaf.getNextSibling();
        // comment before first property get bound to the file, not property
        if (leaf instanceof PropertiesList && leaf.getFirstChild() == property && text.contains("suppress") && text.contains("\"" + toolId + "\"")) {
            return true;
        }
    }
    return false;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PropertiesList(com.intellij.lang.properties.psi.PropertiesList) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Property(com.intellij.lang.properties.psi.Property)

Example 2 with PropertiesList

use of com.intellij.lang.properties.psi.PropertiesList 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

PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)2 PropertiesList (com.intellij.lang.properties.psi.PropertiesList)2 IProperty (com.intellij.lang.properties.IProperty)1 Property (com.intellij.lang.properties.psi.Property)1 ArrayList (java.util.ArrayList)1 NonNls (org.jetbrains.annotations.NonNls)1