Skip to content

Commit 8da777b

Browse files
authored
fix: Auto create .idea directory (#23)
* Ask for creating .idea directory * Remove notification
1 parent 21cc15f commit 8da777b

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/main/kotlin/com/vaadin/plugin/copilot/CopilotPluginUtil.kt

+23-13
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.intellij.notification.NotificationType
88
import com.intellij.notification.Notifications
99
import com.intellij.openapi.application.ApplicationManager
1010
import com.intellij.openapi.application.runInEdt
11+
import com.intellij.openapi.command.WriteCommandAction
1112
import com.intellij.openapi.components.service
1213
import com.intellij.openapi.diagnostic.Logger
1314
import com.intellij.openapi.extensions.PluginId
@@ -16,6 +17,7 @@ import com.intellij.openapi.module.ModuleManager
1617
import com.intellij.openapi.project.Project
1718
import com.intellij.openapi.roots.ModuleRootManager
1819
import com.intellij.openapi.roots.libraries.Library
20+
import com.intellij.openapi.util.IconLoader
1921
import com.intellij.openapi.vfs.VfsUtil
2022
import com.intellij.psi.PsiDirectory
2123
import com.intellij.psi.PsiFileFactory
@@ -45,6 +47,10 @@ class CopilotPluginUtil {
4547

4648
private const val NORMALIZED_LINE_SEPARATOR = "\n"
4749

50+
private const val NOTIFICATION_GROUP = "Vaadin Copilot"
51+
52+
private val COPILOT_ICON = IconLoader.getIcon("/icons/copilot.svg", CopilotPluginUtil::class.java)
53+
4854
private var isVaadinProject = false
4955

5056
private enum class HANDLERS(val command: String) {
@@ -77,7 +83,8 @@ class CopilotPluginUtil {
7783
}
7884

7985
fun notify(content: String, type: NotificationType, project: Project?) {
80-
Notifications.Bus.notify(Notification("Copilot", content, type), project)
86+
Notifications.Bus.notify(Notification(NOTIFICATION_GROUP, content, type)
87+
.setIcon(COPILOT_ICON), project)
8188
}
8289

8390
fun isServerRunning(project: Project): Boolean {
@@ -179,24 +186,27 @@ class CopilotPluginUtil {
179186
}
180187
}
181188

182-
private fun getDotFileDirectory(project: Project): PsiDirectory? {
189+
private fun getIdeaDir(project: Project): File {
190+
return File(project.basePath, IDEA_DIR)
191+
}
192+
193+
fun getDotFileDirectory(project: Project): PsiDirectory? {
183194
return ApplicationManager.getApplication().runReadAction<PsiDirectory?> {
184-
val basePath = project.basePath
185-
if (basePath != null) {
186-
val ideaDir = File(basePath, IDEA_DIR)
187-
VfsUtil.findFileByIoFile(ideaDir, false)?.let {
188-
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
189-
}
190-
VfsUtil.createDirectoryIfMissing(ideaDir.path)?.let {
191-
LOG.info("$ideaDir created")
192-
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
193-
}
195+
VfsUtil.findFileByIoFile(getIdeaDir(project), false)?.let {
196+
return@runReadAction PsiManager.getInstance(project).findDirectory(it)
194197
}
195198
return@runReadAction null
196199
}
197200
}
198201

202+
fun createIdeaDirectoryIfMissing(project: Project) {
203+
WriteCommandAction.runWriteCommandAction(project) {
204+
val ideaDir = getIdeaDir(project).path
205+
VfsUtil.createDirectoryIfMissing(ideaDir)?.let {
206+
LOG.info("$ideaDir created")
207+
}
208+
}
209+
}
199210
}
200211

201-
202212
}

src/main/kotlin/com/vaadin/plugin/copilot/activity/CopilotPostStartupProjectActivity.kt

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ class CopilotPostStartupProjectActivity: ProjectActivity {
99
override suspend fun execute(project: Project) {
1010
val isVaadinProject = CopilotPluginUtil.isVaadinProject(project)
1111
if (isVaadinProject) {
12+
val dotFileDirectory = CopilotPluginUtil.getDotFileDirectory(project)
13+
if (dotFileDirectory == null) {
14+
CopilotPluginUtil.createIdeaDirectoryIfMissing(project)
15+
}
1216
CopilotPluginUtil.startServer(project)
1317
}
1418
}

src/main/resources/META-INF/plugin.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<!-- Extension points defined by the plugin.
2929
Read more: https://plugins.jetbrains.com/docs/intellij/plugin-extension-points.html -->
3030
<extensions defaultExtensionNs="com.intellij">
31-
<notificationGroup id="Copilot" displayType="BALLOON"/>
31+
<notificationGroup id="Vaadin Copilot" displayType="BALLOON"/>
3232
<projectService serviceImplementation="com.vaadin.plugin.copilot.service.CopilotServerServiceImpl"
3333
serviceInterface="com.vaadin.plugin.copilot.service.CopilotServerService"/>
3434
<postStartupActivity implementation="com.vaadin.plugin.copilot.activity.CopilotPostStartupProjectActivity"/>

0 commit comments

Comments
 (0)