|
| 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 | +} |
0 commit comments