Skip to content

Add 'View project root' flag #6422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class Sections {
BazelBinarySection.PARSER,
BuildConfigSection.PARSER,
UseExclusionPatternsSection.PARSER,
UseQuerySyncSection.PARSER);
UseQuerySyncSection.PARSER,
ViewProjectRootSection.PARSER);

public static List<SectionParser> getParsers() {
List<SectionParser> parsers = Lists.newArrayList(PARSERS);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/
package com.google.idea.blaze.base.projectview.section.sections;

import com.google.idea.blaze.base.projectview.section.ScalarSection;
import com.google.idea.blaze.base.projectview.section.SectionKey;
import com.google.idea.blaze.base.projectview.section.SectionParser;

/** If set to true, automatically derives targets from the project directories. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is out of date.

public class ViewProjectRootSection {
public static final SectionKey<Boolean, ScalarSection<Boolean>> KEY =
SectionKey.of("view_project_root");
public static final SectionParser PARSER = new BooleanSectionParser(
KEY,
"If set to true, the project root and its subdirectories are visible in the 'Project' tool window"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public static void createContentEntries(
File rootFile = workspaceRoot.fileForPath(rootDirectory);
ContentEntry contentEntry =
modifiableRootModel.addContentEntry(UrlUtil.pathToUrl(rootFile.getPath()));
contentEntry.clearExcludeFolders();

for (WorkspacePath exclude : excludesByRootDirectory.get(rootDirectory)) {
File excludeFolder = workspaceRoot.fileForPath(exclude);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@
import com.google.idea.blaze.base.projectview.section.sections.DirectoryEntry;
import com.google.idea.blaze.base.projectview.section.sections.DirectorySection;
import com.google.idea.blaze.base.projectview.section.sections.TargetSection;
import com.google.idea.blaze.base.projectview.section.sections.ViewProjectRootSection;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.settings.BuildSystemName;
import com.google.idea.blaze.base.sync.data.BlazeDataStorage;
import com.google.idea.blaze.base.util.WorkspacePathUtil;
import com.google.idea.common.experiments.BoolExperiment;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -86,6 +91,7 @@ public static class Builder {

private final WorkspaceRoot workspaceRoot;
private final BuildSystemName buildSystemName;
private Boolean viewProjectRoot = false;

private Builder(WorkspaceRoot workspaceRoot, BuildSystemName buildSystemName) {
this.workspaceRoot = workspaceRoot;
Expand All @@ -100,6 +106,7 @@ public Builder add(ProjectViewSet projectViewSet) {
projectTargets.addAll(projectViewSet.listItems(TargetSection.KEY));
deriveTargetsFromDirectories =
projectViewSet.getScalarValue(AutomaticallyDeriveTargetsSection.KEY).orElse(false);
viewProjectRoot = projectViewSet.getScalarValue(ViewProjectRootSection.KEY).orElse(false);
return this;
}

Expand Down Expand Up @@ -127,6 +134,10 @@ public Builder exclude(WorkspacePath entry) {
}

public ImportRoots build() {
if (viewProjectRoot) {
rootDirectoriesBuilder.add(workspaceRoot.workspacePathFor(workspaceRoot.directory()));
}

ImmutableCollection<WorkspacePath> rootDirectories = rootDirectoriesBuilder.build();
if (buildSystemName == BuildSystemName.Bazel) {
if (hasWorkspaceRoot(rootDirectories)) {
Expand All @@ -136,6 +147,13 @@ public ImportRoots build() {
excludeBazelIgnoredPaths();
}

if (viewProjectRoot) {
Arrays.stream(Objects.requireNonNull(workspaceRoot.directory().listFiles()))
.filter(f -> f.isDirectory() && rootDirectoriesBuilder.build().stream().noneMatch(r -> FileUtil.filesEqual(workspaceRoot.fileForPath(r), f)))
.map(workspaceRoot::workspacePathFor)
.forEach(excludeDirectoriesBuilder::add);
}

ImmutableSet<WorkspacePath> minimalExcludes =
WorkspacePathUtil.calculateMinimalWorkspacePaths(excludeDirectoriesBuilder.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.google.idea.blaze.base.projectview.section.sections.TryImportSection;
import com.google.idea.blaze.base.projectview.section.sections.UseExclusionPatternsSection;
import com.google.idea.blaze.base.projectview.section.sections.UseQuerySyncSection;
import com.google.idea.blaze.base.projectview.section.sections.ViewProjectRootSection;
import com.google.idea.blaze.base.projectview.section.sections.WorkspaceTypeSection;
import com.google.idea.blaze.base.sync.BlazeSyncPlugin;
import com.google.idea.common.experiments.ExperimentService;
Expand Down Expand Up @@ -120,6 +121,7 @@ public void testProjectViewSetSerializable() {
ScalarSection.builder(BuildConfigSection.KEY)
.set(new WorkspacePath("test")))
.add(ScalarSection.builder(UseExclusionPatternsSection.KEY).set(false))
.add(ScalarSection.builder(ViewProjectRootSection.KEY).set(false))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test where we actually set it in a project view? We don't have to assert anything on the actual behaviour, I just want to check that it parses.

.build())
.build();

Expand Down
Loading