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;
}
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));
}
}
}
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);
}
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());
}
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;
}
Aggregations