Skip to content

Commit

Permalink
Fix indentation when formatting text blocks
Browse files Browse the repository at this point in the history
Updates the formatter to indent the node value of a node kvp when
they are text blocks.

A test case was added for a text block as the node value of a node
kvp, and a test case was updated for when text blocks are in traits.
  • Loading branch information
milesziemer committed Jul 21, 2023
1 parent 1fdd79b commit 69209ea
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import com.opencastsoftware.prettier4j.Doc;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.smithy.model.loader.ModelSyntaxException;
import software.amazon.smithy.model.shapes.ShapeId;
Expand Down Expand Up @@ -178,6 +180,7 @@ private Doc visit(TreeCursor cursor) {
case APPLY_STATEMENT:
case NODE_VALUE:
case NODE_KEYWORD:
case NODE_STRING_VALUE:
case SIMPLE_TYPE_NAME:
case ENUM_TYPE_NAME:
case AGGREGATE_TYPE_NAME:
Expand Down Expand Up @@ -408,12 +411,25 @@ private Doc visit(TreeCursor cursor) {
}

case NODE_OBJECT_KVP: {
// Since text blocks span multiple lines, when they are the NODE_VALUE for NODE_OBJECT_KVP,
// they have to be indented. Since we only format valid models, NODE_OBJECT_KVP is guaranteed to
// have a NODE_VALUE child.
TreeCursor nodeValue = cursor.getFirstChild(TreeType.NODE_VALUE);
boolean isTextBlock = Optional.ofNullable(nodeValue.getFirstChild(TreeType.NODE_STRING_VALUE))
.map(nodeString -> nodeString.getFirstChild(TreeType.TEXT_BLOCK))
.isPresent();
Doc nodeValueDoc = visit(nodeValue);
if (isTextBlock) {
nodeValueDoc = nodeValueDoc.indent(4);
}


// Hoist awkward comments in the KVP *before* the KVP rather than between the values and colon.
// If there is an awkward comment before the TRAIT value, hoist it above the statement.
return skippedComments(cursor, false)
.append(visit(cursor.getFirstChild(TreeType.NODE_OBJECT_KEY)))
.append(Doc.text(": "))
.append(visit(cursor.getFirstChild(TreeType.NODE_VALUE)));
.append(nodeValueDoc);
}

case NODE_OBJECT_KEY: {
Expand All @@ -427,9 +443,18 @@ private Doc visit(TreeCursor cursor) {
: Doc.text(tree.concatTokens());
}

case TEXT_BLOCK: {
// Dispersing the lines of the text block preserves any indentation applied from formatting parent
// nodes.
List<Doc> lines = Arrays.stream(tree.concatTokens()
.split(System.lineSeparator()))
.map(String::trim)
.map(Doc::text)
.collect(Collectors.toList());
return Doc.intersperse(Doc.line(), lines);
}

case TOKEN:
case TEXT_BLOCK:
case NODE_STRING_VALUE:
case QUOTED_TEXT:
case NUMBER:
case SHAPE_ID:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$version: "2.0"

metadata validators = [
{
name: "EmitEachSelector"
id: "MissingStringInputLengthValidation"
severity: "DANGER"
message: "This string is missing required length trait"
configuration: {
selector: """
operation -[input]-> structure > member
:test(member > string:not([trait|enum]))
:test(member > string:not([trait|length]))
:test(member > string:not([trait|aws.api#arnReference]))
:test(member > string:not([trait|aws.api#providesPassRole]))
"""
}
}
]

namespace smithy.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
$version: "2.0"

metadata validators = [
{
name: "EmitEachSelector"
id: "MissingStringInputLengthValidation"
severity: "DANGER"
message: "This string is missing required length trait"
configuration: {
selector: """
operation -[input]-> structure > member
:test(member > string:not([trait|enum]))
:test(member > string:not([trait|length]))
:test(member > string:not([trait|aws.api#arnReference]))
:test(member > string:not([trait|aws.api#providesPassRole]))
"""
}
}
]

namespace smithy.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$version: "2.0"

namespace smithy.example

@documentation(
"""
This is the documentation for Foo.
Lorem ipsum dolor."""
)
string Foo

@documentation(
"""
This is the documentation for Bar.
Lorem ipsum dolor.
"""
)
string Bar

@documentation(
"""
This is the documentation for Baz.
Lorem ipsum dolor.
"""
)
string Baz
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ string Foo
"""
)
string Bar

@documentation(
"""
This is the documentation for Baz.
Lorem ipsum dolor.
"""
)
string Baz

0 comments on commit 69209ea

Please sign in to comment.