Skip to content

Commit

Permalink
fix: Auto create .idea directory (#23)
Browse files Browse the repository at this point in the history
* Ask for creating .idea directory

* Remove notification
  • Loading branch information
MarcinVaadin authored Mar 26, 2024
1 parent 21cc15f commit 8da777b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 14 deletions.
36 changes: 23 additions & 13 deletions src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.notification.NotificationType
import com.intellij.notification.Notifications
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.command.WriteCommandAction
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.extensions.PluginId
Expand All @@ -16,6 +17,7 @@ import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.openapi.roots.libraries.Library
import com.intellij.openapi.util.IconLoader
import com.intellij.openapi.vfs.VfsUtil
import com.intellij.psi.PsiDirectory
import com.intellij.psi.PsiFileFactory
Expand Down Expand Up @@ -45,6 +47,10 @@ class CopilotPluginUtil {

private const val NORMALIZED_LINE_SEPARATOR = "\n"

private const val NOTIFICATION_GROUP = "Vaadin Copilot"

private val COPILOT_ICON = IconLoader.getIcon("/icons/copilot.svg", CopilotPluginUtil::class.java)

private var isVaadinProject = false

private enum class HANDLERS(val command: String) {
Expand Down Expand Up @@ -77,7 +83,8 @@ class CopilotPluginUtil {
}

fun notify(content: String, type: NotificationType, project: Project?) {
Notifications.Bus.notify(Notification("Copilot", content, type), project)
Notifications.Bus.notify(Notification(NOTIFICATION_GROUP, content, type)
.setIcon(COPILOT_ICON), project)
}

fun isServerRunning(project: Project): Boolean {
Expand Down Expand Up @@ -179,24 +186,27 @@ class CopilotPluginUtil {
}
}

private fun getDotFileDirectory(project: Project): PsiDirectory? {
private fun getIdeaDir(project: Project): File {
return File(project.basePath, IDEA_DIR)
}

fun getDotFileDirectory(project: Project): PsiDirectory? {
return ApplicationManager.getApplication().runReadAction<PsiDirectory?> {
val basePath = project.basePath
if (basePath != null) {
val ideaDir = File(basePath, IDEA_DIR)
VfsUtil.findFileByIoFile(ideaDir, false)?.let {
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
}
VfsUtil.createDirectoryIfMissing(ideaDir.path)?.let {
LOG.info("$ideaDir created")
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
}
VfsUtil.findFileByIoFile(getIdeaDir(project), false)?.let {
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
}
return@runReadAction null
}
}

fun createIdeaDirectoryIfMissing(project: Project) {
WriteCommandAction.runWriteCommandAction(project) {
val ideaDir = getIdeaDir(project).path
VfsUtil.createDirectoryIfMissing(ideaDir)?.let {
LOG.info("$ideaDir created")
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class CopilotPostStartupProjectActivity: ProjectActivity {
override suspend fun execute(project: Project) {
val isVaadinProject = CopilotPluginUtil.isVaadinProject(project)
if (isVaadinProject) {
val dotFileDirectory = CopilotPluginUtil.getDotFileDirectory(project)
if (dotFileDirectory == null) {
CopilotPluginUtil.createIdeaDirectoryIfMissing(project)
}
CopilotPluginUtil.startServer(project)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<!-- Extension points defined by the plugin.
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
<extensions defaultExtensionNs="com.intellij">
<notificationGroup id="Copilot" displayType="BALLOON"/>
<notificationGroup id="Vaadin Copilot" displayType="BALLOON"/>
<projectService serviceImplementation="com.vaadin.plugin.copilot.service.CopilotServerServiceImpl"
serviceInterface="com.vaadin.plugin.copilot.service.CopilotServerService"/>
<postStartupActivity implementation="com.vaadin.plugin.copilot.activity.CopilotPostStartupProjectActivity"/>
Expand Down

0 comments on commit 8da777b

Please sign in to comment.