Search in sources :

Example 46 with PropertiesFile

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

the class PropertiesInspectionSuppressor method isSuppressedFor.

@Override
public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String toolId) {
    Property property = PsiTreeUtil.getParentOfType(element, Property.class, false);
    PropertiesFile file;
    if (property == null) {
        PsiFile containingFile = element.getContainingFile();
        if (containingFile instanceof PropertiesFile) {
            file = (PropertiesFile) containingFile;
        } else {
            return false;
        }
    } else {
        PsiElement prev = property.getPrevSibling();
        while (prev instanceof PsiWhiteSpace || prev instanceof PsiComment) {
            if (prev instanceof PsiComment) {
                @NonNls String text = prev.getText();
                if (text.contains("suppress") && text.contains("\"" + toolId + "\""))
                    return true;
            }
            prev = prev.getPrevSibling();
        }
        file = property.getPropertiesFile();
    }
    PsiElement leaf = file.getContainingFile().findElementAt(0);
    while (leaf instanceof PsiWhiteSpace) leaf = leaf.getNextSibling();
    while (leaf instanceof PsiComment) {
        @NonNls String text = leaf.getText();
        if (text.contains("suppress") && text.contains("\"" + toolId + "\"") && text.contains("file")) {
            return true;
        }
        leaf = leaf.getNextSibling();
        if (leaf instanceof PsiWhiteSpace)
            leaf = leaf.getNextSibling();
        // comment before first property get bound to the file, not property
        if (leaf instanceof PropertiesList && leaf.getFirstChild() == property && text.contains("suppress") && text.contains("\"" + toolId + "\"")) {
            return true;
        }
    }
    return false;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PropertiesList(com.intellij.lang.properties.psi.PropertiesList) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Property(com.intellij.lang.properties.psi.Property)

Example 47 with PropertiesFile

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

the class DuplicatePropertyInspection method prepareDuplicateKeysByFile.

private void prepareDuplicateKeysByFile(final Map<String, Set<PsiFile>> keyToFiles, final InspectionManager manager, final Map<String, Set<String>> keyToValues, final List<ProblemDescriptor> problemDescriptors, final PsiFile psiFile, final ProgressIndicator progress) {
    for (String key : keyToFiles.keySet()) {
        if (progress != null) {
            progress.setText2(InspectionsBundle.message("duplicate.property.key.progress.indicator.text", key));
            if (progress.isCanceled())
                throw new ProcessCanceledException();
        }
        final StringBuffer message = new StringBuffer();
        int duplicatesCount = 0;
        Set<PsiFile> psiFilesWithDuplicates = keyToFiles.get(key);
        for (PsiFile file : psiFilesWithDuplicates) {
            if (!(file instanceof PropertiesFile))
                continue;
            PropertiesFile propertiesFile = (PropertiesFile) file;
            final List<IProperty> propertiesByKey = propertiesFile.findPropertiesByKey(key);
            for (IProperty property : propertiesByKey) {
                if (duplicatesCount == 0) {
                    message.append(InspectionsBundle.message("duplicate.property.key.problem.descriptor", key));
                }
                surroundWithHref(message, property.getPsiElement().getFirstChild(), false);
                duplicatesCount++;
                //prepare for filter same keys different values
                Set<String> values = keyToValues.get(key);
                if (values == null) {
                    values = new HashSet<>();
                    keyToValues.put(key, values);
                }
                values.add(property.getValue());
            }
        }
        if (duplicatesCount > 1 && CHECK_DUPLICATE_KEYS) {
            problemDescriptors.add(manager.createProblemDescriptor(psiFile, message.toString(), false, null, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
        }
    }
}
Also used : IProperty(com.intellij.lang.properties.IProperty) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 48 with PropertiesFile

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

the class DuplicatePropertyInspection method checkFile.

private void checkFile(final PsiFile file, final InspectionManager manager, GlobalInspectionContextBase context, final RefManager refManager, final ProblemDescriptionsProcessor processor) {
    if (!(file instanceof PropertiesFile))
        return;
    if (!context.isToCheckFile(file, this) || SuppressionUtil.inspectionResultSuppressed(file, this))
        return;
    final PsiSearchHelper searchHelper = PsiSearchHelper.SERVICE.getInstance(file.getProject());
    final PropertiesFile propertiesFile = (PropertiesFile) file;
    final List<IProperty> properties = propertiesFile.getProperties();
    Module module = ModuleUtilCore.findModuleForPsiElement(file);
    if (module == null)
        return;
    final GlobalSearchScope scope = CURRENT_FILE ? GlobalSearchScope.fileScope(file) : MODULE_WITH_DEPENDENCIES ? GlobalSearchScope.moduleWithDependenciesScope(module) : GlobalSearchScope.projectScope(file.getProject());
    final Map<String, Set<PsiFile>> processedValueToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
    final Map<String, Set<PsiFile>> processedKeyToFiles = Collections.synchronizedMap(new HashMap<String, Set<PsiFile>>());
    final ProgressIndicator original = ProgressManager.getInstance().getProgressIndicator();
    final ProgressIndicator progress = ProgressWrapper.wrap(original);
    ProgressManager.getInstance().runProcess(() -> {
        if (!JobLauncher.getInstance().invokeConcurrentlyUnderProgress(properties, progress, false, property -> {
            if (original != null) {
                if (original.isCanceled())
                    return false;
                original.setText2(PropertiesBundle.message("searching.for.property.key.progress.text", property.getUnescapedKey()));
            }
            processTextUsages(processedValueToFiles, property.getValue(), processedKeyToFiles, searchHelper, scope);
            processTextUsages(processedKeyToFiles, property.getUnescapedKey(), processedValueToFiles, searchHelper, scope);
            return true;
        }))
            throw new ProcessCanceledException();
        List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
        Map<String, Set<String>> keyToDifferentValues = new HashMap<>();
        if (CHECK_DUPLICATE_KEYS || CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
            prepareDuplicateKeysByFile(processedKeyToFiles, manager, keyToDifferentValues, problemDescriptors, file, original);
        }
        if (CHECK_DUPLICATE_VALUES)
            prepareDuplicateValuesByFile(processedValueToFiles, manager, problemDescriptors, file, original);
        if (CHECK_DUPLICATE_KEYS_WITH_DIFFERENT_VALUES) {
            processDuplicateKeysWithDifferentValues(keyToDifferentValues, processedKeyToFiles, problemDescriptors, manager, file, original);
        }
        if (!problemDescriptors.isEmpty()) {
            processor.addProblemElement(refManager.getReference(file), problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]));
        }
    }, progress);
}
Also used : java.util(java.util) ActionListener(java.awt.event.ActionListener) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) JobLauncher(com.intellij.concurrency.JobLauncher) VirtualFile(com.intellij.openapi.vfs.VirtualFile) URL(java.net.URL) Document(com.intellij.openapi.editor.Document) THashSet(gnu.trove.THashSet) LowLevelSearchUtil(com.intellij.psi.impl.search.LowLevelSearchUtil) StringSearcher(com.intellij.util.text.StringSearcher) RefManager(com.intellij.codeInspection.reference.RefManager) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) Comparing(com.intellij.openapi.util.Comparing) ProgressWrapper(com.intellij.openapi.progress.util.ProgressWrapper) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) Module(com.intellij.openapi.module.Module) ProgressManager(com.intellij.openapi.progress.ProgressManager) Property(com.intellij.lang.properties.psi.Property) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) PropertiesBundle(com.intellij.lang.properties.PropertiesBundle) ModuleUtilCore(com.intellij.openapi.module.ModuleUtilCore) MalformedURLException(java.net.MalformedURLException) StringUtil(com.intellij.openapi.util.text.StringUtil) Processors(com.intellij.util.Processors) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) com.intellij.codeInspection(com.intellij.codeInspection) ActionEvent(java.awt.event.ActionEvent) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) IProperty(com.intellij.lang.properties.IProperty) GlobalInspectionContextBase(com.intellij.codeInspection.ex.GlobalInspectionContextBase) NotNull(org.jetbrains.annotations.NotNull) CharArrayUtil(com.intellij.util.text.CharArrayUtil) javax.swing(javax.swing) THashSet(gnu.trove.THashSet) PsiSearchHelper(com.intellij.psi.search.PsiSearchHelper) IProperty(com.intellij.lang.properties.IProperty) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) Module(com.intellij.openapi.module.Module) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 49 with PropertiesFile

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

