Search in sources :

Example 26 with IProperty

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

the class I18nizeQuickFixDialog method suggestSelectedFileUrl.

private String suggestSelectedFileUrl(List<String> paths) {
    if (myDefaultPropertyValue != null) {
        for (String path : paths) {
            VirtualFile file = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(path));
            if (file == null)
                continue;
            PsiFile psiFile = myContext.getManager().findFile(file);
            if (!(psiFile instanceof PropertiesFile))
                continue;
            for (IProperty property : ((PropertiesFile) psiFile).getProperties()) {
                if (property.getValue().equals(myDefaultPropertyValue))
                    return path;
            }
        }
    }
    return LastSelectedPropertiesFileStore.getInstance().suggestLastSelectedPropertiesFileUrl(myContext);
}
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)

Example 27 with IProperty

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

the class ResourceBundleKeyReference method isReferenceTo.

public boolean isReferenceTo(final PsiElement element) {
    if (!(element instanceof IProperty)) {
        return false;
    }
    IProperty property = (IProperty) element;
    String baseName = ResourceBundleManager.getInstance(element.getProject()).getFullName(property.getPropertiesFile());
    return baseName != null && myBundleName.equals(baseName.replace('.', '/')) && getRangeText().equals(property.getUnescapedKey());
}
Also used : IProperty(com.intellij.lang.properties.IProperty)

Example 28 with IProperty

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

the class PropertyFoldingBuilder method checkLiteral.

private static void checkLiteral(PsiLiteralExpression expression, List<FoldingDescriptor> result) {
    if (isI18nProperty(expression)) {
        final IProperty property = getI18nProperty(expression);
        final HashSet<Object> set = new HashSet<>();
        set.add(property != null ? property : PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT);
        final String msg = formatI18nProperty(expression, property);
        final PsiElement parent = expression.getParent();
        if (!msg.equals(expression.getText()) && parent instanceof PsiExpressionList && ((PsiExpressionList) parent).getExpressions()[0] == expression) {
            final PsiExpressionList expressions = (PsiExpressionList) parent;
            final int count = JavaI18nUtil.getPropertyValueParamsMaxCount(expression);
            final PsiExpression[] args = expressions.getExpressions();
            if (args.length == 1 + count && parent.getParent() instanceof PsiMethodCallExpression) {
                boolean ok = true;
                for (int i = 1; i < count + 1; i++) {
                    Object value = JavaConstantExpressionEvaluator.computeConstantExpression(args[i], false);
                    if (value == null) {
                        if (!(args[i] instanceof PsiReferenceExpression)) {
                            ok = false;
                            break;
                        }
                    }
                }
                if (ok) {
                    result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(parent.getParent().getNode()), parent.getParent().getTextRange(), null, set));
                    return;
                }
            }
        }
        result.add(new FoldingDescriptor(ObjectUtils.assertNotNull(expression.getNode()), expression.getTextRange(), null, set));
    }
}
Also used : FoldingDescriptor(com.intellij.lang.folding.FoldingDescriptor) IProperty(com.intellij.lang.properties.IProperty)

Example 29 with IProperty

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

the class PropertyFoldingBuilder method isI18nProperty.

public static boolean isI18nProperty(@NotNull PsiLiteralExpression expr) {
    if (!isStringLiteral(expr))
        return false;
    final IProperty property = expr.getUserData(CACHE);
    if (property == NULL)
        return false;
    if (property != null)
        return true;
    final Map<String, Object> annotationParams = new HashMap<>();
    annotationParams.put(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER, null);
    final boolean isI18n = JavaI18nUtil.mustBePropertyKey(expr, annotationParams);
    if (!isI18n) {
        expr.putUserData(CACHE, NULL);
    }
    return isI18n;
}
Also used : IProperty(com.intellij.lang.properties.IProperty)

Example 30 with IProperty

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

the class InlinePropertyHandler method inlineElement.

public void inlineElement(final Project project, Editor editor, PsiElement psiElement) {
    if (!(psiElement instanceof IProperty))
        return;
    IProperty property = (IProperty) psiElement;
    final String propertyValue = property.getValue();
    if (propertyValue == null)
        return;
    final List<PsiElement> occurrences = Collections.synchronizedList(ContainerUtil.<PsiElement>newArrayList());
    final Collection<PsiFile> containingFiles = Collections.synchronizedSet(new HashSet<PsiFile>());
    containingFiles.add(psiElement.getContainingFile());
    boolean result = ReferencesSearch.search(psiElement).forEach(psiReference -> {
        PsiElement element = psiReference.getElement();
        PsiElement parent = element.getParent();
        if (parent instanceof PsiExpressionList && parent.getParent() instanceof PsiMethodCallExpression) {
            if (((PsiExpressionList) parent).getExpressions().length == 1) {
                occurrences.add(parent.getParent());
                containingFiles.add(element.getContainingFile());
                return true;
            }
        }
        return false;
    });
    if (!result) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Property has non-method usages", REFACTORING_NAME, null);
    }
    if (occurrences.isEmpty()) {
        CommonRefactoringUtil.showErrorHint(project, editor, "Property has no usages", REFACTORING_NAME, null);
        return;
    }
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        String occurrencesString = RefactoringBundle.message("occurrences.string", occurrences.size());
        String question = PropertiesBundle.message("inline.property.confirmation", property.getName(), propertyValue) + " " + occurrencesString;
        RefactoringMessageDialog dialog = new RefactoringMessageDialog(REFACTORING_NAME, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
        if (!dialog.showAndGet()) {
            return;
        }
    }
    final RefactoringEventData data = new RefactoringEventData();
    data.addElement(psiElement.copy());
    new WriteCommandAction.Simple(project, REFACTORING_NAME, containingFiles.toArray(new PsiFile[containingFiles.size()])) {

        @Override
        protected void run() throws Throwable {
            project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringStarted(REFACTORING_ID, data);
            PsiLiteral stringLiteral = (PsiLiteral) JavaPsiFacade.getInstance(getProject()).getElementFactory().createExpressionFromText("\"" + StringUtil.escapeStringCharacters(propertyValue) + "\"", null);
            for (PsiElement occurrence : occurrences) {
                occurrence.replace(stringLiteral.copy());
            }
            project.getMessageBus().syncPublisher(RefactoringEventListener.REFACTORING_EVENT_TOPIC).refactoringDone(REFACTORING_ID, null);
        }
    }.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) RefactoringMessageDialog(com.intellij.refactoring.util.RefactoringMessageDialog) IProperty(com.intellij.lang.properties.IProperty) RefactoringEventData(com.intellij.refactoring.listeners.RefactoringEventData)

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