Search in sources :

Example 31 with IProperty

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

the class AntDomProperty method buildProperties.

private Map<String, String> buildProperties() {
    Map<String, String> result = myCachedProperties;
    if (result != null) {
        return result;
    }
    result = Collections.emptyMap();
    final String propertyName = getName().getRawText();
    if (propertyName != null) {
        final String propertyValue = getValue().getRawText();
        if (propertyValue != null) {
            result = Collections.singletonMap(propertyName, propertyValue);
        } else {
            String locValue = getLocation().getStringValue();
            if (locValue != null) {
                final File file = new File(locValue);
                if (!file.isAbsolute()) {
                    final String baseDir = getContextAntProject().getProjectBasedirPath();
                    if (baseDir != null) {
                        locValue = PathUtil.getCanonicalPath(new File(baseDir, locValue).getPath());
                    }
                }
                result = Collections.singletonMap(propertyName, FileUtil.toSystemDependentName(locValue));
            } else {
                // todo: process refid attrib if specified for the value
                final String tagText = getXmlTag().getText();
                result = Collections.singletonMap(propertyName, tagText);
            }
        }
    } else {
        // name attrib is not specified
        final PsiFileSystemItem psiFile = getFile().getValue();
        if (psiFile != null) {
            final PropertiesFile file = toPropertiesFile(psiFile);
            if (file != null) {
                result = new HashMap<>();
                for (final IProperty property : file.getProperties()) {
                    result.put(property.getUnescapedKey(), property.getUnescapedValue());
                }
            }
        } else if (getEnvironment().getRawText() != null) {
            String prefix = getEnvironment().getRawText();
            if (!prefix.endsWith(".")) {
                prefix += ".";
            }
            result = new HashMap<>();
            for (Map.Entry<String, String> entry : System.getenv().entrySet()) {
                result.put(prefix + entry.getKey(), entry.getValue());
            }
        } else {
            // todo: consider Url attribute?
            final String resource = getResource().getStringValue();
            if (resource != null) {
                final ClassLoader loader = getClassLoader();
                if (loader != null) {
                    final InputStream stream = loader.getResourceAsStream(resource);
                    if (stream != null) {
                        try {
                            // todo: Remote file can be XmlPropertiesFile
                            final PropertiesFile propFile = (PropertiesFile) CustomAntElementsRegistry.loadContentAsFile(getXmlTag().getProject(), stream, StdFileTypes.PROPERTIES);
                            result = new HashMap<>();
                            for (final IProperty property : propFile.getProperties()) {
                                result.put(property.getUnescapedKey(), property.getUnescapedValue());
                            }
                        } catch (IOException ignored) {
                        }
                    }
                }
            }
        }
    }
    return (myCachedProperties = result);
}
Also used : IProperty(com.intellij.lang.properties.IProperty) HashMap(com.intellij.util.containers.HashMap) InputStream(java.io.InputStream) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) IOException(java.io.IOException) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) File(java.io.File) PsiFile(com.intellij.psi.PsiFile)

Example 32 with IProperty

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

