Skip to content

Commit 6b25135

Browse files
authored
Localizing inspections and fixes (#4)
* Localizing inspections and fixes. * Localizing XPathInspection. Refactoring.
1 parent cdfa068 commit 6b25135

12 files changed

Lines changed: 522 additions & 433 deletions

File tree

xpath-lang/src/main/java/org/intellij/lang/xpath/validation/ExpressionReplacementFix.java

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,53 @@
1515
*/
1616
package org.intellij.lang.xpath.validation;
1717

18+
import consulo.annotation.access.RequiredReadAction;
19+
import consulo.annotation.access.RequiredWriteAction;
1820
import consulo.codeEditor.Editor;
19-
import consulo.language.editor.intention.IntentionAction;
2021
import consulo.language.editor.intention.SyntheticIntentionAction;
2122
import consulo.language.psi.PsiFile;
2223
import consulo.language.util.IncorrectOperationException;
24+
import consulo.localize.LocalizeValue;
2325
import consulo.project.Project;
26+
import jakarta.annotation.Nonnull;
2427
import org.intellij.lang.xpath.psi.XPathExpression;
2528
import org.intellij.lang.xpath.psi.impl.XPathChangeUtil;
2629

27-
import jakarta.annotation.Nonnull;
28-
2930
class ExpressionReplacementFix implements SyntheticIntentionAction {
30-
private final String myReplacement;
31-
private final String myDisplay;
32-
private final XPathExpression myExpr;
31+
private final String myReplacement;
32+
private final String myDisplay;
33+
private final XPathExpression myExpr;
3334

34-
public ExpressionReplacementFix(String replacement, XPathExpression expr) {
35-
this(replacement, replacement, expr);
36-
}
35+
public ExpressionReplacementFix(String replacement, XPathExpression expr) {
36+
this(replacement, replacement, expr);
37+
}
3738

38-
public ExpressionReplacementFix(String replacement, String display, XPathExpression expression) {
39-
myReplacement = replacement;
40-
myDisplay = display;
41-
myExpr = expression;
42-
}
39+
public ExpressionReplacementFix(String replacement, String display, XPathExpression expression) {
40+
myReplacement = replacement;
41+
myDisplay = display;
42+
myExpr = expression;
43+
}
4344

44-
@Nonnull
45-
@Override
46-
public String getText() {
47-
return "Replace with '" + myDisplay + "'";
48-
}
45+
@Nonnull
46+
@Override
47+
public LocalizeValue getText() {
48+
return LocalizeValue.localizeTODO("Replace with '" + myDisplay + "'");
49+
}
4950

50-
@Override
51-
public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file) {
52-
return myExpr.isValid();
53-
}
51+
@Override
52+
@RequiredReadAction
53+
public boolean isAvailable(@Nonnull Project project, Editor editor, PsiFile file) {
54+
return myExpr.isValid();
55+
}
5456

55-
@Override
56-
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
57-
myExpr.replace(XPathChangeUtil.createExpression(myExpr, myReplacement));
58-
}
57+
@Override
58+
@RequiredWriteAction
59+
public void invoke(@Nonnull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
60+
myExpr.replace(XPathChangeUtil.createExpression(myExpr, myReplacement));
61+
}
5962

60-
@Override
61-
public boolean startInWriteAction() {
62-
return true;
63-
}
63+
@Override
64+
public boolean startInWriteAction() {
65+
return true;
66+
}
6467
}

xpath-lang/src/main/java/org/intellij/lang/xpath/validation/inspections/CheckNodeTest.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
*/
1616
package org.intellij.lang.xpath.validation.inspections;
1717

18+
import consulo.annotation.access.RequiredReadAction;
1819
import consulo.annotation.component.ExtensionImpl;
1920
import consulo.language.editor.inspection.LocalQuickFix;
2021
import consulo.language.editor.inspection.ProblemHighlightType;
2122
import consulo.language.editor.inspection.ProblemsHolder;
2223
import consulo.language.editor.inspection.scheme.InspectionManager;
24+
import consulo.localize.LocalizeValue;
2325
import consulo.xml.psi.xml.XmlElement;
26+
import jakarta.annotation.Nonnull;
27+
import jakarta.annotation.Nullable;
2428
import org.intellij.lang.xpath.context.ContextProvider;
2529
import org.intellij.lang.xpath.context.NamespaceContext;
2630
import org.intellij.lang.xpath.psi.PrefixedName;
2731
import org.intellij.lang.xpath.psi.XPathNodeTest;
28-
import org.jetbrains.annotations.NonNls;
2932

