Look for string literals with mixed languages and repair some found violations to fix IDEA-239390 Inspection to detect mixed characters in strings (strings containing both ASCII and non-ASCII)

GitOrigin-RevId: 312b7bcfd4e9c8ae438ae3d08aa0a60188fa226a
This commit is contained in:
Alexey Kudravtsev
2020-04-29 11:54:28 +00:00
committed by intellij-monorepo-bot
parent 346bc951cd
commit f88eeb5101
12 changed files with 62 additions and 33 deletions
@@ -1,6 +1,6 @@
class X {
int <warning descr="Non-ASCII characters in an identifier">Ж</warning> = 0;
class <warning descr="Identifier contains symbols from different languages: [LATIN, CYRILLIC]"><warning descr="Non-ASCII characters in an identifier">InnerП</warning></warning> {}
class <warning descr="Non-ASCII characters in an identifier"><warning descr="Symbols from different languages found: [LATIN, CYRILLIC]">InnerП</warning></warning> {}
// comment<warning descr="Non-ASCII characters in a comment">жп</warning> 234
String s = "12<warning descr="Non-ASCII characters in a string literal">л</warning>3<warning descr="Non-ASCII characters in a string literal">орыва</warning>0";
void <warning descr="Non-ASCII characters in an identifier">жжж</warning>() {
@@ -1,9 +1,12 @@
class X {
int <warning descr="Non-ASCII characters in an identifier">Ж</warning> = 0;
class <warning descr="Identifier contains symbols from different languages: [LATIN, CYRILLIC]"><warning descr="Non-ASCII characters in an identifier">InnerП</warning></warning> {}
class <warning descr="Non-ASCII characters in an identifier"><warning descr="Symbols from different languages found: [LATIN, CYRILLIC]">InnerП</warning></warning> {}
// comment<warning descr="Non-ASCII characters in a comment">жп</warning> 234
String s = "12<warning descr="Non-ASCII characters in a string literal">л</warning>3<warning descr="Non-ASCII characters in a string literal">орыва</warning>0";
void <warning descr="Non-ASCII characters in an identifier">жжж</warning>() {
жжж();
String s = <warning descr="Symbols from different languages found: [LATIN, CYRILLIC]">"<warning descr="Non-ASCII characters in a string literal">С</warning>ore"</warning>;
System.out.println(<warning descr="Symbols from different languages found: [LATIN, CYRILLIC]">"<warning descr="Non-ASCII characters in a string literal">С</warning>ore"</warning>);
String u = "\5\0\51\2\3\0\136\2\21\0\33\2\65\0\20\2\u0200\0\u19b6\2"; // ignore slash u
}
}
@@ -30,6 +30,7 @@ public class NonAsciiCharactersTest extends DaemonAnalyzerTestCase {
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
NonAsciiCharactersInspection inspection = new NonAsciiCharactersInspection();
inspection.CHECK_FOR_DIFFERENT_LANGUAGES_IN_IDENTIFIER_NAME = true;
inspection.CHECK_FOR_DIFFERENT_LANGUAGES_IN_STRING = true;
inspection.CHECK_FOR_NOT_ASCII_IDENTIFIER_NAME = true;
inspection.CHECK_FOR_NOT_ASCII_COMMENT = true;
inspection.CHECK_FOR_NOT_ASCII_STRING_LITERAL = true;
@@ -578,7 +578,9 @@ label.non.ascii.chars.example.characters.in.comments=<html>E.g.: <code>// hello
label.non.ascii.chars.example.characters.in.identifiers=<html>E.g.: <code>int \u041F\u043E\u043B\u0435 = 0;</code>
label.non.ascii.chars.example.characters.in.strings=<html>E.g.: <code>println("\u0E29 \u0E24\u0E45\u0E29\u0E35")</code>
label.non.ascii.chars.example.different.languages.in.identifiers=<html>E.g.: <code>void print\u0415\u0440\u0443\u043D\u0434\u0430()</code>
label.non.ascii.chars.example.different.languages.in.string=<html>E.g.: <code>foo("\u0421ore"); //cyrillic \u0421</code>
checkbox.non.ascii.option.different.languages.in.identifiers=Different languages in identifiers
checkbox.non.ascii.option.different.languages.in.string=Different languages in strings
checkbox.non.ascii.option.files.containing.bom=Files containing BOM
checkbox.non.ascii.option.characters.in.identifiers=Non-ASCII characters in Identifiers
checkbox.non.ascii.option.characters.in.comments=Non-ASCII characters in comments
@@ -129,7 +129,7 @@ popup.advertisement.press.or.to.replace=Press {0} or {1} to replace
inspection.message.file.contains.bom=File contains BOM: ''{0}''{1}
inspection.message.charset.signature=\ (charset ''{0}'' signature)
inspection.message.non.ascii.characters.in=Non-ASCII characters in {0}
inspection.message.identifier.contains.symbols.from.different.languages=Identifier contains symbols from different languages: {0}
inspection.message.symbols.from.different.languages.found=Symbols from different languages found: {0}
hint.text.unfortunately.currently.available.for.batch.mode=Unfortunately ''{0}'' is currently not available for batch mode\n User interaction is required for each problem found
progress.title.inspect.code=Inspect Code...
hint.text.can.t.modify.decompiled.code=Can't modify decompiled code
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.intellij.codeInspection.NonAsciiCharactersInspectionForm">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="7" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="8" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="640" height="400"/>
@@ -23,7 +23,7 @@
</hspacer>
<vspacer id="8f7a7">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="7" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="d43de" class="com.intellij.ui.components.JBCheckBox" binding="myASCIIIdentifiers">
@@ -58,10 +58,18 @@
<text resource-bundle="messages/CodeInsightBundle" key="checkbox.non.ascii.option.different.languages.in.identifiers"/>
</properties>
</component>
<component id="92c40" class="com.intellij.ui.components.JBCheckBox" binding="myFilesContainingBOM">
<component id="9cf83" class="com.intellij.ui.components.JBCheckBox" binding="myDifferentLanguagesInStrings" default-binding="true">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CodeInsightBundle" key="checkbox.non.ascii.option.different.languages.in.string"/>
</properties>
</component>
<component id="92c40" class="com.intellij.ui.components.JBCheckBox" binding="myFilesContainingBOM">
<constraints>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CodeInsightBundle" key="checkbox.non.ascii.option.files.containing.bom"/>
</properties>
@@ -98,10 +106,18 @@
<text resource-bundle="messages/CodeInsightBundle" key="label.non.ascii.chars.example.different.languages.in.identifiers"/>
</properties>
</component>
<component id="39165" class="com.intellij.ui.components.JBLabel">
<component id="df9e3" class="com.intellij.ui.components.JBLabel">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CodeInsightBundle" key="label.non.ascii.chars.example.different.languages.in.string"/>
</properties>
</component>
<component id="39165" class="com.intellij.ui.components.JBLabel">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="messages/CodeInsightBundle" key="label.non.ascii.chars.example.files.containing.bom"/>
</properties>
@@ -29,7 +29,6 @@ import com.intellij.util.io.IOUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.nio.charset.Charset;
@@ -46,6 +45,7 @@ public class NonAsciiCharactersInspection extends LocalInspectionTool {
public boolean CHECK_FOR_NOT_ASCII_COMMENT;
public boolean CHECK_FOR_DIFFERENT_LANGUAGES_IN_IDENTIFIER_NAME = true;
public boolean CHECK_FOR_DIFFERENT_LANGUAGES_IN_STRING;
public boolean CHECK_FOR_FILES_CONTAINING_BOM;
@Override
@@ -94,9 +94,13 @@ public class NonAsciiCharactersInspection extends LocalInspectionTool {
checkAsciiRange(element, element.getText(), holder, "a comment");
}
}
if (CHECK_FOR_NOT_ASCII_STRING_LITERAL) {
if (element instanceof PsiLiteralValue) {
checkAsciiRange(element, element.getText(), holder, "a string literal");
if (element instanceof PsiLiteralValue) {
String text = element.getText();
if (CHECK_FOR_NOT_ASCII_STRING_LITERAL) {
checkAsciiRange(element, text, holder, "a string literal");
}
if (CHECK_FOR_DIFFERENT_LANGUAGES_IN_STRING) {
checkSameLanguage(element, text, holder);
}
}
}
@@ -126,7 +130,7 @@ public class NonAsciiCharactersInspection extends LocalInspectionTool {
};
}
private static boolean isFileWorthIt(PsiFile file) {
private static boolean isFileWorthIt(@NotNull PsiFile file) {
if (InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) return false;
VirtualFile virtualFile = file.getVirtualFile();
if (virtualFile == null) return false;
@@ -139,9 +143,9 @@ public class NonAsciiCharactersInspection extends LocalInspectionTool {
}
private static void checkSameLanguage(PsiElement element,
String text,
ProblemsHolder holder) {
private static void checkSameLanguage(@NotNull PsiElement element,
@NotNull String text,
@NotNull ProblemsHolder holder) {
Set<Character.UnicodeScript> scripts = text.codePoints()
.mapToObj(Character.UnicodeScript::of)
.filter(script -> !script.equals(Character.UnicodeScript.COMMON))
@@ -149,23 +153,23 @@ public class NonAsciiCharactersInspection extends LocalInspectionTool {
if (scripts.size() > 1) {
List<Character.UnicodeScript> list = new ArrayList<>(scripts);
Collections.sort(list); // a little bit of stability
holder.registerProblem(element, LangBundle.message("inspection.message.identifier.contains.symbols.from.different.languages", list),
holder.registerProblem(element, LangBundle.message("inspection.message.symbols.from.different.languages.found", list),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
private static void checkAscii(PsiElement element,
String text,
ProblemsHolder holder,
String where) {
private static void checkAscii(@NotNull PsiElement element,
@NotNull String text,
@NotNull ProblemsHolder holder,
@NotNull String where) {
if (!IOUtil.isAscii(text)) {
holder.registerProblem(element, LangBundle.message("inspection.message.non.ascii.characters.in", where), ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
private static void checkAsciiRange(PsiElement element,
String text,
ProblemsHolder holder,
String where) {
private static void checkAsciiRange(@NotNull PsiElement element,
@NotNull String text,
@NotNull ProblemsHolder holder,
@NotNull String where) {
int errorCount = 0;
int start = -1;
for (int i = 0; i <= text.length(); i++) {
@@ -186,7 +190,7 @@ public class NonAsciiCharactersInspection extends LocalInspectionTool {
}
}
@Nullable
@NotNull
@Override
public JComponent createOptionsPanel() {
return new NonAsciiCharactersInspectionForm(this).myPanel;
@@ -18,6 +18,7 @@ package com.intellij.codeInspection;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.util.ReflectionUtil;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.util.Map;
@@ -30,20 +31,22 @@ class NonAsciiCharactersInspectionForm {
private JBCheckBox myAlienIdentifiers;
JPanel myPanel;
private JBCheckBox myFilesContainingBOM;
private JBCheckBox myDifferentLanguagesInStrings;
private final Map<JCheckBox, String> myBindings = new THashMap<>();
NonAsciiCharactersInspectionForm(NonAsciiCharactersInspection inspection) {
NonAsciiCharactersInspectionForm(@NotNull NonAsciiCharactersInspection inspection) {
myInspection = inspection;
bind(myASCIIIdentifiers, "CHECK_FOR_NOT_ASCII_IDENTIFIER_NAME");
bind(myASCIIStringLiterals, "CHECK_FOR_NOT_ASCII_STRING_LITERAL");
bind(myASCIIComments, "CHECK_FOR_NOT_ASCII_COMMENT");
bind(myAlienIdentifiers, "CHECK_FOR_DIFFERENT_LANGUAGES_IN_IDENTIFIER_NAME");
bind(myDifferentLanguagesInStrings, "CHECK_FOR_DIFFERENT_LANGUAGES_IN_STRING");
bind(myFilesContainingBOM, "CHECK_FOR_FILES_CONTAINING_BOM");
reset();
}
private void bind(JCheckBox checkBox, String property) {
private void bind(@NotNull JCheckBox checkBox, @NotNull String property) {
myBindings.put(checkBox, property);
reset(checkBox, property);
checkBox.addChangeListener(__ -> {
@@ -52,7 +55,7 @@ class NonAsciiCharactersInspectionForm {
});
}
private void reset(JCheckBox checkBox, String property) {
private void reset(@NotNull JCheckBox checkBox, @NotNull String property) {
checkBox.setSelected(ReflectionUtil.getField(myInspection.getClass(), myInspection, boolean.class, property));
}
@@ -40,7 +40,7 @@ public class EditorColorSchemesUsagesCollector extends ApplicationUsagesCollecto
"Aptana",
"Flash Builder",
"IdeaLight",
"High сontrast",
"High contrast",
"ReSharper",
"Rider"
};
@@ -1,10 +1,10 @@
<scheme name="High сontrast" version="142" parent_scheme="Darcula">
<scheme name="High contrast" version="142" parent_scheme="Darcula">
<metaInfo>
<property name="created">2018-11-06T13:11:44</property>
<property name="ide">idea</property>
<property name="ideVersion">2019.1.0.0</property>
<property name="modified">2018-11-06T13:11:49</property>
<property name="originalScheme">_@user_High сontrast</property>
<property name="originalScheme">_@user_High contrast</property>
</metaInfo>
<colors>
<option name="ADDED_LINES_COLOR" value="9924" />
@@ -187,5 +187,5 @@ intention.category.other=Java/Other
0.already.extends.1.and.will.not.compile.after.converting.2.to.a.class={0} implementing {2} already extends {1} and will not compile after converting {2} to a class
change.modifier.text=Make ''{0}'' {1}
convert.datapoints.fix.family.name=Convert @DataPoint(s) annotation holder
make.method.\u0432default.family.name=Make method default
make.method.default.family.name=Make method default
create.missing.switch.branches.family.name=Create missing switch branches
@@ -24,7 +24,7 @@ public class MakeMethodDefaultIntention extends BaseElementAtCaretIntentionActio
@NotNull
@Override
public String getFamilyName() {
return IntentionPowerPackBundle.message("make.method.вdefault.family.name");
return IntentionPowerPackBundle.message("make.method.default.family.name");
}
@Override