Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle file extensions case sensitive #3542

Merged
merged 9 commits into from
May 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ abstract class LatexPathProviderBase : CompletionProvider<CompletionParameters>(
*/
private fun addAbsolutePathCompletion(baseDir: String) {
if (baseDir.isBlank()) return
LocalFileSystem.getInstance().findFileByPath(baseDir)?.let { dirFile ->
LocalFileSystem.getInstance().refreshAndFindFileByPath(baseDir)?.let { dirFile ->
if (searchFolders()) {
addFolderNavigations(baseDir)
getContents(dirFile, true).forEach {
Expand Down Expand Up @@ -273,18 +273,6 @@ abstract class LatexPathProviderBase : CompletionProvider<CompletionParameters>(
* search in given path for subfiles or directories
*/
private fun getContents(base: VirtualFile?, directory: Boolean): List<VirtualFile> {
val contents = java.util.ArrayList<VirtualFile>()

if (base == null) {
return contents
}

for (file in base.children) {
if (file.isDirectory == directory) {
contents.add(file)
}
}

return contents
return base?.children?.filter { it.isDirectory == directory } ?: mutableListOf()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ open class RequiredFileArgument(name: String?, open val isAbsolutePathSupported:
regex.append("(")
for (extension in extensions) {
regex.append("\\.")
val extensionLower = extension.lowercase(Locale.getDefault())
regex.append(extensionLower)
supportedExtensions.add(extensionLower)
regex.append(extension)
supportedExtensions.add(extension)
if (extension != extensions[extensions.size - 1]) {
regex.append("|")
}
Expand Down
5 changes: 2 additions & 3 deletions src/nl/hannahsten/texifyidea/util/Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,10 @@ fun String.getIndent(): String {
fun String.appendExtension(extensionWithoutDot: String): String {
if (extensionWithoutDot == "") return this

val dottedExtension = ".${extensionWithoutDot.lowercase(Locale.getDefault())}"
val thisLower = lowercase(Locale.getDefault())
val dottedExtension = ".$extensionWithoutDot"

return when {
thisLower.endsWith(dottedExtension) -> this
this.endsWith(dottedExtension) -> this
endsWith('.') -> this + extensionWithoutDot
else -> this + dottedExtension
}
Expand Down
2 changes: 1 addition & 1 deletion src/nl/hannahsten/texifyidea/util/files/VirtualFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ fun VirtualFile.findFile(filePath: String, extensions: List<String> = emptyList(
else {
LocalFileSystem.getInstance().findFileByPath(filePath)
}
if (file != null && !file.isDirectory) return file
if (file != null && !file.isDirectory && (extensions.isEmpty() || file.extension in extensions)) return file

extensions.forEach { extension ->
val lookFor = filePath.appendExtension(extension)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.intellij.codeInsight.completion.CompletionType
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.testFramework.fixtures.BasePlatformTestCase
import nl.hannahsten.texifyidea.file.LatexFileType
import org.junit.Test
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
Expand All @@ -21,7 +20,6 @@ class LatexAbsolutePathCompletionTest : BasePlatformTestCase() {
absoluteWorkingPath = currentRelativePath.toAbsolutePath().toString().replace(File.separatorChar, '/')
}

@Test
fun testAbsoluteFolderCompletion() {
myFixture.configureByText(LatexFileType, """"\graphicspath{{$absoluteWorkingPath/test/resources/completion/path/<caret>}}""")

Expand All @@ -31,7 +29,6 @@ class LatexAbsolutePathCompletionTest : BasePlatformTestCase() {
assert(result.removeFolderEntries().isEmpty())
}

@Test
fun testAbsoluteFileCompletion() {
myFixture.configureByText(LatexFileType, """\input{$absoluteWorkingPath/test/resources/completion/path/<caret>}""")

Expand All @@ -40,17 +37,15 @@ class LatexAbsolutePathCompletionTest : BasePlatformTestCase() {
assert(result.isNotEmpty())
}

@Test
fun testSupportedPictureExtensions() {
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/resources/completion/path/<caret>}""")

val result = myFixture.complete(CompletionType.BASIC)

// is only allowed to show folders and the png file
assert(result.removeFolderEntries().size == 1)
assertEquals(1, result.removeFolderEntries().size)
}

@Test
fun testSupportedInputExtensions() {
myFixture.configureByText(LatexFileType, """\input{$absoluteWorkingPath/test/resources/completion/path/<caret>}""")

Expand All @@ -60,7 +55,6 @@ class LatexAbsolutePathCompletionTest : BasePlatformTestCase() {
assert(result.removeFolderEntries().size == 1)
}

@Test
fun testNonAbsolutePathSupport() {
myFixture.configureByText(LatexFileType, """\include{$absoluteWorkingPath/test/resources/completion/cite/<caret>}""")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import io.mockk.mockkStatic
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.inspections.TexifyInspectionTestBase
import nl.hannahsten.texifyidea.util.runCommandWithExitCode
import org.junit.Test
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.test.assertFails

class LatexFileNotFoundInspectionTest : TexifyInspectionTestBase(LatexFileNotFoundInspection()) {

Expand All @@ -30,40 +28,32 @@ class LatexFileNotFoundInspectionTest : TexifyInspectionTestBase(LatexFileNotFou
return "test/resources/inspections/latex/filenotfound"
}

@Test
fun testInvalidAbsolutePath() {
fun testMissingAbsolutePath() {
myFixture.configureByText(LatexFileType, """\includegraphics{<error>$absoluteWorkingPath/test/resources/completion/path/myPicture.myinvalidextension</error>}""")
myFixture.checkHighlighting()
}

@Test
fun testValidAbsolutePath() {
myFixture.configureByText(LatexFileType, """\includegraphics{<error>$absoluteWorkingPath/test/resources/completion/path/myPicture.png</error>}""")
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/resources/completion/path/myPicture.png}""")

assertFails {
myFixture.checkHighlighting()
}
myFixture.checkHighlighting()
}

@Test
fun testBackActionAbsolute() {
myFixture.configureByText(LatexFileType, """\includegraphics{<error>$absoluteWorkingPath/test/resources/completion/path/../path/../path/myPicture.png</error>}""")
fun testValidAbsolutePathCaps() {
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/resources/inspections/latex/filenotfound/myOtherPicture.PNG}""")
myFixture.checkHighlighting()
}

assertFails {
myFixture.checkHighlighting()
}
fun testBackActionAbsolute() {
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/resources/completion/path/../path/../path/myPicture.png}""")
myFixture.checkHighlighting()
}

@Test
fun testCurrDirActionAbsolute() {
myFixture.configureByText(LatexFileType, """\includegraphics{<error>$absoluteWorkingPath/test/./resources/./././completion/path/././myPicture.png</error>}""")

assertFails {
myFixture.checkHighlighting()
}
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/./resources/./././completion/path/././myPicture.png}""")
myFixture.checkHighlighting()
}

@Test
fun testAbsoluteGraphicsDirWithInclude() {
myFixture.copyFileToProject("myPicture.png")
myFixture.configureByText(
Expand All @@ -77,16 +67,35 @@ class LatexFileNotFoundInspectionTest : TexifyInspectionTestBase(LatexFileNotFou
myFixture.checkHighlighting()
}

@Test
fun testUpperCaseAbsoluteGraphicsDirWithInclude() {
myFixture.copyFileToProject("myOtherPicture.PNG")
myFixture.configureByText(
LatexFileType,
"""
\graphicspath{{$absoluteWorkingPath/test/resources/completion/path/}}
\includegraphics{myOtherPicture}
""".trimIndent()
)

myFixture.checkHighlighting()
}

fun testDefaultExtensionCompletion() {
myFixture.configureByText(LatexFileType, """\includegraphics{<error>$absoluteWorkingPath/test/resources/completion/path/myPicture</error>}""")
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/resources/completion/path/myPicture}""")
myFixture.checkHighlighting()
}

fun testDefaultUpperCaseExtensionCompletion() {
myFixture.configureByText(LatexFileType, """\includegraphics{$absoluteWorkingPath/test/resources/inspections/latex/filenotfound/myOtherPicture}""")
myFixture.checkHighlighting()
}

fun testDefaultMixedCaseExtensionCompletion() {
myFixture.configureByText(LatexFileType, """\includegraphics{<error>$absoluteWorkingPath/test/resources/completion/path/myBadPicture</error>}""")

assertFails {
myFixture.checkHighlighting()
}
myFixture.checkHighlighting()
}

@Test
fun testNoWarningInDefinition() {
myFixture.configureByText(LatexFileType, """\newcommand*{\gridelement}[1]{\subbottom[#1]{\includegraphics[width=2cm]{media/#1}}}""")

Expand All @@ -101,40 +110,34 @@ class LatexFileNotFoundInspectionTest : TexifyInspectionTestBase(LatexFileNotFou
// myFixture.checkHighlighting()
// }

@Test
fun testInvalidImportAbsolutePath() {
myFixture.copyFileToProject("chapters/included.tex")
myFixture.configureByText(LatexFileType, """\import{/does/not/exist}{<error>included</error>}""")
myFixture.checkHighlighting()
}

@Test
fun testImportAbsolutePathIncludedFile() {
val files = myFixture.configureByFiles("ImportPackageAbsolutePath.tex", "chapters/included.tex", "chapters/included2.tex")
myFixture.type("$absoluteWorkingPath/chapters/")
myFixture.openFileInEditor(files[1].virtualFile)
myFixture.checkHighlighting()
}

@Test
fun testImportRelativePathIncludedFile() {
myFixture.configureByFiles("chapters/included.tex", "ImportPackageRelativePath.tex", "chapters/included2.tex")
myFixture.checkHighlighting()
}

@Test
fun testInvalidImportRelativePathIncludedFile() {
myFixture.configureByFiles("chapters/notincluded.tex", "ImportPackageRelativePathInvalid.tex", "chapters/included2.tex")
myFixture.checkHighlighting()
}

@Test
fun `test command expansion in root file`() {
myFixture.configureByFiles("commandexpansion/main.tex", "commandexpansion/main.bib", "commandexpansion/nest/sub.tex")
myFixture.checkHighlighting()
}

@Test
fun `test command expansion in subfile`() {
myFixture.configureByFiles("commandexpansion/nest/sub.tex", "commandexpansion/main.tex", "commandexpansion/nest/sub2.tex")
myFixture.checkHighlighting()
Expand Down
9 changes: 0 additions & 9 deletions test/nl/hannahsten/texifyidea/util/TexifyUtilTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,4 @@ class TexifyUtilTest {
val expectedResult = "SomePath.tex.tex"
Assert.assertEquals("SomePath.tex.tex + tex", expectedResult, actualResult)
}

@Test
fun appendExtensionCrazyCapitals() {
val path = "SoMEPaTH.TEx"
val extension = "tEX"
val actualResult = path.appendExtension(extension)
val expectedResult = "SomePath.tex"
Assert.assertTrue("SoMEPaTH.TEx + tEX", actualResult.equals(expectedResult, ignoreCase = true))
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading