Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 30, 2024
2 parents da28600 + 15c8d09 commit 280947f
Show file tree
Hide file tree
Showing 20 changed files with 317 additions and 65 deletions.
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,25 @@
* Support label references to user defined listings environment
* Add option to disable automatic compilation in power save mode
* Convert automatic compilation settings to a combobox

* Add experimental support for the addtoluatexpath package




* Add inspection to check for LaTeX package updates



* Add checkboxes to graphic insertion wizard for relative width or height

### Fixed
* Fix parse error when using commands with arguments in parameter of \href or \url
* Fix parse error when using parentheses in a group in a key value command argument
* Fix parse erron when using inline math in cases* environment in inline math
*
* Fix exceptions #3813, #3818
* Fix false positive non-breaking space warning for \nameref
* Fix confusion with \micro and \mu unicode characters

## [0.9.10-alpha.4] - 2024-12-21

Expand Down
7 changes: 7 additions & 0 deletions Writerside/topics/Packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ This inspection is for TeX Live only, MiKTeX automatically installs packages on
When using `\usepackage` or `\RequirePackage`, TeXiFy checks if the packages is installed.
If it isn’t installed, it provides a quick fix to install the package.

## Package update available
_Since b0.9.10_

When a package has an update available on CTAN, this inspection will provide a quickfix to update the package.
Currently, it only works when tlmgr (TeX Live manager) is installed.
The list of available package updates is cached until IntelliJ is restarted or the quickfix is used.

## Package name does not match file name
_Since b0.6.10_

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
groupPath="LaTeX" groupName="Probable bugs" displayName="Package is not installed"
enabledByDefault="true"
level="WARNING" />
<localInspection language="Latex" implementationClass="nl.hannahsten.texifyidea.inspections.latex.probablebugs.packages.LatexPackageUpdateInspection"
groupPath="LaTeX" groupName="Probable bugs" displayName="Package has an update available"
enabledByDefault="true"
level="WARNING" />
<localInspection language="Latex" implementationClass="nl.hannahsten.texifyidea.inspections.latex.probablebugs.packages.LatexPackageNameDoesNotMatchFileNameInspection"
groupPath="LaTeX" groupName="Probable bugs" displayName="Package name does not match file name"
enabledByDefault="true"
Expand Down
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>
8 changes: 8 additions & 0 deletions resources/inspectionDescriptions/LatexPackageUpdate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

