Skip to content

Commit

Permalink
simplify location formatter code (fixes some bugs)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luro02 committed Feb 23, 2025
1 parent 05751ea commit 0a14f0b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 159 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,7 @@ public String format() {
// if all locations are in the same file, it is unnecessary to display the filename:
// File:(L1, L2, L3) -> L1, L2, L3
if (segment.elements().isEmpty()) {
return segment.toString(new DelegatingPathFormatter(formatter) {
@Override
public String formatFile(String name, List<Location> locations) {
return locations.stream().map(this::formatLocation).collect(Collectors.joining(", "));
}
});
return segment.toString(formatter.showFilePath(false));
}

return segment.elements().stream()
Expand All @@ -190,7 +185,7 @@ public String formatFile(String name, List<Location> locations) {
}

private PathFormatter getActualPathFormatter() {
PathFormatter pathFormatter = new DefaultPathFormatter() {
return new PathFormatter() {
@Override
public String formatLocation(Location location) {
if (LocationFormatter.this.locationToString == null) {
Expand All @@ -199,29 +194,22 @@ public String formatLocation(Location location) {

return LocationFormatter.this.locationToString.apply(location);
}
};

if (this.shouldMergeLines) {
pathFormatter = new LocationMergingPathFormatter(pathFormatter);
}

if (this.shouldRemoveExtension) {
// this removes the extension from the filename:
return new DelegatingPathFormatter(pathFormatter) {
@Override
public String formatFile(String name, List<Location> locations) {
@Override
public String formatFile(String name, List<Location> locations) {
if (LocationFormatter.this.shouldRemoveExtension) {
return super.formatFile(getFilenameWithoutExtension(name), locations);
}

private static String getFilenameWithoutExtension(String path) {
String[] parts = path.split("[\\\\\\/]");
String file = parts[parts.length - 1];
return super.formatFile(name, locations);
}

return file.split("\\.")[0];
}
};
}
private static String getFilenameWithoutExtension(String path) {
String[] parts = path.split("[\\\\\\/]");
String file = parts[parts.length - 1];

return pathFormatter;
return file.split("\\.")[0];
}
}.shouldMergeLines(this.shouldMergeLines);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,113 @@
/* Licensed under EPL-2.0 2025. */
package edu.kit.kastel.sdq.artemis4j.grading.location;

import java.util.ArrayList;
import java.util.List;
import java.util.SequencedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

interface PathFormatter {
String formatLocation(Location location);
class PathFormatter {
private boolean shouldMergeLines;
private boolean showFilePath;

String formatFile(String name, List<Location> locations);
PathFormatter() {
this.shouldMergeLines = false;
this.showFilePath = true;

String formatFolder(String name, List<String> segments);
}

PathFormatter shouldMergeLines(boolean shouldMergeLines) {
this.shouldMergeLines = shouldMergeLines;
return this;
}

PathFormatter showFilePath(boolean showFilePath) {
this.showFilePath = showFilePath;
return this;
}

private static List<Location> mergeLocations(List<Location> locations) {
// merges locations in the format L\d+(-\d+)?
if (locations.size() == 1) {
return locations;
}

// NOTE: lines are 0-indexed
SequencedSet<Integer> lines = new TreeSet<>();

for (var location : locations) {
int start = location.start().line();
lines.add(start);
int end = location.end().line();

if (start != end) {
for (int i = start + 1; i <= end; i++) {
lines.add(i);
}
}
}

// L1, L2, L3, L5, L6, L7 can be merged to L1-7
List<Location> result = new ArrayList<>();
while (!lines.isEmpty()) {
int start = lines.getFirst();
int end = start;
// this merges the lines that are consecutive
while (lines.remove(end + 1)) {
end += 1;
}

// the filepath is different for the merged locations, so it is left empty
// (does not matter, because the file path is indicated through the location of the parent PathSegment)
result.add(new Location("", start, end));
lines.remove(start);
}

return result;
}

public String formatLocation(Location location) {
LineColumn start = location.start();
LineColumn end = location.end();

if (start.line() == end.line()) {
return "L%d".formatted(start.line() + 1);
}

return "L%d-%d".formatted(start.line() + 1, end.line() + 1);
}

public String formatFile(String name, List<Location> locations) {
List<Location> mergedLocations = locations;
if (this.shouldMergeLines) {
mergedLocations = mergeLocations(locations);
}

String result = mergedLocations.stream().map(this::formatLocation).collect(Collectors.joining(", "));
if (mergedLocations.size() > 1 && this.showFilePath) {
result = "(%s)".formatted(result);
}

if (this.showFilePath) {
return name + ":" + result;
}

return result;
}

String formatFolder(String name, List<String> segments) {
if (segments.isEmpty()) {
return name;
}

// if the segment has elements, display the name and the elements
String result = name + "/";

if (segments.size() == 1) {
return result + segments.getFirst();
}

return "%s(%s)".formatted(result, String.join(", ", segments));
}
}

0 comments on commit 0a14f0b

Please sign in to comment.