Skip to content

Commit

Permalink
#6370 Add line numbers to sections and text-blocks (#6371)
Browse files Browse the repository at this point in the history
* Closes #6370
Add line numbers to text blocks and sections.
Create removeAll() method to remove all the instances of an item regardless of the line number

* Revert local change

* Fix Android SDK Platform section

* Fix Android SDK Platform section
Add new section items with dummy line number

* Fix list parser test

* #6370 Fix parsing

* #6370
Remove redundant method
Add constant for temporary line numbers

* Update base/src/com/google/idea/blaze/base/projectview/ProjectView.java

Co-authored-by: Borja Lorente <[email protected]>

* #6370
Do not add line numbers on printing sections

* Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkPlatformSection.java

Co-authored-by: Borja Lorente <[email protected]>

* Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkPlatformSection.java

Co-authored-by: Borja Lorente <[email protected]>

* Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkPlatformSection.java

Co-authored-by: Borja Lorente <[email protected]>

* Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkPlatformSection.java

Co-authored-by: Borja Lorente <[email protected]>

* Update base/src/com/google/idea/blaze/base/projectview/ProjectView.java

Co-authored-by: Borja Lorente <[email protected]>

* Update base/src/com/google/idea/blaze/base/projectview/section/sections/DirectorySection.java

Co-authored-by: Borja Lorente <[email protected]>

* Update base/src/com/google/idea/blaze/base/projectview/section/sections/AutomaticallyDeriveTargetsSection.java

Co-authored-by: Borja Lorente <[email protected]>

* #6370
Remove line calculation from print() methods

* #6370
Remove redundant method

* #6265
Update comment

* Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkPlatformSection.java

Co-authored-by: Borja Lorente <[email protected]>

---------

Co-authored-by: Borja Lorente <[email protected]>
Co-authored-by: Ahmed Yarub Hani Al Nuaimi <[email protected]>
  • Loading branch information
3 people authored May 7, 2024
1 parent 023536e commit 560f899
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.idea.blaze.android.projectview;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -77,14 +78,15 @@ public ProjectView addProjectViewDefaultValue(
}
List<Sdk> sdks = BlazeSdkProvider.getInstance().getAllAndroidSdks();
ProjectView.Builder builder =
ProjectView.builder(topLevelProjectView).add(TextBlockSection.of(TextBlock.newLine()));
ProjectView.builder(topLevelProjectView).add(TextBlockSection.of(TextBlock.newLine(TEMPORARY_LINE_NUMBER)));