the class AntCreatePropertyFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement psiElement = descriptor.getPsiElement();
    final PsiFile containingFile = psiElement.getContainingFile();
    final FileModificationService modificationService = FileModificationService.getInstance();
    Navigatable result = null;
    if (myPropFile != null) {
        final VirtualFile vFile = myPropFile.getVirtualFile();
        boolean canModify = true;
        if (myPropFile instanceof PsiFile) {
            canModify = modificationService.prepareFileForWrite((PsiFile) myPropFile);
        } else if (vFile != null) {
            canModify = modificationService.prepareVirtualFilesForWrite(project, Collections.singleton(vFile));
        }
        if (canModify) {
            final IProperty generatedProperty = myPropFile.addProperty(myCanonicalText, "");
            result = vFile != null ? new OpenFileDescriptor(project, vFile, generatedProperty.getPsiElement().getTextRange().getEndOffset()) : generatedProperty;
        }
    } else {
        if (containingFile instanceof XmlFile) {
            final XmlFile xmlFile = (XmlFile) containingFile;
            final XmlTag rootTag = xmlFile.getRootTag();
            if (rootTag != null && modificationService.prepareFileForWrite(xmlFile)) {
                final XmlTag propTag = rootTag.createChildTag(PROPERTY, rootTag.getNamespace(), null, false);
                propTag.setAttribute(NAME_ATTR, myCanonicalText);
                propTag.setAttribute(VALUE_ATTR, "");
                final DomElement contextElement = DomUtil.getDomElement(descriptor.getPsiElement());
                PsiElement generated;
                if (contextElement == null) {
                    generated = rootTag.addSubTag(propTag, true);
                } else {
                    final AntDomTarget containingTarget = contextElement.getParentOfType(AntDomTarget.class, false);
                    final DomElement anchor = containingTarget != null ? containingTarget : contextElement;
                    final XmlTag tag = anchor.getXmlTag();
                    if (!rootTag.equals(tag)) {
                        generated = tag.getParent().addBefore(propTag, tag);
                    } else {
                        generated = rootTag.addSubTag(propTag, true);
                    }
                }
                if (generated instanceof XmlTag) {
                    final XmlAttribute valueAttrib = ((XmlTag) generated).getAttribute(VALUE_ATTR);
                    if (valueAttrib != null) {
                        final XmlAttributeValue valueElement = valueAttrib.getValueElement();
                        if (valueElement instanceof Navigatable) {
                            result = (Navigatable) valueElement;
                        }
                    }
                }
                if (result == null && generated instanceof Navigatable) {
                    result = (Navigatable) generated;
                }
            }
        }
    }
    if (result != null) {
        result.navigate(true);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AntDomTarget(com.intellij.lang.ant.dom.AntDomTarget) Navigatable(com.intellij.pom.Navigatable) DomElement(com.intellij.util.xml.DomElement) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) PsiElement(com.intellij.psi.PsiElement) FileModificationService(com.intellij.codeInsight.FileModificationService) XmlTag(com.intellij.psi.xml.XmlTag)

Example 33 with IProperty

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

the class AndroidPropertyFilesUpdater method updateProjectTypeProperty.

public static void updateProjectTypeProperty(@NotNull AndroidFacet facet, @NotNull final PropertiesFile propertiesFile, @NotNull List<Runnable> changes) {
    IProperty property = propertiesFile.findPropertyByKey(AndroidUtils.ANDROID_PROJECT_TYPE_PROPERTY);
    String value = Integer.toString(facet.getProjectType());
    if (property != null) {
        if (!value.equals(property.getValue())) {
            changes.add(() -> property.setValue(value));
        }
    } else {
        changes.add(() -> propertiesFile.addProperty(AndroidUtils.ANDROID_PROJECT_TYPE_PROPERTY, value));
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty)

Example 34 with IProperty

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

the class GradleImplicitPropertyUsageProviderTest method testGradleWrapper.

public void testGradleWrapper() {
    VirtualFile vFile = myFixture.copyFileToProject("projects/projectWithAppandLib/gradle/wrapper/gradle-wrapper.properties", "gradle/wrapper/gradle-wrapper.properties");
    PsiFile file = PsiManager.getInstance(getProject()).findFile(vFile);
    assertNotNull(file);
    PropertiesFile propertiesFile = (PropertiesFile) file;
    GradleImplicitPropertyUsageProvider provider = new GradleImplicitPropertyUsageProvider();
    for (IProperty property : propertiesFile.getProperties()) {
        // All properties are considered used in this file
        String name = property.getName();
        assertTrue(name, provider.isUsed((Property) property));
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty)

Example 35 with IProperty

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

the class GradleImplicitPropertyUsageProviderTest method testLocalProperties.

public void testLocalProperties() {
    VirtualFile vFile = myFixture.copyFileToProject("test.properties", "local.properties");
    PsiFile file = PsiManager.getInstance(getProject()).findFile(vFile);
    assertNotNull(file);
    PropertiesFile propertiesFile = (PropertiesFile) file;
    GradleImplicitPropertyUsageProvider provider = new GradleImplicitPropertyUsageProvider();
    for (IProperty property : propertiesFile.getProperties()) {
        Property p = (Property) property;
        // Only but the property with "unused" in its name are considered used
        String name = property.getName();
        if (name.contains("unused")) {
            assertFalse(name, provider.isUsed(p));
        } else {
            assertTrue(name, provider.isUsed(p));
        }
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty)

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