Skip to content

Commit

Permalink
Merge 626b6e4 into a9e6d05
Browse files Browse the repository at this point in the history
  • Loading branch information
calda committed Oct 7, 2020
2 parents a9e6d05 + 626b6e4 commit 9ee62e3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 25 deletions.
30 changes: 20 additions & 10 deletions Sources/ParsingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1067,21 +1067,31 @@ extension Formatter {
$0.string == keyword
}) else { return nil }

guard var typeNameIndex = openingFormatter.index(of: .identifier, after: keywordIndex) else {
guard let typeNameIndex = openingFormatter.index(of: .identifier, after: keywordIndex) else {
return nil
}

// If the identifier is followed by a dot, it's actually the first
// part of the fully-qualified name and we should skip through
// to the last component of the name.
while openingFormatter.token(at: typeNameIndex + 1)?.string == ".",
openingFormatter.token(at: typeNameIndex + 2)?.is(.identifier) == true
{
typeNameIndex += 2
}
return openingFormatter.fullyQualifiedName(startingAt: typeNameIndex).name
}
}

return openingFormatter.token(at: typeNameIndex)?.string
/// The fully qualified name starting at the given index
func fullyQualifiedName(startingAt index: Int) -> (name: String, endIndex: Int) {
// If the identifier is followed by a dot, it's actually the first
// part of the fully-qualified name and we should skip through
// to the last component of the name.
var name = tokens[index].string
var index = index

while token(at: index + 1)?.string == ".",
let nextIdentifier = token(at: index + 2),
nextIdentifier.is(.identifier) == true
{
name = "\(name).\(nextIdentifier.string)"
index += 2
}

return (name, index)
}

// get type of declaration starting at index of declaration keyword
Expand Down
12 changes: 5 additions & 7 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5165,12 +5165,10 @@ public struct _FormatRules {
while let token = openingFormatter.token(at: conformanceSearchIndex),
conformanceSearchIndex < endOfConformances
{
if token.isIdentifier,
// Ignore identifiers followed by dots, which are components
// of a fully-qualified name but not the protocol name itself.
openingFormatter.token(at: conformanceSearchIndex + 1)?.string != "."
{
conformances.append(token.string)
if token.isIdentifier {
let (fullyQualifiedName, next) = openingFormatter.fullyQualifiedName(startingAt: conformanceSearchIndex)
conformances.append(fullyQualifiedName)
conformanceSearchIndex = next
}

conformanceSearchIndex += 1
Expand All @@ -5182,7 +5180,7 @@ public struct _FormatRules {

// If the type being extended was defined further up in this same file,
// it would be repetitive to include the type name in the scope name for this extension.
if declarations[..<index].contains(where: { $0.name == typeName }) {
if declarations[..<index].contains(where: { $0.name == typeName && $0.keyword != "extension" }) {
scopeName = "\(conformances.joined(separator: ", "))"
} else {
scopeName = "\(typeName) + \(conformances.joined(separator: ", "))"
Expand Down
32 changes: 24 additions & 8 deletions Tests/RulesTests+Organization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1836,18 +1836,15 @@ extension RulesTests {

func testFullyQualifiedTypeNames() {
let input = """
struct Foo {}
extension MyModule.Foo: MyModule.MyNamespace.BarProtocol {}
extension MyModule.Foo: MyModule.MyNamespace.BarProtocol, QuuxProtocol {}
extension MyModule.Foo {}
"""

let output = """
// MARK: - Foo
struct Foo {}
// MARK: BarProtocol
// MARK: MyModule.Foo + MyModule.MyNamespace.BarProtocol, QuuxProtocol
extension MyModule.Foo: MyModule.MyNamespace.BarProtocol {}
extension MyModule.Foo: MyModule.MyNamespace.BarProtocol, QuuxProtocol {}
extension MyModule.Foo {}
"""

testFormatting(for: input, output, rule: FormatRules.markTypes)
Expand Down Expand Up @@ -1997,4 +1994,23 @@ extension RulesTests {

testFormatting(for: input, rule: FormatRules.markTypes)
}

func testMultipleExtensionsOfSameType() {
let input = """
extension Foo: BarProtocol {}
extension Foo: QuuxProtocol {}
"""

let output = """
// MARK: Foo + BarProtocol
extension Foo: BarProtocol {}
// MARK: Foo + QuuxProtocol
extension Foo: QuuxProtocol {}
"""

testFormatting(for: input, output, rule: FormatRules.markTypes)
}
}
1 change: 1 addition & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,7 @@ extension RulesTests {
("testMultilineSwitchCases", testMultilineSwitchCases),
("testMultipleAttributesNotSeparated", testMultipleAttributesNotSeparated),
("testMultipleClosureArgumentUnwrapped", testMultipleClosureArgumentUnwrapped),
("testMultipleExtensionsOfSameType", testMultipleExtensionsOfSameType),
("testMultipleNestedClosures", testMultipleNestedClosures),
("testNamedClosureArgumentNotMadeTrailing", testNamedClosureArgumentNotMadeTrailing),
("testNamespacedVoidLiteralNotConverted", testNamespacedVoidLiteralNotConverted),
Expand Down

0 comments on commit 9ee62e3

Please sign in to comment.