Skip to content

Commit

Permalink
Add experimental support for the addtoluatexpath package
Browse files Browse the repository at this point in the history
  • Loading branch information
PHPirates committed Dec 28, 2024
1 parent c748e1f commit fe4b3aa
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* 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 checkboxes to graphic insertion wizard for relative width or height

### Fixed
Expand Down
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
1 change: 1 addition & 0 deletions src/nl/hannahsten/texifyidea/lang/LatexPackage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ open class LatexPackage @JvmOverloads constructor(
// Predefined packages.
val DEFAULT = LatexPackage("")

val ADDTOLUATEXPATH = LatexPackage("addtoluatexpath")
val ALGORITHM2E = LatexPackage("algorithm2e")
val ALGORITHMICX = LatexPackage("algorithmicx")
val ALGPSEUDOCODE = LatexPackage("algpseudocode")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum class LatexGenericRegularCommand(
) : LatexCommand {

ADDTOCOUNTER("addtocounter", "countername".asRequired(), "value".asRequired()),
ADDTOLUATEXPATH("addtoluatexpath", RequiredFolderArgument("paths")),
A_RING("aa", display = "å"),
CAPITAL_A_RING("AA", display = "Å"),
ADDBIBRESOURCE("addbibresource", RequiredFileArgument("bibliographyfile", true, false, "bib"), dependency = BIBLATEX),
Expand Down
8 changes: 8 additions & 0 deletions src/nl/hannahsten/texifyidea/reference/InputFileReference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ class InputFileReference(
targetFile = findAnywhereInProject(processedKey)
}

// addtoluatexpath package
if (targetFile == null) {
for (path in addToLuatexPathSearchDirectories(element.project)) {
targetFile = path.findFile(processedKey, extensions, supportsAnyExtension)
if (targetFile != null) break
}
}

if (targetFile == null) return null

// Return a reference to the target file.
Expand Down
36 changes: 36 additions & 0 deletions src/nl/hannahsten/texifyidea/util/files/FileSet.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package nl.hannahsten.texifyidea.util.files

import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.psi.search.GlobalSearchScope
import nl.hannahsten.texifyidea.index.BibtexEntryIndex
import nl.hannahsten.texifyidea.index.LatexCommandsIndex
import nl.hannahsten.texifyidea.index.LatexDefinitionIndex
import nl.hannahsten.texifyidea.index.LatexIncludesIndex
import nl.hannahsten.texifyidea.lang.LatexPackage
import nl.hannahsten.texifyidea.lang.commands.LatexGenericRegularCommand
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.magic.cmd
import nl.hannahsten.texifyidea.util.parser.isDefinition
import nl.hannahsten.texifyidea.util.parser.requiredParameter

/**
* Finds all the files in the project that are somehow related using includes.
Expand Down Expand Up @@ -95,3 +104,30 @@ fun PsiFile.definitionsInFileSet(): Collection<LatexCommands> {
fun PsiFile.definitionsAndRedefinitionsInFileSet(): Collection<LatexCommands> {
return LatexDefinitionIndex.Util.getItemsInFileSet(this)
}

/**
* The addtoluatexpath package supports adding to \input@path in different ways
*/
fun addToLuatexPathSearchDirectories(project: Project): List<VirtualFile> {
val direct = runReadAction { LatexCommandsIndex.Util.getCommandsByNames(setOf(LatexGenericRegularCommand.ADDTOLUATEXPATH.cmd), project, GlobalSearchScope.projectScope(project)) }
.mapNotNull { command -> runReadAction { command.requiredParameter(0) } }
.flatMap { it.split(",") }
val viaUsepackage = runReadAction { LatexIncludesIndex.Util.getCommandsByNames(CommandMagic.packageInclusionCommands, project, GlobalSearchScope.projectScope(project)) }
.filter { runReadAction { it.requiredParameter(0) } == LatexPackage.ADDTOLUATEXPATH.name }
.flatMap { runReadAction { it.getOptionalParameterMap().keys } }
.flatMap { it.text.split(",") }

val luatexPathDirectories = (direct + viaUsepackage).flatMap {
val basePath = LocalFileSystem.getInstance().findFileByPath(it.trimEnd('/', '*')) ?: return@flatMap emptyList()
if (it.endsWith("/**")) {
basePath.allChildDirectories()
}
else if (it.endsWith("/*")) {
basePath.children.filter { it.isDirectory }
}
else {
listOf(basePath)
}
}
return luatexPathDirectories
}

0 comments on commit fe4b3aa

Please sign in to comment.