diff --git a/CHANGELOG.md b/CHANGELOG.md index 28bd56da3..74f38eaa6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/nl/hannahsten/texifyidea/inspections/latex/probablebugs/LatexFileNotFoundInspection.kt b/src/nl/hannahsten/texifyidea/inspections/latex/probablebugs/LatexFileNotFoundInspection.kt index e13a9fb52..7345b34ef 100644 --- a/src/nl/hannahsten/texifyidea/inspections/latex/probablebugs/LatexFileNotFoundInspection.kt +++ b/src/nl/hannahsten/texifyidea/inspections/latex/probablebugs/LatexFileNotFoundInspection.kt @@ -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( diff --git a/src/nl/hannahsten/texifyidea/lang/commands/LatexGenericRegularCommand.kt b/src/nl/hannahsten/texifyidea/lang/commands/LatexGenericRegularCommand.kt index 3cc09bcb2..83522ac09 100644 --- a/src/nl/hannahsten/texifyidea/lang/commands/LatexGenericRegularCommand.kt +++ b/src/nl/hannahsten/texifyidea/lang/commands/LatexGenericRegularCommand.kt @@ -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")), diff --git a/src/nl/hannahsten/texifyidea/lang/commands/RequiredFileArgument.kt b/src/nl/hannahsten/texifyidea/lang/commands/RequiredFileArgument.kt index 8cbf230e4..acb2b5b9d 100644 --- a/src/nl/hannahsten/texifyidea/lang/commands/RequiredFileArgument.kt +++ b/src/nl/hannahsten/texifyidea/lang/commands/RequiredFileArgument.kt @@ -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 - lateinit var defaultExtension: String - private set private var pattern: Pattern? = null init { @@ -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("\\.") diff --git a/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt b/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt index 3866b0888..55d1b802d 100644 --- a/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt +++ b/src/nl/hannahsten/texifyidea/reference/InputFileReference.kt @@ -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, - val defaultExtension: String, val supportsAnyExtension: Boolean, ) : PsiReferenceBase(element) { @@ -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() diff --git a/src/nl/hannahsten/texifyidea/util/parser/LatexCommandsImplMixinUtil.kt b/src/nl/hannahsten/texifyidea/util/parser/LatexCommandsImplMixinUtil.kt index 5efd15c18..180982770 100644 --- a/src/nl/hannahsten/texifyidea/util/parser/LatexCommandsImplMixinUtil.kt +++ b/src/nl/hannahsten/texifyidea/util/parser/LatexCommandsImplMixinUtil.kt @@ -60,7 +60,7 @@ fun LatexCommands.getFileArgumentsReferences(): List { } for (subParamRange in subParamRanges) { - inputFileReferences.add(InputFileReference(this, subParamRange, extensions, fileArgument.defaultExtension, supportsAnyExtension = true)) + inputFileReferences.add(InputFileReference(this, subParamRange, extensions, supportsAnyExtension = fileArgument.supportsAnyExtension)) } } @@ -69,7 +69,7 @@ fun LatexCommands.getFileArgumentsReferences(): List { 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)) } } diff --git a/test/nl/hannahsten/texifyidea/gutter/LatexGutterTest.kt b/test/nl/hannahsten/texifyidea/gutter/LatexGutterTest.kt index 9908b1b7c..075616798 100644 --- a/test/nl/hannahsten/texifyidea/gutter/LatexGutterTest.kt +++ b/test/nl/hannahsten/texifyidea/gutter/LatexGutterTest.kt @@ -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() { @@ -22,7 +22,6 @@ class LatexGutterTest : BasePlatformTestCase() { return "test/resources/gutter" } - @Test fun testShowCompileGutter() { val testName = getTestName(false) val gutters = myFixture.findAllGutters("$testName.tex") @@ -30,7 +29,13 @@ class LatexGutterTest : BasePlatformTestCase() { 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 { @@ -44,7 +49,6 @@ class LatexGutterTest : BasePlatformTestCase() { } } - @Test fun testShowNavigationGutter() { val testName = getTestName(false) myFixture.configureByFile("$testName.tex") diff --git a/test/resources/gutter/GraphicsExtensions.tex b/test/resources/gutter/GraphicsExtensions.tex new file mode 100644 index 000000000..0e019f1eb --- /dev/null +++ b/test/resources/gutter/GraphicsExtensions.tex @@ -0,0 +1,8 @@ +\documentclass{article} + +\usepackage{graphicx} +\DeclareGraphicsExtensions{.png, .pdf} + +\begin{document} + \includegraphics{figures/duck} +\end{document} diff --git a/test/resources/gutter/figures/duck.pdf b/test/resources/gutter/figures/duck.pdf new file mode 100644 index 000000000..77e9d9685 Binary files /dev/null and b/test/resources/gutter/figures/duck.pdf differ diff --git a/test/resources/gutter/figures/duck.png b/test/resources/gutter/figures/duck.png new file mode 100644 index 000000000..436f058d0 Binary files /dev/null and b/test/resources/gutter/figures/duck.png differ