use of com.google.idea.blaze.base.dependencies.AddSourceToProjectHelper.LocationContext in project intellij by bazelbuild.
the class AddSourceToProjectAction method actionPerformedInBlazeProject.
@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
VirtualFile vf = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (vf == null) {
return;
}
File file = new File(vf.getPath());
LocationContext context = AddSourceToProjectHelper.getContext(project, file);
if (context == null) {
return;
}
boolean inProjectDirectories = AddSourceToProjectHelper.sourceInProjectDirectories(context);
PsiFileSystemItem psiFile = findFileOrDirectory(PsiManager.getInstance(project), vf);
if (psiFile instanceof PsiDirectory) {
if (!inProjectDirectories) {
AddDirectoryToProjectAction.runAction(project, new File(vf.getPath()));
} else {
Messages.showErrorDialog(project, "This directory is already included in the project view file's 'directories' section.", "Cannot add directory to project");
}
return;
}
if (psiFile instanceof BuildFile && ((BuildFile) psiFile).getBlazeFileType() == BlazeFileType.BuildPackage) {
AddSourceToProjectHelper.addSourceAndTargetsToProject(project, context.workspacePath, ImmutableList.of(TargetExpression.allFromPackageNonRecursive(context.blazePackage)));
return;
}
if (AddSourceToProjectHelper.packageCoveredByWildcardPattern(context)) {
return;
}
// otherwise find the targets building this source file, then add them to the project
ListenableFuture<List<TargetInfo>> targetsFuture = AddSourceToProjectHelper.getTargetsBuildingSource(context);
if (targetsFuture == null) {
return;
}
targetsFuture.addListener(() -> {
List<TargetInfo> targets = FuturesUtil.getIgnoringErrors(targetsFuture);
if (inProjectDirectories && (targets == null || targets.isEmpty())) {
Messages.showWarningDialog(project, "Add source to project action failed", "No targets found building this source file");
}
}, MoreExecutors.directExecutor());
AddSourceToProjectHelper.addSourceToProject(project, context.workspacePath, inProjectDirectories, targetsFuture);
// update editor notifications, to handle the case where the file is currently open.
EditorNotifications.getInstance(project).updateNotifications(vf);
}
use of com.google.idea.blaze.base.dependencies.AddSourceToProjectHelper.LocationContext in project intellij by bazelbuild.
the class AddSourceToProjectAction method actionDescription.
/**
* Initial checks for whether this action should be enabled. Returns the relevant action string,
* or null if the action shouldn't be shown.
*/
@Nullable
private static String actionDescription(Project project, @Nullable VirtualFile vf) {
if (vf == null) {
return null;
}
File file = new File(vf.getPath());
LocationContext context = AddSourceToProjectHelper.getContext(project, file);
if (context == null) {
return null;
}
boolean inProjectDirectories = AddSourceToProjectHelper.sourceInProjectDirectories(context);
PsiFileSystemItem psiFile = findFileOrDirectory(PsiManager.getInstance(project), vf);
if (psiFile instanceof PsiDirectory) {
return !inProjectDirectories ? "Add directory to project" : null;
}
if (psiFile instanceof BuildFile && ((BuildFile) psiFile).getBlazeFileType() == BlazeFileType.BuildPackage) {
// always enabled for directories and BUILD files
return "Add BUILD package to project";
}
if (!SourceToTargetProvider.hasProvider()) {
return null;
}
if (!SourceToTargetMap.getInstance(project).getRulesForSourceFile(file).isEmpty()) {
// early-out if source covered by previously built targets
return null;
}
if (AddSourceToProjectHelper.packageCoveredByWildcardPattern(context)) {
return null;
}
return "Add source file to project";
}
use of com.google.idea.blaze.base.dependencies.AddSourceToProjectHelper.LocationContext in project intellij by bazelbuild.
the class ExternalFileProjectManagementHelper method createNotificationPanel.
@Nullable
@Override
public EditorNotificationPanel createNotificationPanel(VirtualFile vf, FileEditor fileEditor) {
if (!enabled.getValue()) {
return null;
}
if (!BlazeUserSettings.getInstance().getShowAddFileToProjectNotification() || suppressedFiles.contains(new File(vf.getPath()))) {
return null;
}
File file = new File(vf.getPath());
if (!supportedFileType(file)) {
return null;
}
LocationContext context = AddSourceToProjectHelper.getContext(project, file);
if (context == null) {
return null;
}
ListenableFuture<List<TargetInfo>> targetsFuture = AddSourceToProjectHelper.getTargetsBuildingSource(context);
if (targetsFuture == null) {
return null;
}
boolean inProjectDirectories = AddSourceToProjectHelper.sourceInProjectDirectories(context);
EditorNotificationPanel panel = new EditorNotificationPanel();
// starts off not visible until we get the query results
panel.setVisible(false);
panel.setText("Do you want to add this file to your project sources?");
panel.createActionLabel("Add file to project", () -> {
AddSourceToProjectHelper.addSourceToProject(project, context.workspacePath, inProjectDirectories, targetsFuture);
EditorNotifications.getInstance(project).updateNotifications(vf);
});
panel.createActionLabel("Hide notification", () -> {
// suppressed for this file until the editor is restarted
suppressedFiles.add(file);
EditorNotifications.getInstance(project).updateNotifications(vf);
});
panel.createActionLabel("Don't show again", () -> {
// disables the notification permanently, and focuses the relevant setting, so users know
// how to turn it back on
BlazeUserSettings.getInstance().setShowAddFileToProjectNotification(false);
ShowSettingsUtilImpl.showSettingsDialog(project, BlazeUserSettingsConfigurable.ID, BlazeUserSettingsConfigurable.SHOW_ADD_FILE_TO_PROJECT_LABEL_TEXT);
});
targetsFuture.addListener(() -> {
try {
List<TargetInfo> targets = targetsFuture.get();
if (!targets.isEmpty() || !inProjectDirectories) {
panel.setVisible(true);
}
} catch (InterruptedException | ExecutionException e) {
// ignore
}
}, MoreExecutors.directExecutor());
return panel;
}
Aggregations