diff --git a/java/java-impl/src/com/intellij/spellchecker/LiteralExpressionTokenizer.java b/java/java-impl/src/com/intellij/spellchecker/LiteralExpressionTokenizer.java index adc0dd40a442..3781f3fe4ed4 100644 --- a/java/java-impl/src/com/intellij/spellchecker/LiteralExpressionTokenizer.java +++ b/java/java-impl/src/com/intellij/spellchecker/LiteralExpressionTokenizer.java @@ -3,17 +3,19 @@ package com.intellij.spellchecker; import com.intellij.codeInsight.AnnotationUtil; import com.intellij.openapi.util.text.StringUtil; -import com.intellij.psi.JavaTokenType; -import com.intellij.psi.PsiLiteralExpression; -import com.intellij.psi.PsiModifierListOwner; +import com.intellij.psi.*; import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; import com.intellij.psi.impl.source.tree.java.PsiLiteralExpressionImpl; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiUtil; import com.intellij.spellchecker.inspections.PlainTextSplitter; import com.intellij.spellchecker.tokenizer.EscapeSequenceTokenizer; import com.intellij.spellchecker.tokenizer.TokenConsumer; +import com.siyeh.ig.psiutils.ExpressionUtils; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; + /** * @author shkate@jetbrains.com */ @@ -31,7 +33,15 @@ public class LiteralExpressionTokenizer extends EscapeSequenceTokenizer s.getReturnValue()).anyMatch(e -> e == targetElement)) { + return; + } + } + else if (listOwner instanceof PsiVariable && ((PsiVariable)listOwner).getInitializer() == targetElement) { + return; + } } if (!text.contains("\\")) { @@ -44,9 +54,13 @@ public class LiteralExpressionTokenizer extends EscapeSequenceTokenizerCONASTANT + */ + @NonNls String s = ""; +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/inspections/spellchecker/MethodReturnTypeWithNonNls.java b/plugins/java-i18n/testData/inspections/spellchecker/MethodReturnTypeWithNonNls.java new file mode 100644 index 000000000000..28af50708d4c --- /dev/null +++ b/plugins/java-i18n/testData/inspections/spellchecker/MethodReturnTypeWithNonNls.java @@ -0,0 +1,9 @@ +import org.jetbrains.annotations.NonNls; + +class Test { + @NonNls + public String foo() { + System.out.println("CONASTANT"); + return null; + } +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/inspections/spellchecker/MethodReturnTypeWithNonNlsReturnsLiteral.java b/plugins/java-i18n/testData/inspections/spellchecker/MethodReturnTypeWithNonNlsReturnsLiteral.java new file mode 100644 index 000000000000..8c0b890eca8f --- /dev/null +++ b/plugins/java-i18n/testData/inspections/spellchecker/MethodReturnTypeWithNonNlsReturnsLiteral.java @@ -0,0 +1,8 @@ +import org.jetbrains.annotations.NonNls; + +class Test { + @NonNls + public String foo() { + return "CONASTANT"; + } +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/inspections/spellchecker/NonNlsField.java b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsField.java new file mode 100644 index 000000000000..6c1e7887022b --- /dev/null +++ b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsField.java @@ -0,0 +1,5 @@ +import org.jetbrains.annotations.NonNls; + +class Test { + @NonNls String s = "CONASTANT"; +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/inspections/spellchecker/NonNlsField2.java b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsField2.java new file mode 100644 index 000000000000..67bb8b582288 --- /dev/null +++ b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsField2.java @@ -0,0 +1,9 @@ +import org.jetbrains.annotations.NonNls; + +class Test { + @NonNls String s = getValue("CONASTANT"); + + String getValue(String key) { + return null; + } +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/inspections/spellchecker/NonNlsLocalVariable.java b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsLocalVariable.java new file mode 100644 index 000000000000..29bcc0370426 --- /dev/null +++ b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsLocalVariable.java @@ -0,0 +1,7 @@ +import org.jetbrains.annotations.NonNls; + +class Test { + void m() { + @NonNls String s = "CONASTANT" + " some " + " text " + " concatenation "; + } +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/inspections/spellchecker/NonNlsLocalVariableAndComment.java b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsLocalVariableAndComment.java new file mode 100644 index 000000000000..6a3449aba9b2 --- /dev/null +++ b/plugins/java-i18n/testData/inspections/spellchecker/NonNlsLocalVariableAndComment.java @@ -0,0 +1,7 @@ +import org.jetbrains.annotations.NonNls; + +class Test { + void m() { + @NonNls String s = "CONASTANT"; // CONASTANT comment + } +} \ No newline at end of file diff --git a/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerInspectionTest.java b/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerInspectionTest.java index a3af1060b856..79251edcd1bf 100644 --- a/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerInspectionTest.java +++ b/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerInspectionTest.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2013 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.spellchecker.inspection; import com.intellij.openapi.application.PluginPathManager; @@ -24,6 +10,16 @@ public class JavaSpellcheckerInspectionTest extends LightCodeInsightFixtureTestC return PluginPathManager.getPluginHomePathRelative("java-i18n") + "/testData/inspections/spellchecker"; } + @Override + public void setUp() throws Exception { + super.setUp(); + myFixture.addClass("package org.jetbrains.annotations;" + + "import java.lang.annotation.*;" + + "@Retention(RetentionPolicy.CLASS)" + + "@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE, ElementType.PACKAGE})" + + "public @interface NonNls {}"); + } + public void testCorrectJava() { doTest(); } public void testTypoInJava() { doTest(); } public void testVarArg() { doTest(); } @@ -38,6 +34,15 @@ public class JavaSpellcheckerInspectionTest extends LightCodeInsightFixtureTestC public void testStringLiteralEscaping() { doTest(); } public void testSuppressions() { doTest(); } + // suppression by @NonNls + public void testMethodReturnTypeWithNonNls() { doTest(); } + public void testMethodReturnTypeWithNonNlsReturnsLiteral() { doTest(); } + public void testNonNlsField() { doTest(); } + public void testNonNlsField2() { doTest(); } + public void testNonNlsLocalVariable() { doTest(); } + public void testNonNlsLocalVariableAndComment() { doTest(); } + public void testFieldComment() { doTest(); } + private void doTest() { myFixture.enableInspections(SpellcheckerInspectionTestCase.getInspectionTools()); myFixture.testHighlighting(false, false, true, getTestName(false) + ".java");