<commit_before>package org.scalaide.ui.internal.migration

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer
import org.eclipse.jface.bindings.Binding
import org.eclipse.jface.bindings.keys.KeyBinding
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.keys.IBindingService

/**
 * The purpose of this class is to keep user defined preferences when they are
 * replaced with another preference due to future advancements of the Scala IDE.
 */
class MigrationPreferenceInitializer extends AbstractPreferenceInitializer {

  override def initializeDefaultPreferences(): Unit = {
    val service = PlatformUI.getWorkbench().getAdapter(classOf[IBindingService]).asInstanceOf[IBindingService]

    /**
     * This copies the old preference value and set it as default value for the
     * new preference. This is only done when the new preference is not already
     * set, i.e exaclty once when a new version of the IDE is started and all
     * values are copied.
     *
     * Furthermore, the old preference value is set to an undefined state, which
     * has the effect that it isn't shown anymore in the key binding page (nor on
     * any other pages where it could be shown). Removing a command entirely
     * from the workbench seems not to be possible.
     *
     * At the moment only the migration of preferences that are related to key
     * bindings is supported.
     */
    def copyKeyBinding(oldCommandId: String, newCommandId: String): Unit = {
      def bindingsOf(id: String) = {
        def hasSameId(b: Binding) =
          Option(b.getParameterizedCommand()).map(_.getId() == id).getOrElse(false)

        service.getBindings().filter(hasSameId)
      }

      val newBindings = bindingsOf(newCommandId)
      val userBindings = newBindings.filter(_.getType() == Binding.USER)
      val oldBindings = bindingsOf(oldCommandId).filter(_.getType() == Binding.USER)
      val allBindings = service.getBindings().filterNot(oldBindings contains _)

      val executeCopyOperation =
        newBindings.nonEmpty && userBindings.isEmpty && oldBindings.nonEmpty

      if (executeCopyOperation) {
        val migratedBindings =
          for (b <- oldBindings) yield new KeyBinding(
            b.asInstanceOf[KeyBinding].getKeySequence(),
            newBindings.head.getParameterizedCommand(),
            b.getSchemeId(),
            b.getContextId(),
            b.getLocale(),
            b.getPlatform(),
            null,
            b.getType())

        oldBindings foreach (_.getParameterizedCommand().getCommand().undefine())
        service.savePreferences(service.getActiveScheme(), allBindings ++ migratedBindings)
      }
    }

    // These values are added for the 4.0 release
    copyKeyBinding("scala.tools.eclipse.refactoring.method.command.SplitParameterLists", "org.scalaide.refactoring.SplitParameterLists")
    copyKeyBinding("scala.tools.eclipse.refactoring.method.command.MergeParameterLists", "org.scalaide.refactoring.MergeParameterLists")
    copyKeyBinding("scala.tools.eclipse.refactoring.method.command.ChangeParameterOrder", "org.scalaide.refactoring.ChangeParameterOrder")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.MoveConstructorToCompanionObject", "org.scalaide.refactoring.MoveConstructorToCompanionObject")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExtractTrait", "org.scalaide.refactoring.ExtractTrait")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.MoveClass", "org.scalaide.refactoring.MoveClass")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExtractMethod", "org.scalaide.refactoring.ExtractMethod")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExtractLocal", "org.scalaide.refactoring.ExtractLocal")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.InlineLocal", "org.scalaide.refactoring.InlineLocal")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.OrganizeImports", "org.scalaide.refactoring.OrganizeImports")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.Rename", "org.scalaide.refactoring.Rename")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExpandCaseClassBinding", "org.scalaide.refactoring.ExpandCaseClassBinding")
    copyKeyBinding("scala.tools.eclipse.refactoring.commands.quickMenu", "org.scalaide.ui.menu.quickMenu")
  }
}<commit_msg>Disable MigrationPreferenceInitializer in UI less environment<commit_after>package org.scalaide.ui.internal.migration

import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer
import org.eclipse.jface.bindings.Binding
import org.eclipse.jface.bindings.keys.KeyBinding
import org.eclipse.ui.PlatformUI
import org.eclipse.ui.keys.IBindingService
import org.scalaide.core.ScalaPlugin

