Skip to content

Commit

Permalink
Use PSW API in bazel for CLion
Browse files Browse the repository at this point in the history
  • Loading branch information
TomatorCZ committed Jul 22, 2024
1 parent 87916b8 commit 7e493bb
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolver;
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl;
import com.intellij.ide.DataManager;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.actionSystem.ActionPlaces;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.ex.ActionUtil;
import com.intellij.openapi.project.Project;
import com.intellij.ui.EditorNotificationPanel;
import java.io.File;
import org.jetbrains.annotations.NotNull;

Expand All @@ -49,7 +48,7 @@ public static boolean projectCouldBeImported(Project project) {
return BazelWorkspaceRootProvider.INSTANCE.isWorkspaceRoot(new File(root));
}

public static Runnable createAction(EditorNotificationPanel panel, String basePath) {
public static Runnable createAction(String basePath) {
File baseFile = new File(basePath);
BazelImportCurrentProjectAction action = new BazelImportCurrentProjectAction(baseFile);

Expand All @@ -58,7 +57,7 @@ public static Runnable createAction(EditorNotificationPanel panel, String basePa
action,
null,
ActionPlaces.UNKNOWN,
DataManager.getInstance().getDataContext(panel)
DataContext.EMPTY_CONTEXT
);

if (ActionUtil.lastUpdateAndCheckDumb(action, event, true)) {
Expand All @@ -69,7 +68,8 @@ public static Runnable createAction(EditorNotificationPanel panel, String basePa

final File workspaceRootFile;

private BazelImportCurrentProjectAction(File workspaceRootFile) {
public BazelImportCurrentProjectAction(File workspaceRootFile) {
super("Import current project");
this.workspaceRootFile = workspaceRootFile;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class BazelNotificationProvider implements EditorNotificationProvider, Du

return fileEditor -> {
EditorNotificationPanel panel = new EditorNotificationPanel(fileEditor, Status.Warning);
Runnable importAction = BazelImportCurrentProjectAction.createAction(panel, root);
Runnable importAction = BazelImportCurrentProjectAction.createAction(root);

panel.setText("Project is not configured");
panel.createActionLabel("Import Bazel project", importAction);
Expand Down
1 change: 1 addition & 0 deletions clwb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ java_library(
"//intellij_platform_sdk:jsr305",
"//intellij_platform_sdk:plugin_api",
"//sdkcompat",
"//clwb/sdkcompat",
],
)

Expand Down
38 changes: 38 additions & 0 deletions clwb/sdkcompat/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright 2024 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.

load(
"//intellij_platform_sdk:build_defs.bzl",
"select_for_plugin_api",
)

java_library(
name = "sdkcompat",
srcs = select_for_plugin_api({
"clion-2021.3": ["//clwb/sdkcompat/vOlder"],
"clion-2022.1": ["//clwb/sdkcompat/vOlder"],
"clion-2022.2": ["//clwb/sdkcompat/vOlder"],
"clion-2022.3": ["//clwb/sdkcompat/vOlder"],
"clion-2023.1": ["//clwb/sdkcompat/vOlder"],
"clion-2023.2": ["//clwb/sdkcompat/vOlder"],
"clion-2023.3": ["//clwb/sdkcompat/vOlder"],
"clion-2024.1": ["//clwb/sdkcompat/vOlder"],
"clion-2024.2": ["//clwb/sdkcompat/v242"],
}),
visibility = ["//clwb:__pkg__"],
deps = [
"//base",
"//intellij_platform_sdk:plugin_api",
],
)
19 changes: 19 additions & 0 deletions clwb/sdkcompat/v242/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 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.

filegroup(
name = "v242",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.function.Function;

class BazelCNotificationProvider implements EditorNotificationProvider {
@Override
public @Nullable Function<? super @NotNull FileEditor, ? extends @Nullable JComponent> collectNotificationData(@NotNull Project project, @NotNull VirtualFile virtualFile) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.google.idea.blaze.clwb;

import com.google.idea.blaze.base.lang.buildfile.language.BuildFileType;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.wizard2.BazelImportCurrentProjectAction;
import com.google.idea.blaze.base.wizard2.BazelNotificationProvider;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationProvider;
import com.jetbrains.cidr.project.ui.ProjectStatusHelperKt;
import com.jetbrains.cidr.project.ui.notifications.EditorNotificationWarningProvider;
import com.jetbrains.cidr.project.ui.notifications.ProjectNotification;
import com.jetbrains.cidr.project.ui.popup.ProjectFixesProvider;
import com.jetbrains.cidr.project.ui.widget.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import static com.jetbrains.cidr.project.ui.ProjectStatusHelperKt.isProjectAwareFile;

// #api242
public class CLionNotificationProvider implements ProjectFixesProvider, WidgetStatusProvider, EditorNotificationWarningProvider {
private static void unregisterGenericProvider(Project project) {
final var extensionPoint = EditorNotificationProvider.EP_NAME.getPoint(project);

for (final var extension : extensionPoint.getExtensions()) {
if (extension instanceof BazelNotificationProvider) {
extensionPoint.unregisterExtension(extension);
}
}
}

public static void register(Project project) {
unregisterGenericProvider(project);


final var projectFixes = ProjectFixesProvider.Companion.getEP_NAME().getPoint();
projectFixes.registerExtension(new CLionNotificationProvider());
final var projectNotifications = EditorNotificationWarningProvider.Companion.getEP_NAME().getPoint();
projectNotifications.registerExtension(new CLionNotificationProvider());
final var widgetStatus = WidgetStatusProvider.Companion.getEP_NAME().getPoint();
widgetStatus.registerExtension(new CLionNotificationProvider());
}

@NotNull
@Override
public List<AnAction> collectFixes(@NotNull Project project, @Nullable VirtualFile file, @NotNull DataContext dataContext) {
if (file == null)
return List.of();

if (Blaze.isBlazeProject(project))
return List.of();

if (!isProjectAwareFile(file, project) && file.getFileType() != BuildFileType.INSTANCE) {
return List.of();
}
if (!BazelImportCurrentProjectAction.projectCouldBeImported(project)) {
return List.of();
}

String root = project.getBasePath();
if (root == null) {
return List.of();
}

return List.of(new ImportBazelAction(root));
}

private static class ImportBazelAction extends AnAction {
private final String root;

public ImportBazelAction(String root) {
super("Import Bazel project");
this.root = root;
}

@Override
public void actionPerformed(@NotNull AnActionEvent anActionEvent) {
BazelImportCurrentProjectAction.createAction(root).run();
}
}

@Nullable
@Override
public ProjectNotification getProjectNotification(@NotNull Project project, @NotNull VirtualFile virtualFile) {
return ProjectStatusHelperKt.convertStatus(getWidgetStatus(project,virtualFile));
}

@Nullable
@Override
public WidgetStatus getWidgetStatus(@NotNull Project project, @Nullable VirtualFile file) {

if (Blaze.isBlazeProject(project))
return new DefaultWidgetStatus(Status.OK, Scope.Project, "Project is configured");

if (file == null)
return null;

if (!isProjectAwareFile(file, project) && file.getFileType() != BuildFileType.INSTANCE) {
return null;
}
if (!BazelImportCurrentProjectAction.projectCouldBeImported(project)) {
return null;
}

String root = project.getBasePath();
if (root == null) {
return null;
}

return new DefaultWidgetStatus(Status.Warning, Scope.Project, "Project is not configured");
}
}
19 changes: 19 additions & 0 deletions clwb/sdkcompat/vOlder/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2024 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.

filegroup(
name = "vOlder",
srcs = glob(["**/*.java"]),
visibility = ["//clwb/sdkcompat:__pkg__"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.google.idea.blaze.clwb;

import com.google.idea.blaze.base.wizard2.BazelDisableImportNotification;
import com.google.idea.blaze.base.wizard2.BazelImportCurrentProjectAction;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotificationProvider;
import com.jetbrains.cidr.lang.OCFileType;
import groovyjarjarantlr4.v4.runtime.misc.NotNull;

import javax.annotation.Nullable;
import javax.swing.*;
import java.util.function.Function;

/**
* Provide notification for C-family files temporarily until moving to new CLion project status
* api.
*/
public class BazelCNotificationProvider implements EditorNotificationProvider {

@Override
public @Nullable Function<? super FileEditor, ? extends JComponent> collectNotificationData(
@NotNull Project project, @NotNull VirtualFile file) {
if (file.getFileType() != OCFileType.INSTANCE) {
return null;
}

if (!BazelImportCurrentProjectAction.projectCouldBeImported(project)) {
return null;
}
if (BazelDisableImportNotification.isNotificationDisabled(project)) {
return null;
}

String root = project.getBasePath();
if (root == null) {
return null;
}

return fileEditor -> {
EditorNotificationPanel panel = new EditorNotificationPanel(fileEditor, EditorNotificationPanel.Status.Warning);
Runnable importAction = BazelImportCurrentProjectAction.createAction(root);

panel.setText("Project is not configured");
panel.createActionLabel("Import Bazel project", importAction);
panel.createActionLabel("Dismiss", "Bazel.DisableImportNotification");

return panel;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.google.idea.blaze.clwb;

import com.intellij.openapi.project.Project;

// #api242
public class CLionNotificationProvider {
public static void register(Project project) {}
}
Loading

0 comments on commit 7e493bb

Please sign in to comment.