Skip to content

Commit

Permalink
Set end position to same line, expand test files
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell committed Apr 19, 2022
1 parent 16893c0 commit 7aa59d0
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 24 deletions.
33 changes: 28 additions & 5 deletions src/main/java/software/amazon/smithy/lsp/ext/SmithyProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ private static Map<String, List<Location>> collectLocations(Model model) {
.collect(Collectors.toList());
for (String modelFile : modelFiles) {
List<String> lines = getFileLines(modelFile);
int endMarker = lines.size();
int endMarker = getInitialEndMarker(lines);

// Get shapes reverse-sorted by source location to work from bottom of file to top.
// Get shapes reverse-sorted by source location to work from bottom of file to top.
List<Shape> shapes = model.shapes()
.filter(shape -> shape.getSourceLocation().getFilename().equals(modelFile))
// TODO: Once the change in https://github.com/awslabs/smithy/pull/1192 lands, replace with with
// `.sorted(Comparator.comparing(Shape::getSourceLocation).reversed())`
// `.sorted(Comparator.comparing(Shape::getSourceLocation).reversed())`.
.sorted(new SourceLocationSorter().reversed())
.collect(Collectors.toList());

Expand All @@ -184,7 +184,7 @@ private static Map<String, List<Location>> collectLocations(Model model) {
if (endMarker < sourceLocation.getLine()) {
endPosition = new Position(sourceLocation.getLine() - 1, sourceLocation.getColumn() - 1);
} else {
endPosition = new Position(endMarker, 0);
endPosition = getEndPosition(endMarker, lines);
}

// If a shape is not a member, move the end marker for setting the next shape location.
Expand Down Expand Up @@ -212,10 +212,33 @@ private static Map<String, List<Location>> collectLocations(Model model) {
return locations;
}

private static int getInitialEndMarker(List<String> lines) {
int endMarker = lines.size();
// Remove empty lines from the end of the file.
if (lines.size() > 0) {
while (lines.get(endMarker - 1).trim().equals("")) {
endMarker = endMarker - 1;
}
}
return endMarker;
}

// If the lines of the model were successfully loaded, return the end position of the actual shape line,
// otherwise set it to the start of the next line.
private static Position getEndPosition(int endMarker, List<String> lines) {
if (lines.size() >= endMarker) {
return new Position(endMarker - 1, lines.get(endMarker - 1).length());
}
return new Position(endMarker, 0);
}

private static List<String> getFileLines(String file) {
try {
if (Utils.isSmithyJarFile(file) || Utils.isJarFile(file)) {
return Utils.jarFileContents(file);
List<String> jarFileContents = Utils.jarFileContents(file);
LspLog.println(jarFileContents);
return jarFileContents;
//return Utils.jarFileContents(file);
} else {
return Files.readAllLines(Paths.get(file));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,20 @@ public void definitionLocations() throws Exception {
try (Harness hs = Harness.create(SmithyBuildExtensions.builder().build(), modelFiles)) {
Map<String, List<Location>> locations = hs.getProject().getLocations();

correctLocation(locations, "SingleLine", 4, 5);
correctLocation(locations, "MultiLine", 6, 11);
correctLocation(locations, "SingleTrait", 13, 14);
correctLocation(locations, "MultiTrait", 17, 20);
correctLocation(locations, "MultiTraitAndLineComments", 33, 36);
correctLocation(locations,"MultiTraitAndDocComments", 41, 44);
correctLocation(locations, "OtherStructure", 4, 9);
correctLocation(locations, "SingleLine", 4, 0, 4, 23);
correctLocation(locations, "MultiLine", 6, 8,10, 9);
correctLocation(locations, "SingleTrait", 13, 0, 13, 18);
correctLocation(locations, "MultiTrait", 17, 0,18, 14);
correctLocation(locations, "MultiTraitAndLineComments", 32, 0,34, 1);
correctLocation(locations,"MultiTraitAndDocComments", 43, 0,45, 1);
correctLocation(locations, "OtherStructure", 4, 0, 8, 1);
}
}

private void correctLocation(Map<String, List<Location>> locations, String shapeName, int start, int end) {
private void correctLocation(Map<String, List<Location>> locations, String shapeName, int startLine,
int startColumn, int endLine, int endColumn) {
Location location = locations.get(shapeName).get(0);
Range range = new Range(new Position(start, 0), new Position(end, 0));
Range range = new Range(new Position(startLine, startColumn), new Position(endLine, endColumn));
assertEquals(range, location.getRange());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@ namespace example.foo

structure SingleLine {}

structure MultiLine {
a: String,
b: String,
c: String
}
structure MultiLine {
a: String,
b: String,
c: String
}

@pattern("^[A-Za-z0-9 ]+$")
@pattern("^[A-Za-z0-9 ]+$")
string SingleTrait

@input
@error("client")
structure MultiTrait {
a: String
}
a: String}

// Line comments
// comments
@input
// comments
@error("client")
// comments
@error("client")
@references(
[
{
Expand All @@ -35,6 +34,9 @@ structure MultiTraitAndLineComments {
a: String
}




/// Doc comments
/// comments
@input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ structure OtherStructure {
bar: String,
baz: Integer
}



0 comments on commit 7aa59d0

Please sign in to comment.