/**
 * The purpose of this class is to keep user defined preferences when they are
 * replaced with another preference due to future advancements of the Scala IDE.
 */
class MigrationPreferenceInitializer extends AbstractPreferenceInitializer {

  override def initializeDefaultPreferences(): Unit = {
    // do not run in an UI less environment
    if (!ScalaPlugin.plugin.headlessMode) {
      copyKeyBindings()
    }
  }

  private def copyKeyBindings() = {
    val service = PlatformUI.getWorkbench().getAdapter(classOf[IBindingService]).asInstanceOf[IBindingService]

    /**
     * This copies the old preference value and set it as default value for the
     * new preference. This is only done when the new preference is not already
     * set, i.e exaclty once when a new version of the IDE is started and all
     * values are copied.
     *
     * Furthermore, the old preference value is set to an undefined state, which
     * has the effect that it isn't shown anymore in the key binding page (nor on
     * any other pages where it could be shown). Removing a command entirely
     * from the workbench seems not to be possible.
     *
     * At the moment only the migration of preferences that are related to key
     * bindings is supported.
     */
    def copyKeyBinding(oldCommandId: String, newCommandId: String): Unit = {
      def bindingsOf(id: String) = {
        def hasSameId(b: Binding) =
          Option(b.getParameterizedCommand()).map(_.getId() == id).getOrElse(false)

        service.getBindings().filter(hasSameId)
      }

      val newBindings = bindingsOf(newCommandId)
      val userBindings = newBindings.filter(_.getType() == Binding.USER)
      val oldBindings = bindingsOf(oldCommandId).filter(_.getType() == Binding.USER)
      val allBindings = service.getBindings().filterNot(oldBindings contains _)

      val executeCopyOperation =
        newBindings.nonEmpty && userBindings.isEmpty && oldBindings.nonEmpty

      if (executeCopyOperation) {
        val migratedBindings =
          for (b <- oldBindings) yield new KeyBinding(
            b.asInstanceOf[KeyBinding].getKeySequence(),
            newBindings.head.getParameterizedCommand(),
            b.getSchemeId(),
            b.getContextId(),
            b.getLocale(),
            b.getPlatform(),
            null,
            b.getType())

        oldBindings foreach (_.getParameterizedCommand().getCommand().undefine())
        service.savePreferences(service.getActiveScheme(), allBindings ++ migratedBindings)
      }
    }

    // These values are added for the 4.0 release
    copyKeyBinding("scala.tools.eclipse.refactoring.method.command.SplitParameterLists", "org.scalaide.refactoring.SplitParameterLists")
    copyKeyBinding("scala.tools.eclipse.refactoring.method.command.MergeParameterLists", "org.scalaide.refactoring.MergeParameterLists")
    copyKeyBinding("scala.tools.eclipse.refactoring.method.command.ChangeParameterOrder", "org.scalaide.refactoring.ChangeParameterOrder")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.MoveConstructorToCompanionObject", "org.scalaide.refactoring.MoveConstructorToCompanionObject")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExtractTrait", "org.scalaide.refactoring.ExtractTrait")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.MoveClass", "org.scalaide.refactoring.MoveClass")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExtractMethod", "org.scalaide.refactoring.ExtractMethod")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExtractLocal", "org.scalaide.refactoring.ExtractLocal")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.InlineLocal", "org.scalaide.refactoring.InlineLocal")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.OrganizeImports", "org.scalaide.refactoring.OrganizeImports")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.Rename", "org.scalaide.refactoring.Rename")
    copyKeyBinding("scala.tools.eclipse.refactoring.command.ExpandCaseClassBinding", "org.scalaide.refactoring.ExpandCaseClassBinding")
    copyKeyBinding("scala.tools.eclipse.refactoring.commands.quickMenu", "org.scalaide.ui.menu.quickMenu")
  }
}
{'fraction_non_alphanumeric': 0.09823818019625334, 'fraction_numerical': 0.00044603033006244426, 'mean_word_length': 4.096022727272727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '2021258', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2458, 'n_words': 521}