Skip to content

Commit

Permalink
Clean up LatexPackageNotInstalledInspection
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 27, 2024
1 parent b410288 commit 6d7b1c2
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 60 deletions.
1 change: 0 additions & 1 deletion resources/META-INF/extensions/startup.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<idea-plugin>
<extensions defaultExtensionNs="com.intellij">
<postStartupActivity implementation="nl.hannahsten.texifyidea.startup.StartEvinceInverseSearchListener"/>
<postStartupActivity implementation="nl.hannahsten.texifyidea.startup.TexLivePackageListInitializer"/>
<postStartupActivity implementation="nl.hannahsten.texifyidea.startup.LatexPackageLocationCacheInitializer"/>
</extensions>
</idea-plugin>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nl.hannahsten.texifyidea.inspections.latex.probablebugs.packages

import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
import com.intellij.codeInsight.intention.preview.IntentionPreviewInfo
import com.intellij.codeInspection.InspectionManager
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemDescriptor
Expand All @@ -17,10 +18,13 @@ import com.intellij.psi.SmartPsiElementPointer
import nl.hannahsten.texifyidea.index.LatexDefinitionIndex
import nl.hannahsten.texifyidea.inspections.InsightGroup
import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase
import nl.hannahsten.texifyidea.lang.commands.LatexGenericRegularCommand
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.reference.InputFileReference
import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil
import nl.hannahsten.texifyidea.settings.sdk.TexliveSdk
import nl.hannahsten.texifyidea.util.TexLivePackages
import nl.hannahsten.texifyidea.util.magic.cmd
import nl.hannahsten.texifyidea.util.parser.childrenOfType
import nl.hannahsten.texifyidea.util.parser.requiredParameter
import nl.hannahsten.texifyidea.util.projectSearchScope
Expand Down Expand Up @@ -49,48 +53,54 @@ class LatexPackageNotInstalledInspection : TexifyInspectionBase() {
override fun inspectFile(file: PsiFile, manager: InspectionManager, isOntheFly: Boolean): List<ProblemDescriptor> {
val descriptors = descriptorList()
// We have to check whether tlmgr is installed, for those users who don't want to install TeX Live in the official way
if (LatexSdkUtil.isTlmgrAvailable(file.project)) {
val installedPackages = TexLivePackages.packageList
val customPackages = LatexDefinitionIndex.Util.getCommandsByName(
"\\ProvidesPackage", file.project,
file.project
.projectSearchScope
)
.map { it.requiredParameter(0) }
.mapNotNull { it?.lowercase(Locale.getDefault()) }
val packages = installedPackages + customPackages

val commands = file.childrenOfType(LatexCommands::class)
.filter { it.name == "\\usepackage" || it.name == "\\RequirePackage" }

for (command in commands) {
@Suppress("ktlint:standard:property-naming")
val `package` = command.getRequiredParameters().firstOrNull()?.lowercase(Locale.getDefault()) ?: continue
if (`package` !in packages) {
// Use the cache or check if the file reference resolves (in the same way we resolve for the gutter icon).
if (
knownNotInstalledPackages.contains(`package`) ||
command.references.filterIsInstance<InputFileReference>().mapNotNull { it.resolve() }.isEmpty()
) {
descriptors.add(
manager.createProblemDescriptor(
command,
"Package is not installed or \\ProvidesPackage is missing",
InstallPackage(
SmartPointerManager.getInstance(file.project).createSmartPsiElementPointer(file),
`package`,
knownNotInstalledPackages
),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOntheFly
)
if (!LatexSdkUtil.isTlmgrAvailable(file.project)) return descriptors

if (TexLivePackages.packageList.isEmpty() && TexliveSdk.Cache.isAvailable) {
val result = "tlmgr list --only-installed".runCommand() ?: return emptyList()
TexLivePackages.packageList = Regex("i\\s(.*):").findAll(result)
.map { it.groupValues.last() }.toMutableList()
}

val installedPackages = TexLivePackages.packageList
val customPackages = LatexDefinitionIndex.Util.getCommandsByName(
LatexGenericRegularCommand.PROVIDESPACKAGE.cmd, file.project,
file.project
.projectSearchScope
)
.map { it.requiredParameter(0) }
.mapNotNull { it?.lowercase(Locale.getDefault()) }
val packages = installedPackages + customPackages

val commands = file.childrenOfType(LatexCommands::class)
.filter { it.name == LatexGenericRegularCommand.USEPACKAGE.cmd || it.name == LatexGenericRegularCommand.REQUIREPACKAGE.cmd }

for (command in commands) {
@Suppress("ktlint:standard:property-naming")
val `package` = command.getRequiredParameters().firstOrNull()?.lowercase(Locale.getDefault()) ?: continue
if (`package` !in packages) {
// Use the cache or check if the file reference resolves (in the same way we resolve for the gutter icon).
if (
knownNotInstalledPackages.contains(`package`) ||
command.references.filterIsInstance<InputFileReference>().mapNotNull { it.resolve() }.isEmpty()
) {
descriptors.add(
manager.createProblemDescriptor(
command,
"Package is not installed or \\ProvidesPackage is missing",
InstallPackage(
SmartPointerManager.getInstance(file.project).createSmartPsiElementPointer(file),
`package`,
knownNotInstalledPackages
),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOntheFly
)
knownNotInstalledPackages.add(`package`)
}
else {
// Apparently the package is installed, but was not found initially by the TexLivePackageListInitializer (for example stackrel, contained in the oberdiek bundle)
TexLivePackages.packageList.add(`package`)
}
)
knownNotInstalledPackages.add(`package`)
}
else {
// Apparently the package is installed, but was not found initially by the TexLivePackageListInitializer (for example stackrel, contained in the oberdiek bundle)
TexLivePackages.packageList.add(`package`)
}
}
}
Expand All @@ -101,6 +111,11 @@ class LatexPackageNotInstalledInspection : TexifyInspectionBase() {

override fun getFamilyName(): String = "Install $packageName"

override fun generatePreview(project: Project, previewDescriptor: ProblemDescriptor): IntentionPreviewInfo {
// Nothing is modified
return IntentionPreviewInfo.EMPTY
}

/**
* Install the package in the background and add it to the list of installed
* packages when done.
Expand Down

This file was deleted.

0 comments on commit 6d7b1c2

Please sign in to comment.