Skip to content

Commit

Permalink
supporting property references (IDEADEV-34519)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Mossienko authored and Maxim Mossienko committed Feb 11, 2009
1 parent c0671e2 commit fb71f10
Show file tree
Hide file tree
Showing 108 changed files with 764 additions and 458 deletions.
Expand Up @@ -4,7 +4,7 @@
import com.intellij.codeInsight.daemon.GroupNames;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.ex.BaseLocalInspectionTool;
import com.intellij.codeInspection.i18n.I18nUtil;
import com.intellij.codeInspection.i18n.JavaI18nUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Comparing;
Expand Down Expand Up @@ -193,7 +193,7 @@ public String fun(final PsiClass aClass) {
}

private boolean shouldCheck(final PsiLiteralExpression expression) {
if (IGNORE_PROPERTY_KEYS && I18nUtil.mustBePropertyKey(expression, new THashMap<String, Object>())) return false;
if (IGNORE_PROPERTY_KEYS && JavaI18nUtil.mustBePropertyKey(expression, new THashMap<String, Object>())) return false;
PsiAnnotation annotation = PsiTreeUtil.getParentOfType(expression, PsiAnnotation.class, true, PsiCodeBlock.class, PsiField.class);
return annotation == null || !"java.lang.SuppressWarnings".equals(annotation.getQualifiedName());
}
Expand Down
Expand Up @@ -454,7 +454,7 @@ private boolean canBeI18ned(PsiLiteralExpression expression, String value, final
nonNlsTargets.add(parentField);
}

