@NotNull public static List<PropertiesFile> propertiesFilesByBundleName(final String resourceBundleName, final PsiElement context) { PsiFile containingFile = context.getContainingFile(); PsiElement containingFileContext = InjectedLanguageManager.getInstance(containingFile.getProject()).getInjectionHost(containingFile); if (containingFileContext != null) containingFile = containingFileContext.getContainingFile(); VirtualFile virtualFile = containingFile.getVirtualFile(); if (virtualFile == null) { virtualFile = containingFile.getOriginalFile().getVirtualFile(); } if (virtualFile != null) { Project project = containingFile.getProject(); final Module module = ProjectRootManager.getInstance(project).getFileIndex().getModuleForFile(virtualFile); if (module != null) { PropertiesReferenceManager refManager = PropertiesReferenceManager.getInstance(project); return refManager.findPropertiesFiles(module, resourceBundleName); } } return Collections.emptyList(); }
public static List<String> defaultSuggestPropertiesFiles(Project project) { final List<String> paths = new ArrayList<String>(); final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex(); PropertiesReferenceManager.getInstance(project).processAllPropertiesFiles(new PropertiesFileProcessor() { @Override public boolean process(String baseName, PropertiesFile propertiesFile) { if (propertiesFile instanceof XmlPropertiesFile) { return true; } VirtualFile virtualFile = propertiesFile.getVirtualFile(); if (projectFileIndex.isInContent(virtualFile)) { String path = FileUtil.toSystemDependentName(virtualFile.getPath()); paths.add(path); } return true; } }); return paths; }
@NotNull @Override public Object[] getVariants() { final Project project = myElement.getProject(); PropertiesReferenceManager referenceManager = PropertiesReferenceManager.getInstance(project); final List<LookupElement> variants = new ArrayList<LookupElement>(); referenceManager.processPropertiesFiles(GlobalSearchScopesCore.projectProductionScope(project), new PropertiesFileProcessor() { public boolean process(String baseName, PropertiesFile propertiesFile) { final Icon icon = propertiesFile.getContainingFile().getIcon(Iconable.ICON_FLAG_READ_STATUS); final String relativePath = ProjectUtil.calcRelativeToProjectPath(propertiesFile.getVirtualFile(), project); variants.add(LookupElementBuilder.create(propertiesFile, baseName) .withIcon(icon) .withTailText(" (" + relativePath + ")", true)); return true; } }, this); return variants.toArray(new LookupElement[variants.size()]); }
@Override protected List<PropertiesFile> retrievePropertyFilesByBundleName(String bundleName, PsiElement element) { final List<String> allBundleNames; if (bundleName == null) { allBundleNames = getPluginResourceBundles(element); } else { allBundleNames = Collections.singletonList(bundleName); } final Project project = element.getProject(); final PropertiesReferenceManager propertiesReferenceManager = PropertiesReferenceManager.getInstance(project); final GlobalSearchScope searchScope = GlobalSearchScope.projectScope(project); final List<PropertiesFile> allPropertiesFiles = new ArrayList<PropertiesFile>(); for (String name : allBundleNames) { final List<PropertiesFile> propertiesFiles = propertiesReferenceManager .findPropertiesFiles(searchScope, name, BundleNameEvaluator.DEFAULT); allPropertiesFiles.addAll(propertiesFiles); } return allPropertiesFiles; }
public static List<String> defaultGetPropertyFiles(Project project) { final List<String> paths = new ArrayList<String>(); final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex(); PropertiesReferenceManager.getInstance(project).processAllPropertiesFiles(new PropertiesFileProcessor() { @Override public boolean process(String baseName, PropertiesFile propertiesFile) { VirtualFile virtualFile = propertiesFile.getVirtualFile(); if (projectFileIndex.isInContent(virtualFile)) { String path = FileUtil.toSystemDependentName(virtualFile.getPath()); paths.add(path); } return true; } }); return paths; }
public static Locale[] collectUsedLocales(final Module module, final IRootContainer rootContainer) { final Set<Locale> locales = new HashSet<Locale>(); final PropertiesReferenceManager propManager = PropertiesReferenceManager.getInstance(module.getProject()); for (String bundleName : collectUsedBundleNames(rootContainer)) { List<PropertiesFile> propFiles = propManager.findPropertiesFiles(module, bundleName.replace('/', '.')); for (PropertiesFile propFile : propFiles) { locales.add(propFile.getLocale()); } } return locales.toArray(new Locale[locales.size()]); }
@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; }
public void testXmlProperties() throws Exception { myFixture.configureByFile("foo.xml"); List<PropertiesFile> files = PropertiesReferenceManager.getInstance(getProject()).findPropertiesFiles(myModule, "foo"); assertEquals(1, files.size()); PropertiesFile file = files.get(0); assertEquals(1, file.findPropertiesByKey("foo").size()); List<IProperty> properties = PropertiesImplUtil.findPropertiesByKey(getProject(), "foo"); assertEquals(1, properties.size()); }
static Set<Object> getPropertiesKeys(final PropertyReferenceBase propertyReference) { final Set<Object> variants = new THashSet<Object>(new TObjectHashingStrategy<Object>() { public int computeHashCode(final Object object) { if (object instanceof IProperty) { final String key = ((IProperty)object).getKey(); return key == null ? 0 : key.hashCode(); } else { return 0; } } public boolean equals(final Object o1, final Object o2) { return o1 instanceof IProperty && o2 instanceof IProperty && Comparing.equal(((IProperty)o1).getKey(), ((IProperty)o2).getKey(), true); } }); List<PropertiesFile> propertiesFileList = propertyReference.getPropertiesFiles(); if (propertiesFileList == null) { PropertiesReferenceManager .getInstance(propertyReference.getElement().getProject()).processAllPropertiesFiles(new PropertiesFileProcessor() { @Override public boolean process(String baseName, PropertiesFile propertiesFile) { addVariantsFromFile(propertyReference, propertiesFile, variants); return true; } }); } else { for (PropertiesFile propFile : propertiesFileList) { addVariantsFromFile(propertyReference, propFile, variants); } } return variants; }
public void testXmlProperties() throws Exception { myFixture.configureByFile("foo.xml"); List<PropertiesFile> files = PropertiesReferenceManager.getInstance(getProject()).findPropertiesFiles(myModule, "foo"); assertEquals(1, files.size()); PropertiesFile file = files.get(0); assertEquals(1, file.findPropertiesByKey("foo").size()); List<IProperty> properties = PropertiesUtil.findPropertiesByKey(getProject(), "foo"); assertEquals(1, properties.size()); }
@Override public void registerReferenceProviders(PsiReferenceRegistrar registrar) { ElementPattern pattern = createPattern("key", "groupKey"); registrar.registerReferenceProvider(pattern, new InspectionsKeyPropertiesReferenceProvider(false), PsiReferenceRegistrar.DEFAULT_PRIORITY); ElementPattern bundlePattern = createPattern("bundle", "groupBundle"); registrar.registerReferenceProvider(bundlePattern, new PsiReferenceProvider() { @NotNull @Override public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) { ResourceBundleReference reference = new ResourceBundleReference(element, false) { @NotNull @Override public Object[] getVariants() { PropertiesReferenceManager referenceManager = PropertiesReferenceManager.getInstance(myElement.getProject()); final List<LookupElement> variants = new ArrayList<LookupElement>(); referenceManager.processPropertiesFiles(GlobalSearchScope.projectScope(myElement.getProject()), new PropertiesFileProcessor() { public boolean process(String baseName, PropertiesFile propertiesFile) { variants.add(LookupElementBuilder.create(propertiesFile, baseName) .withIcon(propertiesFile.getContainingFile().getIcon(Iconable.ICON_FLAG_READ_STATUS))); return true; } }, this); return variants.toArray(new LookupElement[variants.size()]); } }; return new PsiReference[]{reference}; } }, PsiReferenceRegistrar.DEFAULT_PRIORITY); }
public static Locale[] collectUsedLocales(final Module module, final IRootContainer rootContainer) { final Set<Locale> locales = new HashSet<Locale>(); final PropertiesReferenceManager propManager = PropertiesReferenceManager.getInstance(module.getProject()); for(String bundleName : collectUsedBundleNames(rootContainer)) { List<PropertiesFile> propFiles = propManager.findPropertiesFiles(module, bundleName.replace('/', '.')); for(PropertiesFile propFile : propFiles) { locales.add(propFile.getLocale()); } } return locales.toArray(new Locale[locales.size()]); }
private PropertiesFile getPropertiesFile(final StringDescriptor descriptor) { final PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(myEditor.getProject()); return manager.findPropertiesFile(myEditor.getModule(), descriptor.getDottedBundleName(), myLocale); }
private void visitPropertyKeyAnnotationParameter(PsiExpression expression, String key) { Ref<String> resourceBundleName = new Ref<String>(); if (!JavaI18nUtil.isValidPropertyReference(myManager.getProject(), expression, key, resourceBundleName)) { String bundleName = resourceBundleName.get(); if (bundleName != null) { // can be null if we were unable to resolve literal expression, e.g. when JDK was not set appendPropertyKeyNotFoundProblem(bundleName, key, expression, myManager, myProblems, onTheFly); } } else if (expression.getParent() instanceof PsiNameValuePair) { PsiNameValuePair nvp = (PsiNameValuePair)expression.getParent(); if (Comparing.equal(nvp.getName(), AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER)) { PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(expression.getProject()); Module module = ModuleUtilCore.findModuleForPsiElement(expression); if (module != null) { List<PropertiesFile> propFiles = manager.findPropertiesFiles(module, key); if (propFiles.isEmpty()) { final String description = CodeInsightBundle.message("inspection.invalid.resource.bundle.reference", key); final ProblemDescriptor problem = myManager.createProblemDescriptor(expression, description, (LocalQuickFix)null, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, onTheFly); myProblems.add(problem); } } } } else if (expression.getParent() instanceof PsiExpressionList && expression.getParent().getParent() instanceof PsiMethodCallExpression) { final Map<String, Object> annotationParams = new HashMap<String, Object>(); annotationParams.put(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER, null); if (!JavaI18nUtil.mustBePropertyKey(myManager.getProject(), expression, annotationParams)) return; final SortedSet<Integer> paramsCount = JavaI18nUtil.getPropertyValueParamsCount(expression, resourceBundleName.get()); if (paramsCount.isEmpty() || (paramsCount.size() != 1 && resourceBundleName.get() == null)) { return; } final int maxParamCount = paramsCount.last(); final PsiExpressionList expressions = (PsiExpressionList)expression.getParent(); final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expressions.getParent(); final PsiMethod method = methodCall.resolveMethod(); final PsiExpression[] args = expressions.getExpressions(); for (int i = 0; i < args.length; i++) { if (args[i] == expression) { if (i + maxParamCount >= args.length && method != null && method.getParameterList().getParametersCount() == i + 2 && method.getParameterList().getParameters()[i + 1].isVarArgs() && !hasArrayTypeAt(i + 1, methodCall)) { myProblems.add(myManager.createProblemDescriptor(methodCall, CodeInsightBundle.message("property.has.more.parameters.than.passed", key, maxParamCount, args.length - i - 1), onTheFly, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR)); } break; } } } }
@Override public void visitLiteralExpression(PsiLiteralExpression expression) { Object value = expression.getValue(); if (!(value instanceof String)) return; String key = (String)value; if (isComputablePropertyExpression(expression)) return; Ref<String> resourceBundleName = new Ref<String>(); if (!JavaI18nUtil.isValidPropertyReference(myManager.getProject(), expression, key, resourceBundleName)) { String bundleName = resourceBundleName.get(); if (bundleName != null) { // can be null if we were unable to resolve literal expression, e.g. when JDK was not set appendPropertyKeyNotFoundProblem(bundleName, key, expression, myManager, myProblems, onTheFly); } } else if (expression.getParent() instanceof PsiNameValuePair) { PsiNameValuePair nvp = (PsiNameValuePair)expression.getParent(); if (Comparing.equal(nvp.getName(), AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER)) { PropertiesReferenceManager manager = PropertiesReferenceManager.getInstance(expression.getProject()); Module module = ModuleUtil.findModuleForPsiElement(expression); if (module != null) { List<PropertiesFile> propFiles = manager.findPropertiesFiles(module, key); if (propFiles.isEmpty()) { final String description = CodeInsightBundle.message("inspection.invalid.resource.bundle.reference", key); final ProblemDescriptor problem = myManager.createProblemDescriptor(expression, description, (LocalQuickFix)null, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, onTheFly); myProblems.add(problem); } } } } else if (expression.getParent() instanceof PsiExpressionList && expression.getParent().getParent() instanceof PsiMethodCallExpression) { final Map<String, Object> annotationParams = new HashMap<String, Object>(); annotationParams.put(AnnotationUtil.PROPERTY_KEY_RESOURCE_BUNDLE_PARAMETER, null); if (!JavaI18nUtil.mustBePropertyKey(myManager.getProject(), expression, annotationParams)) return; final int paramsCount = JavaI18nUtil.getPropertyValueParamsMaxCount(expression); if (paramsCount == -1) return; final PsiExpressionList expressions = (PsiExpressionList)expression.getParent(); final PsiMethodCallExpression methodCall = (PsiMethodCallExpression)expressions.getParent(); final PsiMethod method = methodCall.resolveMethod(); final PsiExpression[] args = expressions.getExpressions(); for (int i = 0; i < args.length; i++) { if (args[i] == expression) { if (i + paramsCount >= args.length && method != null && method.getParameterList().getParametersCount() == i + 2 && method.getParameterList().getParameters()[i + 1].isVarArgs() && !hasArrayTypeAt(i + 1, methodCall)) { myProblems.add(myManager.createProblemDescriptor(methodCall, CodeInsightBundle.message("property.has.more.parameters.than.passed", key, paramsCount, args.length - i - 1), onTheFly, new LocalQuickFix[0], ProblemHighlightType.GENERIC_ERROR)); } break; } } } }