Skip to content

Commit

Permalink
created service for CLionNotificationProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
LeFrosch committed Nov 4, 2024
1 parent f9ce324 commit fc28246
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ import com.google.idea.blaze.base.lang.buildfile.language.BuildFileType
import com.google.idea.blaze.base.settings.Blaze
import com.google.idea.blaze.base.wizard2.BazelImportCurrentProjectAction
import com.google.idea.blaze.base.wizard2.BazelNotificationProvider
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.ui.EditorNotificationProvider
Expand All @@ -33,31 +36,6 @@ import com.jetbrains.cidr.project.ui.notifications.ProjectNotification
import com.jetbrains.cidr.project.ui.popup.ProjectFixesProvider
import com.jetbrains.cidr.project.ui.widget.*

private var providerRegistered: Boolean = false

private fun unregisterGenericProvider(project: Project) {
val extensionPoint = EditorNotificationProvider.EP_NAME.getPoint(project)

// Note: We need to remove the default style of showing project status and fixes used in
// Android Studio and IDEA to introduce CLion's PSW style.
for (extension in extensionPoint.extensions) {
if (extension is BazelNotificationProvider) {
extensionPoint.unregisterExtension(extension)
}
}
}

private fun registerSpecificProvider() {
val projectFixes = ProjectFixesProvider.Companion.EP_NAME.point
projectFixes.registerExtension(CLionNotificationProvider())

val projectNotifications = EditorNotificationWarningProvider.EP_NAME.point
projectNotifications.registerExtension(CLionNotificationProvider())

val widgetStatus = WidgetStatusProvider.EP_NAME.point
widgetStatus.registerExtension(CLionNotificationProvider())
}

private fun isBazelAwareFile(project: Project, file: VirtualFile): Boolean {
if (Blaze.isBlazeProject(project)) {
return false
Expand All @@ -79,21 +57,51 @@ private fun isBazelAwareFile(project: Project, file: VirtualFile): Boolean {
}

// #api241
class CLionNotificationProvider : ProjectFixesProvider, WidgetStatusProvider, EditorNotificationWarningProvider {
@Service(Service.Level.APP)
class CLionNotificationProvider : ProjectFixesProvider, WidgetStatusProvider, EditorNotificationWarningProvider,
Disposable.Default {

companion object {
@JvmStatic
fun register(project: Project) {
unregisterGenericProvider(project)
service<CLionNotificationProvider>().unregisterGenericProvider(project)
}
}

init {
registerSpecificProvider();
}

private fun registerSpecificProvider() {
val projectFixes = ProjectFixesProvider.Companion.EP_NAME.point
projectFixes.registerExtension(this, this)

val projectNotifications = EditorNotificationWarningProvider.EP_NAME.point
projectNotifications.registerExtension(this, this)

val widgetStatus = WidgetStatusProvider.EP_NAME.point
widgetStatus.registerExtension(this, this)
}

private fun unregisterGenericProvider(project: Project) {
val extensionPoint = EditorNotificationProvider.EP_NAME.getPoint(project)

if (!providerRegistered) {
registerSpecificProvider();
// Note: We need to remove the default style of showing project status and fixes used in
// Android Studio and IDEA to introduce CLion's PSW style.
for (extension in extensionPoint.extensions) {
if (extension is BazelNotificationProvider) {
extensionPoint.unregisterExtension(extension)
}
providerRegistered = true;
}
}

override fun collectFixes(project: Project, file: VirtualFile?, dataContext: DataContext): List<AnAction> {
if (file == null || !isBazelAwareFile(project, file)) {
if (file == null) {
return emptyList()
}

val isBazelFile = isBazelAwareFile(project, file)
if (!isBazelFile) {
return emptyList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import com.google.idea.blaze.base.lang.buildfile.language.BuildFileType
import com.google.idea.blaze.base.settings.Blaze
import com.google.idea.blaze.base.wizard2.BazelImportCurrentProjectAction
import com.google.idea.blaze.base.wizard2.BazelNotificationProvider
import com.intellij.openapi.Disposable
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.application.readAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.extensions.LoadingOrder
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
Expand All @@ -35,31 +38,6 @@ import com.jetbrains.cidr.project.ui.notifications.ProjectNotification
import com.jetbrains.cidr.project.ui.popup.ProjectFixesProvider
import com.jetbrains.cidr.project.ui.widget.*

private var providerRegistered: Boolean = false

private fun unregisterGenericProvider(project: Project) {
val extensionPoint = EditorNotificationProvider.EP_NAME.getPoint(project)

// Note: We need to remove the default style of showing project status and fixes used in
// Android Studio and IDEA to introduce CLion's PSW style.
for (extension in extensionPoint.extensions) {
if (extension is BazelNotificationProvider) {
extensionPoint.unregisterExtension(extension)
}
}
}

private fun registerSpecificProvider() {
val projectFixes = ProjectFixesProvider.Companion.EP_NAME.point
projectFixes.registerExtension(CLionNotificationProvider())

val projectNotifications = EditorNotificationWarningProvider.EP_NAME.point
projectNotifications.registerExtension(CLionNotificationProvider())

val widgetStatus = WidgetStatusProvider.EP_NAME.point
widgetStatus.registerExtension(CLionNotificationProvider())
}

private fun isBazelAwareFile(project: Project, file: VirtualFile): Boolean {
if (Blaze.isBlazeProject(project)) {
return false
Expand All @@ -81,16 +59,41 @@ private fun isBazelAwareFile(project: Project, file: VirtualFile): Boolean {
}

// #api241
class CLionNotificationProvider : ProjectFixesProvider, WidgetStatusProvider, EditorNotificationWarningProvider {
@Service(Service.Level.APP)
class CLionNotificationProvider : ProjectFixesProvider, WidgetStatusProvider, EditorNotificationWarningProvider,
Disposable.Default {

companion object {
@JvmStatic
fun register(project: Project) {
unregisterGenericProvider(project)
service<CLionNotificationProvider>().unregisterGenericProvider(project)
}
}

init {
registerSpecificProvider();
}

private fun registerSpecificProvider() {
val projectFixes = ProjectFixesProvider.Companion.EP_NAME.point
projectFixes.registerExtension(this, this)

val projectNotifications = EditorNotificationWarningProvider.EP_NAME.point
projectNotifications.registerExtension(this, this)

val widgetStatus = WidgetStatusProvider.EP_NAME.point
widgetStatus.registerExtension(this, this)
}

private fun unregisterGenericProvider(project: Project) {
val extensionPoint = EditorNotificationProvider.EP_NAME.getPoint(project)

if (!providerRegistered) {
registerSpecificProvider();
// Note: We need to remove the default style of showing project status and fixes used in
// Android Studio and IDEA to introduce CLion's PSW style.
for (extension in extensionPoint.extensions) {
if (extension is BazelNotificationProvider) {
extensionPoint.unregisterExtension(extension)
}
providerRegistered = true;
}
}

Expand Down

0 comments on commit fc28246

Please sign in to comment.