<commit_before>// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.actions

import com.intellij.ide.IdeBundle.message
import com.intellij.ide.ui.UISettings
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.editor.colors.EditorFontType.PLAIN
import com.intellij.openapi.fileEditor.impl.EditorWindow
import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.OpenMode
import com.intellij.openapi.keymap.KeymapUtil.getShortcutText
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Iconable.ICON_FLAG_READ_STATUS
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.io.FileUtil.getLocationRelativeToUserHome
import com.intellij.openapi.util.text.StringUtil.naturalCompare
import com.intellij.openapi.vcs.FileStatusManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.newvfs.VfsPresentationUtil.getFileBackgroundColor
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.openapi.wm.impl.ToolWindowEventSource
import com.intellij.openapi.wm.impl.ToolWindowManagerImpl
import com.intellij.problems.WolfTheProblemSolver
import com.intellij.ui.BackgroundSupplier
import com.intellij.ui.CellRendererPanel
import com.intellij.ui.JBColor
import com.intellij.ui.SimpleColoredComponent
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.paint.EffectPainter.LINE_UNDERSCORE
import com.intellij.ui.render.RenderingUtil
import com.intellij.ui.speedSearch.SpeedSearchUtil.applySpeedSearchHighlighting
import com.intellij.util.IconUtil
import com.intellij.util.ui.JBUI
import java.awt.BorderLayout
import java.awt.Component
import java.awt.Graphics
import java.awt.Graphics2D
import javax.swing.Icon
import javax.swing.JList
import javax.swing.ListCellRenderer

private fun shortcutText(actionId: String) = ActionManager.getInstance().getKeyboardShortcut(actionId)?.let { getShortcutText(it) }


internal interface SwitcherListItem {
  val mainText: String
  val statusText: String? get() = null
  val shortcutText: String? get() = null
  val separatorAbove: Boolean get() = false

  fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode)
  fun close(switcher: Switcher.SwitcherPanel) = Unit

  fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean)
  fun prepareExtraRenderer(component: SimpleColoredComponent, selected: Boolean) {
    shortcutText?.let {
      component.append(it, when (selected) {
        true -> SimpleTextAttributes.REGULAR_ATTRIBUTES
        else -> SimpleTextAttributes.SHORTCUT_ATTRIBUTES
      })
    }
  }
}


internal class SwitcherRecentLocations(val switcher: Switcher.SwitcherPanel) : SwitcherListItem {
  override val separatorAbove = true
  override val mainText: String
    get() = when (switcher.isOnlyEditedFilesShown) {
      true -> message("recent.locations.changed.locations")
      else -> message("recent.locations.popup.title")
    }!!
  override val statusText: String
    get() = when (switcher.isOnlyEditedFilesShown) {
      true -> message("recent.files.accessible.open.recently.edited.locations")
      else -> message("recent.files.accessible.open.recently.viewed.locations")
    }!!
  override val shortcutText: String?
    get() = when (switcher.isOnlyEditedFilesShown) {
      true -> null
      else -> shortcutText(RecentLocationsAction.RECENT_LOCATIONS_ACTION_ID)
    }

  override fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode) {
    RecentLocationsAction.showPopup(switcher.project, switcher.isOnlyEditedFilesShown)
  }

  override fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean) {
    component.append(mainText)
  }
}


internal class SwitcherToolWindow(val window: ToolWindow, shortcut: Boolean) : SwitcherListItem {
  private val actionId = ActivateToolWindowAction.getActionIdForToolWindow(window.id)
  var mnemonic: String? = null

  override val mainText = window.stripeTitle
  override val statusText = message("recent.files.accessible.show.tool.window", mainText)
  override val shortcutText = if (shortcut) shortcutText(actionId) else null

  override fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode) {
    val manager = ToolWindowManager.getInstance(switcher.project) as? ToolWindowManagerImpl
    manager?.activateToolWindow(window.id, null, true, when (switcher.isSpeedSearchPopupActive) {
      true -> ToolWindowEventSource.SwitcherSearch
      else -> ToolWindowEventSource.Switcher
    }) ?: window.activate(null, true)
  }

  override fun close(switcher: Switcher.SwitcherPanel) {
    val manager = ToolWindowManager.getInstance(switcher.project) as? ToolWindowManagerImpl
    manager?.hideToolWindow(window.id, false, false, ToolWindowEventSource.CloseFromSwitcher) ?: window.hide()
  }

  override fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean) {
    component.icon = MnemonicIcon(window.icon, mnemonic, selected)
    component.append(mainText)
  }
}