30-
import jakarta.annotation.Nonnull;
31-
import jakarta.annotation.Nullable;
3233
import javax.xml.namespace.QName;
3334
import java.text.MessageFormat;
3435
import java.util.Set;
@@ -37,21 +38,24 @@
3738
public class CheckNodeTest extends XPathInspection<Object> {
3839
private static final String SHORT_NAME = "CheckNodeTest";
3940

41+
@Override
4042
protected Visitor createVisitor(InspectionManager manager, ProblemsHolder holder, boolean isOnTheFly, Object state) {
4143
return new MyVisitor(manager, isOnTheFly, holder, state);
4244
}
4345

4446
@Nonnull
45-
public String getDisplayName() {
46-
return "Check Node Test";
47+
@Override
48+
public LocalizeValue getDisplayName() {
49+
return LocalizeValue.localizeTODO("Check Node Test");
4750
}
4851

4952
@Nonnull
50-
@NonNls
53+
@Override
5154
public String getShortName() {
5255
return SHORT_NAME;
5356
}
5457

58+
@Override
5559
public boolean isEnabledByDefault() {
5660
return true;
5761
}
@@ -61,22 +65,24 @@ final static class MyVisitor extends Visitor<Object> {
6165
super(manager, holder, isOnTheFly, state);
6266
}
6367

68+
@Override
69+
@RequiredReadAction
6470
protected void checkNodeTest(XPathNodeTest nodeTest) {
65-
final ContextProvider contextProvider = ContextProvider.getContextProvider(nodeTest.getContainingFile());
66-
final XmlElement contextNode = contextProvider.getContextElement();
67-
final NamespaceContext namespaceContext = contextProvider.getNamespaceContext();
71+
ContextProvider contextProvider = ContextProvider.getContextProvider(nodeTest.getContainingFile());
72+
XmlElement contextNode = contextProvider.getContextElement();
73+
NamespaceContext namespaceContext = contextProvider.getNamespaceContext();
6874
if (namespaceContext == null) {
6975
return;
7076
}
7177

7278
if (nodeTest.isNameTest() && contextNode != null) {
73-
final PrefixedName prefixedName = nodeTest.getQName();
79+
PrefixedName prefixedName = nodeTest.getQName();
7480
assert prefixedName != null;
7581
if (!"*".equals(prefixedName.getLocalName()) && !"*".equals(prefixedName.getPrefix())) {
7682
boolean found;
7783

7884
if (nodeTest.getPrincipalType() == XPathNodeTest.PrincipalType.ELEMENT) {
79-
final Set<QName> elementNames = contextProvider.getElements(true);
85+
Set<QName> elementNames = contextProvider.getElements(true);
8086
if (elementNames != null) {
8187
found = false;
8288
for (QName pair : elementNames) {
@@ -91,7 +97,7 @@ protected void checkNodeTest(XPathNodeTest nodeTest) {
9197
}
9298
}
9399
else if (nodeTest.getPrincipalType() == XPathNodeTest.PrincipalType.ATTRIBUTE) {
94-
final Set<QName> attributeNames = contextProvider.getAttributes(true);
100+
Set<QName> attributeNames = contextProvider.getAttributes(true);
95101
if (attributeNames != null) {
96102
found = false;
97103
for (QName pair : attributeNames) {
@@ -109,51 +115,54 @@ else if (nodeTest.getPrincipalType() == XPathNodeTest.PrincipalType.ATTRIBUTE) {
109115
}
110116
}
111117

118+
@RequiredReadAction
112119
private void registerProblem(ContextProvider contextProvider, PrefixedName prefixedName, XPathNodeTest nodeTest, String type) {
113-
final QName qName = contextProvider.getQName(prefixedName, nodeTest);
114-
final String name;
120+
QName qName = contextProvider.getQName(prefixedName, nodeTest);
121+
String name;
115122
if (qName != null) {
116-
final String pattern;
117-
if (!"".equals(qName.getNamespaceURI())) {
118-
pattern = "''<b>{0}</b>'' (<i>{1}</i>)";
119-
}
120-
else {
121-
pattern = "''<b>{0}</b>''";
122-
}
123+
String pattern = qName.getNamespaceURI().isEmpty() ? "''<b>{0}</b>''" : "''<b>{0}</b>'' (<i>{1}</i>)";
123124
name = MessageFormat.format(pattern, qName.getLocalPart(), qName.getNamespaceURI());
124125
}
125126
else {
126127
name = MessageFormat.format("''<b>{0}</b>''", prefixedName.getLocalName());
127128
}
128129

129-
final LocalQuickFix[] fixes = contextProvider.getQuickFixFactory().createUnknownNodeTestFixes(nodeTest);
130-
addProblem(myManager.createProblemDescriptor(nodeTest, "<html>Unknown " + type + " name " + name + "</html>", myOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
130+
LocalQuickFix[] fixes = contextProvider.getQuickFixFactory().createUnknownNodeTestFixes(nodeTest);
131+
addProblem(myManager.createProblemDescriptor(
132+
nodeTest,
133+
"<html>Unknown " + type + " name " + name + "</html>",
134+
myOnTheFly,
135+
fixes,
136+
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
137+
));
131138
}
132139

133-
private static boolean matches(@Nullable PrefixedName prefixedName,
134-
QName element,
135-
NamespaceContext namespaceContext,
136-
XmlElement context,
137-
boolean allowDefaultNamespace) {
140+
private static boolean matches(
141+
@Nullable PrefixedName prefixedName,
142+
QName element,
143+
NamespaceContext namespaceContext,
144+
XmlElement context,
145+
boolean allowDefaultNamespace
146+
) {
138147
if (prefixedName == null) {
139148
return false;
140149
}
141150

142151
boolean b = prefixedName.getLocalName().equals(element.getLocalPart()) || "*".equals(element.getLocalPart());
143152

144-
final String prefix = prefixedName.getPrefix();
153+
String prefix = prefixedName.getPrefix();
145154
if (prefix != null) {
146155
if (!"*".equals(prefix)) {
147-
final String namespaceURI = namespaceContext.getNamespaceURI(prefix, context);
156+
String namespaceURI = namespaceContext.getNamespaceURI(prefix, context);
148157
b = b && element.getNamespaceURI().equals(namespaceURI);
149158
}
150159
}
151160
else if (allowDefaultNamespace) {
152-
final String namespaceURI = namespaceContext.getDefaultNamespace(context);
153-
b = b && (element.getNamespaceURI().equals(namespaceURI) || (element.getNamespaceURI().length() == 0 && namespaceURI == null));
161+
String namespaceURI = namespaceContext.getDefaultNamespace(context);
162+
b = b && (element.getNamespaceURI().equals(namespaceURI) || element.getNamespaceURI().isEmpty() && namespaceURI == null);
154163
}
155164
else {
156-
b = b && element.getNamespaceURI().length() == 0;
165+
b = b && element.getNamespaceURI().isEmpty();
157166
}
158167
return b;
159168
}

xpath-lang/src/main/java/org/intellij/lang/xpath/validation/inspections/HardwiredNamespacePrefix.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,62 @@
1515
*/
1616
package org.intellij.lang.xpath.validation.inspections;
1717

18+
import consulo.annotation.access.RequiredReadAction;
1819
import consulo.annotation.component.ExtensionImpl;
19-
import consulo.language.Language;
2020
import consulo.language.editor.inspection.LocalQuickFix;
2121
import consulo.language.editor.inspection.ProblemDescriptor;
2222
import consulo.language.editor.inspection.ProblemHighlightType;
2323
import consulo.language.editor.inspection.ProblemsHolder;
2424
import consulo.language.editor.inspection.scheme.InspectionManager;
25-
import org.intellij.lang.xpath.XPathFileType;
25+
import consulo.localize.LocalizeValue;
26+
import jakarta.annotation.Nonnull;
2627
import org.intellij.lang.xpath.XPathTokenTypes;
2728
import org.intellij.lang.xpath.psi.XPathBinaryExpression;
2829
import org.intellij.lang.xpath.psi.XPathExpression;
2930
import org.intellij.lang.xpath.psi.XPathFunctionCall;
3031
import org.intellij.lang.xpath.psi.XPathString;
31-
import org.jetbrains.annotations.Nls;
32-
import org.jetbrains.annotations.NonNls;
33-
34-
import jakarta.annotation.Nonnull;
3532

3633
@ExtensionImpl
3734
public class HardwiredNamespacePrefix extends XPathInspection<Object> {
35+
@Override
3836
public boolean isEnabledByDefault() {
3937
return true;
4038
}
4139

40+
@Override
4241
protected Visitor createVisitor(final InspectionManager manager, ProblemsHolder holder, final boolean isOnTheFly, Object state) {
4342
return new Visitor<>(manager, holder, isOnTheFly, state) {
43+
@Override
44+
@RequiredReadAction
4445
protected void checkExpression(XPathExpression expression) {
4546
if (!(expression instanceof XPathBinaryExpression)) {
4647
return;
4748
}
48-
final XPathBinaryExpression expr = (XPathBinaryExpression) expression;
49+
XPathBinaryExpression expr = (XPathBinaryExpression) expression;
4950
if (expr.getOperator() == XPathTokenTypes.EQ) {
50-
final XPathExpression lop = expr.getLOperand();
51-
final XPathExpression rop = expr.getROperand();
51+
XPathExpression lop = expr.getLOperand();
52+
XPathExpression rop = expr.getROperand();
5253

5354
if (isNameComparison(lop, rop)) {
5455
assert rop != null;
55-
final ProblemDescriptor p = manager.createProblemDescriptor(rop, "Hardwired namespace prefix", isOnTheFly,
56-
LocalQuickFix.EMPTY_ARRAY,
57-
ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
56+
ProblemDescriptor p = manager.createProblemDescriptor(
57+
rop,
58+
"Hardwired namespace prefix",
59+
isOnTheFly,
60+
LocalQuickFix.EMPTY_ARRAY,
61+
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
62+
);
5863
addProblem(p);
5964
}
6065
else if (isNameComparison(rop, lop)) {
6166
assert lop != null;
62-
final ProblemDescriptor p = manager.createProblemDescriptor(lop, "Hardwired namespace prefix", isOnTheFly,
63-
LocalQuickFix.EMPTY_ARRAY,
64-
ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
67+
ProblemDescriptor p = manager.createProblemDescriptor(
68+
lop,
69+
"Hardwired namespace prefix",
70+
isOnTheFly,
71+
LocalQuickFix.EMPTY_ARRAY,
72+
ProblemHighlightType.GENERIC_ERROR_OR_WARNING
73+
);
6574
addProblem(p);
6675
}
6776
else if (isNameFunctionCall(lop)) {
@@ -76,32 +85,25 @@ else if (isNameFunctionCall(rop)) {
7685
}
7786

7887
private static boolean isNameComparison(XPathExpression op1, XPathExpression op2) {
79-
if (!isNameFunctionCall(op1)) {
80-
return false;
81-
}
82-
if (!(op2 instanceof XPathString)) {
88+
if (!(isNameFunctionCall(op1) && op2 instanceof XPathString str)) {
8389
return false;
8490
}
85-
final String value = ((XPathString) op2).getValue();
91+
String value = str.getValue();
8692
return value != null && value.contains(":");
8793
}
8894

8995
private static boolean isNameFunctionCall(XPathExpression op1) {
90-
if (!(op1 instanceof XPathFunctionCall)) {
91-
return false;
92-
}
93-
final XPathFunctionCall fc = (XPathFunctionCall) op1;
94-
return "name".equals(fc.getFunctionName());
96+
return op1 instanceof XPathFunctionCall fc && "name".equals(fc.getFunctionName());
9597
}
9698

97-
@Nls
9899
@Nonnull
99-
public String getDisplayName() {
100-
return "Hardwired Namespace Prefix";
100+
@Override
101+
public LocalizeValue getDisplayName() {
102+
return LocalizeValue.localizeTODO("Hardwired Namespace Prefix");
101103
}
102104

103-
@NonNls
104105
@Nonnull
106+
@Override
105107
public String getShortName() {
106108
return "HardwiredNamespacePrefix";
107109
}

0 commit comments

Comments
 (0)