the class CustomResourceBundle method fromState.

public static CustomResourceBundle fromState(final CustomResourceBundleState state, final Project project) {
    final PsiManager psiManager = PsiManager.getInstance(project);
    final List<PropertiesFile> files = ContainerUtil.map(state.getFiles(VirtualFileManager.getInstance()), virtualFile -> PropertiesImplUtil.getPropertiesFile(psiManager.findFile(virtualFile)));
    return files.size() < 2 ? null : new CustomResourceBundle(files, state.getBaseName());
}
Also used : PsiManager(com.intellij.psi.PsiManager) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile)

Example 50 with PropertiesFile

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

the class PropertiesImplUtil method createByUrl.

@Nullable
public static ResourceBundle createByUrl(@NotNull final String url, @NotNull final Project project) {
    final int idx = url.lastIndexOf('/');
    if (idx == -1)
        return null;
    final String baseDirectoryName = url.substring(0, idx);
    final String baseName = url.substring(idx + 1);
    final VirtualFile baseDirectoryVirtualFile = VirtualFileManager.getInstance().findFileByUrl(baseDirectoryName);
    if (baseDirectoryVirtualFile == null) {
        return null;
    }
    final PsiDirectory baseDirectory = PsiManager.getInstance(project).findDirectory(baseDirectoryVirtualFile);
    if (baseDirectory == null) {
        return null;
    }
    final ResourceBundleManager bundleBaseNameManager = ResourceBundleManager.getInstance(project);
    for (PsiFile file : baseDirectory.getFiles()) {
        final PropertiesFile propertiesFile = getPropertiesFile(file);
        if (propertiesFile == null)
            continue;
        final String currBaseName = bundleBaseNameManager.getBaseName(file);
        if (currBaseName.equals(baseName)) {
            return getResourceBundle(propertiesFile);
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) 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