Skip to content

Commit 1dc67b2

Browse files
committed
Add AT inspection suppressor
1 parent 4c4697b commit 1dc67b2

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Minecraft Development for IntelliJ
3+
*
4+
* https://mcdev.io/
5+
*
6+
* Copyright (C) 2024 minecraft-dev
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published
10+
* by the Free Software Foundation, version 3.0 only.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
package com.demonwav.mcdev.platform.mcp.at
22+
23+
import com.demonwav.mcdev.platform.mcp.at.gen.psi.AtEntry
24+
import com.intellij.codeInspection.InspectionSuppressor
25+
import com.intellij.codeInspection.LocalQuickFixOnPsiElement
26+
import com.intellij.codeInspection.SuppressQuickFix
27+
import com.intellij.codeInspection.util.IntentionFamilyName
28+
import com.intellij.codeInspection.util.IntentionName
29+
import com.intellij.openapi.project.Project
30+
import com.intellij.psi.PsiElement
31+
import com.intellij.psi.PsiFile
32+
import com.intellij.psi.util.parentOfType
33+
34+
class AtInspectionSuppressor : InspectionSuppressor {
35+
36+
override fun isSuppressedFor(element: PsiElement, toolId: String): Boolean {
37+
val entry = element.parentOfType<AtEntry>(withSelf = true) ?: return false
38+
val comment = entry.commentText ?: return false
39+
val suppressed = comment.substringAfter("Suppress:").substringBefore(' ').split(',')
40+
return toolId in suppressed
41+
}
42+
43+
override fun getSuppressActions(
44+
element: PsiElement?,
45+
toolId: String
46+
): Array<out SuppressQuickFix> {
47+
if (element == null) {
48+
return SuppressQuickFix.EMPTY_ARRAY
49+
}
50+
51+
return arrayOf(AtSuppressQuickFix(element, toolId))
52+
}
53+
54+
class AtSuppressQuickFix(element: PsiElement, val toolId: String) : LocalQuickFixOnPsiElement(element), SuppressQuickFix {
55+
56+
override fun getText(): @IntentionName String = "Suppress $toolId"
57+
58+
override fun getFamilyName(): @IntentionFamilyName String = "Suppress inspection"
59+
60+
override fun invoke(
61+
project: Project,
62+
file: PsiFile,
63+
startElement: PsiElement,
64+
endElement: PsiElement
65+
) {
66+
val entry = startElement.parentOfType<AtEntry>(withSelf = true) ?: return
67+
val commentText = entry.commentText?.trim()
68+
if (commentText == null) {
69+
entry.setComment("Suppress:$toolId")
70+
return
71+
}
72+
73+
val suppressStart = commentText.indexOf("Suppress:")
74+
if (suppressStart == -1) {
75+
entry.setComment("Suppress:$toolId $commentText")
76+
return
77+
}
78+
79+
val suppressEnd = commentText.indexOf(' ', suppressStart).takeUnless { it == -1 } ?: commentText.length
80+
val newComment = commentText.substring(suppressStart, suppressEnd) + ",$toolId" + commentText.substring(suppressEnd)
81+
entry.setComment(newComment)
82+
}
83+
84+
override fun isAvailable(
85+
project: Project,
86+
context: PsiElement
87+
): Boolean = context.isValid
88+
89+
override fun isSuppressAll(): Boolean = false
90+
}
91+
}

src/main/kotlin/platform/mcp/at/psi/mixins/AtEntryMixin.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.demonwav.mcdev.platform.mcp.at.gen.psi.AtFieldName
2727
import com.demonwav.mcdev.platform.mcp.at.gen.psi.AtFunction
2828
import com.demonwav.mcdev.platform.mcp.at.gen.psi.AtKeyword
2929
import com.demonwav.mcdev.platform.mcp.at.psi.AtElement
30+
import com.intellij.psi.PsiComment
3031

3132
interface AtEntryMixin : AtElement {
3233

@@ -35,13 +36,16 @@ interface AtEntryMixin : AtElement {
3536
val fieldName: AtFieldName?
3637
val function: AtFunction?
3738
val keyword: AtKeyword
39+
val comment: PsiComment?
40+
val commentText: String?
3841

3942
fun setEntry(entry: String)
4043
fun setKeyword(keyword: AtElementFactory.Keyword)
4144
fun setClassName(className: String)
4245
fun setFieldName(fieldName: String)
4346
fun setFunction(function: String)
4447
fun setAsterisk()
48+
fun setComment(text: String?)
4549

4650
fun replaceMember(element: AtElement) {
4751
// One of these must be true

src/main/kotlin/platform/mcp/at/psi/mixins/impl/AtEntryImplMixin.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ import com.demonwav.mcdev.platform.mcp.at.AtElementFactory
2424
import com.demonwav.mcdev.platform.mcp.at.psi.mixins.AtEntryMixin
2525
import com.intellij.extapi.psi.ASTWrapperPsiElement
2626
import com.intellij.lang.ASTNode
27+
import com.intellij.psi.PsiComment
28+
import com.intellij.psi.util.PsiTreeUtil
2729

2830
abstract class AtEntryImplMixin(node: ASTNode) : ASTWrapperPsiElement(node), AtEntryMixin {
2931

32+
override val comment: PsiComment?
33+
get() = PsiTreeUtil.skipWhitespacesForward(this) as? PsiComment
34+
35+
override val commentText: String?
36+
get() = comment?.text?.substring(1)
37+
3038
override fun setEntry(entry: String) {
3139
replace(AtElementFactory.createEntry(project, entry))
3240
}
@@ -53,4 +61,20 @@ abstract class AtEntryImplMixin(node: ASTNode) : ASTWrapperPsiElement(node), AtE
5361
val asterisk = AtElementFactory.createAsterisk(project)
5462
replaceMember(asterisk)
5563
}
64+
65+
override fun setComment(text: String?) {
66+
if (text == null) {
67+
comment?.delete()
68+
return
69+
}
70+
71+
val newComment = AtElementFactory.createComment(project, text)
72+
val existingComment = comment
73+
if (existingComment == null) {
74+
parent.addAfter(newComment, this)
75+
return
76+
}
77+
78+
existingComment.replace(newComment)
79+
}
5680
}

src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@
631631
implementationClass="com.demonwav.mcdev.platform.mcp.at.completion.AtCompletionContributor"/>
632632

633633
<annotator language="Access Transformers" implementationClass="com.demonwav.mcdev.platform.mcp.at.AtAnnotator"/>
634+
<lang.inspectionSuppressor language="Access Transformers" implementationClass="com.demonwav.mcdev.platform.mcp.at.AtInspectionSuppressor"/>
634635
<psi.referenceContributor language="Access Transformers" implementation="com.demonwav.mcdev.platform.mcp.at.AtReferenceContributor"/>
635636
<lang.elementManipulator forClass="com.demonwav.mcdev.platform.mcp.at.gen.psi.AtClassName"
636637
implementationClass="com.demonwav.mcdev.platform.mcp.at.AtClassNameElementManipulator"/>

0 commit comments

Comments
 (0)