Skip to content

Commit fa775da

Browse files
committed
Inspections localizing and refactoring (part 2).
1 parent 1a6f29f commit fa775da

17 files changed

Lines changed: 2852 additions & 2828 deletions

‎java-properties-impl/src/main/java/consulo/java/properties/impl/i18n/I18nInspection.java‎

Lines changed: 793 additions & 793 deletions
Large diffs are not rendered by default.

‎java-properties-impl/src/main/java/consulo/java/properties/impl/psi/UnusedMessageFormatParameterInspection.java‎

Lines changed: 87 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
import com.intellij.lang.properties.PropertiesLanguage;
2121
import com.intellij.lang.properties.psi.PropertiesFile;
2222
import com.intellij.lang.properties.psi.Property;
23+
import consulo.annotation.access.RequiredReadAction;
2324
import consulo.annotation.component.ExtensionImpl;
2425
import consulo.ide.impl.idea.codeInsight.daemon.impl.quickfix.RenameElementFix;
2526
import consulo.language.Language;
2627
import consulo.language.ast.ASTNode;
2728
import consulo.language.editor.inspection.LocalQuickFix;
2829
import consulo.language.editor.inspection.ProblemDescriptor;
29-
import consulo.language.editor.inspection.ProblemHighlightType;
3030
import consulo.language.editor.inspection.scheme.InspectionManager;
3131
import consulo.language.psi.PsiElement;
3232
import consulo.language.psi.PsiFile;
@@ -41,78 +41,101 @@
4141
import java.util.Set;
4242

4343
/**
44-
* User: anna
45-
* Date: 07-Sep-2005
44+
* @since anna
45+
* @since 2005-09-07
4646
*/
4747
@ExtensionImpl
4848
public class UnusedMessageFormatParameterInspection extends BaseLocalInspectionTool {
49-
public static final String REGEXP = "regexp";
49+
public static final String REGEXP = "regexp";
5050

51-
@Nullable
52-
@Override
53-
public Language getLanguage() {
54-
return PropertiesLanguage.INSTANCE;
55-
}
51+
@Nullable
52+
@Override
53+
public Language getLanguage() {
54+
return PropertiesLanguage.INSTANCE;
55+
}
5656

57-
@Nonnull
58-
public LocalizeValue getDisplayName() {
59-
return PropertiesLocalize.unusedMessageFormatParameterDisplayName();
60-
}
57+
@Nonnull
58+
@Override
59+
public LocalizeValue getDisplayName() {
60+
return PropertiesLocalize.unusedMessageFormatParameterDisplayName();
61+
}
6162

62-
@Override
63-
@Nonnull
64-
public String getShortName() {
65-
return "UnusedMessageFormatParameter";
66-
}
63+
@Nonnull
64+
@Override
65+
public String getShortName() {
66+
return "UnusedMessageFormatParameter";
67+
}
6768

68-
@Nullable
69-
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly, Object state) {
70-
if (!(file instanceof PropertiesFile)) return null;
71-
PropertiesFile propertiesFile = (PropertiesFile) file;
72-
final List<IProperty> properties = propertiesFile.getProperties();
73-
List<ProblemDescriptor> problemDescriptors = new ArrayList<ProblemDescriptor>();
74-
for (IProperty property : properties) {
75-
String name = property.getName();
76-
if (name != null) {
77-
if (name.startsWith("log4j")) continue;
78-
if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP)) continue;
79-
}
80-
String value = property.getValue();
81-
Set<Integer> parameters = new HashSet<Integer>();
82-
if (value != null) {
83-
int index = value.indexOf('{');
84-
while (index != -1) {
85-
value = value.substring(index + 1);
86-
final int comma = value.indexOf(',');
87-
final int brace = value.indexOf('}');
88-
if (brace == -1) break; //misformatted string
89-
if (comma == -1) {
90-
index = brace;
91-
} else {
92-
index = Math.min(comma, brace);
93-
}
94-
try {
95-
parameters.add(Integer.valueOf(value.substring(0, index)));
96-
} catch (NumberFormatException e) {
97-
break;
98-
}
99-
index = value.indexOf('{');
69+
@Nullable
70+
@Override
71+
@RequiredReadAction
72+
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly, Object state) {
73+
if (!(file instanceof PropertiesFile)) {
74+
return null;
10075
}
101-
for (Integer integer : parameters) {
102-
for (int i = 0; i < integer; i++) {
103-
if (!parameters.contains(i)) {
104-
ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
105-
PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
106-
final String message = PropertiesLocalize.unusedMessageFormatParameterProblemDescriptor(integer.toString(), Integer.toString(i)).get();
107-
final String propertyKey = property.getKey();
108-
final LocalQuickFix[] fixes = isOnTheFly ? new LocalQuickFix[]{new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP)} : null;
109-
problemDescriptors.add(manager.createProblemDescriptor(valElement, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
110-
break;
76+
PropertiesFile propertiesFile = (PropertiesFile) file;
77+
List<IProperty> properties = propertiesFile.getProperties();
78+
List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
79+
for (IProperty property : properties) {
80+
String name = property.getName();
81+
if (name != null) {
82+
if (name.startsWith("log4j")) {
83+
continue;
84+
}
85+
if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP)) {
86+
continue;
87+
}
88+
}
89+
String value = property.getValue();
90+
Set<Integer> parameters = new HashSet<>();
91+
if (value != null) {
92+
int index = value.indexOf('{');
93+
while (index != -1) {
94+
value = value.substring(index + 1);
95+
int comma = value.indexOf(',');
96+
int brace = value.indexOf('}');
97+
if (brace == -1) {
98+
break; //misformatted string
99+
}
100+
if (comma == -1) {
101+
index = brace;
102+
}
103+
else {
104+
index = Math.min(comma, brace);
105+
}
106+
try {
107+
parameters.add(Integer.valueOf(value.substring(0, index)));
108+
}
109+
catch (NumberFormatException e) {
110+
break;
111+
}
112+
index = value.indexOf('{');
113+
}
114+
for (Integer integer : parameters) {
115+
for (int i = 0; i < integer; i++) {
116+
if (!parameters.contains(i)) {
117+
ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
118+
PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
119+
String propertyKey = property.getKey();
120+
LocalQuickFix fix = isOnTheFly
121+
? new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP)
122+
: null;
123+
problemDescriptors.add(
124+
manager.newProblemDescriptor(PropertiesLocalize.unusedMessageFormatParameterProblemDescriptor(
125+
integer.toString(),
126+
Integer.toString(i)
127+
))
128+
.range(valElement)
129+
.onTheFly(isOnTheFly)
130+
.withOptionalFix(fix)
131+
.create()
132+
);
133+
break;
134+
}
135+
}
136+
}
111137
}
112-
}
113138
}
114-
}
139+
return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
115140
}
116-
return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
117-
}
118141
}

