-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add inspection suppressor to allow suppression on fields
- Loading branch information
Showing
5 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...ugin/src/main/kotlin/com/apollographql/ijplugin/inspection/GraphQLInspectionSuppressor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.apollographql.ijplugin.inspection | ||
|
||
import com.apollographql.ijplugin.ApolloBundle | ||
import com.intellij.codeInspection.InspectionSuppressor | ||
import com.intellij.codeInspection.SuppressQuickFix | ||
import com.intellij.codeInspection.SuppressionUtil | ||
import com.intellij.lang.jsgraphql.ide.validation.fixes.GraphQLSuppressByCommentFix | ||
import com.intellij.lang.jsgraphql.psi.GraphQLField | ||
import com.intellij.psi.PsiComment | ||
import com.intellij.psi.PsiElement | ||
import com.intellij.psi.util.PsiTreeUtil | ||
import java.util.regex.Pattern | ||
|
||
private val SUPPRESS_IN_LINE_COMMENT_PATTERN = Pattern.compile("#" + SuppressionUtil.COMMON_SUPPRESS_REGEXP) | ||
|
||
class GraphQLInspectionSuppressor : InspectionSuppressor { | ||
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean { | ||
return element.isSuppressedOnSelfOrParent(toolId, SUPPRESS_IN_LINE_COMMENT_PATTERN) | ||
} | ||
|
||
override fun getSuppressActions( | ||
element: PsiElement?, | ||
toolId: String, | ||
): Array<SuppressQuickFix> { | ||
return if (element == null) { | ||
SuppressQuickFix.EMPTY_ARRAY | ||
} else { | ||
arrayOf( | ||
GraphQLSuppressByCommentFix(toolId, GraphQLField::class.java, ApolloBundle.message("inspection.suppress.field")), | ||
) | ||
} | ||
} | ||
} | ||
|
||
fun PsiElement.isSuppressedOnSelfOrParent( | ||
toolId: String, | ||
suppressInLineCommentPattern: Pattern, | ||
): Boolean { | ||
var element: PsiElement? = this | ||
while (element != null) { | ||
val prev = PsiTreeUtil.skipWhitespacesBackward(element) | ||
if (prev is PsiComment) { | ||
val text = prev.getText() | ||
val matcher = suppressInLineCommentPattern.matcher(text) | ||
if (matcher.matches() && SuppressionUtil.isInspectionToolIdMentioned(matcher.group(1), toolId)) { | ||
return true | ||
} | ||
} | ||
element = element.parent | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ query animals { | |
} | ||
... on dog { | ||
id | ||
# noinspection ApolloUnusedField | ||
barkVolume | ||
fieldOnDogAndCat | ||
} | ||
|