use of com.intellij.lang.properties.psi.Property in project intellij-community by JetBrains.
the class StringEditorDialog method saveModifiedPropertyValue.
@Nullable
public static String saveModifiedPropertyValue(final Module module, final StringDescriptor descriptor, final Locale locale, final String editedValue, final PsiFile formFile) {
final PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(module.getProject());
final PropertiesFile propFile = manager.findPropertiesFile(module, descriptor.getDottedBundleName(), locale);
if (propFile != null) {
final IProperty propertyByKey = propFile.findPropertyByKey(descriptor.getKey());
if (propertyByKey instanceof Property && !editedValue.equals(propertyByKey.getValue())) {
final Collection<PsiReference> references = findPropertyReferences((Property) propertyByKey, module);
String newKeyName = null;
if (references.size() > 1) {
final int rc = Messages.showYesNoCancelDialog(module.getProject(), UIDesignerBundle.message("edit.text.multiple.usages", propertyByKey.getUnescapedKey(), references.size()), UIDesignerBundle.message("edit.text.multiple.usages.title"), UIDesignerBundle.message("edit.text.change.all"), UIDesignerBundle.message("edit.text.make.unique"), CommonBundle.getCancelButtonText(), Messages.getWarningIcon());
if (rc == Messages.CANCEL) {
return null;
}
if (rc == Messages.NO) {
newKeyName = promptNewKeyName(module.getProject(), propFile, descriptor.getKey());
if (newKeyName == null)
return null;
}
}
final ReadonlyStatusHandler.OperationStatus operationStatus = ReadonlyStatusHandler.getInstance(module.getProject()).ensureFilesWritable(propFile.getVirtualFile());
if (operationStatus.hasReadonlyFiles()) {
return null;
}
final String newKeyName1 = newKeyName;
CommandProcessor.getInstance().executeCommand(module.getProject(), () -> {
UndoUtil.markPsiFileForUndo(formFile);
ApplicationManager.getApplication().runWriteAction(() -> {
PsiDocumentManager.getInstance(module.getProject()).commitAllDocuments();
try {
if (newKeyName1 != null) {
propFile.addProperty(newKeyName1, editedValue);
} else {
final IProperty propertyByKey1 = propFile.findPropertyByKey(descriptor.getKey());
if (propertyByKey1 != null) {
propertyByKey1.setValue(editedValue);
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
});
}, UIDesignerBundle.message("command.update.property"), null);
return newKeyName;
}
}
return null;
}
use of com.intellij.lang.properties.psi.Property in project intellij-community by JetBrains.
the class PropertyFoldingBuilder method getI18nProperty.
@Nullable
public static IProperty getI18nProperty(PsiLiteralExpression literal) {
final Property property = (Property) literal.getUserData(CACHE);
if (property == NULL)
return null;
if (property != null && isValid(property, literal))
return property;
if (isI18nProperty(literal)) {
final PsiReference[] references = literal.getReferences();
for (PsiReference reference : references) {
if (reference instanceof PsiPolyVariantReference) {
final ResolveResult[] results = ((PsiPolyVariantReference) reference).multiResolve(false);
for (ResolveResult result : results) {
final PsiElement element = result.getElement();
if (element instanceof IProperty) {
IProperty p = (IProperty) element;
literal.putUserData(CACHE, p);
return p;
}
}
} else {
final PsiElement element = reference.resolve();
if (element instanceof IProperty) {
IProperty p = (IProperty) element;
literal.putUserData(CACHE, p);
return p;
}
}
}
}
return null;
}
use of com.intellij.lang.properties.psi.Property in project intellij-community by JetBrains.
the class GotoPropertyDeclarationsProvider method getItems.
@NotNull
@Override
public List<? extends GotoRelatedItem> getItems(@NotNull DataContext context) {
final FileEditor editor = PlatformDataKeys.FILE_EDITOR.getData(context);
if (!(editor instanceof ResourceBundleEditor)) {
return Collections.emptyList();
}
final ResourceBundleEditor resourceBundleEditor = (ResourceBundleEditor) editor;
final Collection<ResourceBundleEditorViewElement> elements = resourceBundleEditor.getSelectedElements();
if (elements.size() != 1) {
return Collections.emptyList();
}
final IProperty[] properties = ContainerUtil.getFirstItem(elements).getProperties();
if (properties == null || properties.length != 1 || !(properties[0] instanceof Property)) {
return Collections.emptyList();
}
final IProperty property = properties[0];
final String propertyKey = property.getKey();
final PropertiesFile file = PropertiesImplUtil.getPropertiesFile(property.getPsiElement().getContainingFile());
assert file != null;
final ResourceBundle resourceBundle = file.getResourceBundle();
return ContainerUtil.mapNotNull(resourceBundle.getPropertiesFiles(), (NullableFunction<PropertiesFile, GotoRelatedItem>) f -> {
final IProperty foundProperty = f.findPropertyByKey(propertyKey);
return foundProperty == null ? null : new GotoRelatedItem(foundProperty.getPsiElement(), "Property Declarations");
});
}
use of com.intellij.lang.properties.psi.Property in project intellij-community by JetBrains.
the class PropertiesAnnotator method annotate.
public void annotate(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
if (!(element instanceof IProperty))
return;
final Property property = (Property) element;
PropertiesFile propertiesFile = property.getPropertiesFile();
Collection<IProperty> others = propertiesFile.findPropertiesByKey(property.getUnescapedKey());
ASTNode keyNode = ((PropertyImpl) property).getKeyNode();
if (others.size() != 1) {
Annotation annotation = holder.createErrorAnnotation(keyNode, PropertiesBundle.message("duplicate.property.key.error.message"));
annotation.registerFix(PropertiesQuickFixFactory.getInstance().createRemovePropertyFix(property));
}
highlightTokens(property, keyNode, holder, new PropertiesHighlighter());
ASTNode valueNode = ((PropertyImpl) property).getValueNode();
if (valueNode != null) {
highlightTokens(property, valueNode, holder, new PropertiesValueHighlighter());
}
}
use of com.intellij.lang.properties.psi.Property in project intellij-community by JetBrains.
the class RemovePropertyLocalFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
Property property = PsiTreeUtil.getParentOfType(element, Property.class, false);
if (property == null)
return;
try {
new RemovePropertyFix(property).invoke(project, null, property.getPropertiesFile().getContainingFile());
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
Aggregations