<html>
<body>
The package given in a <tt>\usepackage</tt> or <tt>\RequirePackage</tt> has an update available.
<!-- tooltip end -->
This inspection only checks installed packages on texlive systems (with tlmgr).
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import nl.hannahsten.texifyidea.index.LatexIncludesIndex
import nl.hannahsten.texifyidea.settings.TexifySettings
import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil
import nl.hannahsten.texifyidea.util.Log
import nl.hannahsten.texifyidea.util.files.addToLuatexPathSearchDirectories
import nl.hannahsten.texifyidea.util.getTexinputsPaths
import nl.hannahsten.texifyidea.util.isTestProject
import nl.hannahsten.texifyidea.util.magic.CommandMagic
Expand Down Expand Up @@ -76,8 +77,8 @@ class LatexIndexableSetContributor : IndexableSetContributor() {

// Using the index while building it may be problematic, cache the result and hope it doesn't create too much trouble
if (Cache.externalDirectFileInclusions == null && !DumbService.isDumb(project)) {
runInBackground(project, "Searching for external bib files...") {
// For now, just do this for bibliography and direct input commands, as there this is most common
runInBackground(project, "Searching for inclusions by absolute path...") {
// Bibliography and direct input commands
val commandNames = CommandMagic.includeOnlyExtensions.entries.filter { it.value.contains("bib") || it.value.contains("tex") }.map { it.key }.toSet()
val externalFiles = runReadAction {
LatexIncludesIndex.Util.getCommandsByNames(commandNames, project, GlobalSearchScope.projectScope(project))
Expand All @@ -93,6 +94,12 @@ class LatexIndexableSetContributor : IndexableSetContributor() {
}
runReadAction { file?.parent }
}
.toMutableList()

// addtoluatexpath package
val luatexPathDirectories = addToLuatexPathSearchDirectories(project)
externalFiles.addAll(luatexPathDirectories)

Cache.externalDirectFileInclusions = externalFiles.toSet()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class LatexUnicodeInspection : TexifyInspectionBase() {
// Try to find in lookup for special command
val replacement: String?
val command: LatexCommand? = if (inMathMode) {
LatexMathCommand.findByDisplay(c)?.firstOrNull()
LatexMathCommand.findByDisplay(c)?.firstOrNull() ?: LatexRegularCommand.findByDisplay(c)?.firstOrNull()
}
else {
LatexRegularCommand.findByDisplay(c)?.firstOrNull()
Expand Down
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task.Backgroundable
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import com.intellij.psi.SmartPointerManager
import com.intellij.psi.SmartPsiElementPointer
import nl.hannahsten.texifyidea.inspections.InsightGroup
import nl.hannahsten.texifyidea.inspections.TexifyInspectionBase
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.settings.sdk.LatexSdkUtil
import nl.hannahsten.texifyidea.settings.sdk.TexliveSdk
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.parser.childrenOfType
import nl.hannahsten.texifyidea.util.parser.requiredParameter
import nl.hannahsten.texifyidea.util.runCommand
import nl.hannahsten.texifyidea.util.runCommandWithExitCode

/**
* Check for available updates for LaTeX packages.
* Also see [LatexPackageNotInstalledInspection].
*/
class LatexPackageUpdateInspection : TexifyInspectionBase() {

object Cache {
/** Map package name to old and new revision number */
var availablePackageUpdates = mapOf<String, Pair<String?, String?>>()
}

override val inspectionGroup = InsightGroup.LATEX

override val inspectionId = "PackageUpdate"

override fun getDisplayName() = "Package has an update available"

override fun inspectFile(file: PsiFile, manager: InspectionManager, isOntheFly: Boolean): List<ProblemDescriptor> {
if (!LatexSdkUtil.isTlmgrAvailable(file.project) || !TexliveSdk.Cache.isAvailable) return emptyList()

if (Cache.availablePackageUpdates.isEmpty()) {
val tlmgrExecutable = LatexSdkUtil.getExecutableName("tlmgr", file.project)
val result = runCommand(tlmgrExecutable, "update", "--list") ?: return emptyList()
Cache.availablePackageUpdates = """update:\s*(?<package>[^ ]+).*local:\s*(?<local>\d+), source:\s*(?<source>\d+)""".toRegex()
.findAll(result)
.mapNotNull { Pair(it.groups["package"]?.value ?: return@mapNotNull null, Pair(it.groups["local"]?.value, it.groups["source"]?.value)) }
.associate { it }
}

return file.childrenOfType<LatexCommands>()
.filter { it.name in CommandMagic.packageInclusionCommands }
.filter { it.requiredParameter(0) in Cache.availablePackageUpdates.keys }
.mapNotNull {
val packageName = it.requiredParameter(0) ?: return@mapNotNull null
val packageVersions = Cache.availablePackageUpdates[packageName] ?: return@mapNotNull null
manager.createProblemDescriptor(
it,
"Update available for package $packageName",
arrayOf(
UpdatePackage(SmartPointerManager.getInstance(file.project).createSmartPsiElementPointer(file), packageName, packageVersions.first, packageVersions.second),
UpdatePackage(SmartPointerManager.getInstance(file.project).createSmartPsiElementPointer(file), "--all", null, null),
),
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
isOntheFly,
false,
)
}
}

private class UpdatePackage(val filePointer: SmartPsiElementPointer<PsiFile>, val packageName: String, val old: String?, val new: String?) : LocalQuickFix {

override fun getFamilyName(): String = if (packageName == "--all") "Update all packages" else if (old != null && new != null) "Update $packageName from revision $old to revision $new" else "Update $packageName"

override fun generatePreview(project: Project, previewDescriptor: ProblemDescriptor): IntentionPreviewInfo {
// Nothing is modified
return IntentionPreviewInfo.Html("Run tlngr update $packageName")
}

override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val message = if (packageName == "--all") "Updating all packages" else "Updating $packageName..."
ProgressManager.getInstance().run(object : Backgroundable(project, message) {
override fun run(indicator: ProgressIndicator) {
val tlmgrExecutable = LatexSdkUtil.getExecutableName("tlmgr", project)

val timeout: Long = if (packageName == "--all") 1200 else 15
var (output, exitCode) = runCommandWithExitCode(tlmgrExecutable, "update", packageName, returnExceptionMessage = true, timeout = timeout)
if (output?.contains("tlmgr update --self") == true) {
val (tlmgrOutput, tlmgrExitCode) = runCommandWithExitCode(tlmgrExecutable, "update", "--self", returnExceptionMessage = true, timeout = 20)
if (tlmgrExitCode != 0) {
Notification(
"LaTeX",
"Package $packageName not updated",
"Could not update tlmgr: $tlmgrOutput",
NotificationType.ERROR
).notify(project)
indicator.cancel()
}
title = message
val (secondOutput, secondExitCode) = runCommandWithExitCode(tlmgrExecutable, "update", packageName, returnExceptionMessage = true, timeout = timeout)
output = secondOutput
exitCode = secondExitCode
}

if (exitCode != 0) {
Notification(
"LaTeX",
if (packageName == "--all") "Could not update packages" else "Package $packageName not updated",
"Could not update $packageName${if (exitCode == 143) " due to a timeout" else ""}: $output",
NotificationType.ERROR
).notify(project)
indicator.cancel()
}
}

override fun onSuccess() {
// Clear cache, since we changed something
Cache.availablePackageUpdates = mapOf()
// Rerun inspections
DaemonCodeAnalyzer.getInstance(project)
.restart(
filePointer.containingFile
?: return
)
}
})
}
}
}
Loading

0 comments on commit 280947f

Please sign in to comment.