From 86808a12a43e1be5c6ade7bc57090bf5ddd55895 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 16 Jan 2013 12:53:58 +0100 Subject: [PATCH] IDEA-98245 (spellchecker suppression for Java) --- .../quickFix/LightQuickFixTestCase.java | 22 ++++++++---- .../JavaSpellcheckingStrategy.java | 35 +++++++++++++----- .../testData/inspection/Suppressions.java | 16 +++++++++ .../testData/suppression/afterClassName.java | 4 +++ .../suppression/afterStringLiteral.java | 8 +++++ .../testData/suppression/beforeClassName.java | 3 ++ .../suppression/beforeStringLiteral.java | 7 ++++ .../JavaSpellcheckerInspectionTest.java | 1 + .../JavaSpellcheckerSuppressionTest.java | 36 +++++++++++++++++++ 9 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 plugins/java-i18n/testData/inspection/Suppressions.java create mode 100644 plugins/java-i18n/testData/suppression/afterClassName.java create mode 100644 plugins/java-i18n/testData/suppression/afterStringLiteral.java create mode 100644 plugins/java-i18n/testData/suppression/beforeClassName.java create mode 100644 plugins/java-i18n/testData/suppression/beforeStringLiteral.java create mode 100644 plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerSuppressionTest.java diff --git a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java index cf8637bbb502..cf95fb2560e4 100644 --- a/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/daemon/quickFix/LightQuickFixTestCase.java @@ -49,8 +49,12 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import static com.intellij.util.ObjectUtils.notNull; + public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase { @NonNls private static final String BEFORE_PREFIX = "before"; + @NonNls private static final String AFTER_PREFIX = "after"; + private static QuickFixTestCase myWrapper; protected boolean shouldBeAvailableAfterExecution() { @@ -62,10 +66,11 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase } private static void doTestFor(final String testName, final QuickFixTestCase quickFixTestCase) { - final String relativePath = quickFixTestCase.getBasePath() + "/" + BEFORE_PREFIX + testName; + final String relativePath = notNull(quickFixTestCase.getBasePath(), "") + "/" + BEFORE_PREFIX + testName; final String testFullPath = quickFixTestCase.getTestDataPath().replace(File.separatorChar, '/') + relativePath; final File testFile = new File(testFullPath); CommandProcessor.getInstance().executeCommand(quickFixTestCase.getProject(), new Runnable() { + @SuppressWarnings({"AssignmentToStaticFieldFromInstanceMethod", "CallToPrintStackTrace"}) @Override public void run() { try { @@ -118,6 +123,7 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase } // "quick fix action text to perform" "should be available" + assert comment != null : commenter; Pattern pattern = Pattern.compile("^" + comment.replace("*", "\\*") + actionPattern, Pattern.DOTALL); Matcher matcher = pattern.matcher(contents); assertTrue("No comment found in "+file.getVirtualFile(), matcher.matches()); @@ -126,10 +132,11 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase return Pair.create(text, actionShouldBeAvailable); } - @SuppressWarnings({"HardCodedStringLiteral"}) - public static void doAction(final String text, final boolean actionShouldBeAvailable, final String testFullPath, final String testName, - QuickFixTestCase quickFix) - throws Exception { + public static void doAction(String text, + boolean actionShouldBeAvailable, + String testFullPath, + String testName, + QuickFixTestCase quickFix) throws Exception { IntentionAction action = quickFix.findActionWithText(text); if (action == null) { if (actionShouldBeAvailable) { @@ -139,7 +146,8 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase texts.add(intentionAction.getText()); } Collection infos = quickFix.doHighlighting(); - fail("Action with text '" + text + "' is not available in test " + testFullPath+"\nAvailable actions ("+texts.size()+"): "+texts+"\n"+actions+"\nInfos:"+infos); + fail("Action with text '" + text + "' is not available in test " + testFullPath + "\n" + + "Available actions (" + texts.size() + "): " + texts + "\n" + actions + "\nInfos:" + infos); } } else { @@ -155,7 +163,7 @@ public abstract class LightQuickFixTestCase extends LightDaemonAnalyzerTestCase fail("Action '" + text + "' is still available after its invocation in test " + testFullPath); } } - final String expectedFilePath = quickFix.getBasePath() + "/after" + testName; + String expectedFilePath = notNull(quickFix.getBasePath(), "") + "/" + AFTER_PREFIX + testName; quickFix.checkResultByFile("In file :" + expectedFilePath, expectedFilePath, false); } } diff --git a/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java b/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java index 0a27282a9806..78471ae43f85 100644 --- a/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java +++ b/plugins/java-i18n/src/com/intellij/spellchecker/JavaSpellcheckingStrategy.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2009 JetBrains s.r.o. + * 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. @@ -15,22 +15,22 @@ */ package com.intellij.spellchecker; +import com.intellij.codeInsight.daemon.HighlightDisplayKey; +import com.intellij.codeInspection.SuppressIntentionAction; import com.intellij.codeInspection.SuppressManager; import com.intellij.psi.PsiElement; import com.intellij.psi.PsiLiteralExpression; import com.intellij.psi.PsiMethod; import com.intellij.psi.PsiNamedElement; import com.intellij.psi.javadoc.PsiDocComment; -import com.intellij.spellchecker.tokenizer.SpellcheckingStrategy; +import com.intellij.spellchecker.tokenizer.SuppressibleSpellcheckingStrategy; import com.intellij.spellchecker.tokenizer.Tokenizer; import org.jetbrains.annotations.NotNull; /** - * Created by IntelliJ IDEA. - * * @author shkate@jetbrains.com */ -public class JavaSpellcheckingStrategy extends SpellcheckingStrategy { +public class JavaSpellcheckingStrategy extends SuppressibleSpellcheckingStrategy { private final MethodNameTokenizerJava myMethodNameTokenizer = new MethodNameTokenizerJava(); private final DocCommentTokenizer myDocCommentTokenizer = new DocCommentTokenizer(); private final LiteralExpressionTokenizer myLiteralExpressionTokenizer = new LiteralExpressionTokenizer(); @@ -39,15 +39,32 @@ public class JavaSpellcheckingStrategy extends SpellcheckingStrategy { @NotNull @Override public Tokenizer getTokenizer(PsiElement element) { - if (element instanceof PsiMethod) return myMethodNameTokenizer; - if (element instanceof PsiDocComment) return myDocCommentTokenizer; + if (element instanceof PsiMethod) { + return myMethodNameTokenizer; + } + if (element instanceof PsiDocComment) { + return myDocCommentTokenizer; + } if (element instanceof PsiLiteralExpression) { - if (SuppressManager.isSuppressedInspectionName((PsiLiteralExpression)element)){ + if (SuppressManager.isSuppressedInspectionName((PsiLiteralExpression)element)) { return EMPTY_TOKENIZER; } return myLiteralExpressionTokenizer; } - if (element instanceof PsiNamedElement) return myNamedElementTokenizer; + if (element instanceof PsiNamedElement) { + return myNamedElementTokenizer; + } + return super.getTokenizer(element); } + + @Override + public boolean isSuppressedFor(@NotNull PsiElement element, @NotNull String name) { + return SuppressManager.getInstance().isSuppressedFor(element, name); + } + + @Override + public SuppressIntentionAction[] getSuppressActions(@NotNull PsiElement element, @NotNull String name) { + return SuppressManager.getInstance().createSuppressActions(HighlightDisplayKey.find(name)); + } } diff --git a/plugins/java-i18n/testData/inspection/Suppressions.java b/plugins/java-i18n/testData/inspection/Suppressions.java new file mode 100644 index 000000000000..bb6634f849b9 --- /dev/null +++ b/plugins/java-i18n/testData/inspection/Suppressions.java @@ -0,0 +1,16 @@ +@SuppressWarnings("SpellCheckingInspection") +class BaddName1 { + int baddField; + void baddMethod() { } +} + +@SuppressWarnings("ALL") +class BaddName2 { + int baddField; + void baddMethod() { } +} + +class GoodName { + @SuppressWarnings("SpellCheckingInspection") int baddField; + @SuppressWarnings("SpellCheckingInspection") void baddMethod() { } +} diff --git a/plugins/java-i18n/testData/suppression/afterClassName.java b/plugins/java-i18n/testData/suppression/afterClassName.java new file mode 100644 index 000000000000..7c8a1454b4a7 --- /dev/null +++ b/plugins/java-i18n/testData/suppression/afterClassName.java @@ -0,0 +1,4 @@ +// "Suppress for class" "true" +@SuppressWarnings("SpellCheckingInspection") +class BaddName { +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/suppression/afterStringLiteral.java b/plugins/java-i18n/testData/suppression/afterStringLiteral.java new file mode 100644 index 000000000000..4e51b224e977 --- /dev/null +++ b/plugins/java-i18n/testData/suppression/afterStringLiteral.java @@ -0,0 +1,8 @@ +// "Suppress for statement with comment" "true" +class C { + { + //noinspection SpellCheckingInspection + String s = "tyypoo"; + System.out.println(s); + } +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/suppression/beforeClassName.java b/plugins/java-i18n/testData/suppression/beforeClassName.java new file mode 100644 index 000000000000..f7f317087643 --- /dev/null +++ b/plugins/java-i18n/testData/suppression/beforeClassName.java @@ -0,0 +1,3 @@ +// "Suppress for class" "true" +class BaddName { +} \ No newline at end of file diff --git a/plugins/java-i18n/testData/suppression/beforeStringLiteral.java b/plugins/java-i18n/testData/suppression/beforeStringLiteral.java new file mode 100644 index 000000000000..fc9a077516a9 --- /dev/null +++ b/plugins/java-i18n/testData/suppression/beforeStringLiteral.java @@ -0,0 +1,7 @@ +// "Suppress for statement with comment" "true" +class C { + { + String s = "tyypoo"; + System.out.println(s); + } +} \ 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 96d98e8f2bad..e65e866ad427 100644 --- a/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerInspectionTest.java +++ b/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerInspectionTest.java @@ -31,6 +31,7 @@ public class JavaSpellcheckerInspectionTest extends LightCodeInsightFixtureTestC public void testDocComment() { doTest(); } public void testStringLiteral() { doTest(); } public void testStringLiteralEscaping() { doTest(); } + public void testSuppressions() { doTest(); } private void doTest() { myFixture.enableInspections(SpellcheckerInspectionTestCase.getInspectionTools()); diff --git a/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerSuppressionTest.java b/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerSuppressionTest.java new file mode 100644 index 000000000000..7634d117b84e --- /dev/null +++ b/plugins/java-i18n/testSrc/com/intellij/spellchecker/inspection/JavaSpellcheckerSuppressionTest.java @@ -0,0 +1,36 @@ +/* + * 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. + */ +package com.intellij.spellchecker.inspection; + +import com.intellij.codeInsight.daemon.quickFix.LightQuickFixTestCase; +import com.intellij.openapi.application.PluginPathManager; +import org.jetbrains.annotations.NotNull; + +public class JavaSpellcheckerSuppressionTest extends LightQuickFixTestCase { + @NotNull + @Override + protected String getTestDataPath() { + return PluginPathManager.getPluginHomePath("java-i18n") + "/testData/suppression"; + } + + public void testClassName() { doTest(); } + public void testStringLiteral() { doTest(); } + + private void doTest() { + enableInspectionTools(SpellcheckerInspectionTestCase.getInspectionTools()); + doSingleTest(getTestName(false) + ".java"); + } +}