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

fix: check every module when looking if a file is inside the project #164

Merged
merged 7 commits into from
Nov 20, 2024
22 changes: 22 additions & 0 deletions src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import com.intellij.openapi.application.WriteAction
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.extensions.PluginId
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.vfs.*
import com.vaadin.plugin.copilot.handler.*
import com.vaadin.plugin.utils.VaadinIcons
Expand Down Expand Up @@ -153,5 +155,25 @@ class CopilotPluginUtil {
fun getDotFile(project: Project): VirtualFile? {
return getDotFileDirectory(project)?.findFile(DOTFILE)
}

/**
* Returns a map of all base directories related to the project. This includes any external module and the main
* project base folders. For the main project, the base directory is the project root and the module name is
* "base-module".
*/
fun getBaseDirectoriesForProject(project: Project): Map<String, List<String>> {
MarcinVaadin marked this conversation as resolved.
Show resolved Hide resolved
val moduleManager = ModuleManager.getInstance(project)
val moduleBaseDirectories = mutableMapOf<String, List<String>>()

moduleManager.modules.forEach { module ->
val contentRootPaths =
ModuleRootManager.getInstance(module).contentRoots.map {
it.path
} // Convert each VirtualFile to its path as a String
moduleBaseDirectories[module.name] = contentRootPaths
}
moduleBaseDirectories["base-module"] = listOf(project.basePath) as List<String>
MarcinVaadin marked this conversation as resolved.
Show resolved Hide resolved
return moduleBaseDirectories
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.findDocument
import com.intellij.psi.PsiDocumentManager
import com.vaadin.plugin.copilot.CopilotPluginUtil
import com.vaadin.plugin.copilot.CopilotPluginUtil.Companion.getBaseDirectoriesForProject
import com.vaadin.plugin.copilot.service.CopilotUndoManager
import io.netty.handler.codec.http.HttpResponseStatus
import java.io.File
Expand Down Expand Up @@ -45,7 +46,9 @@ abstract class AbstractHandler(val project: Project) : Handler {
fun isFileInsideProject(project: Project, file: File): Boolean {
if (file.exists()) {
val path = file.toPath()
return path.toRealPath().startsWith(project.basePath!!)
MarcinVaadin marked this conversation as resolved.
Show resolved Hide resolved
return getBaseDirectoriesForProject(project).values.flatten().any { baseDirectory ->
path.startsWith(baseDirectory)
}
} else {
// New file
return isFileInsideProject(project, file.parentFile)
Expand Down