Search in sources :

Example 16 with Property

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;
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PsiReference(com.intellij.psi.PsiReference) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) ReadonlyStatusHandler(com.intellij.openapi.vfs.ReadonlyStatusHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with Property

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;
}
Also used : IProperty(com.intellij.lang.properties.IProperty) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with Property

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");
    });
}
Also used : Property(com.intellij.lang.properties.psi.Property) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) DataContext(com.intellij.openapi.actionSystem.DataContext) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) ContainerUtil(com.intellij.util.containers.ContainerUtil) FileEditor(com.intellij.openapi.fileEditor.FileEditor) GotoRelatedProvider(com.intellij.navigation.GotoRelatedProvider) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PlatformDataKeys(com.intellij.openapi.actionSystem.PlatformDataKeys) PropertiesImplUtil(com.intellij.lang.properties.PropertiesImplUtil) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) ResourceBundle(com.intellij.lang.properties.ResourceBundle) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) FileEditor(com.intellij.openapi.fileEditor.FileEditor) IProperty(com.intellij.lang.properties.IProperty) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) Property(com.intellij.lang.properties.psi.Property) IProperty(com.intellij.lang.properties.IProperty) GotoRelatedItem(com.intellij.navigation.GotoRelatedItem) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with Property

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());
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertyImpl(com.intellij.lang.properties.psi.impl.PropertyImpl) Property(com.intellij.lang.properties.psi.Property) Annotation(com.intellij.lang.annotation.Annotation) PropertiesValueHighlighter(com.intellij.lang.properties.editor.PropertiesValueHighlighter)

Example 20 with Property

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);
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) Property(com.intellij.lang.properties.psi.Property) PsiElement(com.intellij.psi.PsiElement)

Aggregations

Property (com.intellij.lang.properties.psi.Property)23 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)16 IProperty (com.intellij.lang.properties.IProperty)12 NotNull (org.jetbrains.annotations.NotNull)6 Nullable (org.jetbrains.annotations.Nullable)5 Module (com.intellij.openapi.module.Module)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 PsiElement (com.intellij.psi.PsiElement)4 PsiFile (com.intellij.psi.PsiFile)4 ASTNode (com.intellij.lang.ASTNode)3 Logger (com.intellij.openapi.diagnostic.Logger)3 ProgressManager (com.intellij.openapi.progress.ProgressManager)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 com.intellij.codeInspection (com.intellij.codeInspection)2 PropertiesImplUtil (com.intellij.lang.properties.PropertiesImplUtil)2 ModuleUtilCore (com.intellij.openapi.module.ModuleUtilCore)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Project (com.intellij.openapi.project.Project)2 PsiSearchHelper (com.intellij.psi.search.PsiSearchHelper)2 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2