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);
}
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);
}
}
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));
}
}
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));
}
}
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));
}
}
}
Aggregations