diff --git a/Package.swift b/Package.swift index 35e9da443..1b4d88877 100644 --- a/Package.swift +++ b/Package.swift @@ -103,6 +103,7 @@ var targets: [PackageDescription.Target] = [ dependencies: [ .target(name: "Configuration"), .product(name: "SwiftSyntax", package: "swift-syntax"), + .product(name: "SystemPackage", package: "swift-system"), .target(name: "Shared"), ] ), diff --git a/Sources/BUILD.bazel b/Sources/BUILD.bazel index 0c4263aa4..ceecc10de 100644 --- a/Sources/BUILD.bazel +++ b/Sources/BUILD.bazel @@ -96,6 +96,7 @@ swift_library( "//Sources:Configuration", "//Sources:Shared", "@swift-syntax//:SwiftSyntax", + "@swift-system//:SystemPackage", ], ) diff --git a/Sources/PeripheryKit/Results/OutputDeclarationFilter.swift b/Sources/PeripheryKit/Results/OutputDeclarationFilter.swift index f85b7ea39..efbf8a563 100644 --- a/Sources/PeripheryKit/Results/OutputDeclarationFilter.swift +++ b/Sources/PeripheryKit/Results/OutputDeclarationFilter.swift @@ -46,20 +46,9 @@ public final class OutputDeclarationFilter { var path = $0.declaration.location.file.path // If the declaration has a location override, use it as the path for filtering. - for command in $0.declaration.commentCommands { - switch command { - case let .override(overrides): - for override in overrides { - switch override { - case let .location(overridePath, _, _): - path = FilePath(overridePath) - default: - break - } - } - default: - break - } + if let override = $0.declaration.commentCommands.locationOverride { + let (overridePath, _, _) = override + path = FilePath(overridePath) } if configuration.reportIncludeMatchers.isEmpty { diff --git a/Sources/PeripheryKit/Results/OutputFormatter.swift b/Sources/PeripheryKit/Results/OutputFormatter.swift index 925022c55..cfd5b4679 100644 --- a/Sources/PeripheryKit/Results/OutputFormatter.swift +++ b/Sources/PeripheryKit/Results/OutputFormatter.swift @@ -95,20 +95,8 @@ extension OutputFormatter { func declarationKind(from declaration: Declaration) -> String { var kind = declaration.kind.rawValue - for command in declaration.commentCommands { - switch command { - case let .override(overrides): - for override in overrides { - switch override { - case let .kind(overrideKind): - kind = overrideKind - default: - break - } - } - default: - break - } + if let overrideKind = declaration.commentCommands.kindOverride { + kind = overrideKind } return kind @@ -117,20 +105,8 @@ extension OutputFormatter { func declarationKindDisplayName(from declaration: Declaration) -> String { var kind = declaration.kind.displayName - for command in declaration.commentCommands { - switch command { - case let .override(overrides): - for override in overrides { - switch override { - case let .kind(overrideKind): - kind = overrideKind - default: - break - } - } - default: - break - } + if let overrideKind = declaration.commentCommands.kindOverride { + kind = overrideKind } return kind @@ -139,22 +115,11 @@ extension OutputFormatter { func declarationLocation(from declaration: Declaration) -> Location { var location = declaration.location - for command in declaration.commentCommands { - switch command { - case let .override(overrides): - for override in overrides { - switch override { - case let .location(file, line, column): - let sourceFile = SourceFile(path: FilePath(String(file)), modules: []) - let overrideLocation = Location(file: sourceFile, line: line, column: column) - location = overrideLocation - default: - break - } - } - default: - break - } + if let override = declaration.commentCommands.locationOverride { + let (path, line, column) = override + let sourceFile = SourceFile(path: FilePath(path), modules: []) + let overrideLocation = Location(file: sourceFile, line: line, column: column) + location = overrideLocation } return location diff --git a/Sources/SourceGraph/Elements/CommentCommand.swift b/Sources/SourceGraph/Elements/CommentCommand.swift index cec98c715..bea3baf84 100644 --- a/Sources/SourceGraph/Elements/CommentCommand.swift +++ b/Sources/SourceGraph/Elements/CommentCommand.swift @@ -35,3 +35,45 @@ public enum CommentCommand: CustomStringConvertible, Hashable { } } } + +public extension Sequence { + var locationOverride: (String, Int, Int)? { + for command in self { + switch command { + case let .override(overrides): + for override in overrides { + switch override { + case let .location(path, line, column): + return (path, line, column) + default: + break + } + } + default: + break + } + } + + return nil + } + + var kindOverride: String? { + for command in self { + switch command { + case let .override(overrides): + for override in overrides { + switch override { + case let .kind(kind): + return kind + default: + break + } + } + default: + break + } + } + + return nil + } +} diff --git a/Sources/SourceGraph/Elements/Declaration.swift b/Sources/SourceGraph/Elements/Declaration.swift index 51079c874..3f3e17505 100644 --- a/Sources/SourceGraph/Elements/Declaration.swift +++ b/Sources/SourceGraph/Elements/Declaration.swift @@ -1,4 +1,5 @@ import Foundation +import SystemPackage public final class Declaration { public enum Kind: String, RawRepresentable, CaseIterable { @@ -339,11 +340,26 @@ extension Declaration: CustomStringConvertible { extension Declaration: Comparable { public static func < (lhs: Declaration, rhs: Declaration) -> Bool { - if lhs.location == rhs.location { + var lhsLocation = lhs.location + var rhsLocation = rhs.location + + if let locationOverride = lhs.commentCommands.locationOverride { + let (path, line, column) = locationOverride + let sourceFile = SourceFile(path: FilePath(path), modules: []) + lhsLocation = Location(file: sourceFile, line: line, column: column) + } + + if let locationOverride = rhs.commentCommands.locationOverride { + let (path, line, column) = locationOverride + let sourceFile = SourceFile(path: FilePath(path), modules: []) + rhsLocation = Location(file: sourceFile, line: line, column: column) + } + + if lhsLocation == rhsLocation { return lhs.usrs.sorted().joined() < rhs.usrs.sorted().joined() } - return lhs.location < rhs.location + return lhsLocation < rhsLocation } }