Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import com.intellij.lang.properties.PropertiesLanguage;
import com.intellij.lang.properties.psi.PropertiesFile;
import com.intellij.lang.properties.psi.Property;
import consulo.annotation.access.RequiredReadAction;
import consulo.annotation.component.ExtensionImpl;
import consulo.ide.impl.idea.codeInsight.daemon.impl.quickfix.RenameElementFix;
import consulo.language.Language;
import consulo.language.ast.ASTNode;
import consulo.language.editor.inspection.LocalQuickFix;
import consulo.language.editor.inspection.ProblemDescriptor;
import consulo.language.editor.inspection.ProblemHighlightType;
import consulo.language.editor.inspection.scheme.InspectionManager;
import consulo.language.psi.PsiElement;
import consulo.language.psi.PsiFile;
Expand All @@ -41,78 +41,101 @@
import java.util.Set;

/**
* User: anna
* Date: 07-Sep-2005
* @since anna
* @since 2005-09-07
*/
@ExtensionImpl
public class UnusedMessageFormatParameterInspection extends BaseLocalInspectionTool {
public static final String REGEXP = "regexp";
public static final String REGEXP = "regexp";

@Nullable
@Override
public Language getLanguage() {
return PropertiesLanguage.INSTANCE;
}
@Nullable
@Override
public Language getLanguage() {
return PropertiesLanguage.INSTANCE;
}

@Nonnull
public LocalizeValue getDisplayName() {
return PropertiesLocalize.unusedMessageFormatParameterDisplayName();
}
@Nonnull
@Override
public LocalizeValue getDisplayName() {
return PropertiesLocalize.unusedMessageFormatParameterDisplayName();
}

@Override
@Nonnull
public String getShortName() {
return "UnusedMessageFormatParameter";
}
@Nonnull
@Override
public String getShortName() {
return "UnusedMessageFormatParameter";
}

@Nullable
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly, Object state) {
if (!(file instanceof PropertiesFile)) return null;
PropertiesFile propertiesFile = (PropertiesFile) file;
final List<IProperty> properties = propertiesFile.getProperties();
List<ProblemDescriptor> problemDescriptors = new ArrayList<ProblemDescriptor>();
for (IProperty property : properties) {
String name = property.getName();
if (name != null) {
if (name.startsWith("log4j")) continue;
if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP)) continue;
}
String value = property.getValue();
Set<Integer> parameters = new HashSet<Integer>();
if (value != null) {
int index = value.indexOf('{');
while (index != -1) {
value = value.substring(index + 1);
final int comma = value.indexOf(',');
final int brace = value.indexOf('}');
if (brace == -1) break; //misformatted string
if (comma == -1) {
index = brace;
} else {
index = Math.min(comma, brace);
}
try {
parameters.add(Integer.valueOf(value.substring(0, index)));
} catch (NumberFormatException e) {
break;
}
index = value.indexOf('{');
@Nullable
@Override
@RequiredReadAction
public ProblemDescriptor[] checkFile(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean isOnTheFly, Object state) {
if (!(file instanceof PropertiesFile)) {
return null;
}
for (Integer integer : parameters) {
for (int i = 0; i < integer; i++) {
if (!parameters.contains(i)) {
ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
final String message = PropertiesLocalize.unusedMessageFormatParameterProblemDescriptor(integer.toString(), Integer.toString(i)).get();
final String propertyKey = property.getKey();
final LocalQuickFix[] fixes = isOnTheFly ? new LocalQuickFix[]{new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP)} : null;
problemDescriptors.add(manager.createProblemDescriptor(valElement, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
break;
PropertiesFile propertiesFile = (PropertiesFile) file;
List<IProperty> properties = propertiesFile.getProperties();
List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
for (IProperty property : properties) {
String name = property.getName();
if (name != null) {
if (name.startsWith("log4j")) {
continue;
}
if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP)) {
continue;
}
}
String value = property.getValue();
Set<Integer> parameters = new HashSet<>();
if (value != null) {
int index = value.indexOf('{');
while (index != -1) {
value = value.substring(index + 1);
int comma = value.indexOf(',');
int brace = value.indexOf('}');
if (brace == -1) {
break; //misformatted string
}
if (comma == -1) {
index = brace;
}
else {
index = Math.min(comma, brace);
}
try {
parameters.add(Integer.valueOf(value.substring(0, index)));
}
catch (NumberFormatException e) {
break;
}
index = value.indexOf('{');
}
for (Integer integer : parameters) {
for (int i = 0; i < integer; i++) {
if (!parameters.contains(i)) {
ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
String propertyKey = property.getKey();
LocalQuickFix fix = isOnTheFly
? new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP)
: null;
problemDescriptors.add(
manager.newProblemDescriptor(PropertiesLocalize.unusedMessageFormatParameterProblemDescriptor(
integer.toString(),
Integer.toString(i)
))
.range(valElement)
.onTheFly(isOnTheFly)
.withOptionalFix(fix)
.create()
);
break;
}
}
}
}
}
}
}
return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@

import com.intellij.java.language.impl.JavaFileType;
import com.intellij.java.language.impl.codeInsight.template.JavaTemplateUtil;
import consulo.annotation.access.RequiredReadAction;
import consulo.annotation.access.RequiredWriteAction;
import consulo.fileTemplate.FileTemplate;
import consulo.fileTemplate.FileTemplateManager;
import consulo.fileTemplate.FileTemplateUtil;
import consulo.language.editor.inspection.LocalQuickFix;
import consulo.language.editor.inspection.ProblemDescriptor;
import consulo.language.editor.inspection.ProblemHighlightType;
import consulo.language.editor.inspection.localize.InspectionLocalize;
import consulo.language.editor.inspection.scheme.InspectionManager;
import consulo.language.psi.PsiComment;
Expand All @@ -47,85 +47,94 @@
* @author cdr
*/
public class FileHeaderChecker {
private static final Logger LOG = Logger.getInstance(FileHeaderChecker.class);

static ProblemDescriptor checkFileHeader(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean onTheFly) {
IntObjectMap<String> offsetToProperty = IntMaps.newIntObjectHashMap();
FileTemplate defaultTemplate = FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);
Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
if (!matcher.matches()) {
return null;
}

PsiComment element = PsiTreeUtil.findElementOfClassAtRange(file, matcher.start(1), matcher.end(1), PsiComment.class);
if (element == null) {
return null;
}

LocalQuickFix[] fixes = createQuickFix(matcher, offsetToProperty, file.getProject(), onTheFly);
String description = InspectionLocalize.defaultFileTemplateDescription().get();
return manager.createProblemDescriptor(element, description, onTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}

private static final Logger LOG = Logger.getInstance(FileHeaderChecker.class);

@RequiredReadAction
static ProblemDescriptor checkFileHeader(@Nonnull PsiFile file, @Nonnull InspectionManager manager, boolean onTheFly) {
IntObjectMap<String> offsetToProperty = IntMaps.newIntObjectHashMap();
FileTemplate defaultTemplate =
FileTemplateManager.getInstance(file.getProject()).getDefaultTemplate(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);
Pattern pattern = FileTemplateUtil.getTemplatePattern(defaultTemplate, file.getProject(), offsetToProperty);
Matcher matcher = pattern.matcher(file.getViewProvider().getContents());
if (!matcher.matches()) {
return null;
}

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

int[] offsets = offsetToProperty.keys();
Arrays.sort(offsets);
for (int i = 0; i < offsets.length; i++) {
final int offset = offsets[i];
String propName = offsetToProperty.get(offset);
int groupNum = i + 2; // first group is whole doc comment
String propValue = matcher.group(groupNum);
properties.setProperty(propName, propValue);
return manager.newProblemDescriptor(InspectionLocalize.defaultFileTemplateDescription())
.range(element)
.onTheFly(onTheFly)
.withFixes(createQuickFixes(matcher, offsetToProperty, file.getProject(), onTheFly))
.create();
}

return properties;
}

private static LocalQuickFix[] createQuickFix(final Matcher matcher, final IntObjectMap<String> offsetToProperty, Project project, boolean onTheFly) {
final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);

ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() {
@Override
@RequiredWriteAction
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element == null) {
return;
private static Properties computeProperties(Matcher matcher, IntObjectMap<String> offsetToProperty, Project project) {
Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties());

int[] offsets = offsetToProperty.keys();
Arrays.sort(offsets);
for (int i = 0; i < offsets.length; i++) {
int offset = offsets[i];
String propName = offsetToProperty.get(offset);
int groupNum = i + 2; // first group is whole doc comment
String propValue = matcher.group(groupNum);
properties.setProperty(propName, propValue);
}

String newText;
try {
newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim();
} catch (IOException e) {
LOG.error(e);
return;
}
return properties;
}

if (!newText.isEmpty()) {
PsiElement parent = element.getParent();
PsiFile tempFile = PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText);
for (PsiElement child : tempFile.getChildren()) {
if (child.getTextLength() > 0) {
parent.addBefore(child, element);
private static LocalQuickFix[] createQuickFixes(
final Matcher matcher,
final IntObjectMap<String> offsetToProperty,
Project project,
boolean onTheFly
) {
final FileTemplate template = FileTemplateManager.getInstance(project).getPattern(JavaTemplateUtil.FILE_HEADER_TEMPLATE_NAME);

ReplaceWithFileTemplateFix replaceTemplateFix = new ReplaceWithFileTemplateFix() {
@Override
@RequiredWriteAction
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
if (element == null) {
return;
}

String newText;
try {
newText = template.getText(computeProperties(matcher, offsetToProperty, project)).trim();
}
catch (IOException e) {
LOG.error(e);
return;
}

if (!newText.isEmpty()) {
PsiElement parent = element.getParent();
PsiFile tempFile =
PsiFileFactory.getInstance(project).createFileFromText("template.java", JavaFileType.INSTANCE, newText);
for (PsiElement child : tempFile.getChildren()) {
if (child.getTextLength() > 0) {
parent.addBefore(child, element);
}
}
}

element.delete();
}
}
}
};

element.delete();
}
};

if (onTheFly) {
LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
return template.isDefault() ? new LocalQuickFix[]{editFileTemplateFix} : new LocalQuickFix[]{
replaceTemplateFix,
editFileTemplateFix
};
if (onTheFly) {
LocalQuickFix editFileTemplateFix = DefaultFileTemplateUsageInspection.createEditFileTemplateFix(template, replaceTemplateFix);
return template.isDefault()
? new LocalQuickFix[]{editFileTemplateFix}
: new LocalQuickFix[]{replaceTemplateFix, editFileTemplateFix};
}
return template.isDefault() ? LocalQuickFix.EMPTY_ARRAY : new LocalQuickFix[]{replaceTemplateFix};
}
return template.isDefault() ? null : new LocalQuickFix[]{replaceTemplateFix};
}
}
Loading
Loading