Skip to content
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

#6370 Add line numbers to sections and text-blocks #6371

Merged
merged 30 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
17dee1f
Closes #6370
ahmedyarub Apr 7, 2024
17e14d0
Revert local change
ahmedyarub Apr 7, 2024
5f4a576
Fix Android SDK Platform section
ahmedyarub Apr 7, 2024
1b8c93c
Fix Android SDK Platform section
ahmedyarub Apr 7, 2024
28957ed
Fix list parser test
ahmedyarub Apr 7, 2024
9d5dbe2
#6370 Fix parsing
ahmedyarub Apr 7, 2024
09e8cea
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
ahmedyarub Apr 8, 2024
763bd00
#6370
ahmedyarub Apr 8, 2024
30ca35d
Update base/src/com/google/idea/blaze/base/projectview/ProjectView.java
ahmedyarub Apr 8, 2024
88f1366
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
ahmedyarub Apr 8, 2024
44ff380
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
ahmedyarub Apr 9, 2024
0190e70
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
ahmedyarub Apr 10, 2024
a828159
Merge remote-tracking branch 'ahmedyarub/ay/section_line_number' into…
ahmedyarub Apr 10, 2024
3f1665d
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
Apr 18, 2024
f7a18e3
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
Apr 19, 2024
10945c6
#6370
Apr 22, 2024
f4c21ce
Merge branch 'master' of https://github.com/bazelbuild/intellij into …
Apr 22, 2024
f409635
Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkP…
ahmedyarub Apr 23, 2024
ba6bd57
Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkP…
ahmedyarub Apr 23, 2024
9b0d15b
Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkP…
ahmedyarub Apr 23, 2024
0f710f9
Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkP…
ahmedyarub Apr 23, 2024
16161ba
Update base/src/com/google/idea/blaze/base/projectview/ProjectView.java
ahmedyarub Apr 23, 2024
111957c
Update base/src/com/google/idea/blaze/base/projectview/section/sectio…
ahmedyarub Apr 23, 2024
3b96874
Update base/src/com/google/idea/blaze/base/projectview/section/sectio…
ahmedyarub Apr 23, 2024
9cd006a
#6370
Apr 23, 2024
66a0f50
Merge remote-tracking branch 'ahmedyarub/ay/section_line_number' into…
Apr 23, 2024
a28e51f
#6370
Apr 23, 2024
9aa305e
#6265
Apr 23, 2024
2302d6a
Merge branch 'master' into ay/section_line_number
ahmedyarub Apr 25, 2024
64d085f
Update aswb/src/com/google/idea/blaze/android/projectview/AndroidSdkP…
ahmedyarub May 2, 2024
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 @@ -15,6 +15,8 @@
*/
package com.google.idea.blaze.android.projectview;

import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;
import static com.google.idea.blaze.base.projectview.parser.ProjectViewParser.TEMPORARY_LINE_NUMBER;
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
import static java.util.stream.Collectors.toList;

import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -77,14 +79,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(-1)));
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved

if (sdks.isEmpty()) {
builder
.add(TextBlockSection.of(TextBlock.of("# Please set to an android SDK platform")))
.add(TextBlockSection.of(TextBlock.of(-1, "# Please set to an android SDK platform")))
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
.add(
TextBlockSection.of(
TextBlock.of(
-1,
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
"# 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 +97,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), -1)));
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -146,6 +146,7 @@ public <T> Builder remove(Section<T> section) {
return this;
}


ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
/** Replaces a section if it already exists. If it doesn't, just add the section. */
@CanIgnoreReturnValue
public <T, SectionType extends Section<T>> Builder replace(
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 create a file using print() statements
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) {
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
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,32 @@ 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;
}

@CanIgnoreReturnValue
public final Builder<T> replaceElement(int lineIndex, T with) {
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getLineIndex() == lineIndex) {
items.set(i, new ItemOrTextBlock<>(with, lineIndex));
break;
}
}

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,14 @@ public ProjectView addProjectViewDefaultValue(
return topLevelProjectView;
}
ListSection.Builder<LanguageClass> builder = ListSection.builder(KEY);
builder.add(TextBlock.of(" # Uncomment any additional languages you want supported"));
// Line index is not known at build time which is supposed to be corrected by the print() operations
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
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(-1)))
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
.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(-1));
ahmedyarub marked this conversation as resolved.
Show resolved Hide resolved
return ProjectView.builder(topLevelProjectView).add(builder).build();
}

Expand Down
Loading