Skip to content

Commit

Permalink
Merge pull request #3807 from Hannah-Sten/declare-graphics-extensions
Browse files Browse the repository at this point in the history
Add support for DeclareGraphicsExtensions
  • Loading branch information
PHPirates authored Dec 15, 2024
2 parents 99c20e4 + f8959d3 commit a3d271f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 17 deletions.
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
* Add support for DeclareGraphicsExtensions
* Add inspection to warn about a missing reference for a glossary occurrence
* Do not fold sections in a command definition
* Include optional parameters in spellcheck, if it contains text
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ open class LatexFileNotFoundInspection : TexifyInspectionBase() {

// Find expected extension
val extension = fileName.getFileExtension().ifEmpty {
reference.defaultExtension
}
reference.extensions.firstOrNull()
} ?: "tex"

descriptors.add(
manager.createProblemDescriptor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ enum class LatexGenericRegularCommand(
TEXT_DAGGER("dag", display = ""),
TEXT_DOUBLE_DAGGER("ddag", display = ""),
DATE("date", "text".asRequired(Argument.Type.TEXT)),
DECLAREGRAPHICSEXTENSIONS("DeclareGraphicsExtensions", "extensions".asRequired(), dependency = GRAPHICX),
DECLARE_MATH_OPERATOR("DeclareMathOperator", "command".asRequired(), "operator".asRequired(Argument.Type.TEXT), dependency = AMSMATH),
DEF("def"),
DOCUMENTCLASS("documentclass", "options".asOptional(), RequiredFileArgument("class", true, false, "cls")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import java.util.regex.Pattern
open class RequiredFileArgument(name: String?, open val isAbsolutePathSupported: Boolean = true, open val commaSeparatesArguments: Boolean, vararg extensions: String, open val supportsAnyExtension: Boolean = true) : RequiredArgument(name!!, Type.FILE), FileNameMatcher, FileExtensionMatcher {

lateinit var supportedExtensions: List<String>
lateinit var defaultExtension: String
private set
private var pattern: Pattern? = null

init {
Expand All @@ -44,12 +42,9 @@ open class RequiredFileArgument(name: String?, open val isAbsolutePathSupported:
if (extensions.isEmpty()) {
setRegex(regex.toString())
this.supportedExtensions = supportedExtensions
this.defaultExtension = ""
return
}
else {
defaultExtension = extensions[0]
}

regex.append("(")
for (extension in extensions) {
regex.append("\\.")
Expand Down
28 changes: 25 additions & 3 deletions src/nl/hannahsten/texifyidea/reference/InputFileReference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiReferenceBase
import com.intellij.psi.search.GlobalSearchScope
import nl.hannahsten.texifyidea.algorithm.BFS
import nl.hannahsten.texifyidea.completion.pathcompletion.LatexGraphicsPathProvider
import nl.hannahsten.texifyidea.index.LatexCommandsIndex
import nl.hannahsten.texifyidea.lang.LatexPackage
import nl.hannahsten.texifyidea.lang.commands.LatexCommand
import nl.hannahsten.texifyidea.lang.commands.LatexGenericRegularCommand
import nl.hannahsten.texifyidea.psi.LatexCommands
import nl.hannahsten.texifyidea.psi.LatexPsiHelper
import nl.hannahsten.texifyidea.util.*
import nl.hannahsten.texifyidea.util.files.*
import nl.hannahsten.texifyidea.util.magic.CommandMagic
import nl.hannahsten.texifyidea.util.parser.requiredParameter

/**
* Reference to a file, based on the command and the range of the filename within the command text.
*
* @param defaultExtension Default extension of the command in which this reference is, in case the argument does not have an extension.
*/
class InputFileReference(
element: LatexCommands,
val range: TextRange,
val extensions: List<String>,
val defaultExtension: String,
val supportsAnyExtension: Boolean,
) : PsiReferenceBase<LatexCommands>(element) {

Expand Down Expand Up @@ -122,6 +124,26 @@ class InputFileReference(
}
}

// Overrides the default for commands from the graphicx package
val extensions = if (!isBuildingFileset) {
val command = LatexCommand.lookup(element.name)?.firstOrNull()
if (command?.dependency == LatexPackage.GRAPHICX) {
// We cannot use the file set at this point, so we take the first command in the project and hope for the best
LatexCommandsIndex.Util.getCommandsByName(LatexGenericRegularCommand.DECLAREGRAPHICSEXTENSIONS.command, element.project, GlobalSearchScope.projectScope(element.project))
.firstOrNull()
?.requiredParameter(0)
?.split(",")
// Graphicx requires the dot to be included
?.map { it.trim(' ', '.') } ?: extensions
}
else {
extensions
}
}
else {
extensions
}

var processedKey = expandCommandsOnce(key, element.project, file = rootFiles.firstOrNull()?.psiFile(element.project)) ?: key
// Leading and trailing whitespaces seem to be ignored, at least it holds for \include-like commands
processedKey = processedKey.trim()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fun LatexCommands.getFileArgumentsReferences(): List<InputFileReference> {
}

for (subParamRange in subParamRanges) {
inputFileReferences.add(InputFileReference(this, subParamRange, extensions, fileArgument.defaultExtension, supportsAnyExtension = true))
inputFileReferences.add(InputFileReference(this, subParamRange, extensions, supportsAnyExtension = fileArgument.supportsAnyExtension))
}
}

Expand All @@ -69,7 +69,7 @@ fun LatexCommands.getFileArgumentsReferences(): List<InputFileReference> {
if (name == LatexGenericRegularCommand.DOCUMENTCLASS.cmd && SUBFILES.name in getRequiredParameters() && getOptionalParameterMap().isNotEmpty()) {
val range = this.firstChildOfType(LatexParameter::class)?.textRangeInParent
if (range != null) {
inputFileReferences.add(InputFileReference(this, range.shrink(1), listOf("tex"), "tex", supportsAnyExtension = true))
inputFileReferences.add(InputFileReference(this, range.shrink(1), listOf("tex"), supportsAnyExtension = true))
}
}

Expand Down
12 changes: 8 additions & 4 deletions test/nl/hannahsten/texifyidea/gutter/LatexGutterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import io.mockk.every
import io.mockk.mockkStatic
import nl.hannahsten.texifyidea.TexifyIcons
import nl.hannahsten.texifyidea.util.runCommandWithExitCode
import org.junit.Test

class LatexGutterTest : BasePlatformTestCase() {

Expand All @@ -22,15 +22,20 @@ class LatexGutterTest : BasePlatformTestCase() {
return "test/resources/gutter"
}

@Test
fun testShowCompileGutter() {
val testName = getTestName(false)
val gutters = myFixture.findAllGutters("$testName.tex")
assertEquals(1, gutters.size)
assertEquals("Compile document", gutters.first().tooltipText)
}

@Test
fun testGraphicsExtensions() {
val testName = getTestName(false)
myFixture.copyDirectoryToProject("figures", "figures")
val gutters = myFixture.findAllGutters("$testName.tex")
assertEquals(TexifyIcons.FILE, gutters.last().icon)
}

fun testShowMethodSeparators() {
val testName = getTestName(false)
withLineMarkersEnabled {
Expand All @@ -44,7 +49,6 @@ class LatexGutterTest : BasePlatformTestCase() {
}
}

@Test
fun testShowNavigationGutter() {
val testName = getTestName(false)
myFixture.configureByFile("$testName.tex")
Expand Down
8 changes: 8 additions & 0 deletions test/resources/gutter/GraphicsExtensions.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
\documentclass{article}

\usepackage{graphicx}
\DeclareGraphicsExtensions{.png, .pdf}

\begin{document}
\includegraphics{figures/duck}
\end{document}
Binary file added test/resources/gutter/figures/duck.pdf
Binary file not shown.
Binary file added test/resources/gutter/figures/duck.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a3d271f

Please sign in to comment.