JavaCreatePropertyFix replaces key literal by newly created one

This commit is contained in:
Maxim.Medvedev
2012-05-10 11:09:35 +04:00
parent a458b2d49e
commit b172063f8f
2 changed files with 47 additions and 17 deletions
@@ -18,11 +18,15 @@ 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.lang.properties.references.I18nizeQuickFixModel;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
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 com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.*;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -32,19 +36,47 @@ import java.util.List;
* @author Maxim.Mossienko
*/
public class JavaCreatePropertyFix extends CreatePropertyFix {
private static final Logger LOG = Logger.getInstance(JavaCreatePropertyFix.class);
public JavaCreatePropertyFix() {}
public JavaCreatePropertyFix(PsiElement element, String key, final List<PropertiesFile> propertiesFiles) {
super(element, key, propertiesFiles);
}
@Override
protected Pair<String, String> doAction(Project project, PsiElement psiElement, I18nizeQuickFixModel model) {
final Pair<String, String> result = super.doAction(project, psiElement, model);
if (result != null && psiElement instanceof PsiLiteralExpression) {
final String key = result.first;
final StringBuilder buffer = new StringBuilder();
buffer.append('"');
StringUtil.escapeStringCharacters(key.length(), key, buffer);
buffer.append('"');
final AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(JavaCreatePropertyFix.class);
try {
final PsiExpression newKeyLiteral = JavaPsiFacade.getElementFactory(project).createExpressionFromText(buffer.toString(), null);
psiElement.replace(newKeyLiteral);
}
catch (IncorrectOperationException e) {
LOG.error(e);
}
finally {
token.finish();
}
}
return result;
}
@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) {
protected 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;
@@ -59,5 +91,4 @@ public class JavaCreatePropertyFix extends CreatePropertyFix {
);
return doAction(project, psiElement, dialog);
}
}
@@ -88,11 +88,11 @@ public class CreatePropertyFix implements IntentionAction, LocalQuickFix {
}
@Nullable
private static Pair<String, String> invokeAction(@NotNull final Project project,
@NotNull PsiFile file,
@NotNull PsiElement psiElement,
@Nullable final String suggestedKey,
@Nullable final List<PropertiesFile> propertiesFiles) {
private Pair<String, String> invokeAction(@NotNull final Project project,
@NotNull PsiFile file,
@NotNull PsiElement psiElement,
@Nullable final String suggestedKey,
@Nullable final List<PropertiesFile> propertiesFiles) {
final I18nizeQuickFixModel model;
final I18nizeQuickFixDialog.DialogCustomization dialogCustomization = createDefaultCustomization(suggestedKey, propertiesFiles);
@@ -128,8 +128,7 @@ public class CreatePropertyFix implements IntentionAction, LocalQuickFix {
return new I18nizeQuickFixDialog.DialogCustomization(NAME, false, true, propertiesFiles, suggestedKey == null ? "" : suggestedKey);
}
protected static Pair<String, String> doAction(Project project, PsiElement psiElement,
I18nizeQuickFixModel model) {
protected Pair<String, String> doAction(Project project, PsiElement psiElement, I18nizeQuickFixModel model) {
if (!model.hasValidData()) {
return null;
}