Skip to content

Commit

Permalink
Merge pull request #3749 from Hannah-Sten/index-foreign-bib
Browse files Browse the repository at this point in the history
Also index bibliography files that are included but are outside the project
  • Loading branch information
PHPirates authored Dec 2, 2024
2 parents 33fa9ab + 1ab42c5 commit 82e8ff0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased]

### Added
* Automatically index bibliography files outside the project that are included by an absolute path
* Disable quotes inspection when TeX ligatures are disabled by fontspec

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ package nl.hannahsten.texifyidea.index.file
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.progress.Task.Backgroundable
import com.intellij.openapi.project.DumbService
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.util.indexing.IndexableSetContributor
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.getTexinputsPaths
import nl.hannahsten.texifyidea.util.isTestProject
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.parser.requiredParameter
import org.codehaus.plexus.archiver.ArchiverException
import org.codehaus.plexus.archiver.tar.TarBZip2UnArchiver
import org.codehaus.plexus.archiver.tar.TarXZUnArchiver
Expand All @@ -24,6 +29,10 @@ import java.nio.file.Path
*/
class LatexIndexableSetContributor : IndexableSetContributor() {

object Cache {
var externalDirectFileInclusions: Set<VirtualFile>? = null
}

override fun getAdditionalProjectRootsToIndex(project: Project): MutableSet<VirtualFile> {
// Avoid indexing in tests
if (project.isTestProject()) {
Expand Down Expand Up @@ -62,8 +71,28 @@ class LatexIndexableSetContributor : IndexableSetContributor() {
roots.addAll(LatexSdkUtil.getSdkSourceRoots(project) { sdkType, homePath -> sdkType.getDefaultStyleFilesPath(homePath) })

roots.addAll(getTexinputsPaths(project, rootFiles = listOf(), expandPaths = false).mapNotNull { LocalFileSystem.getInstance().findFileByPath(it) })
Log.debug("Indexing source roots $roots")

// 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) {
if (!DumbService.isDumb(project)) {
// For now, just do this for bibliography and direct input commands, as there this is most common
val externalFiles = LatexIncludesIndex.Util.getCommandsByNames(CommandMagic.includeOnlyExtensions.entries.filter { it.value.contains("bib") || it.value.contains("tex") }.map { it.key }.toSet(), project, GlobalSearchScope.projectScope(project))
// We can't add single files, so take the parent
.mapNotNull {
val path = it.requiredParameter(0) ?: return@mapNotNull null
if (File(path).isAbsolute) {
LocalFileSystem.getInstance().findFileByPath(path)?.parent
}
else {
it.containingFile.parent?.virtualFile?.findFileByRelativePath(path)?.parent
}
}
Cache.externalDirectFileInclusions = externalFiles.toSet()
}
}
roots.addAll(Cache.externalDirectFileInclusions?.filter { it.exists() } ?: emptyList())

Log.debug("Indexing source roots $roots")
return roots
}

Expand Down
2 changes: 2 additions & 0 deletions test/nl/hannahsten/texifyidea/psi/LatexParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class LatexParserTest : BasePlatformTestCase() {
\def\@xyz[#1]#2{do something with #1 and #2}
\@namedef{#1}{\@ifnextchar{^}{\@nameuse{#1@}}{\@nameuse{#1@}^{}}}
\newcommand{\abc}{\@ifnextchar${'$'}{Math coming: }{No math}}
""".trimIndent()
)
myFixture.checkHighlighting()
Expand Down

0 comments on commit 82e8ff0

Please sign in to comment.