From c4bba135bf2b6f9aee8222ab52f0dcfb8c129397 Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 29 Jun 2018 10:43:02 +0300 Subject: [PATCH 1/3] Introduce KotlinInspectionSuppressor So #KT-25169 Fixed So #KT-11154 Fixed --- .../inspections/KotlinInspectionSuppressor.kt | 45 +++++++++++++++++++ idea/src/META-INF/plugin.xml | 2 + idea/src/META-INF/plugin.xml.172 | 2 + idea/src/META-INF/plugin.xml.173 | 2 + idea/src/META-INF/plugin.xml.182 | 2 + idea/src/META-INF/plugin.xml.as31 | 2 + idea/src/META-INF/plugin.xml.as32 | 2 + 7 files changed, 57 insertions(+) create mode 100644 idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt new file mode 100644 index 00000000000..9cb42fd87b9 --- /dev/null +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.idea.inspections + +import com.intellij.codeInspection.InspectionSuppressor +import com.intellij.codeInspection.ProblemDescriptor +import com.intellij.codeInspection.SuppressManager +import com.intellij.codeInspection.SuppressQuickFix +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.caches.resolve.KotlinCacheService +import org.jetbrains.kotlin.diagnostics.Severity +import org.jetbrains.kotlin.idea.highlighter.createSuppressWarningActions + +class KotlinInspectionSuppressor : InspectionSuppressor { + override fun getSuppressActions(element: PsiElement?, toolId: String): Array { + element ?: return emptyArray() + return createSuppressWarningActions(element, Severity.WARNING, toolId).map { + object : SuppressQuickFix { + override fun getFamilyName() = it.familyName + + override fun applyFix(project: Project, descriptor: ProblemDescriptor) = it.invoke(project, null, descriptor.psiElement) + + override fun isAvailable(project: Project, context: PsiElement) = it.isAvailable(project, null, context) + + override fun isSuppressAll() = it.isSuppressAll + } + }.toTypedArray() + } + + override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean { + if (SuppressManager.getInstance()!!.isSuppressedFor(element, toolId)) { + return true + } + + if (KotlinCacheService.getInstance(element.project).getSuppressionCache().isSuppressed(element, toolId, Severity.WARNING)) { + return true + } + + return false + } +} diff --git a/idea/src/META-INF/plugin.xml b/idea/src/META-INF/plugin.xml index 1a60606b8c1..c8376edf13e 100644 --- a/idea/src/META-INF/plugin.xml +++ b/idea/src/META-INF/plugin.xml @@ -1642,6 +1642,8 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio. Kotlin + + Kotlin + + Kotlin + + Kotlin + + Kotlin + + Kotlin + + Date: Fri, 29 Jun 2018 11:44:48 +0300 Subject: [PATCH 2/3] Add a pair of tests for KT-11154 --- .../suppress/external/suppressActive.kt | 8 +++++++ .../suppress/external/suppressAvailable.kt | 16 +++++++++++++ .../external/suppressAvailable.kt.after | 17 ++++++++++++++ .../QuickFixMultiFileTestGenerated.java | 13 +++++++++++ .../idea/quickfix/QuickFixTestGenerated.java | 23 +++++++++++++++++++ 5 files changed, 77 insertions(+) create mode 100644 idea/testData/quickfix/suppress/external/suppressActive.kt create mode 100644 idea/testData/quickfix/suppress/external/suppressAvailable.kt create mode 100644 idea/testData/quickfix/suppress/external/suppressAvailable.kt.after diff --git a/idea/testData/quickfix/suppress/external/suppressActive.kt b/idea/testData/quickfix/suppress/external/suppressActive.kt new file mode 100644 index 00000000000..c3aa6c8c2ad --- /dev/null +++ b/idea/testData/quickfix/suppress/external/suppressActive.kt @@ -0,0 +1,8 @@ +// "Typo: Change to..." "false" +// TOOL: com.intellij.spellchecker.inspections.SpellCheckingInspection +// ACTION: Add 'const' modifier +// ACTION: Convert property initializer to getter +// ACTION: To raw string literal + +@Suppress("SpellCheckingInspection") +val str = "kjsghkjshtiurhuig" \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/external/suppressAvailable.kt b/idea/testData/quickfix/suppress/external/suppressAvailable.kt new file mode 100644 index 00000000000..e7e5b49a53d --- /dev/null +++ b/idea/testData/quickfix/suppress/external/suppressAvailable.kt @@ -0,0 +1,16 @@ +// "Suppress Warnings" "true" +// TOOL: com.intellij.spellchecker.inspections.SpellCheckingInspection +// ACTION: Add 'const' modifier +// ACTION: Typo: Change to... +// ACTION: Edit inspection profile setting +// ACTION: Fix all 'Typo' problems in file +// ACTION: Run inspection on ... +// ACTION: Disable inspection +// ACTION: Save 'kjsghkjshtiurhuig' to project-level dictionary +// ACTION: Show property type hints +// ACTION: Edit intention settings +// ACTION: Convert property initializer to getter +// ACTION: Inject language or reference +// ACTION: To raw string literal + +val str = "kjsghkjshtiurhuig" \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after b/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after new file mode 100644 index 00000000000..8fa8b38296c --- /dev/null +++ b/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after @@ -0,0 +1,17 @@ +// "Suppress Warnings" "true" +// TOOL: com.intellij.spellchecker.inspections.SpellCheckingInspection +// ACTION: Add 'const' modifier +// ACTION: Typo: Change to... +// ACTION: Edit inspection profile setting +// ACTION: Fix all 'Typo' problems in file +// ACTION: Run inspection on ... +// ACTION: Disable inspection +// ACTION: Save 'kjsghkjshtiurhuig' to project-level dictionary +// ACTION: Show property type hints +// ACTION: Edit intention settings +// ACTION: Convert property initializer to getter +// ACTION: Inject language or reference +// ACTION: To raw string literal + +@Suppress("SpellCheckingInspection") +val str = "kjsghkjshtiurhuig" \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java index 51e0c7a3916..5dd4d35af59 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixMultiFileTestGenerated.java @@ -3696,6 +3696,19 @@ public class QuickFixMultiFileTestGenerated extends AbstractQuickFixMultiFileTes } } + @TestMetadata("idea/testData/quickfix/suppress/external") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class External extends AbstractQuickFixMultiFileTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTestWithExtraFile, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInExternal() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/suppress/external"), Pattern.compile("^(\\w+)\\.((before\\.Main\\.\\w+)|(test))$"), TargetBackend.ANY, true); + } + } + @TestMetadata("idea/testData/quickfix/suppress/forStatement") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java index 5a7686c9f8a..f6a85de10ba 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/quickfix/QuickFixTestGenerated.java @@ -10286,6 +10286,29 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest { } } + @TestMetadata("idea/testData/quickfix/suppress/external") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class External extends AbstractQuickFixTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath); + } + + public void testAllFilesPresentInExternal() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/suppress/external"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); + } + + @TestMetadata("suppressActive.kt") + public void testSuppressActive() throws Exception { + runTest("idea/testData/quickfix/suppress/external/suppressActive.kt"); + } + + @TestMetadata("suppressAvailable.kt") + public void testSuppressAvailable() throws Exception { + runTest("idea/testData/quickfix/suppress/external/suppressAvailable.kt"); + } + } + @TestMetadata("idea/testData/quickfix/suppress/forStatement") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) From 537655a4c24af56f671eea3fe195dc35084e2a8c Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Fri, 29 Jun 2018 11:58:28 +0300 Subject: [PATCH 3/3] KotlinInspectionSuppressor: introduce more precise quick-fix names Related to KT-11154, KT-25169 --- .../kotlin/idea/inspections/KotlinInspectionSuppressor.kt | 2 ++ idea/testData/quickfix/suppress/external/suppressAvailable.kt | 3 ++- .../quickfix/suppress/external/suppressAvailable.kt.after | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt index 9cb42fd87b9..a79db4063b2 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/inspections/KotlinInspectionSuppressor.kt @@ -22,6 +22,8 @@ class KotlinInspectionSuppressor : InspectionSuppressor { object : SuppressQuickFix { override fun getFamilyName() = it.familyName + override fun getName() = it.text + override fun applyFix(project: Project, descriptor: ProblemDescriptor) = it.invoke(project, null, descriptor.psiElement) override fun isAvailable(project: Project, context: PsiElement) = it.isAvailable(project, null, context) diff --git a/idea/testData/quickfix/suppress/external/suppressAvailable.kt b/idea/testData/quickfix/suppress/external/suppressAvailable.kt index e7e5b49a53d..6fa33be07d9 100644 --- a/idea/testData/quickfix/suppress/external/suppressAvailable.kt +++ b/idea/testData/quickfix/suppress/external/suppressAvailable.kt @@ -1,4 +1,4 @@ -// "Suppress Warnings" "true" +// "Suppress 'SpellCheckingInspection' for val str" "true" // TOOL: com.intellij.spellchecker.inspections.SpellCheckingInspection // ACTION: Add 'const' modifier // ACTION: Typo: Change to... @@ -12,5 +12,6 @@ // ACTION: Convert property initializer to getter // ACTION: Inject language or reference // ACTION: To raw string literal +// ACTION: Suppress 'SpellCheckingInspection' for file suppressAvailable.kt val str = "kjsghkjshtiurhuig" \ No newline at end of file diff --git a/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after b/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after index 8fa8b38296c..ea3e174503c 100644 --- a/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after +++ b/idea/testData/quickfix/suppress/external/suppressAvailable.kt.after @@ -1,4 +1,4 @@ -// "Suppress Warnings" "true" +// "Suppress 'SpellCheckingInspection' for val str" "true" // TOOL: com.intellij.spellchecker.inspections.SpellCheckingInspection // ACTION: Add 'const' modifier // ACTION: Typo: Change to... @@ -12,6 +12,7 @@ // ACTION: Convert property initializer to getter // ACTION: Inject language or reference // ACTION: To raw string literal +// ACTION: Suppress 'SpellCheckingInspection' for file suppressAvailable.kt @Suppress("SpellCheckingInspection") val str = "kjsghkjshtiurhuig" \ No newline at end of file