mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-08 15:09:39 +07:00
[i18n] show relevant properties files in "I18nize hardcoded String" dialog (IDEA-233362)
GitOrigin-RevId: 2dc7d8f0783bad54ba727790818574836e171ba8
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b729ddc198
commit
bc4916a4fe
@@ -14,6 +14,8 @@ import com.intellij.lang.properties.references.I18nUtil;
|
||||
import com.intellij.lang.properties.references.I18nizeQuickFixDialog;
|
||||
import com.intellij.openapi.command.WriteCommandAction;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.ComboBox;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
@@ -69,6 +71,7 @@ public class I18nizeBatchQuickFix extends I18nizeQuickFix implements BatchQuickF
|
||||
Set<PsiElement> distinct = new HashSet<>();
|
||||
Map<String, ReplacementBean> keyValuePairs = new LinkedHashMap<>();
|
||||
UniqueNameGenerator uniqueNameGenerator = new UniqueNameGenerator();
|
||||
Set<Module> contextModules = new LinkedHashSet<>();
|
||||
for (CommonProblemDescriptor descriptor : descriptors) {
|
||||
PsiElement psiElement = ((ProblemDescriptor)descriptor).getPsiElement();
|
||||
ULiteralExpression literalExpression = UastUtils.findContaining(psiElement, ULiteralExpression.class);
|
||||
@@ -92,6 +95,7 @@ public class I18nizeBatchQuickFix extends I18nizeQuickFix implements BatchQuickF
|
||||
uExpressions.add(literalExpression);
|
||||
keyValuePairs.put(value, new ReplacementBean(uniqueNameGenerator.generateUniqueName(key), value, uExpressions, elements, Collections.emptyList()));
|
||||
}
|
||||
ContainerUtil.addIfNotNull(contextModules, ModuleUtilCore.findModuleForPsiElement(psiElement));
|
||||
}
|
||||
}
|
||||
else if (distinct.add(concatenation)) {
|
||||
@@ -105,6 +109,7 @@ public class I18nizeBatchQuickFix extends I18nizeQuickFix implements BatchQuickF
|
||||
Collections.singletonList(UastUtils.findContaining(concatenation, UPolyadicExpression.class)),
|
||||
Collections.singletonList(concatenation),
|
||||
ContainerUtil.map(args, arg -> UastUtils.findContaining(arg, UExpression.class))));
|
||||
ContainerUtil.addIfNotNull(contextModules, ModuleUtilCore.findModuleForPsiElement(psiElement));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -112,7 +117,7 @@ public class I18nizeBatchQuickFix extends I18nizeQuickFix implements BatchQuickF
|
||||
if (keyValuePairs.isEmpty()) return;
|
||||
|
||||
ArrayList<ReplacementBean> replacements = new ArrayList<>(keyValuePairs.values());
|
||||
I18NBatchDialog dialog = new I18NBatchDialog(project, replacements);
|
||||
I18NBatchDialog dialog = new I18NBatchDialog(project, replacements, contextModules);
|
||||
if (dialog.showAndGet()) {
|
||||
PropertiesFile propertiesFile = dialog.getPropertiesFile();
|
||||
Set<PsiFile> files = new HashSet<>();
|
||||
@@ -238,13 +243,17 @@ public class I18nizeBatchQuickFix extends I18nizeQuickFix implements BatchQuickF
|
||||
|
||||
@NotNull private final Project myProject;
|
||||
private final List<ReplacementBean> myKeyValuePairs;
|
||||
private final Set<Module> myContextModules;
|
||||
private JComboBox<String> myPropertiesFile;
|
||||
private UsagePreviewPanel myUsagePreviewPanel;
|
||||
|
||||
protected I18NBatchDialog(@NotNull Project project, List<ReplacementBean> keyValuePairs) {
|
||||
protected I18NBatchDialog(@NotNull Project project,
|
||||
List<ReplacementBean> keyValuePairs,
|
||||
Set<Module> contextModules) {
|
||||
super(project, true);
|
||||
myProject = project;
|
||||
myKeyValuePairs = keyValuePairs;
|
||||
myContextModules = contextModules;
|
||||
setTitle(PropertiesBundle.message("i18nize.dialog.title"));
|
||||
init();
|
||||
}
|
||||
@@ -257,7 +266,7 @@ public class I18nizeBatchQuickFix extends I18nizeQuickFix implements BatchQuickF
|
||||
@Nullable
|
||||
@Override
|
||||
protected JComponent createNorthPanel() {
|
||||
List<String> files = I18nUtil.defaultSuggestPropertiesFiles(myProject);
|
||||
List<String> files = I18nUtil.defaultSuggestPropertiesFiles(myProject, myContextModules);
|
||||
myPropertiesFile = new ComboBox<>(ArrayUtil.toStringArray(files));
|
||||
new ComboboxSpeedSearch(myPropertiesFile);
|
||||
LabeledComponent<JComboBox<String>> component = new LabeledComponent<>();
|
||||
|
||||
@@ -220,7 +220,7 @@ public class JavaI18nizeQuickFixDialog extends I18nizeQuickFixDialog {
|
||||
|
||||
@Override
|
||||
protected List<String> defaultSuggestPropertiesFiles() {
|
||||
return myResourceBundleManager.suggestPropertiesFiles();
|
||||
return myResourceBundleManager.suggestPropertiesFiles(myContextModules);
|
||||
}
|
||||
|
||||
public String getI18nizedText() {
|
||||
|
||||
@@ -4,6 +4,7 @@ package com.intellij.lang.properties.psi;
|
||||
import com.intellij.codeInsight.intention.IntentionAction;
|
||||
import com.intellij.lang.properties.references.I18nUtil;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiFile;
|
||||
@@ -12,6 +13,7 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public abstract class ResourceBundleManager {
|
||||
private static final ExtensionPointName<ResourceBundleManager> RESOURCE_BUNDLE_MANAGER = ExtensionPointName.create("com.intellij.java-i18n.resourceBundleManager");
|
||||
@@ -27,8 +29,8 @@ public abstract class ResourceBundleManager {
|
||||
@Nullable
|
||||
public abstract PsiClass getResourceBundle();
|
||||
|
||||
public List<String> suggestPropertiesFiles(){
|
||||
return I18nUtil.defaultSuggestPropertiesFiles(myProject);
|
||||
public List<String> suggestPropertiesFiles(Set<Module> contextModules){
|
||||
return I18nUtil.defaultSuggestPropertiesFiles(myProject, contextModules);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -4,12 +4,15 @@
|
||||
package com.intellij.lang.properties.references;
|
||||
|
||||
import com.intellij.lang.injection.InjectedLanguageManager;
|
||||
import com.intellij.lang.properties.BundleNameEvaluator;
|
||||
import com.intellij.lang.properties.IProperty;
|
||||
import com.intellij.lang.properties.PropertiesFileProcessor;
|
||||
import com.intellij.lang.properties.PropertiesReferenceManager;
|
||||
import com.intellij.lang.properties.psi.PropertiesFile;
|
||||
import com.intellij.lang.properties.xml.XmlPropertiesFile;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.roots.ProjectFileIndex;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
@@ -18,14 +21,13 @@ import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiDocumentManager;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class I18nUtil {
|
||||
@NotNull
|
||||
@@ -79,21 +81,36 @@ public class I18nUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static List<String> defaultSuggestPropertiesFiles(@NotNull Project project) {
|
||||
final List<String> paths = new ArrayList<>();
|
||||
public static List<String> defaultSuggestPropertiesFiles(@NotNull Project project,
|
||||
@NotNull Set<Module> contextModules) {
|
||||
List<String> relevantPaths = new ArrayList<>();
|
||||
List<String> otherPaths = new ArrayList<>();
|
||||
final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
|
||||
|
||||
PropertiesReferenceManager.getInstance(project).processAllPropertiesFiles((baseName, propertiesFile) -> {
|
||||
PropertiesFileProcessor processor = (baseName, propertiesFile) -> {
|
||||
if (propertiesFile instanceof XmlPropertiesFile) {
|
||||
return true;
|
||||
}
|
||||
VirtualFile virtualFile = propertiesFile.getVirtualFile();
|
||||
if (projectFileIndex.isInContent(virtualFile)) {
|
||||
String path = FileUtil.toSystemDependentName(virtualFile.getPath());
|
||||
paths.add(path);
|
||||
Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
|
||||
boolean relevant = contextModules.contains(module);
|
||||
(relevant ? relevantPaths : otherPaths).add(path);
|
||||
}
|
||||
return true;
|
||||
});
|
||||
return paths;
|
||||
};
|
||||
|
||||
if (contextModules.isEmpty()) {
|
||||
PropertiesReferenceManager.getInstance(project).processAllPropertiesFiles(processor);
|
||||
}
|
||||
else {
|
||||
GlobalSearchScope scope = GlobalSearchScope.union(ContainerUtil.map(contextModules, Module::getModuleWithDependenciesScope));
|
||||
PropertiesReferenceManager.getInstance(project).processPropertiesFiles(scope, processor, BundleNameEvaluator.DEFAULT);
|
||||
}
|
||||
Collections.sort(relevantPaths);
|
||||
Collections.sort(otherPaths);
|
||||
relevantPaths.addAll(otherPaths);
|
||||
return relevantPaths;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,13 +7,9 @@ import com.intellij.openapi.components.*;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.roots.ProjectFileIndex;
|
||||
import com.intellij.openapi.roots.ProjectRootManager;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.vfs.VfsUtilCore;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.vfs.VirtualFileManager;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.statistics.StatisticsInfo;
|
||||
import com.intellij.psi.statistics.StatisticsManager;
|
||||
import com.intellij.util.containers.hash.LinkedHashMap;
|
||||
import org.jdom.Element;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
@@ -29,8 +25,6 @@ import java.util.Map;
|
||||
storages = @Storage(value = "lastSelectedPropertiesFile.xml", roamingType = RoamingType.DISABLED)
|
||||
)
|
||||
public class LastSelectedPropertiesFileStore implements PersistentStateComponent<Element> {
|
||||
private static final String PROPERTIES_FILE_STATISTICS_KEY = "PROPERTIES_FILE";
|
||||
|
||||
private final Map<String, String> lastSelectedUrls = new LinkedHashMap<>();
|
||||
private String lastSelectedFileUrl;
|
||||
|
||||
@@ -60,10 +54,6 @@ public class LastSelectedPropertiesFileStore implements PersistentStateComponent
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int getUseCount(@NotNull String path) {
|
||||
return StatisticsManager.getInstance().getUseCount(new StatisticsInfo(PROPERTIES_FILE_STATISTICS_KEY, path));
|
||||
}
|
||||
|
||||
public void saveLastSelectedPropertiesFile(PsiFile context, PropertiesFile file) {
|
||||
VirtualFile virtualFile = context.getVirtualFile();
|
||||
if (virtualFile instanceof VirtualFileWindow) {
|
||||
@@ -78,7 +68,6 @@ public class LastSelectedPropertiesFileStore implements PersistentStateComponent
|
||||
VirtualFile containingDir = virtualFile.getParent();
|
||||
lastSelectedUrls.put(containingDir.getUrl(), url);
|
||||
lastSelectedFileUrl = url;
|
||||
StatisticsManager.getInstance().incUseCount(new StatisticsInfo(PROPERTIES_FILE_STATISTICS_KEY, FileUtil.toSystemDependentName(VfsUtilCore.urlToPath(url))));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,8 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.fileTypes.FileType;
|
||||
import com.intellij.openapi.fileTypes.FileTypeManager;
|
||||
import com.intellij.openapi.fileTypes.StdFileTypes;
|
||||
import com.intellij.openapi.module.Module;
|
||||
import com.intellij.openapi.module.ModuleUtilCore;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
import com.intellij.openapi.ui.Messages;
|
||||
@@ -34,6 +36,7 @@ import com.intellij.psi.impl.source.resolve.FileContextUtil;
|
||||
import com.intellij.ui.DocumentAdapter;
|
||||
import com.intellij.ui.GuiUtils;
|
||||
import com.intellij.ui.TextFieldWithHistory;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -45,10 +48,8 @@ import java.awt.event.ActionListener;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.text.Normalizer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -66,6 +67,7 @@ public class I18nizeQuickFixDialog extends DialogWrapper implements I18nizeQuick
|
||||
private JCheckBox myUseResourceBundle;
|
||||
protected final Project myProject;
|
||||
protected final PsiFile myContext;
|
||||
protected final Set<Module> myContextModules;
|
||||
|
||||
private JPanel myPropertiesFilePanel;
|
||||
protected JPanel myExtensibilityPanel;
|
||||
@@ -108,13 +110,14 @@ public class I18nizeQuickFixDialog extends DialogWrapper implements I18nizeQuick
|
||||
}
|
||||
|
||||
protected I18nizeQuickFixDialog(@NotNull Project project,
|
||||
@NotNull final PsiFile context,
|
||||
String defaultPropertyValue,
|
||||
DialogCustomization customization,
|
||||
boolean ancestorResponsible) {
|
||||
@NotNull final PsiFile context,
|
||||
String defaultPropertyValue,
|
||||
DialogCustomization customization,
|
||||
boolean ancestorResponsible) {
|
||||
super(false);
|
||||
myProject = project;
|
||||
myContext = FileContextUtil.getContextFile(context);
|
||||
myContextModules = ContainerUtil.createMaybeSingletonSet(ModuleUtilCore.findModuleForFile(myContext));
|
||||
|
||||
myDefaultPropertyValue = defaultPropertyValue;
|
||||
myCustomization = customization != null ? customization:new DialogCustomization();
|
||||
@@ -297,12 +300,10 @@ public class I18nizeQuickFixDialog extends DialogWrapper implements I18nizeQuick
|
||||
List<String> paths = suggestPropertiesFiles();
|
||||
final String lastUrl = suggestSelectedFileUrl(paths);
|
||||
final String lastPath = lastUrl == null ? null : FileUtil.toSystemDependentName(VfsUtil.urlToPath(lastUrl));
|
||||
Collections.sort(paths, (path1, path2) -> {
|
||||
if (lastPath != null && lastPath.equals(path1)) return -1;
|
||||
if (lastPath != null && lastPath.equals(path2)) return 1;
|
||||
int r = LastSelectedPropertiesFileStore.getUseCount(path2) - LastSelectedPropertiesFileStore.getUseCount(path1);
|
||||
return r == 0 ? path1.compareTo(path2) : r;
|
||||
});
|
||||
if (lastPath != null) {
|
||||
paths.remove(lastPath);
|
||||
paths.add(0, lastPath);
|
||||
}
|
||||
myPropertiesFile.setHistory(paths);
|
||||
if (lastPath != null) {
|
||||
myPropertiesFile.setSelectedItem(lastPath);
|
||||
@@ -349,7 +350,7 @@ public class I18nizeQuickFixDialog extends DialogWrapper implements I18nizeQuick
|
||||
}
|
||||
|
||||
protected List<String> defaultSuggestPropertiesFiles() {
|
||||
return I18nUtil.defaultSuggestPropertiesFiles(myProject);
|
||||
return I18nUtil.defaultSuggestPropertiesFiles(myProject, myContextModules);
|
||||
}
|
||||
|
||||
protected PropertiesFile getPropertiesFile() {
|
||||
|
||||
Reference in New Issue
Block a user