Skip to content

Commit

Permalink
Merged branch idea243.release into idea243.x
Browse files Browse the repository at this point in the history
  • Loading branch information
builduser committed Dec 4, 2024
2 parents a89aefe + a00c539 commit f80ae12
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 56 deletions.
3 changes: 3 additions & 0 deletions scala/scala-impl/resources/META-INF/scala-plugin-common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,9 @@
<listener topic="com.intellij.openapi.project.DumbService$DumbModeListener"
class="org.jetbrains.plugins.scala.components.TypeAwareWidgetFactory$Listener"/>

<listener topic="com.intellij.openapi.roots.ModuleRootListener"
class="org.jetbrains.plugins.scala.components.TypeAwareWidgetModuleRootListener"/>

<listener topic="com.intellij.openapi.roots.ModuleRootListener"
class="org.jetbrains.plugins.scala.project.ConfigureIncrementalCompilerModuleRootListener"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.intellij.openapi.project.Project
import com.intellij.util.FileContentUtil
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.plugins.scala.actions.ToggleTypeAwareHighlightingAction.toggleSettingAndRehighlight
import org.jetbrains.plugins.scala.components.TypeAwareWidgetFactory
import org.jetbrains.plugins.scala.components.TypeAwareWidgetUpdater
import org.jetbrains.plugins.scala.extensions._
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings

Expand All @@ -35,9 +35,7 @@ object ToggleTypeAwareHighlightingAction {
val settings = ScalaProjectSettings.getInstance(project)
settings.toggleTypeAwareHighlighting()

project.getMessageBus
.syncPublisher(TypeAwareWidgetFactory.Topic)
.updateWidget()
TypeAwareWidgetUpdater.updateWidget(project)

invokeLater(ModalityState.nonModal()) {
reparseActiveFiles(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,22 @@ package org.jetbrains.plugins.scala.components
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.keymap.KeymapUtil
import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.StatusBarWidget
import com.intellij.openapi.wm.StatusBarWidget.WidgetPresentation
import com.intellij.openapi.wm.impl.status.widget.StatusBarWidgetsManager
import com.intellij.openapi.wm.{StatusBar, StatusBarWidget}
import com.intellij.util.Consumer
import com.intellij.util.messages.MessageBusConnection
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.plugins.scala.actions.ToggleTypeAwareHighlightingAction
import org.jetbrains.plugins.scala.extensions.invokeLater
import org.jetbrains.plugins.scala.icons.Icons
import org.jetbrains.plugins.scala.project.ProjectExt
import org.jetbrains.plugins.scala.settings.ScalaProjectSettings

import java.awt.event.MouseEvent
import javax.swing.Icon

private final class TypeAwareWidget(project: Project, factory: TypeAwareWidgetFactory)
extends StatusBarWidget
with StatusBarWidget.IconPresentation
with TypeAwareWidgetFactory.UpdateListener {

private var statusBar: StatusBar = _
private val connection: MessageBusConnection = project.getMessageBus.connect()
private val widgetsManager: StatusBarWidgetsManager = project.getService(classOf[StatusBarWidgetsManager])
private final class TypeAwareWidget(project: Project)
extends StatusBarWidget with StatusBarWidget.IconPresentation {

override def ID(): String = TypeAwareWidgetFactory.ID

override def install(statusBar: StatusBar): Unit = synchronized {
this.statusBar = statusBar
connection.subscribe(TypeAwareWidgetFactory.Topic, this)
subscribeToRootsChange()
}

override def dispose(): Unit = synchronized {
connection.dispose()
statusBar = null
}

override def getPresentation: WidgetPresentation = this

override def getIcon: Icon =
Expand All @@ -61,26 +40,11 @@ private final class TypeAwareWidget(project: Project, factory: TypeAwareWidgetFa
override def getClickConsumer: Consumer[MouseEvent] =
_ => ToggleTypeAwareHighlightingAction.toggleSettingAndRehighlight(project)

override def updateWidget(): Unit = synchronized {
if (statusBar ne null) {
widgetsManager.updateWidget(factory)
statusBar.updateWidget(ID())
}
}

private def isEnabled: Boolean =
ScalaProjectSettings.getInstance(project).isTypeAwareHighlightingEnabled

private def shortcutText: Option[String] = {
val action = ActionManager.getInstance().getAction("Scala.EnableErrors")
action.getShortcutSet.getShortcuts.headOption.map(KeymapUtil.getShortcutText)
}

private def subscribeToRootsChange(): Unit = {
project.subscribeToModuleRootChanged() { _ =>
invokeLater {
updateWidget()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.wm.impl.status.widget.StatusBarWidgetsManager
import com.intellij.openapi.wm.{StatusBar, StatusBarWidget, StatusBarWidgetFactory}
import com.intellij.util.messages.Topic
import org.jetbrains.plugins.scala.ScalaBundle
import org.jetbrains.plugins.scala.project.ProjectExt

import scala.jdk.CollectionConverters._

private final class TypeAwareWidgetFactory extends StatusBarWidgetFactory {
override def getId: String = TypeAwareWidgetFactory.ID

override def getDisplayName: String = ScalaBundle.message("scala.type.aware.highlighting.indicator")

override def isAvailable(project: Project): Boolean = project.isOpen && project.hasScala

override def createWidget(project: Project): StatusBarWidget = new TypeAwareWidget(project, this)
override def createWidget(project: Project): StatusBarWidget = new TypeAwareWidget(project)

override def disposeWidget(widget: StatusBarWidget): Unit = {
Disposer.dispose(widget)
Expand All @@ -27,14 +24,8 @@ private final class TypeAwareWidgetFactory extends StatusBarWidgetFactory {
override def canBeEnabledOn(statusBar: StatusBar): Boolean = isAvailable(statusBar.getProject)
}

private[scala] object TypeAwareWidgetFactory {
private[components] val ID: String = "TypeAwareHighlighting"

trait UpdateListener {
def updateWidget(): Unit
}

val Topic: Topic[UpdateListener] = new Topic("TypeAwareHighlightingWidget update", classOf[UpdateListener])
private object TypeAwareWidgetFactory {
val ID: String = "TypeAwareHighlighting"

final class Listener(project: Project) extends DumbModeListener {

Expand All @@ -55,7 +46,7 @@ private[scala] object TypeAwareWidgetFactory {
*/
override def exitDumbMode(): Unit = {
val service = project.getService(classOf[StatusBarWidgetsManager])
service.getWidgetFactories.asScala.find(_.getId == ID).foreach(service.updateWidget)
service.updateWidget(classOf[TypeAwareWidgetFactory])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.jetbrains.plugins.scala.components

import com.intellij.openapi.project.Project
import com.intellij.openapi.roots.{ModuleRootEvent, ModuleRootListener}

private final class TypeAwareWidgetModuleRootListener(project: Project) extends ModuleRootListener {
override def rootsChanged(event: ModuleRootEvent): Unit = {
if (!event.isCausedByWorkspaceModelChangesOnly) return
TypeAwareWidgetUpdater.updateWidget(project)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.jetbrains.plugins.scala.components

import com.intellij.openapi.project.Project
import com.intellij.openapi.wm.WindowManager
import com.intellij.openapi.wm.impl.status.widget.StatusBarWidgetsManager

object TypeAwareWidgetUpdater {
def updateWidget(project: Project): Unit = {
if (project.isDisposed) return
project.getService(classOf[StatusBarWidgetsManager]).updateWidget(classOf[TypeAwareWidgetFactory])
val statusBar = WindowManager.getInstance().getStatusBar(project)
if (statusBar eq null) return
statusBar.updateWidget(TypeAwareWidgetFactory.ID)
}
}

0 comments on commit f80ae12

Please sign in to comment.