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

Speed up LatexPackageNotInstalledInspection.kt #1595

Merged
merged 2 commits into from
Sep 29, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.run.latex.LatexDistribution
import nl.hannahsten.texifyidea.util.*

/**
* Check if a LaTeX package is not installed (only for TeX Live, since MiKTeX downloads them automatically).
*/
class LatexPackageNotInstalledInspection : TexifyInspectionBase() {
// This caches packages which are not installed, which is needed
// otherwise we are running the expensive call to tlmgr basically on
// every letter typed - exactly the same call with the same results
private val knownNotInstalledPackages = mutableSetOf<String>()

override val inspectionGroup: InsightGroup = InsightGroup.LATEX

override val inspectionId: String =
Expand All @@ -43,7 +51,7 @@ class LatexPackageNotInstalledInspection : TexifyInspectionBase() {
val customPackages = LatexDefinitionIndex.getCommandsByName("\\ProvidesPackage", file.project, file.project
.projectSearchScope)
.map { it.requiredParameter(0) }
.map { it?.toLowerCase() }
.mapNotNull { it?.toLowerCase() }
val packages = installedPackages + customPackages

val commands = file.childrenOfType(LatexCommands::class)
Expand All @@ -52,31 +60,39 @@ class LatexPackageNotInstalledInspection : TexifyInspectionBase() {
for (command in commands) {
val `package` = command.requiredParameters.firstOrNull()?.toLowerCase() ?: continue
if (`package` !in packages) {
// Manually check if the package is installed (e.g. rubikrotation is listed as rubik, so we need to check it separately).
if ("tlmgr search --file /$`package`.sty".runCommand()
// Use the cache or manually check if the package is installed (e.g. rubikrotation is listed as rubik, so we need to check it separately).
if (knownNotInstalledPackages.contains(`package`) || "tlmgr search --file /$`package`.sty".runCommand()
?.isEmpty() == true) {
descriptors.add(manager.createProblemDescriptor(
command,
"Package is not installed or \\ProvidesPackage is missing",
InstallPackage(SmartPointerManager.getInstance(file.project).createSmartPsiElementPointer(file), `package`),
InstallPackage(SmartPointerManager.getInstance(file.project).createSmartPsiElementPointer(file), `package`, knownNotInstalledPackages),
ProblemHighlightType.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`)
}
}
}
}
return descriptors
}

private class InstallPackage(val filePointer: SmartPsiElementPointer<PsiFile>, val packageName: String) : LocalQuickFix {
private class InstallPackage(val filePointer: SmartPsiElementPointer<PsiFile>, val packageName: String, val knownNotInstalledPackages: MutableSet<String>) : LocalQuickFix {
override fun getFamilyName(): String = "Install $packageName"

/**
* Install the package in the background and add it to the list of installed
* packages when done.
*/
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
// I don't know if you actually could install multiple packages
// with one fix, but it's not a bad idea to clear cache once in a while
knownNotInstalledPackages.clear()
ProgressManager.getInstance()
.run(object : Task.Backgroundable(project, "Installing $packageName...") {
override fun run(indicator: ProgressIndicator) {
Expand Down
4 changes: 4 additions & 0 deletions src/nl/hannahsten/texifyidea/util/Packages.kt
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ object PackageUtils {
}

object TexLivePackages {
/**
* List of installed packages.
*/
var packageList: MutableList<String> = mutableListOf()

/**
Expand All @@ -314,6 +317,7 @@ object TexLivePackages {
fun findTexLiveName(task: Task.Backgroundable, packageName: String): String? {
// Find the package name for tlmgr.
task.title = "Searching for $packageName..."
// Assume that you can not use the bundle name in a \usepackage if it is different from the package name (otherwise this search won't work and we would need to use tlmgr search --global $packageName
val searchResult = "tlmgr search --file --global /$packageName.sty".runCommand()
?: return null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@ class LatexEnvironmentReferenceTest : BasePlatformTestCase() {
\end{nodocument<caret>}
""".trimIndent())
}

fun testEnvironmentRenameBegin() {
myFixture.configureByText(LatexFileType, """
\begin{document<caret>}
\end{document}
""".trimIndent())
myFixture.renameElementAtCaret("nodocument")
myFixture.checkResult("""
\begin{nodocument<caret>}
\end{nodocument}
""".trimIndent())
}
}