Search in sources :

Example 86 with PropertiesFile

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

the class XmlPropertiesTest method testAddProperty2.

public void testAddProperty2() {
    final PsiFile psiFile = myFixture.configureByFile("foo.xml");
    final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(psiFile);
    assertNotNull(propertiesFile);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        propertiesFile.addProperty("kkk", "vvv");
    });
    final IProperty property = propertiesFile.findPropertyByKey("kkk");
    assertNotNull(property);
    assertEquals("vvv", property.getValue());
    WriteCommandAction.runWriteCommandAction(getProject(), () -> {
        propertiesFile.addProperty("kkk2", "vvv");
    });
    final IProperty property2 = propertiesFile.findPropertyByKey("kkk2");
    assertNotNull(property2);
    assertEquals("vvv", property2.getValue());
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 87 with PropertiesFile

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

the class ResourceBundleRenamerFactory method isApplicable.

@Override
public boolean isApplicable(@NotNull final PsiElement element) {
    if (!(element instanceof PsiFile)) {
        return false;
    }
    final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(element);
    if (file == null) {
        return false;
    }
    final ResourceBundle resourceBundle = file.getResourceBundle();
    return resourceBundle.getBaseDirectory() != null && resourceBundle.getPropertiesFiles().size() != 1;
}
Also used : PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle)

Example 88 with PropertiesFile

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

the class I18nizeQuickFixDialog method getExistingValueKeys.

@NotNull
protected List<String> getExistingValueKeys(String value) {
    if (!myCustomization.suggestExistingProperties) {
        return Collections.emptyList();
    }
    final ArrayList<String> result = new ArrayList<>();
    // check if property value already exists among properties file values and suggest corresponding key
    PropertiesFile propertiesFile = getPropertiesFile();
    if (propertiesFile != null) {
        for (IProperty property : propertiesFile.getProperties()) {
            if (Comparing.strEqual(property.getValue(), value)) {
                result.add(0, property.getUnescapedKey());
            }
        }
    }
    return result;
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) NotNull(org.jetbrains.annotations.NotNull)

Example 89 with PropertiesFile

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

the class I18nizeQuickFixDialog method propertiesFileChanged.

private void propertiesFileChanged() {
    PropertiesFile propertiesFile = getPropertiesFile();
    boolean hasResourceBundle = propertiesFile != null && propertiesFile.getResourceBundle().getPropertiesFiles().size() > 1;
    myUseResourceBundle.setEnabled(hasResourceBundle);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 90 with PropertiesFile

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

the class I18nizeQuickFixDialog method suggestPropertyKey.

protected String suggestPropertyKey(String value) {
    if (myCustomization.suggestedName != null) {
        return myCustomization.suggestedName;
    }
    // suggest property key not existing in this file
    String key = defaultSuggestPropertyKey(value);
    value = PATTERN.matcher(Normalizer.normalize(value, Normalizer.Form.NFD)).replaceAll("");
    if (key == null) {
        final StringBuilder result = new StringBuilder();
        boolean insertDotBeforeNextWord = false;
        for (int i = 0; i < value.length(); i++) {
            final char c = value.charAt(i);
            if (Character.isLetterOrDigit(c)) {
                if (insertDotBeforeNextWord) {
                    result.append('.');
                }
                result.append(Character.toLowerCase(c));
                insertDotBeforeNextWord = false;
            } else if (c == '&') {
                //do not insert dot if there is letter after the amp
                if (insertDotBeforeNextWord)
                    continue;
                if (i == value.length() - 1) {
                    continue;
                }
                if (Character.isLetter(value.charAt(i + 1))) {
                    continue;
                }
                insertDotBeforeNextWord = true;
            } else {
                if (result.length() > 0) {
                    insertDotBeforeNextWord = true;
                }
            }
        }
        key = result.toString();
    }
    PropertiesFile propertiesFile = getPropertiesFile();
    if (propertiesFile != null) {
        if (propertiesFile.findPropertyByKey(key) == null)
            return key;
        int suffix = 1;
        while (propertiesFile.findPropertyByKey(key + suffix) != null) {
            suffix++;
        }
        return key + suffix;
    } else {
        return key;
    }
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Aggregations

PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)143 IProperty (com.intellij.lang.properties.IProperty)44 PsiFile (com.intellij.psi.PsiFile)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 ResourceBundle (com.intellij.lang.properties.ResourceBundle)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)19 Nullable (org.jetbrains.annotations.Nullable)18 Property (com.intellij.lang.properties.psi.Property)15 Project (com.intellij.openapi.project.Project)10 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 THashSet (gnu.trove.THashSet)8 ArrayList (java.util.ArrayList)7 Module (com.intellij.openapi.module.Module)6 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (com.intellij.util.containers.HashSet)6 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5