internal class SwitcherVirtualFile(
  val project: Project,
  val file: VirtualFile,
  val window: EditorWindow?
) : SwitcherListItem, BackgroundSupplier {

  private val icon by lazy { IconUtil.getIcon(file, ICON_FLAG_READ_STATUS, project) }

  val isProblemFile
    get() = WolfTheProblemSolver.getInstance(project)?.isProblemFile(file) == true

  override var mainText: String = ""

  override val statusText: String?
    get() = getLocationRelativeToUserHome((file.parent ?: file).presentableUrl)

  override fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode) {
  }

  override fun close(switcher: Switcher.SwitcherPanel) {
  }

  override fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean) {
    component.icon = RenderingUtil.getIcon(icon, selected)
    val foreground = FileStatusManager.getInstance(project).getStatus(file).color
    val effectColor = if (isProblemFile) JBColor.red else null<commit_msg>Fix selection foreground<commit_after>// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.ide.actions

import com.intellij.ide.IdeBundle.message
import com.intellij.ide.ui.UISettings
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.editor.colors.EditorFontType.PLAIN
import com.intellij.openapi.fileEditor.impl.EditorWindow
import com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl.OpenMode
import com.intellij.openapi.keymap.KeymapUtil.getShortcutText
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Iconable.ICON_FLAG_READ_STATUS
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.io.FileUtil.getLocationRelativeToUserHome
import com.intellij.openapi.util.text.StringUtil.naturalCompare
import com.intellij.openapi.vcs.FileStatusManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.openapi.vfs.newvfs.VfsPresentationUtil.getFileBackgroundColor
import com.intellij.openapi.wm.ToolWindow
import com.intellij.openapi.wm.ToolWindowManager
import com.intellij.openapi.wm.impl.ToolWindowEventSource
import com.intellij.openapi.wm.impl.ToolWindowManagerImpl
import com.intellij.problems.WolfTheProblemSolver
import com.intellij.ui.BackgroundSupplier
import com.intellij.ui.CellRendererPanel
import com.intellij.ui.JBColor
import com.intellij.ui.SimpleColoredComponent
import com.intellij.ui.SimpleTextAttributes
import com.intellij.ui.paint.EffectPainter.LINE_UNDERSCORE
import com.intellij.ui.render.RenderingUtil
import com.intellij.ui.speedSearch.SpeedSearchUtil.applySpeedSearchHighlighting
import com.intellij.util.IconUtil
import com.intellij.util.ui.JBUI
import java.awt.BorderLayout
import java.awt.Component
import java.awt.Graphics
import java.awt.Graphics2D
import javax.swing.Icon
import javax.swing.JList
import javax.swing.ListCellRenderer

private fun shortcutText(actionId: String) = ActionManager.getInstance().getKeyboardShortcut(actionId)?.let { getShortcutText(it) }


internal interface SwitcherListItem {
  val mainText: String
  val statusText: String? get() = null
  val shortcutText: String? get() = null
  val separatorAbove: Boolean get() = false

  fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode)
  fun close(switcher: Switcher.SwitcherPanel) = Unit

  fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean)
  fun prepareExtraRenderer(component: SimpleColoredComponent, selected: Boolean) {
    shortcutText?.let {
      component.append(it, when (selected) {
        true -> SimpleTextAttributes.REGULAR_ATTRIBUTES
        else -> SimpleTextAttributes.SHORTCUT_ATTRIBUTES
      })
    }
  }
}


internal class SwitcherRecentLocations(val switcher: Switcher.SwitcherPanel) : SwitcherListItem {
  override val separatorAbove = true
  override val mainText: String
    get() = when (switcher.isOnlyEditedFilesShown) {
      true -> message("recent.locations.changed.locations")
      else -> message("recent.locations.popup.title")
    }!!
  override val statusText: String
    get() = when (switcher.isOnlyEditedFilesShown) {
      true -> message("recent.files.accessible.open.recently.edited.locations")
      else -> message("recent.files.accessible.open.recently.viewed.locations")
    }!!
  override val shortcutText: String?
    get() = when (switcher.isOnlyEditedFilesShown) {
      true -> null
      else -> shortcutText(RecentLocationsAction.RECENT_LOCATIONS_ACTION_ID)
    }

  override fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode) {
    RecentLocationsAction.showPopup(switcher.project, switcher.isOnlyEditedFilesShown)
  }

  override fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean) {
    component.append(mainText)
  }
}


internal class SwitcherToolWindow(val window: ToolWindow, shortcut: Boolean) : SwitcherListItem {
  private val actionId = ActivateToolWindowAction.getActionIdForToolWindow(window.id)
  var mnemonic: String? = null

  override val mainText = window.stripeTitle
  override val statusText = message("recent.files.accessible.show.tool.window", mainText)
  override val shortcutText = if (shortcut) shortcutText(actionId) else null

  override fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode) {
    val manager = ToolWindowManager.getInstance(switcher.project) as? ToolWindowManagerImpl
    manager?.activateToolWindow(window.id, null, true, when (switcher.isSpeedSearchPopupActive) {
      true -> ToolWindowEventSource.SwitcherSearch
      else -> ToolWindowEventSource.Switcher
    }) ?: window.activate(null, true)
  }

  override fun close(switcher: Switcher.SwitcherPanel) {
    val manager = ToolWindowManager.getInstance(switcher.project) as? ToolWindowManagerImpl
    manager?.hideToolWindow(window.id, false, false, ToolWindowEventSource.CloseFromSwitcher) ?: window.hide()
  }

  override fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean) {
    component.icon = MnemonicIcon(window.icon, mnemonic, selected)
    component.append(mainText)
  }
}


internal class SwitcherVirtualFile(
  val project: Project,
  val file: VirtualFile,
  val window: EditorWindow?
) : SwitcherListItem, BackgroundSupplier {

  private val icon by lazy { IconUtil.getIcon(file, ICON_FLAG_READ_STATUS, project) }

  val isProblemFile
    get() = WolfTheProblemSolver.getInstance(project)?.isProblemFile(file) == true

  override var mainText: String = ""

  override val statusText: String?
    get() = getLocationRelativeToUserHome((file.parent ?: file).presentableUrl)

  override fun navigate(switcher: Switcher.SwitcherPanel, mode: OpenMode) {
  }

  override fun close(switcher: Switcher.SwitcherPanel) {
  }

  override fun prepareMainRenderer(component: SimpleColoredComponent, selected: Boolean) {
    component.icon = RenderingUtil.getIcon(icon, selected)
    val foreground = if (selected) null else FileStatusManager.getInstance(project).getStatus(file).color
    val effectColor = if (isProblemFile) JBColor.red else null
{'fraction_non_alphanumeric': 0.08948875255623721, 'fraction_numerical': 0.0017995910020449897, 'mean_word_length': 5.397697540554684, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3874299', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3682, 'n_words': 725}