Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
targerts are now build targets ids
Browse files Browse the repository at this point in the history
  • Loading branch information
abrams27 committed Feb 25, 2022
1 parent 7f0e4a0 commit 026b123
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,11 @@ private ProjectViewTargetsSection combineTargetsSection(
return new ProjectViewTargetsSection(includedTargets, excludedTargets);
}

private <T extends ProjectViewListSection> List<String> combineListValuesWithImported(
private <V, T extends ProjectViewListSection<V>> List<V> combineListValuesWithImported(
List<ProjectView> importedProjectViews,
T section,
Function<ProjectView, T> sectionGetter,
Function<ProjectViewListSection, List<String>> valuesGetter) {
Function<T, List<V>> valuesGetter) {
return importedProjectViews.stream()
.map(sectionGetter)
.map(valuesGetter)
Expand Down Expand Up @@ -199,7 +199,7 @@ private Optional<ProjectViewJavaPathSection> combineJavaPathSection(
return javaPath.or(() -> defaultJavaPathSection);
}

private <T extends ProjectViewSingletonSection> Optional<T> getLastImportedSingletonValue(
private <V, T extends ProjectViewSingletonSection<V>> Optional<T> getLastImportedSingletonValue(
List<ProjectView> importedProjectViews, Function<ProjectView, Optional<T>> sectionGetter) {
return importedProjectViews.stream()
.map(sectionGetter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ java_library(
"//projectview/src/main/java/org/jetbrains/bsp/bazel/projectview:__subpackages__",
"//projectview/src/test/java/org/jetbrains/bsp/bazel/projectview/model/sections:__pkg__",
],
exports = ["@maven//:ch_epfl_scala_bsp4j"],
deps = [
"@maven//:ch_epfl_scala_bsp4j",
"@maven//:com_google_guava_guava",
"@maven//:org_apache_commons_commons_collections4",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.jetbrains.bsp.bazel.projectview.model.sections;

import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
import java.util.List;

public class ProjectViewTargetsSection extends ProjectViewListSection<String> {
public class ProjectViewTargetsSection extends ProjectViewListSection<BuildTargetIdentifier> {

public static final String SECTION_NAME = "targets";

public ProjectViewTargetsSection() {
super(SECTION_NAME);
}

public ProjectViewTargetsSection(List<String> includedValues, List<String> excludedValues) {
public ProjectViewTargetsSection(
List<BuildTargetIdentifier> includedValues, List<BuildTargetIdentifier> excludedValues) {
super(SECTION_NAME, includedValues, excludedValues);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package org.jetbrains.bsp.bazel.projectview.parser.sections;

import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
import java.util.List;
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewTargetsSection;

public class ProjectViewTargetsSectionParser
extends ProjectViewListSectionParser<String, ProjectViewTargetsSection> {
extends ProjectViewListSectionParser<BuildTargetIdentifier, ProjectViewTargetsSection> {

public ProjectViewTargetsSectionParser() {
super(ProjectViewTargetsSection.SECTION_NAME);
}

@Override
protected String mapRawValues(String rawValue) {
return rawValue;
protected BuildTargetIdentifier mapRawValues(String rawValue) {
return new BuildTargetIdentifier(rawValue);
}

@Override
protected ProjectViewTargetsSection createInstance(
List<String> includedValues, List<String> excludedValues) {
List<BuildTargetIdentifier> includedValues, List<BuildTargetIdentifier> excludedValues) {
return new ProjectViewTargetsSection(includedValues, excludedValues);
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
Expand All @@ -16,17 +19,36 @@ public class ProjectViewListSectionTest<V, T extends ProjectViewListSection<V>>

private final BiFunction<List<String>, List<String>, T> sectionConstructor;

public ProjectViewListSectionTest(BiFunction<List<String>, List<String>, T> sectionConstructor) {
this.sectionConstructor = sectionConstructor;
public ProjectViewListSectionTest(
BiFunction<List<V>, List<V>, T> sectionMapper, Function<String, V> elementMapper) {
this.sectionConstructor = createSectionConstructor(sectionMapper, elementMapper);
}

private BiFunction<List<String>, List<String>, T> createSectionConstructor(
BiFunction<List<V>, List<V>, T> sectionMapper, Function<String, V> elementMapper) {

return (includedElements, excludedElements) ->
sectionMapper.apply(
mapElements(elementMapper, includedElements),
mapElements(elementMapper, excludedElements));
}

private List<V> mapElements(Function<String, V> elementMapper, List<String> rawElements) {
return rawElements.stream().map(elementMapper).collect(Collectors.toList());
}

@Parameters(name = "{index}: .equals() on a list section for {0}")
public static Collection<Object[]> data() {
return List.of(
new Object[][] {
{
(BiFunction<List<String>, List<String>, ProjectViewTargetsSection>)
ProjectViewTargetsSection::new
(BiFunction<
List<BuildTargetIdentifier>,
List<BuildTargetIdentifier>,
ProjectViewTargetsSection>)
ProjectViewTargetsSection::new,
(Function<String, BuildTargetIdentifier>)
(rawElement) -> new BuildTargetIdentifier("//:" + rawElement),
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
import com.google.common.net.HostAndPort;
import java.nio.file.Paths;
import java.util.List;
Expand Down Expand Up @@ -158,8 +159,10 @@ public void shouldParseFileWithAllSections() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2"),
List.of("//excluded_target1.1")))
List.of(
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2")),
List.of(new BuildTargetIdentifier("//excluded_target1.1"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand All @@ -186,9 +189,14 @@ public void shouldParseFileWithSingleImportedFileWithoutSingletonValues() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2", "//included_target4.1"),
List.of(
"//excluded_target1.1", "//excluded_target4.1", "//excluded_target4.2")))
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2"),
new BuildTargetIdentifier("//included_target4.1")),
List.of(
new BuildTargetIdentifier("//excluded_target1.1"),
new BuildTargetIdentifier("//excluded_target4.1"),
new BuildTargetIdentifier("//excluded_target4.2"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand All @@ -215,9 +223,14 @@ public void shouldParseFileWithSingleImportedFileWithSingletonValues() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2", "//included_target7.1"),
List.of(
"//excluded_target1.1", "//excluded_target7.1", "//excluded_target7.2")))
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2"),
new BuildTargetIdentifier("//included_target7.1")),
List.of(
new BuildTargetIdentifier("//excluded_target1.1"),
new BuildTargetIdentifier("//excluded_target7.1"),
new BuildTargetIdentifier("//excluded_target7.2"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path7/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand All @@ -244,8 +257,10 @@ public void shouldParseFileWithEmptyImportedFile() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target8.1"),
List.of("//excluded_target8.1", "//excluded_target8.2")))
List.of(new BuildTargetIdentifier("//included_target8.1")),
List.of(
new BuildTargetIdentifier("//excluded_target8.1"),
new BuildTargetIdentifier("//excluded_target8.2"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path8/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -273,15 +288,15 @@ public void shouldParseFileWithThreeImportedFiles() {
.targets(
new ProjectViewTargetsSection(
List.of(
"//included_target1.1",
"//included_target1.2",
"//included_target2.1",
"//included_target3.1"),
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2"),
new BuildTargetIdentifier("//included_target2.1"),
new BuildTargetIdentifier("//included_target3.1")),
List.of(
"//excluded_target1.1",
"//excluded_target2.1",
"//excluded_target5.1",
"//excluded_target5.2")))
new BuildTargetIdentifier("//excluded_target1.1"),
new BuildTargetIdentifier("//excluded_target2.1"),
new BuildTargetIdentifier("//excluded_target5.1"),
new BuildTargetIdentifier("//excluded_target5.2"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path3/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -309,16 +324,16 @@ public void shouldParseFileWithNestedImportedFiles() {
.targets(
new ProjectViewTargetsSection(
List.of(
"//included_target1.1",
"//included_target1.2",
"//included_target2.1",
"//included_target3.1",
"//included_target4.1"),
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2"),
new BuildTargetIdentifier("//included_target2.1"),
new BuildTargetIdentifier("//included_target3.1"),
new BuildTargetIdentifier("//included_target4.1")),
List.of(
"//excluded_target1.1",
"//excluded_target2.1",
"//excluded_target4.1",
"//excluded_target4.2")))
new BuildTargetIdentifier("//excluded_target1.1"),
new BuildTargetIdentifier("//excluded_target2.1"),
new BuildTargetIdentifier("//excluded_target4.1"),
new BuildTargetIdentifier("//excluded_target4.2"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -380,8 +395,10 @@ public void shouldReturnFile1ForEmptyDefaultFile() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2"),
List.of("//excluded_target1.1")))
List.of(
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2")),
List.of(new BuildTargetIdentifier("//excluded_target1.1"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -474,8 +491,10 @@ public void shouldParseFileAndSkipDefaults() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2"),
List.of("//excluded_target1.1")))
List.of(
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2")),
List.of(new BuildTargetIdentifier("//excluded_target1.1"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -503,8 +522,10 @@ public void shouldParseDefaultsForNotExistingFile() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2"),
List.of("//excluded_target1.1")))
List.of(
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2")),
List.of(new BuildTargetIdentifier("//excluded_target1.1"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -532,8 +553,10 @@ public void shouldParseFileAndUseDefaults() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target1.1", "//included_target1.2"),
List.of("//excluded_target1.1")))
List.of(
new BuildTargetIdentifier("//included_target1.1"),
new BuildTargetIdentifier("//included_target1.2")),
List.of(new BuildTargetIdentifier("//excluded_target1.1"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path1/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down Expand Up @@ -561,8 +584,10 @@ public void shouldParseFileAndUseDefaultsWithEmptyImportedFile() {
ProjectView.builder()
.targets(
new ProjectViewTargetsSection(
List.of("//included_target8.1"),
List.of("//excluded_target8.1", "//excluded_target8.2")))
List.of(new BuildTargetIdentifier("//included_target8.1")),
List.of(
new BuildTargetIdentifier("//excluded_target8.1"),
new BuildTargetIdentifier("//excluded_target8.2"))))
.bazelPath(Optional.of(new ProjectViewBazelPathSection(Paths.get("path8/to/bazel"))))
.debuggerAddress(
Optional.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
import java.util.Collection;
import java.util.List;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -72,9 +73,12 @@ public static Collection<Object[]> data() {
{
new ProjectViewTargetsSectionParser(),
(Function<String, String>) (seed) -> "//target:" + seed,
(BiFunction<List<String>, List<String>, ProjectViewTargetsSection>)
(BiFunction<
List<BuildTargetIdentifier>,
List<BuildTargetIdentifier>,
ProjectViewTargetsSection>)
ProjectViewTargetsSection::new,
(Function<String, String>) (rawElement) -> rawElement,
(Function<String, BuildTargetIdentifier>) BuildTargetIdentifier::new,
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.jetbrains.bsp.bazel.server.bsp.config;

import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import io.vavr.control.Try;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.jetbrains.bsp.bazel.projectview.model.ProjectView;
import org.jetbrains.bsp.bazel.projectview.model.ProjectViewProvider;
import org.jetbrains.bsp.bazel.projectview.model.sections.ProjectViewBazelPathSection;
Expand All @@ -24,10 +28,17 @@ public ServerArgsProjectViewProvider(Path bspProjectRoot, String pathToBazel, St
this.pathToBazel = new ProjectViewBazelPathSection(Paths.get(pathToBazel));
this.targets =
Optional.of(
new ProjectViewTargetsSection(Arrays.asList(targets.split(",")), ImmutableList.of()));
new ProjectViewTargetsSection(
calculateIncludedTargets(targets), List.<BuildTargetIdentifier>of()));
this.defaultParserProvider = new ProjectViewDefaultParserProvider(bspProjectRoot);
}

private List<BuildTargetIdentifier> calculateIncludedTargets(String targets) {
return Arrays.stream(targets.split(","))
.map(BuildTargetIdentifier::new)
.collect(Collectors.toList());
}

public ServerArgsProjectViewProvider(Path bspProjectRoot, String pathToBazel) {
this.pathToBazel = new ProjectViewBazelPathSection(Paths.get(pathToBazel));
this.targets = Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public BazelBspQueryManager(
}

public Either<ResponseError, WorkspaceBuildTargetsResult> getWorkspaceBuildTargets() {
List<String> projectInitTargets = projectView.getTargets().getIncludedValues();
var projectInitTargets = projectView.getTargets().getIncludedValues();

List<BuildTarget> targets =
projectInitTargets.stream()
.map(BuildTargetIdentifier::getUri)
.map(this::getBuildTargetForProjectPath)
.flatMap(Collection::stream)
.collect(Collectors.toList());
Expand Down
Loading

0 comments on commit 026b123

Please sign in to comment.