if (I18nUtil.isPassedToAnnotatedParam(expression, AnnotationUtil.NON_NLS, new HashMap<String, Object>(), nonNlsTargets)) {
if (JavaI18nUtil.isPassedToAnnotatedParam(expression, AnnotationUtil.NON_NLS, new HashMap<String, Object>(), nonNlsTargets)) {
return false;
}

Expand All @@ -470,7 +470,7 @@ private boolean canBeI18ned(PsiLiteralExpression expression, String value, final
return false;
}

if (I18nUtil.mustBePropertyKey(expression, new HashMap<String, Object>())) {
if (JavaI18nUtil.mustBePropertyKey(expression, new HashMap<String, Object>())) {
return false;
}

Expand All @@ -492,7 +492,7 @@ private boolean canBeI18ned(PsiLiteralExpression expression, String value, final
if (ignoreForClassReferences && isClassRef(expression, value)) {
return false;
}
if (ignoreForPropertyKeyReferences && I18nUtil.isPropertyRef(expression, value, null)) {
if (ignoreForPropertyKeyReferences && JavaI18nUtil.isPropertyRef(expression, value, null)) {
return false;
}
if (ignoreToString && isToString(expression)) {
Expand Down Expand Up @@ -566,7 +566,7 @@ public static boolean isPackageNonNls(final PsiPackage psiPackage) {
}

private boolean isPassedToNonNlsVariable(final PsiLiteralExpression expression, final Set<PsiModifierListOwner> nonNlsTargets) {
PsiExpression toplevel = I18nUtil.getToplevelExpression(expression);
PsiExpression toplevel = JavaI18nUtil.getToplevelExpression(expression);
PsiVariable var = null;
if (toplevel instanceof PsiAssignmentExpression) {
PsiExpression lExpression = ((PsiAssignmentExpression)toplevel).getLExpression();
Expand Down Expand Up @@ -607,7 +607,7 @@ private static boolean annotatedAsNonNls(final PsiModifierListOwner parent) {
if (declarationScope instanceof PsiMethod) {
final PsiMethod method = (PsiMethod)declarationScope;
final int index = method.getParameterList().getParameterIndex(parameter);
return I18nUtil.isMethodParameterAnnotatedWith(method, index, new HashSet<PsiMethod>(), AnnotationUtil.NON_NLS,
return JavaI18nUtil.isMethodParameterAnnotatedWith(method, index, new HashSet<PsiMethod>(), AnnotationUtil.NON_NLS,
new HashMap<String, Object>(), null);
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ private static boolean isInNonNlsEquals(PsiExpression expression, final Set<PsiM
}

private static boolean isInNonNlsCall(PsiExpression expression, final Set<PsiModifierListOwner> nonNlsTargets) {
expression = I18nUtil.getToplevelExpression(expression);
expression = JavaI18nUtil.getToplevelExpression(expression);
final PsiElement parent = expression.getParent();
if (parent instanceof PsiExpressionList) {
final PsiElement grParent = parent.getParent();
Expand Down
Expand Up @@ -27,5 +27,5 @@ void performI18nization(final PsiFile psiFile,
PsiExpression[] parameters,
PropertyCreationHandler propertyCreationHandler) throws IncorrectOperationException;

I18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile);
JavaI18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile);
}
Expand Up @@ -148,7 +148,7 @@ public void actionPerformed(AnActionEvent e) {
return;
}

final I18nizeQuickFixDialog dialog = handler.createDialog(project, editor, psiFile);
final JavaI18nizeQuickFixDialog dialog = handler.createDialog(project, editor, psiFile);
if (dialog == null) return;
dialog.show();
if (!dialog.isOK()) return;
Expand Down Expand Up @@ -177,13 +177,13 @@ public void run() {
});
}

public I18nizeQuickFixDialog createDialog(final Project project, final Editor editor, final PsiFile psiFile) {
public JavaI18nizeQuickFixDialog createDialog(final Project project, final Editor editor, final PsiFile psiFile) {
JspFile jspFile = (JspFile)psiFile;

TextRange selectedRange = getSelectedRange(editor, psiFile);
if (selectedRange == null) return null;
String text = selectedRange.substring(editor.getDocument().getText());
return new I18nizeQuickFixDialog(project, jspFile, null, text, false, true){
return new JavaI18nizeQuickFixDialog(project, jspFile, null, text, null, false, true){
protected String getTemplateName() {
return JavaTemplateUtil.TEMPLATE_I18NIZED_JSP_EXPRESSION;
}
Expand Down
Expand Up @@ -32,7 +32,7 @@ public void checkApplicability(final PsiFile psiFile, final Editor editor) throw
throw new IncorrectOperationException(message);
}

public I18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile) {
public JavaI18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile) {
PsiBinaryExpression concatenation = ConcatenationToMessageFormatAction.getEnclosingLiteralConcatenation(psiFile,editor);
PsiLiteralExpression literalExpression = ConcatenationToMessageFormatAction.getContainingLiteral(concatenation);
if (literalExpression == null) return null;
Expand Down Expand Up @@ -65,7 +65,7 @@ private static String composeParametersText(final List<PsiExpression> args) {
return result.toString();
}

protected I18nizeQuickFixDialog createDialog(final Project project, final PsiFile context, final PsiLiteralExpression literalExpression) {
protected JavaI18nizeQuickFixDialog createDialog(final Project project, final PsiFile context, final PsiLiteralExpression literalExpression) {
PsiBinaryExpression concatenation = ConcatenationToMessageFormatAction.getEnclosingLiteralConcatenation(literalExpression);
StringBuffer formatString = new StringBuffer();
final List<PsiExpression> args = new ArrayList<PsiExpression>();
Expand All @@ -79,7 +79,7 @@ protected I18nizeQuickFixDialog createDialog(final Project project, final PsiFil

String value = ConcatenationToMessageFormatAction.prepareString(formatString.toString());

return new I18nizeQuickFixDialog(project, context, literalExpression, value, true, true) {
return new JavaI18nizeQuickFixDialog(project, context, literalExpression, value, null, true, true) {
@Nullable
protected String getTemplateName() {
return myResourceBundleManager.getConcatenationTemplateName();
Expand Down
Expand Up @@ -90,18 +90,18 @@ public void performI18nization(final PsiFile psiFile,
}
}

public I18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile) {
public JavaI18nizeQuickFixDialog createDialog(Project project, Editor editor, PsiFile psiFile) {
final PsiLiteralExpression literalExpression = I18nizeAction.getEnclosingStringLiteral(psiFile, editor);
return createDialog(project, psiFile, literalExpression);
}

private void doFix(final ProblemDescriptor descriptor, final Project project) {
final PsiLiteralExpression literalExpression = (PsiLiteralExpression)descriptor.getPsiElement();
final PsiFile psiFile = literalExpression.getContainingFile();
if (!I18nizeQuickFixDialog.isAvailable(psiFile)) {
if (!JavaI18nizeQuickFixDialog.isAvailable(psiFile)) {
return;
}
final I18nizeQuickFixDialog dialog = createDialog(project, psiFile, literalExpression);
final JavaI18nizeQuickFixDialog dialog = createDialog(project, psiFile, literalExpression);
dialog.show();
if (!dialog.isOK()) return;
final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles();
Expand Down Expand Up @@ -130,9 +130,12 @@ public void run() {
}

private static Editor getEditorForFile(@NotNull final PsiFile psiFile) {
VirtualFile virtualFile = psiFile.getOriginalFile().getVirtualFile();
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) {
return null;
PsiFile originalFile = psiFile.getOriginalFile();
if (originalFile == null) return null;
virtualFile = originalFile.getVirtualFile();
if (virtualFile == null) return null;
}
final FileEditor[] editors = FileEditorManager.getInstance(psiFile.getProject()).getEditors(virtualFile);
for (FileEditor editor : editors) {
Expand All @@ -156,15 +159,15 @@ private static void reformatAndCorrectReferences(PsiElement newExpression) throw
CodeStyleManager.getInstance(project).reformat(newExpression);
}

protected I18nizeQuickFixDialog createDialog(final Project project, final PsiFile context, final PsiLiteralExpression literalExpression) {
protected JavaI18nizeQuickFixDialog createDialog(final Project project, final PsiFile context, final PsiLiteralExpression literalExpression) {
String value = (String)literalExpression.getValue();
if (mySelectionRange != null) {
TextRange literalRange = literalExpression.getTextRange();
TextRange intersection = literalRange.intersection(mySelectionRange);
value = literalExpression.getText().substring(intersection.getStartOffset() - literalRange.getStartOffset(), intersection.getEndOffset() - literalRange.getStartOffset());
}
value = StringUtil.escapeStringCharacters(value);
return new I18nizeQuickFixDialog(project, context, literalExpression, value, true, true);
return new JavaI18nizeQuickFixDialog(project, context, literalExpression, value, null, true, true);
}

@Nullable
Expand Down Expand Up @@ -207,4 +210,4 @@ private PsiElement replaceStringLiteral(PsiLiteralExpression literalExpression,
return literalExpression.replace(expression);
}

}
}
Expand Up @@ -134,13 +134,13 @@ public UnresolvedPropertyVisitor(final InspectionManager manager) {
String key = (String)value;
if (isComputablePropertyExpression(expression)) return;
Ref<String> resourceBundleName = new Ref<String>();
if (!I18nUtil.isValidPropertyReference(expression, key, resourceBundleName)) {
if (!JavaI18nUtil.isValidPropertyReference(expression, key, resourceBundleName)) {
final String description = CodeInsightBundle.message("inspection.unresolved.property.key.reference.message", key);
final String bundleName = resourceBundleName.get();
final List<PropertiesFile> propertiesFiles = I18nUtil.propertiesFilesByBundleName(bundleName, expression);
final List<PropertiesFile> propertiesFiles = JavaI18nUtil.propertiesFilesByBundleName(bundleName, expression);
final ProblemDescriptor problem = myManager.createProblemDescriptor(expression,
description,
new CreatePropertyFix(expression, key, propertiesFiles),
new JavaCreatePropertyFix(expression, key, propertiesFiles),
ProblemHighlightType.LIKE_UNKNOWN_SYMBOL);
myProblems.add(problem);
}
Expand Down
@@ -0,0 +1,52 @@
package com.intellij.codeInspection.i18n;

import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.references.CreatePropertyFix;
import com.intellij.lang.properties.references.I18nizeQuickFixDialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiLiteralExpression;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

/**
* Created by IntelliJ IDEA.
* User: Maxim.Mossienko
* Date: 06.02.2009
* Time: 21:13:07
* To change this template use File | Settings | File Templates.
*/
public class JavaCreatePropertyFix extends CreatePropertyFix {
public JavaCreatePropertyFix() {}

public JavaCreatePropertyFix(PsiElement element, String key, final List<PropertiesFile> propertiesFiles) {
super(element, key, propertiesFiles);
}

@Nullable
protected static Pair<String, String> invokeAction(@NotNull final Project project,
@NotNull PsiFile file,
@NotNull PsiElement psiElement,
@Nullable final String suggestedKey,
@Nullable String suggestedValue,
@Nullable final List<PropertiesFile> propertiesFiles) {
final PsiLiteralExpression literalExpression = psiElement instanceof PsiLiteralExpression ? (PsiLiteralExpression)psiElement : null;
final String propertyValue = suggestedValue == null ? "" : suggestedValue;

final I18nizeQuickFixDialog dialog = new JavaI18nizeQuickFixDialog(
project,
file,
literalExpression,
propertyValue,
createDefaultCustomization(suggestedKey, propertiesFiles),
false,
false
);
return doAction(project, psiElement, dialog);
}

}
Expand Up @@ -5,17 +5,12 @@

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.codeInsight.template.macro.MacroUtil;
import com.intellij.lang.properties.PropertiesReferenceManager;
import com.intellij.lang.properties.references.I18nUtil;
import com.intellij.lang.properties.PropertiesUtil;
import com.intellij.lang.properties.psi.PropertiesElementFactory;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.psi.Property;
import com.intellij.lang.properties.psi.PropertyCreationHandler;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Ref;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.*;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.scope.util.PsiScopesUtil;
Expand All @@ -32,15 +27,15 @@
/**
* @author max
*/
public class I18nUtil {
public class JavaI18nUtil extends I18nUtil {
public static final PropertyCreationHandler DEFAULT_PROPERTY_CREATION_HANDLER = new PropertyCreationHandler() {
public void createProperty(final Project project, final Collection<PropertiesFile> propertiesFiles, final String key, final String value,
final PsiExpression[] parameters) throws IncorrectOperationException {
I18nUtil.createProperty(project, propertiesFiles, key, value);
JavaI18nUtil.createProperty(project, propertiesFiles, key, value);
}
};

private I18nUtil() {
private JavaI18nUtil() {
}

public static boolean mustBePropertyKey(final PsiLiteralExpression expression, @NotNull Map<String, Object> annotationAttributeValues) {
Expand Down Expand Up @@ -176,26 +171,6 @@ public static boolean isPropertyRef(final PsiLiteralExpression expression, final
}
}

@NotNull
public static List<PropertiesFile> propertiesFilesByBundleName(final String resourceBundleName, final PsiElement context) {
final PsiFile containingFile = context.getContainingFile();
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile == null) {
final PsiFile originalFile = containingFile.getOriginalFile();
if (originalFile != null) {
virtualFile = originalFile.getVirtualFile();
}
}
if (virtualFile != null) {
final Module module = ProjectRootManager.getInstance(context.getProject()).getFileIndex().getModuleForFile(virtualFile);
if (module != null) {
PropertiesReferenceManager refManager = context.getProject().getComponent(PropertiesReferenceManager.class);
return refManager.findPropertiesFiles(module, resourceBundleName);
}
}
return Collections.emptyList();
}

public static Set<String> suggestExpressionOfType(final PsiClassType type, final PsiLiteralExpression context) {
PsiVariable[] variables = MacroUtil.getVariablesVisibleAt(context, "");
Set<String> result = new LinkedHashSet<String>();
Expand Down Expand Up @@ -239,21 +214,4 @@ public void handleEvent(Event event, Object associated) {
}
}, context, null);
}

public static void createProperty(final Project project,
final Collection<PropertiesFile> propertiesFiles,
final String key,
final String value)
throws IncorrectOperationException {
Property property = PropertiesElementFactory.createProperty(project, key, value);
for (PropertiesFile file : propertiesFiles) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.commitDocument(documentManager.getDocument(file));

Property existingProperty = file.findPropertyByKey(property.getUnescapedKey());
if (existingProperty == null) {
file.addProperty(property);
}
}
}
}

0 comments on commit fb71f10

Please sign in to comment.