Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspection suppression for a single line #3779

Merged
merged 2 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Added
* Automatically index bibliography files outside the project that are included by an absolute path
* Disable quotes inspection when TeX ligatures are disabled by fontspec
* Inspections can now be suppressed for any single line, or block of text

### Fixed

Expand Down
1 change: 1 addition & 0 deletions Writerside/topics/Inspections.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
15 changes: 13 additions & 2 deletions src/nl/hannahsten/texifyidea/lang/magic/MagicComments.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand Down Expand Up @@ -296,7 +299,15 @@ fun PsiElement.magicComment(): MagicComment<String, String>? = 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<String, String>? {
return prevLeafs.firstOrNull { it is PsiWhiteSpace && it.text.contains("\n") }?.backwardMagicCommentLookup { prevLeaf(true) }
}

/**
Expand Down
Loading