if (sdks.isEmpty()) {
builder
.add(TextBlockSection.of(TextBlock.of("# Please set to an android SDK platform")))
.add(TextBlockSection.of(TextBlock.of(TEMPORARY_LINE_NUMBER, "# Please set to an android SDK platform")))
.add(
TextBlockSection.of(
TextBlock.of(
TEMPORARY_LINE_NUMBER,
"# You currently have no SDKs. Please use the SDK manager first.")))
.add(ScalarSection.builder(KEY).set("(android sdk goes here)"));
} else if (sdks.size() == 1) {
Expand All @@ -94,13 +96,13 @@ public ProjectView addProjectViewDefaultValue(
} else {
builder.add(
TextBlockSection.of(
TextBlock.of("# Please uncomment an android-SDK platform. Available SDKs are:")));
TextBlock.of(TEMPORARY_LINE_NUMBER, "# Please uncomment an android-SDK platform. Available SDKs are:")));
List<String> sdkOptions =
AndroidSdkFromProjectView.getAvailableSdkTargetHashes(sdks)
.stream()
.map(androidSdk -> "# android_sdk_platform: " + androidSdk)
.collect(toList());
builder.add(TextBlockSection.of(new TextBlock(ImmutableList.copyOf(sdkOptions))));
builder.add(TextBlockSection.of(new TextBlock(ImmutableList.copyOf(sdkOptions), TEMPORARY_LINE_NUMBER)));
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ private void createDirs(String projectFilePath) {
private ProjectView defaultEmptyProjectView() {
Builder projectViewBuilder = ProjectView.builder();
projectViewBuilder.add(TextBlockSection.of(TextBlock.of(
1,
"# This is a projectview file generated automatically during bazel project auto-import ",
"# For more documentation, please visit https://ij.bazel.build/docs/project-views.html",
"# If your repository contains predefined .projectview files, you use 'import' directive to include them.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class ParseContext {

@Nullable private Line currentLine;
@Nullable private String currentRawLine;

public int getCurrentLineIndex() {
return currentLineIndex;
}

private int currentLineIndex;
private int savedPosition = -1;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

/** Parses and writes project views. */
public class ProjectViewParser {

public static final int TEMPORARY_LINE_NUMBER = -1; // On ly use if you are creating a ProjectView, and then you would parse the persisted string to generate a ProjectVew file with valid line numbers
private final BlazeContext context;
private final WorkspacePathResolver workspacePathResolver;
private final boolean recursive;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.stream.Collectors;
import javax.annotation.Nullable;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;

/**
* List value. Eg.
*
Expand All @@ -38,8 +40,10 @@ public final class ListSection<T> extends Section<T> {
private final ImmutableList<ItemOrTextBlock<T>> itemsOrComments;

ListSection(
SectionKey<T, ? extends ListSection<T>> sectionKey, ImmutableList<ItemOrTextBlock<T>> items) {
super(sectionKey);
SectionKey<T, ? extends ListSection<T>> sectionKey,
ImmutableList<ItemOrTextBlock<T>> items,
int firstLineIndex) {
super(sectionKey, firstLineIndex);
this.itemsOrComments = items;
}

Expand Down Expand Up @@ -88,16 +92,29 @@ public static <T> Builder<T> update(
public static class Builder<T> extends SectionBuilder<T, ListSection<T>> {
private final List<ItemOrTextBlock<T>> items = new ArrayList<>();

private int firstLineNumber = TEMPORARY_LINE_NUMBER;

public Builder(SectionKey<T, ListSection<T>> sectionKey, @Nullable ListSection<T> section) {
super(sectionKey);
if (section != null) {
items.addAll(section.itemsOrComments);
}
}

public Builder<T> setFirstLineNumber(int firstLineNumber) {
this.firstLineNumber = firstLineNumber;
return this;
}

@CanIgnoreReturnValue
public final Builder<T> add(T item) {
items.add(new ItemOrTextBlock<>(item));
items.add(new ItemOrTextBlock<>(item, TEMPORARY_LINE_NUMBER));
return this;
}

@CanIgnoreReturnValue
public final Builder<T> add(T item, int firstLineNumber) {
items.add(new ItemOrTextBlock<>(item, firstLineNumber));
return this;
}

Expand All @@ -111,7 +128,7 @@ public final Builder<T> addAll(List<? extends T> items) {

@CanIgnoreReturnValue
public final Builder<T> add(TextBlock textBlock) {
items.add(new ItemOrTextBlock<T>(textBlock));
items.add(new ItemOrTextBlock<T>(textBlock, textBlock.firstLineIndex));
return this;
}

Expand All @@ -122,14 +139,20 @@ public final Builder<T> removeMatches(Predicate<ItemOrTextBlock<T>> predicate) {
}

@CanIgnoreReturnValue
public final Builder<T> remove(T item) {
items.remove(new ItemOrTextBlock<>(item));
public final Builder<T> remove(T item, int lineIndex) {
items.remove(new ItemOrTextBlock<>(item, lineIndex));
return this;
}

@CanIgnoreReturnValue
public final Builder<T> removeAll(T item) {
items.removeIf(it -> it.item != null && it.item.equals(item));
return this;
}

@Override
public final ListSection<T> build() {
return new ListSection<>(getSectionKey(), ImmutableList.copyOf(items));
return new ListSection<>(getSectionKey(), ImmutableList.copyOf(items), firstLineNumber);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final ListSection<T> parse(ProjectViewParser parser, ParseContext parseCo
if (!parseContext.current().text.equals(name + ':')) {
return null;
}
int firstLineIndex = parseContext.getCurrentLineIndex();
parseContext.consume();

ImmutableList.Builder<ItemOrTextBlock<T>> builder = ImmutableList.builder();
Expand All @@ -62,15 +63,18 @@ public final ListSection<T> parse(ProjectViewParser parser, ParseContext parseCo
}
correctIndentationRun = isIndented;

int currentLineIndex = parseContext.getCurrentLineIndex(); // save the current line number just in case multiple lines are consumed
ItemOrTextBlock<T> itemOrTextBlock = null;
TextBlock textBlock = TextBlockSection.parseTextBlock(parseContext);

if (textBlock != null) {
itemOrTextBlock = new ItemOrTextBlock<>(textBlock);
// the line has already been consumed so we need the address of the previous line
itemOrTextBlock = new ItemOrTextBlock<>(textBlock, currentLineIndex);
} else if (isIndented) {
T item = parseItem(parser, parseContext);
if (item != null) {
itemOrTextBlock = new ItemOrTextBlock<>(item, parseContext.getCurrentLineIndex());
parseContext.consume();
itemOrTextBlock = new ItemOrTextBlock<>(item);
}
}

Expand All @@ -84,7 +88,7 @@ public final ListSection<T> parse(ProjectViewParser parser, ParseContext parseCo
savedTextBlocks.clear();
parseContext.clearSavedPosition();
} else {
savedTextBlocks.add(new ItemOrTextBlock<>(textBlock));
savedTextBlocks.add(new ItemOrTextBlock<>(textBlock, parseContext.getCurrentLineIndex() - 1)); // The line is already consumed
}
}
parseContext.resetToSavedPosition();
Expand All @@ -94,7 +98,7 @@ public final ListSection<T> parse(ProjectViewParser parser, ParseContext parseCo
parseContext.addError(String.format("Empty section: '%s'", name));
}

return new ListSection<>(key, items);
return new ListSection<>(key, items, firstLineIndex);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
import com.google.common.base.Objects;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;

/** Scalar value. */
public final class ScalarSection<T> extends Section<T> {
private static final long serialVersionUID = 1L;

private final T value;

public ScalarSection(SectionKey<T, ScalarSection<T>> sectionKey, T value) {
super(sectionKey);
public ScalarSection(SectionKey<T, ScalarSection<T>> sectionKey, T value, int startingLineIndex) {
super(sectionKey, startingLineIndex);
this.value = value;
}

Expand Down Expand Up @@ -73,7 +75,7 @@ public Builder<T> set(T value) {

@Override
public ScalarSection<T> build() {
return new ScalarSection<>(getSectionKey(), value);
return new ScalarSection<>(getSectionKey(), value, TEMPORARY_LINE_NUMBER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public final ScalarSection<T> parse(ProjectViewParser parser, ParseContext parse
String rest = line.text.substring(name.length() + 1).trim();
parseContext.consume();
T item = parseItem(parser, parseContext, rest);
return item != null ? new ScalarSection<>(key, item) : null;
return item != null ? new ScalarSection<>(key, item, parseContext.getCurrentLineIndex()) : null;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,22 @@
*/
public abstract class Section<T> implements Serializable {
private static final long serialVersionUID = 2L;

private int firstLineIndex;

private final String sectionName;

protected Section(SectionKey<T, ?> key) {
public int getFirstLineIndex() {
return firstLineIndex;
}

public void setFirstLineIndex(int firstLineIndex) {
this.firstLineIndex = firstLineIndex;
}

protected Section(SectionKey<T, ?> key, int firstLineIndex) {
this.sectionName = key.getName();
this.firstLineIndex = firstLineIndex;
}

public boolean isSectionType(SectionKey<?, ?> key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.Set;
import javax.annotation.Nullable;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;

/** Allows users to set the rule classes they want to be imported */
public class AdditionalLanguagesSection {
public static final SectionKey<LanguageClass, ListSection<LanguageClass>> KEY =
Expand Down Expand Up @@ -85,13 +87,13 @@ public ProjectView addProjectViewDefaultValue(
return topLevelProjectView;
}
ListSection.Builder<LanguageClass> builder = ListSection.builder(KEY);
builder.add(TextBlock.of(" # Uncomment any additional languages you want supported"));
builder.add(TextBlock.of(TEMPORARY_LINE_NUMBER, " # Uncomment any additional languages you want supported"));
additionalLanguages
.stream()
.sorted(Ordering.usingToString())
.map(lang -> " # " + lang.getName())
.forEach(string -> builder.add(TextBlock.of(string)));
builder.add(TextBlock.newLine());
.forEach(string -> builder.add(TextBlock.of(TEMPORARY_LINE_NUMBER, string)));
builder.add(TextBlock.newLine(TEMPORARY_LINE_NUMBER));
return ProjectView.builder(topLevelProjectView).add(builder).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.google.idea.blaze.base.projectview.section.SectionParser;
import com.google.idea.blaze.base.settings.BuildSystemName;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;

/** If set to true, automatically derives targets from the project directories. */
public class AutomaticallyDeriveTargetsSection {
public static final SectionKey<Boolean, ScalarSection<Boolean>> KEY =
Expand All @@ -45,10 +47,11 @@ public ProjectView addProjectViewDefaultValue(
.add(
TextBlockSection.of(
TextBlock.of(
TEMPORARY_LINE_NUMBER,
"# Automatically includes all relevant targets under the 'directories'"
+ " above")))
.add(ScalarSection.builder(KEY).set(true))
.add(TextBlockSection.of(TextBlock.newLine()))
.add(TextBlockSection.of(TextBlock.newLine(TEMPORARY_LINE_NUMBER)))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;

/** "directories" section. */
public class DirectorySection {
public static final SectionKey<DirectoryEntry, ListSection<DirectoryEntry>> KEY =
Expand Down Expand Up @@ -87,12 +89,12 @@ public ProjectView addProjectViewDefaultValue(
return topLevelProjectView;
}
ListSection.Builder<DirectoryEntry> builder = ListSection.builder(KEY);
builder.add(TextBlock.of(" # Add the directories you want added as source here"));
builder.add(TextBlock.of(TEMPORARY_LINE_NUMBER, " # Add the directories you want added as source here"));
if (buildSystemName == BuildSystemName.Bazel) {
builder.add(TextBlock.of(" # By default, we've added your entire workspace ('.')"));
builder.add(TextBlock.of(TEMPORARY_LINE_NUMBER, " # By default, we've added your entire workspace ('.')"));
builder.add(DirectoryEntry.include(new WorkspacePath(".")));
}
builder.add(TextBlock.newLine());
builder.add(TextBlock.newLine(TEMPORARY_LINE_NUMBER));
return ProjectView.builder(topLevelProjectView).add(builder).build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@ public class ItemOrTextBlock<T> implements Serializable {
@Nullable public final T item;
@Nullable public final TextBlock textBlock;

public ItemOrTextBlock(T item) {
this(item, null);
private int lineIndex;

public int getLineIndex() {
return lineIndex;
}

public void setLineIndex(int lineIndex) {
this.lineIndex = lineIndex;
}

public ItemOrTextBlock(T item, int lineIndex) {
this(item, null, lineIndex);
}

public ItemOrTextBlock(TextBlock comment) {
this(null, comment);
public ItemOrTextBlock(TextBlock comment, int lineIndex) {
this(null, comment, lineIndex);
}

private ItemOrTextBlock(@Nullable T item, @Nullable TextBlock comment) {
private ItemOrTextBlock(@Nullable T item, @Nullable TextBlock comment, int lineIndex) {
this.item = item;
this.textBlock = comment;
this.lineIndex = lineIndex;
}

@Override
Expand All @@ -47,7 +58,7 @@ public boolean equals(Object o) {
return false;
}
ItemOrTextBlock<?> that = (ItemOrTextBlock<?>) o;
return Objects.equal(item, that.item) && Objects.equal(textBlock, that.textBlock);
return Objects.equal(item, that.item) && Objects.equal(textBlock, that.textBlock) && Objects.equal(lineIndex, that.lineIndex);
}

@Override
Expand Down
Loading

0 comments on commit 560f899

Please sign in to comment.