‎plugin/src/main/java/com/intellij/java/impl/codeInspection/defaultFileTemplateUsage/FileHeaderChecker.java‎

Lines changed: 81 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717

1818
import com.intellij.java.language.impl.JavaFileType;
1919
import com.intellij.java.language.impl.codeInsight.template.JavaTemplateUtil;
20+
import consulo.annotation.access.RequiredReadAction;
2021
import consulo.annotation.access.RequiredWriteAction;
2122
import consulo.fileTemplate.FileTemplate;
2223
import consulo.fileTemplate.FileTemplateManager;
2324
import consulo.fileTemplate.FileTemplateUtil;
2425
import consulo.language.editor.inspection.LocalQuickFix;
2526
import consulo.language.editor.inspection.ProblemDescriptor;
26-
import consulo.language.editor.inspection.ProblemHighlightType;
2727
import consulo.language.editor.inspection.localize.InspectionLocalize;
2828
import consulo.language.editor.inspection.scheme.InspectionManager;
2929
import consulo.language.psi.PsiComment;
@@ -47,85 +47,94 @@
4747
* @author cdr
4848
*/
4949
public class FileHeaderChecker {
50-
private static final Logger LOG = Logger.getInstance(FileHeaderChecker.class);
51-
52-
static ProblemDescriptor checkFileHeader(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean onTheFly) {
53-
IntObjectMap<String> offsetToProperty = IntMaps.newIntObjectHashMap();
54-
FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);
55-
Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
56-
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
57-
if (!matcher.matches()) {
58-
return null;
59-
}
60-
61-
PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class);
62-
if (element == null) {
63-
return null;
64-
}
65-
66-
LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty, file.getProject(), onTheFly);
67-
String description = InspectionLocalize.defaultFileTemplateDescription().get();
68-
return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
69-
}
70-
50+
private static final Logger LOG = Logger.getInstance(FileHeaderChecker.class);
51+
52+
@RequiredReadAction
53+
static ProblemDescriptor checkFileHeader(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean onTheFly) {
54+
IntObjectMap<String> offsetToProperty = IntMaps.newIntObjectHashMap();
55+
FileTemplate defaultTemplate =
56+
FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);
57+
Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
58+
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
59+
if (!matcher.matches()) {
60+
return null;
61+
}
7162

