From 0d4fec336adccc13b796758844481236a03d2735 Mon Sep 17 00:00:00 2001 From: CppCXY <812125110@qq.com> Date: Wed, 13 Nov 2024 14:16:06 +0800 Subject: [PATCH] try fix for debug --- .../com/tang/intellij/lua/psi/LuaFileUtil.kt | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/tang/intellij/lua/psi/LuaFileUtil.kt b/src/main/java/com/tang/intellij/lua/psi/LuaFileUtil.kt index f30b9b6..e457c11 100644 --- a/src/main/java/com/tang/intellij/lua/psi/LuaFileUtil.kt +++ b/src/main/java/com/tang/intellij/lua/psi/LuaFileUtil.kt @@ -22,6 +22,8 @@ import com.intellij.openapi.project.Project import com.intellij.openapi.vfs.VfsUtil import com.intellij.openapi.vfs.VirtualFile import com.intellij.openapi.vfs.VirtualFileManager +import com.intellij.psi.search.FilenameIndex +import com.intellij.psi.search.ProjectAndLibrariesScope import java.io.File /** @@ -59,7 +61,7 @@ object LuaFileUtil { // Check if the fixedShortUrl already has an extension val hasExtension = fixedShortUrl.contains(".") if (hasExtension) { - val virtualFile = VfsUtil.findRelativeFile(fixedShortUrl, project.baseDir) + val virtualFile = findVirtualFile(project, fixedShortUrl); if (virtualFile != null && virtualFile.exists()) { return virtualFile } @@ -69,7 +71,7 @@ object LuaFileUtil { val extensions = LuaFileManager.extensions for (extension in extensions) { val fileName = if (extension.isEmpty()) fixedShortUrl else "$fixedShortUrl$extension" - val virtualFile = VfsUtil.findRelativeFile(fileName, project.baseDir) + val virtualFile = findVirtualFile(project, fileName) if (virtualFile != null && virtualFile.exists()) { return virtualFile } @@ -78,6 +80,25 @@ object LuaFileUtil { return null } + fun findVirtualFile(project: Project, filename: String): VirtualFile? { + val files = FilenameIndex.getVirtualFilesByName(filename, ProjectAndLibrariesScope(project)) + var perfect: VirtualFile? = null + var perfectMatch = Int.MAX_VALUE + for (file in files) { + val path = file.canonicalPath + if (path != null && perfectMatch > path.length && path.endsWith(filename)) { + perfect = file + perfectMatch = path.length + } + } + + if (perfect != null) { + return perfect + } + + return VfsUtil.findRelativeFile(filename, project.baseDir); + } + fun getPluginVirtualFile(path: String): String? { val directory = pluginVirtualDirectory if (directory != null) {