From 6d133237174d01a36e1307334692a8e49f4be2d3 Mon Sep 17 00:00:00 2001 From: Thomas Schouten Date: Thu, 28 Nov 2024 17:40:17 +0100 Subject: [PATCH] Inspection suppression for a single line --- CHANGELOG.md | 1 + Writerside/topics/Inspections.md | 1 + .../texifyidea/lang/magic/MagicComments.kt | 15 +++++++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d841c1d37..5c9e39ac1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased] ### Added +* Inspections can now be suppressed for any single line, or block of text ### Fixed * Disallow unmatched braces after \@ifnextchar diff --git a/Writerside/topics/Inspections.md b/Writerside/topics/Inspections.md index 08dc1e6d4..a9b186a75 100644 --- a/Writerside/topics/Inspections.md +++ b/Writerside/topics/Inspections.md @@ -9,6 +9,7 @@ If you see a minor bug in an inspection, like some missing metadata about comman Most inspections can be ignored for only a single line, environment or file. To do this, use the format `%! Suppress = MyInspectionName`. The easiest way is usually to use the available context menu on the inspection warning itself. +In some cases, like block of text spanning multiple lines, placing a magic comment in front of it will suppress the inspection for the whole block, instead of for a single line. ![Suppression](suppression-menu.png) diff --git a/src/nl/hannahsten/texifyidea/lang/magic/MagicComments.kt b/src/nl/hannahsten/texifyidea/lang/magic/MagicComments.kt index 88eb90b34..bb9768ed5 100644 --- a/src/nl/hannahsten/texifyidea/lang/magic/MagicComments.kt +++ b/src/nl/hannahsten/texifyidea/lang/magic/MagicComments.kt @@ -5,9 +5,12 @@ package nl.hannahsten.texifyidea.lang.magic import com.intellij.psi.PsiComment import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile +import com.intellij.psi.PsiWhiteSpace +import com.intellij.psi.util.prevLeaf +import com.intellij.psi.util.prevLeafs import nl.hannahsten.texifyidea.psi.* -import nl.hannahsten.texifyidea.util.* import nl.hannahsten.texifyidea.util.files.document +import nl.hannahsten.texifyidea.util.lineIndentationByOffset import nl.hannahsten.texifyidea.util.parser.* import java.util.* @@ -296,7 +299,15 @@ fun PsiElement.magicComment(): MagicComment? = when (this) { is LatexMathEnvironment -> this.magicComment() is LatexCommands -> this.magicComment() is LatexGroup -> this.magicComment() - else -> null + else -> this.commentOnPreviousLine() +} + +/** + * To find any comment at the previous line, we need to check for newlines explicitly. + * Return null if the previous line is not a magic comment. + */ +fun PsiElement.commentOnPreviousLine(): MagicComment? { + return prevLeafs.firstOrNull { it is PsiWhiteSpace && it.text.contains("\n") }?.backwardMagicCommentLookup { prevLeaf(true) } } /**