Search in sources :

Example 1 with PropertiesFile

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

the class JavaI18nUtil method isPropertyRef.

static boolean isPropertyRef(final PsiExpression expression, final String key, final String resourceBundleName) {
    if (resourceBundleName == null) {
        return !PropertiesImplUtil.findPropertiesByKey(expression.getProject(), key).isEmpty();
    } else {
        final List<PropertiesFile> propertiesFiles = propertiesFilesByBundleName(resourceBundleName, expression);
        boolean containedInPropertiesFile = false;
        for (PropertiesFile propertiesFile : propertiesFiles) {
            containedInPropertiesFile |= propertiesFile.findPropertyByKey(key) != null;
        }
        return containedInPropertiesFile;
    }
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 2 with PropertiesFile

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

the class InconsistentResourceBundleInspection method checkFile.

@Override
public void checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, @NotNull ProblemsHolder problemsHolder, @NotNull GlobalInspectionContext globalContext, @NotNull ProblemDescriptionsProcessor problemDescriptionsProcessor) {
    Set<ResourceBundle> visitedBundles = globalContext.getUserData(VISITED_BUNDLES_KEY);
    if (!(file instanceof PropertiesFile))
        return;
    final PropertiesFile propertiesFile = (PropertiesFile) file;
    ResourceBundle resourceBundle = propertiesFile.getResourceBundle();
    assert visitedBundles != null;
    if (!visitedBundles.add(resourceBundle))
        return;
    List<PropertiesFile> files = resourceBundle.getPropertiesFiles();
    if (files.size() < 2)
        return;
    BidirectionalMap<PropertiesFile, PropertiesFile> parents = new BidirectionalMap<>();
    for (PropertiesFile f : files) {
        PropertiesFile parent = PropertiesUtil.getParent(f, files);
        if (parent != null) {
            parents.put(f, parent);
        }
    }
    final FactoryMap<PropertiesFile, Map<String, String>> propertiesFilesNamesMaps = new FactoryMap<PropertiesFile, Map<String, String>>() {

        @Nullable
        @Override
        protected Map<String, String> create(PropertiesFile key) {
            return key.getNamesMap();
        }
    };
    Map<PropertiesFile, Set<String>> keysUpToParent = new THashMap<>();
    for (PropertiesFile f : files) {
        Set<String> keys = new THashSet<>(propertiesFilesNamesMaps.get(f).keySet());
        PropertiesFile parent = parents.get(f);
        while (parent != null) {
            keys.addAll(propertiesFilesNamesMaps.get(parent).keySet());
            parent = parents.get(parent);
        }
        keysUpToParent.put(f, keys);
    }
    for (final InconsistentResourceBundleInspectionProvider provider : myInspectionProviders.getValue()) {
        if (isProviderEnabled(provider.getName())) {
            provider.check(parents, files, keysUpToParent, propertiesFilesNamesMaps, manager, globalContext.getRefManager(), problemDescriptionsProcessor);
        }
    }
}
Also used : FactoryMap(com.intellij.util.containers.FactoryMap) THashSet(gnu.trove.THashSet) THashSet(gnu.trove.THashSet) THashMap(gnu.trove.THashMap) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) FactoryMap(com.intellij.util.containers.FactoryMap) THashMap(gnu.trove.THashMap) BidirectionalMap(com.intellij.util.containers.BidirectionalMap) BidirectionalMap(com.intellij.util.containers.BidirectionalMap)

Example 3 with PropertiesFile

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

the class SuspiciousLocalesLanguagesInspection method checkFile.

@Nullable
@Override
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    final PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(file);
    if (propertiesFile == null) {
        return null;
    }
    final ResourceBundle resourceBundle = propertiesFile.getResourceBundle();
    final List<PropertiesFile> files = resourceBundle.getPropertiesFiles();
    if (!(resourceBundle instanceof ResourceBundleImpl) || files.size() < 2) {
        return null;
    }
    List<Locale> bundleLocales = ContainerUtil.mapNotNull(files, propertiesFile1 -> {
        final Locale locale = propertiesFile1.getLocale();
        return locale == PropertiesUtil.DEFAULT_LOCALE ? null : locale;
    });
    bundleLocales = ContainerUtil.filter(bundleLocales, locale -> !JAVA_LOCALES.getValue().contains(locale.getLanguage()) && !myAdditionalLanguages.contains(locale.getLanguage()));
    if (bundleLocales.isEmpty()) {
        return null;
    }
    final ProblemDescriptor descriptor = manager.createProblemDescriptor(file, PropertiesBundle.message("resource.bundle.contains.locales.with.suspicious.locale.languages.desciptor"), new DissociateResourceBundleQuickFix(resourceBundle), ProblemHighlightType.WEAK_WARNING, true);
    return new ProblemDescriptor[] { descriptor };
}
Also used : java.util(java.util) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) SoftLazyValue(com.intellij.reference.SoftLazyValue) DissociateResourceBundleAction(com.intellij.lang.properties.customizeActions.DissociateResourceBundleAction) HashSet(com.intellij.util.containers.HashSet) InvalidDataException(com.intellij.openapi.util.InvalidDataException) ContainerUtil(com.intellij.util.containers.ContainerUtil) ToolbarDecorator(com.intellij.ui.ToolbarDecorator) Nls(org.jetbrains.annotations.Nls) AnActionButtonRunnable(com.intellij.ui.AnActionButtonRunnable) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) Messages(com.intellij.openapi.ui.Messages) AnActionButton(com.intellij.ui.AnActionButton) JBList(com.intellij.ui.components.JBList) StringUtil(com.intellij.openapi.util.text.StringUtil) BaseLocalInspectionTool(com.intellij.codeInspection.ex.BaseLocalInspectionTool) com.intellij.lang.properties(com.intellij.lang.properties) TestOnly(org.jetbrains.annotations.TestOnly) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) InputValidator(com.intellij.openapi.ui.InputValidator) WriteExternalException(com.intellij.openapi.util.WriteExternalException) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) javax.swing(javax.swing) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with PropertiesFile

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