72-
private static Properties computeProperties(final Matcher matcher, final IntObjectMap<String> offsetToProperty, Project project) {
73-
Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties());
63+
PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class);
64+
if (element == null) {
65+
return null;
66+
}
7467

75-
int[] offsets = offsetToProperty.keys();
76-
Arrays.sort(offsets);
77-
for (int i = 0; i < offsets.length; i++) {
78-
final int offset = offsets[i];
79-
String propName = offsetToProperty.get(offset);
80-
int groupNum = i + 2; // first group is whole doc comment
81-
String propValue = matcher.group(groupNum);
82-
properties.setProperty(propName, propValue);
68+
return manager.newProblemDescriptor(InspectionLocalize.defaultFileTemplateDescription())
69+
.range(element)
70+
.onTheFly(onTheFly)
71+
.withFixes(createQuickFixes(matcher, offsetToProperty, file.getProject(), onTheFly))
72+
.create();
8373
}
8474

85-
return properties;
86-
}
87-
88-
private static LocalQuickFix[] createQuickFix(final Matcher matcher, final IntObjectMap<String> offsetToProperty, Project project, boolean onTheFly) {
89-
final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);
90-
91-
ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() {
92-
@Override
93-
@RequiredWriteAction
94-
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
95-
PsiElement element = descriptor.getPsiElement();
96-
if (element == null) {
97-
return;
75+
private static Properties computeProperties(Matcher matcher, IntObjectMap<String> offsetToProperty, Project project) {
76+
Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties());
77+
78+
int[] offsets = offsetToProperty.keys();
79+
Arrays.sort(offsets);
80+
for (int i = 0; i < offsets.length; i++) {
81+
int offset = offsets[i];
82+
String propName = offsetToProperty.get(offset);
83+
int groupNum = i + 2; // first group is whole doc comment
84+
String propValue = matcher.group(groupNum);
85+
properties.setProperty(propName, propValue);
9886
}
9987

100-
String newText;
101-
try {
102-
newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim();
103-
} catch (IOException e) {
104-
LOG.error(e);
105-
return;
106-
}
88+
return properties;
89+
}
10790

108-
if (!newText.isEmpty()) {
109-
PsiElement parent = element.getParent();
110-
PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText);
111-
for (PsiElement child : tempFile.getChildren()) {
112-
if (child.getTextLength() > 0) {
113-
parent.addBefore(child, element);
91+
private static LocalQuickFix[] createQuickFixes(
92+
final Matcher matcher,
93+
final IntObjectMap<String> offsetToProperty,
94+
Project project,
95+
boolean onTheFly
96+
) {
97+
final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);
98+
99+
ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() {
100+
@Override
101+
@RequiredWriteAction
102+
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
103+
PsiElement element = descriptor.getPsiElement();
104+
if (element == null) {
105+
return;
106+
}
107+
108+
String newText;
109+
try {
110+
newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim();
111+
}
112+
catch (IOException e) {
113+
LOG.error(e);
114+
return;
115+
}
116+
117+
if (!newText.isEmpty()) {
118+
PsiElement parent = element.getParent();
119+
PsiFile tempFile =
120+
PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText);
121+
for (PsiElement child : tempFile.getChildren()) {
122+
if (child.getTextLength() > 0) {
123+
parent.addBefore(child, element);
124+
}
125+
}
126+
}
127+
128+
element.delete();
114129
}
115-
}
116-
}
130+
};
117131

118-
element.delete();
119-
}
120-
};
121-
122-
if (onTheFly) {
123-
LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
124-
return template.isDefault() ? new LocalQuickFix[]{editFileTemplateFix} : new LocalQuickFix[]{
125-
replaceTemplateFix,
126-
editFileTemplateFix
127-
};
132+
if (onTheFly) {
133+
LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
134+
return template.isDefault()
135+
? new LocalQuickFix[]{editFileTemplateFix}
136+
: new LocalQuickFix[]{replaceTemplateFix, editFileTemplateFix};
137+
}
138+
return template.isDefault() ? LocalQuickFix.EMPTY_ARRAY : new LocalQuickFix[]{replaceTemplateFix};
128139
}
129-
return template.isDefault() ? null : new LocalQuickFix[]{replaceTemplateFix};
130-
}
131140
}

0 commit comments

Comments
 (0)