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

Router: indent, handle comma in repeated enum cases #3057

Merged
merged 4 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -1484,25 +1484,25 @@ class Router(formatOps: FormatOps) {
)
}
}
splitsOpt.getOrElse {
if (!style.newlines.formatInfix && leftOwner.is[Term.ApplyInfix])
Seq(
// Do whatever the user did if infix.
Split(Space.orNL(newlines == 0), 0)
)
else {
val indent = leftOwner match {
case _: Defn.Val | _: Defn.Var =>
style.indent.getDefnSite(leftOwner)
case _ =>
0
}
def altSplits = leftOwner match {
case _: Term.ApplyInfix if !style.newlines.formatInfix =>
// Do whatever the user did if infix.
Seq(Split(Space.orNL(newlines == 0), 0))
case _: Defn.Val | _: Defn.Var =>
val indent = style.indent.getDefnSite(leftOwner)
Seq(
Split(Space, 0),
Split(Newline, 1).withIndent(indent, right, After)
)
}
case _: Defn.RepeatedEnumCase if {
if (!style.newlines.sourceIgnored) newlines != 0
else style.newlines.source eq Newlines.unfold
} =>
Seq(Split(Newline, 0))
case _ =>
Seq(Split(Space, 0), Split(Newline, 1))
}
splitsOpt.getOrElse(altSplits)
case FormatToken(_, T.Semicolon(), _) =>
Seq(
Split(NoSplit, 0)
Expand Down Expand Up @@ -2085,6 +2085,12 @@ class Router(formatOps: FormatOps) {
insideInfixSplit(app, formatToken)

// Case
case FormatToken(_: T.KwCase, _, _)
if leftOwner.is[Defn.RepeatedEnumCase] =>
val indent =
Indent(style.indent.main, leftOwner.tokens.last, ExpiresOn.After)
Seq(Split(Space, 0).withIndent(indent))

case FormatToken(_: T.KwCase, _, _) if leftOwner.is[CaseTree] =>
val owner = leftOwner.asInstanceOf[CaseTree]
val body = owner.body
Expand Down
27 changes: 27 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3312,3 +3312,30 @@ allMatching(versionString, partialFunctions)
allMatching(versionString,
partialFunctions
)(partialFunctions)
<<< #3056
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE, ORIGIN_NULL_RESOURCE
}
>>>
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE,
ORIGIN_NULL_RESOURCE
}
24 changes: 24 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3155,3 +3155,27 @@ allMatching(versionString, partialFunctions)
allMatching(versionString,
partialFunctions
)(partialFunctions)
<<< #3056
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE, ORIGIN_NULL_RESOURCE
}
>>>
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG, VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN, ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER, ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE,
ORIGIN_NULL_RESOURCE
}
27 changes: 27 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3301,3 +3301,30 @@ allMatching(versionString,
partialFunctions
)
(partialFunctions)
<<< #3056
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE, ORIGIN_NULL_RESOURCE
}
>>>
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE,
ORIGIN_NULL_RESOURCE
}
Original file line number Diff line number Diff line change
Expand Up @@ -3403,3 +3403,35 @@ allMatching
partialFunctions
)
(partialFunctions)
<<< #3056
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER, ORIGIN_TYPE, ORIGIN_URL,
ORIGIN_COMMENTS, ORIGIN_NULL_URL, ORIGIN_NULL_COMMENTS, ORIGIN_RESOURCE, ORIGIN_NULL_RESOURCE
}
>>>
enum SerializedField extends jl.Enum[SerializedField] {
case UNKNOWN, // represents a field code we didn't recognize
END_MARKER, // end of a list of fields
ROOT_VALUE, // Fields at the root
ROOT_WAS_CONFIG,
VALUE_DATA, // Fields that make up a value
VALUE_ORIGIN,
ORIGIN_DESCRIPTION, // Fields that make up an origin
ORIGIN_LINE_NUMBER,
ORIGIN_END_LINE_NUMBER,
ORIGIN_TYPE,
ORIGIN_URL,
ORIGIN_COMMENTS,
ORIGIN_NULL_URL,
ORIGIN_NULL_COMMENTS,
ORIGIN_RESOURCE,
ORIGIN_NULL_RESOURCE
}