the class I18nizeAction method doI18nSelectedString.

public static void doI18nSelectedString(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile psiFile, @NotNull final I18nQuickFixHandler handler) {
    try {
        handler.checkApplicability(psiFile, editor);
    } catch (IncorrectOperationException ex) {
        CommonRefactoringUtil.showErrorHint(project, editor, ex.getMessage(), CodeInsightBundle.message("i18nize.error.title"), null);
        return;
    }
    if (!ApplicationManager.getApplication().isUnitTestMode()) {
        JavaI18nizeQuickFixDialog.isAvailable(psiFile);
    }
    try {
        ResourceBundleManager.getManager(psiFile);
    } catch (ResourceBundleManager.ResourceBundleNotFoundException e) {
        return;
    }
    final JavaI18nizeQuickFixDialog dialog = handler.createDialog(project, editor, psiFile);
    if (dialog == null)
        return;
    if (!dialog.showAndGet()) {
        return;
    }
    if (!FileModificationService.getInstance().prepareFileForWrite(psiFile))
        return;
    final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles();
    for (PropertiesFile file : propertiesFiles) {
        if (!FileModificationService.getInstance().prepareFileForWrite(file.getContainingFile()))
            return;
    }
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(project, () -> {
        try {
            handler.performI18nization(psiFile, editor, dialog.getLiteralExpression(), propertiesFiles, dialog.getKey(), StringUtil.unescapeStringCharacters(dialog.getValue()), dialog.getI18nizedText(), dialog.getParameters(), dialog.getPropertyCreationHandler());
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    }, CodeInsightBundle.message("quickfix.i18n.command.name"), project));
}
Also used : ResourceBundleManager(com.intellij.lang.properties.psi.ResourceBundleManager) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 5 with PropertiesFile

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

the class InvalidPropertyKeyFormInspection method checkDescriptor.

@Nullable
private static String checkDescriptor(final StringDescriptor descriptor, final Module module) {
    final String bundleName = descriptor.getDottedBundleName();
    final String key = descriptor.getKey();
    if (bundleName == null && key == null)
        return null;
    if (bundleName == null) {
        return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.bundle.not.specified");
    }
    if (key == null) {
        return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.property.key.not.specified");
    }
    PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(module.getProject());
    List<PropertiesFile> propFiles = manager.findPropertiesFiles(module, bundleName);
    if (propFiles.size() == 0) {
        return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.bundle.not.found", bundleName);
    }
    for (PropertiesFile propFile : propFiles) {
        final com.intellij.lang.properties.IProperty property = propFile.findPropertyByKey(key);
        if (property == null) {
            return UIDesignerBundle.message("inspection.invalid.property.in.form.quickfix.error.key.not.found", key, bundleName, propFile.getLocale().getDisplayName());
        }
    }
    return null;
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PropertiesReferenceManager(com.intellij.lang.properties.PropertiesReferenceManager) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)143 IProperty (com.intellij.lang.properties.IProperty)44 PsiFile (com.intellij.psi.PsiFile)42 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 ResourceBundle (com.intellij.lang.properties.ResourceBundle)19 PsiElement (com.intellij.psi.PsiElement)19 NotNull (org.jetbrains.annotations.NotNull)19 Nullable (org.jetbrains.annotations.Nullable)18 Property (com.intellij.lang.properties.psi.Property)15 Project (com.intellij.openapi.project.Project)10 XmlPropertiesFile (com.intellij.lang.properties.xml.XmlPropertiesFile)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 THashSet (gnu.trove.THashSet)8 ArrayList (java.util.ArrayList)7 Module (com.intellij.openapi.module.Module)6 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)6 HashSet (com.intellij.util.containers.HashSet)6 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)5