blob: 20fa54c58189820c6a0f50bf92e70e5ac0bf00c9 [file] [log] [blame]
/*
* Copyright 2017 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.blaze.scala.sync;
import com.google.common.collect.ImmutableSet;
import com.google.idea.blaze.base.command.info.BlazeInfo;
import com.google.idea.blaze.base.ideinfo.TargetMap;
import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.model.SyncState;
import com.google.idea.blaze.base.model.SyncState.Builder;
import com.google.idea.blaze.base.model.primitives.LanguageClass;
import com.google.idea.blaze.base.model.primitives.WorkspaceRoot;
import com.google.idea.blaze.base.model.primitives.WorkspaceType;
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.scope.Scope;
import com.google.idea.blaze.base.scope.scopes.TimingScope;
import com.google.idea.blaze.base.sync.BlazeSyncPlugin;
import com.google.idea.blaze.base.sync.libraries.LibrarySource;
import com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings;
import com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder;
import com.google.idea.blaze.base.sync.workspace.WorkingSet;
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver;
import com.google.idea.blaze.scala.sync.importer.BlazeScalaWorkspaceImporter;
import com.google.idea.blaze.scala.sync.model.BlazeScalaImportResult;
import com.google.idea.blaze.scala.sync.model.BlazeScalaSyncData;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.OrderEnumerator;
import com.intellij.openapi.roots.ui.configuration.libraryEditor.ExistingLibraryEditor;
import java.util.Set;
import javax.annotation.Nullable;
import org.jetbrains.plugins.scala.project.ScalaLibraryType;
/** Supports scala. */
public class BlazeScalaSyncPlugin extends BlazeSyncPlugin.Adapter {
@Override
public Set<LanguageClass> getSupportedLanguagesInWorkspace(WorkspaceType workspaceType) {
if (workspaceType.equals(WorkspaceType.JAVA)) {
return ImmutableSet.of(LanguageClass.SCALA);
}
return ImmutableSet.of();
}
@Override
public void updateProjectStructure(
Project project,
BlazeContext context,
WorkspaceRoot workspaceRoot,
ProjectViewSet projectViewSet,
BlazeProjectData blazeProjectData,
@Nullable BlazeProjectData oldBlazeProjectData,
ModuleEditor moduleEditor,
Module workspaceModule,
ModifiableRootModel workspaceModifiableModel) {
if (!blazeProjectData.workspaceLanguageSettings.isLanguageActive(LanguageClass.SCALA)) {
return;
}
OrderEnumerator.orderEntries(workspaceModule)
.forEachLibrary(
library -> {
// Convert the type of the SDK library to prevent the scala plugin from
// showing the missing SDK notification.
// TODO: use a canonical class in the SDK (e.g., scala.App) instead of the name?
if (library.getName() != null && library.getName().startsWith("scala-library")) {
ExistingLibraryEditor editor = new ExistingLibraryEditor(library, null);
editor.setType(ScalaLibraryType.instance());
editor.commit();
return false; // stop
}
return true; // continue
});
}
@Override
public void updateSyncState(
Project project,
BlazeContext context,
WorkspaceRoot workspaceRoot,
ProjectViewSet projectViewSet,
WorkspaceLanguageSettings workspaceLanguageSettings,
BlazeInfo blazeInfo,
@Nullable WorkingSet workingSet,
WorkspacePathResolver workspacePathResolver,
ArtifactLocationDecoder artifactLocationDecoder,
TargetMap targetMap,
Builder syncStateBuilder,
@Nullable SyncState previousSyncState) {
if (!workspaceLanguageSettings.isLanguageActive(LanguageClass.SCALA)) {
return;
}
BlazeScalaWorkspaceImporter blazeScalaWorkspaceImporter =
new BlazeScalaWorkspaceImporter(project, workspaceRoot, projectViewSet, targetMap);
BlazeScalaImportResult importResult =
Scope.push(
context,
(childContext) -> {
childContext.push(new TimingScope("ScalaWorkspaceImporter"));
return blazeScalaWorkspaceImporter.importWorkspace();
});
BlazeScalaSyncData syncData = new BlazeScalaSyncData(importResult);
syncStateBuilder.put(BlazeScalaSyncData.class, syncData);
}
@Nullable
@Override
public LibrarySource getLibrarySource(
ProjectViewSet projectViewSet, BlazeProjectData blazeProjectData) {
if (!blazeProjectData.workspaceLanguageSettings.isLanguageActive(LanguageClass.SCALA)) {
return null;
}
return new BlazeScalaLibrarySource(blazeProjectData);
}
}