Skip to content

Commit

Permalink
fix(compiler): improve handling of file paths and project roots #101
Browse files Browse the repository at this point in the history
The commit addresses an issue where the compiler was not correctly handling file paths and project roots, leading to incorrect behavior when processing source files. The fix includes the use of `PsiManager.getInstance(project)` instead of `com.intellij.psi.PsiManager.getInstance(project)` for consistency, and the use of `VirtualFileManager.getInstance().findFileByNioPath(realpath)` to resolve file paths correctly. Additionally, the commit introduces the use of `FilenameIndex` to find files by their names and `PsiManager` to find the language of the file, which were previously not being used. These changes ensure that the compiler handles file paths and project roots more accurately and consistently.
  • Loading branch information
phodal committed Mar 15, 2024
1 parent 9909f15 commit a3d25f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Expand Up @@ -8,9 +8,14 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.roots.ProjectFileIndex
import com.intellij.openapi.util.NlsSafe
import com.intellij.openapi.util.TextRange
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FilenameIndex
import com.intellij.psi.util.elementType
import kotlin.io.path.readText

Expand Down Expand Up @@ -99,9 +104,18 @@ class DevInCompiler(val myProject: Project, val file: DevInFile, val editor: Edi

val filepath = prop.trim()
val projectPath = myProject.guessProjectDir()?.toNioPath()
val content = projectPath?.resolve(filepath)?.readText()
val realpath = projectPath?.resolve(filepath)
val content = realpath?.readText()

content?.let {
val virtualFile: VirtualFile? = VirtualFileManager.getInstance().findFileByNioPath(realpath)
val lang = virtualFile?.let {
PsiManager.getInstance(myProject).findFile(it)?.language?.displayName
} ?: ""

output.append("\n```$lang\n")
output.append(content)
output.append("\n```\n")
}

}
Expand All @@ -110,10 +124,12 @@ class DevInCompiler(val myProject: Project, val file: DevInFile, val editor: Edi
logger.info("handling rev")
output.append(command.agentName)
}

BuiltinCommand.SYMBOL -> {
logger.info("handling symbol")
output.append(command.agentName)
}

BuiltinCommand.WRITE -> {
logger.info("handling write")
output.append(command.agentName)
Expand Down
Expand Up @@ -12,6 +12,7 @@ import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.psi.PsiManager
import com.intellij.psi.search.FileTypeIndex
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.search.ProjectScope
Expand Down Expand Up @@ -40,7 +41,7 @@ class ReactAutoPage(

init {
val searchScope: GlobalSearchScope = ProjectScope.getContentScope(project)
val psiManager = com.intellij.psi.PsiManager.getInstance(project)
val psiManager = PsiManager.getInstance(project)

val virtualFiles =
FileTypeIndex.getFiles(JavaScriptFileType.INSTANCE, searchScope) +
Expand Down

0 comments on commit a3d25f6

Please sign in to comment.