-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3806 from Hannah-Sten/glossary-usages
Add inspection to warn about a missing reference for a glossary occurrence
- Loading branch information
Showing
8 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
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
10 changes: 10 additions & 0 deletions
10
resources/inspectionDescriptions/LatexMissingGlossaryReference.html
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,10 @@ | ||
<html> | ||
<body> | ||
Reports occurrences of glossary entries that are not marked with a \gls command. | ||
<!-- tooltip end --> | ||
<p> | ||
When using a glossary, it is good practice to reference every glossary entry with a \gls-like command. | ||
This makes sure that the list of pages with occurrences in the glossary is complete. | ||
</p> | ||
</body> | ||
</html> |
64 changes: 64 additions & 0 deletions
64
...hsten/texifyidea/inspections/latex/typesetting/LatexMissingGlossaryReferenceInspection.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,64 @@ | ||
package nl.hannahsten.texifyidea.inspections.latex.typesetting | ||
|
||
import com.intellij.codeInspection.InspectionManager | ||
import com.intellij.codeInspection.LocalQuickFix | ||
import com.intellij.codeInspection.ProblemDescriptor | ||
import com.intellij.codeInspection.ProblemHighlightType | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.PsiFile | ||
import nl.hannahsten.texifyidea.index.LatexGlossaryEntryIndex | ||
import nl.hannahsten.texifyidea.inspections.InsightGroup | ||
import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase | ||
import nl.hannahsten.texifyidea.lang.commands.LatexGlossariesCommand | ||
import nl.hannahsten.texifyidea.psi.LatexNormalText | ||
import nl.hannahsten.texifyidea.psi.LatexPsiHelper | ||
import nl.hannahsten.texifyidea.util.parser.childrenOfType | ||
import nl.hannahsten.texifyidea.util.toTextRange | ||
|
||
/** | ||
* Glossary entries should be referenced for all occurrences. | ||
*/ | ||
class LatexMissingGlossaryReferenceInspection : TexifyInspectionBase() { | ||
override val inspectionGroup = InsightGroup.LATEX | ||
override val inspectionId = "MissingGlossaryReference" | ||
override fun getDisplayName() = "Missing glossary reference" | ||
|
||
override fun inspectFile(file: PsiFile, manager: InspectionManager, isOntheFly: Boolean): List<ProblemDescriptor> { | ||
val descriptors = mutableListOf<ProblemDescriptor>() | ||
val names = LatexGlossaryEntryIndex.Util.getItemsInFileSet(file).mapNotNull { LatexGlossariesCommand.extractGlossaryName(it) } | ||
// Unfortunately the lowest level we have is a block of text, so we have to do a text-based search | ||
file.childrenOfType<LatexNormalText>().forEach { textElement -> | ||
val text = textElement.text | ||
names.forEach { name -> | ||
val correctOccurrences = "\\\\gls[^{]+\\{($name)}".toRegex().findAll(text).mapNotNull { it.groups.firstOrNull()?.range } | ||
val allOccurrences = name.toRegex().findAll(text).map { it.range } | ||
allOccurrences.filter { !correctOccurrences.contains(it) }.forEach { range -> | ||
descriptors.add( | ||
manager.createProblemDescriptor( | ||
textElement, | ||
range.toTextRange(), | ||
"Missing glossary reference", | ||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, | ||
isOntheFly, | ||
AddGlsFix(), | ||
) | ||
) | ||
} | ||
} | ||
} | ||
return descriptors | ||
} | ||
|
||
private class AddGlsFix : LocalQuickFix { | ||
override fun getFamilyName() = "Add \\gls command" | ||
|
||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { | ||
val range = descriptor.textRangeInElement | ||
val newText = descriptor.psiElement.text.replaceRange(range.endOffset, range.endOffset, "}") | ||
.replaceRange(range.startOffset, range.startOffset, "\\gls{") | ||
|
||
val newElement = LatexPsiHelper(project).createFromText(newText).firstChild | ||
descriptor.psiElement.replace(newElement) | ||
} | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
...n/texifyidea/inspections/latex/typesetting/LatexMissingGlossaryReferenceInspectionTest.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,23 @@ | ||
package nl.hannahsten.texifyidea.inspections.latex.typesetting | ||
|
||
import nl.hannahsten.texifyidea.file.LatexFileType | ||
import nl.hannahsten.texifyidea.inspections.TexifyInspectionTestBase | ||
|
||
class LatexMissingGlossaryReferenceInspectionTest : TexifyInspectionTestBase(LatexMissingGlossaryReferenceInspection()) { | ||
|
||
fun testMissingReference() { | ||
myFixture.configureByText(LatexFileType, """\newglossaryentry{sample}{name={sample},description={an example}} \gls{sample} \Glslink{sample} <warning descr="Missing glossary reference">sample</warning>""") | ||
myFixture.checkHighlighting() | ||
} | ||
|
||
fun testAddGls() { | ||
testQuickFix( | ||
""" | ||
\newglossaryentry{sample}{name={sample},description={an example}} \gls{sample} sample text | ||
""".trimIndent(), | ||
""" | ||
\newglossaryentry{sample}{name={sample},description={an example}} \gls{sample} \gls{sample} text | ||
""".trimIndent() | ||
) | ||
} | ||
} |