/*
 * Copyright 2019 The Bazel Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.idea.sdkcompat.platform;

import com.google.common.base.Verify;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ComponentManager;
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
import java.util.Optional;

/** Compat APIs used to replace services. #api192. */
public class ServiceHelperCompat {
  public static <T> void registerService(
      ComponentManager componentManager,
      Class<T> key,
      T implementation,
      Disposable parentDisposable) {
    // TODO(b/144912851): replace "IDEA CORE" reference with PluginManagerCore.CORE_ID
    // once it is available in the plugin API.
    Optional<IdeaPluginDescriptor> platformPlugin =
        PluginManager.getLoadedPlugins().stream()
            .filter(descriptor -> descriptor.getName().startsWith("IDEA CORE"))
            .findAny();

    Verify.verify(platformPlugin.isPresent());

    ((PlatformComponentManagerImpl) componentManager)
        .registerServiceInstance(key, implementation, platformPlugin.get());
  }

  private ServiceHelperCompat() {}
}

/*
 * Copyright 2019 The Bazel Authors. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.idea.sdkcompat.platform;

import com.google.common.base.Verify;
import com.intellij.ide.plugins.IdeaPluginDescriptor;
import com.intellij.ide.plugins.PluginManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.components.ComponentManager;
import com.intellij.serviceContainer.PlatformComponentManagerImpl;
import java.util.Optional;

/** Compat APIs used to replace services. #api192. */
public class ServiceHelperCompat {
  public static <T> void registerService(
      ComponentManager componentManager,
      Class<T> key,
      T implementation,
      Disposable parentDisposable) {
    // #api192: replace "IDEA CORE" reference with PluginManagerCore.CORE_ID
    // once it is available in the plugin API.
    Optional<IdeaPluginDescriptor> platformPlugin =
        PluginManager.getLoadedPlugins().stream()
            .filter(descriptor -> descriptor.getName().startsWith("IDEA CORE"))
            .findAny();

    Verify.verify(platformPlugin.isPresent());

    ((PlatformComponentManagerImpl) componentManager)
        .registerServiceInstance(key, implementation, platformPlugin.get());
  }

  private ServiceHelperCompat() {}
}

Replace bug with #api reminder.