|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | import IndexStoreDB
|
| 14 | +import LSPLogging |
14 | 15 | import LanguageServerProtocol
|
| 16 | +import SwiftSyntax |
15 | 17 |
|
16 | 18 | fileprivate extension SymbolOccurrence {
|
17 | 19 | /// Assuming that this is a symbol occurrence returned by the index, return whether it can constitute the definition
|
@@ -53,11 +55,140 @@ extension SourceKitLSPServer {
|
53 | 55 | for: req.textDocument.uri,
|
54 | 56 | language: snapshot.language
|
55 | 57 | )
|
56 |
| - let testSymbols = workspace.index?.unitTests(referencedByMainFiles: [mainFileUri.pseudoPath]) ?? [] |
57 |
| - return |
58 |
| - testSymbols |
59 |
| - .filter { $0.canBeTestDefinition } |
60 |
| - .sorted() |
61 |
| - .map(WorkspaceSymbolItem.init) |
| 58 | + if let index = workspace.index { |
| 59 | + var outOfDateChecker = IndexOutOfDateChecker() |
| 60 | + let testSymbols = |
| 61 | + index.unitTests(referencedByMainFiles: [mainFileUri.pseudoPath]) |
| 62 | + .filter { $0.canBeTestDefinition && outOfDateChecker.isUpToDate($0.location) } |
| 63 | + |
| 64 | + if !testSymbols.isEmpty { |
| 65 | + return testSymbols.sorted().map(WorkspaceSymbolItem.init) |
| 66 | + } |
| 67 | + if outOfDateChecker.indexHasUpToDateUnit(for: mainFileUri.pseudoPath, index: index) { |
| 68 | + // The index is up-to-date and doesn't contain any tests. We don't need to do a syntactic fallback. |
| 69 | + return [] |
| 70 | + } |
| 71 | + } |
| 72 | + // We don't have any up-to-date index entries for this file. Syntactically look for tests. |
| 73 | + return try await languageService.syntacticDocumentTests(for: req.textDocument.uri) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Scans a source file for `XCTestCase` classes and test methods. |
| 78 | +/// |
| 79 | +/// The syntax visitor scans from class and extension declarations that could be `XCTestCase` classes or extensions |
| 80 | +/// thereof. It then calls into `findTestMethods` to find the actual test methods. |
| 81 | +private final class SyntacticSwiftXCTestScanner: SyntaxVisitor { |
| 82 | + /// The document snapshot of the syntax tree that is being walked. |
| 83 | + private var snapshot: DocumentSnapshot |
| 84 | + |
| 85 | + /// The workspace symbols representing the found `XCTestCase` subclasses and test methods. |
| 86 | + private var result: [WorkspaceSymbolItem] = [] |
| 87 | + |
| 88 | + /// Names of classes that are known to not inherit from `XCTestCase` and can thus be ruled out to be test classes. |
| 89 | + private static let knownNonXCTestSubclasses = ["NSObject"] |
| 90 | + |
| 91 | + private init(snapshot: DocumentSnapshot) { |
| 92 | + self.snapshot = snapshot |
| 93 | + super.init(viewMode: .fixedUp) |
| 94 | + } |
| 95 | + |
| 96 | + public static func findTestSymbols( |
| 97 | + in snapshot: DocumentSnapshot, |
| 98 | + syntaxTreeManager: SyntaxTreeManager |
| 99 | + ) async -> [WorkspaceSymbolItem] { |
| 100 | + let syntaxTree = await syntaxTreeManager.syntaxTree(for: snapshot) |
| 101 | + let visitor = SyntacticSwiftXCTestScanner(snapshot: snapshot) |
| 102 | + visitor.walk(syntaxTree) |
| 103 | + return visitor.result |
| 104 | + } |
| 105 | + |
| 106 | + private func findTestMethods(in members: MemberBlockItemListSyntax, containerName: String) -> [WorkspaceSymbolItem] { |
| 107 | + return members.compactMap { (member) -> WorkspaceSymbolItem? in |
| 108 | + guard let function = member.decl.as(FunctionDeclSyntax.self) else { |
| 109 | + return nil |
| 110 | + } |
| 111 | + guard function.name.text.starts(with: "test") else { |
| 112 | + return nil |
| 113 | + } |
| 114 | + guard function.modifiers.map(\.name.tokenKind).allSatisfy({ $0 != .keyword(.static) && $0 != .keyword(.class) }) |
| 115 | + else { |
| 116 | + // Test methods can't be static. |
| 117 | + return nil |
| 118 | + } |
| 119 | + guard function.signature.returnClause == nil else { |
| 120 | + // Test methods can't have a return type. |
| 121 | + // Technically we are also filtering out functions that have an explicit `Void` return type here but such |
| 122 | + // declarations are probably less common than helper functions that start with `test` and have a return type. |
| 123 | + return nil |
| 124 | + } |
| 125 | + guard let position = snapshot.position(of: function.name.positionAfterSkippingLeadingTrivia) else { |
| 126 | + logger.fault( |
| 127 | + "Failed to convert offset \(function.name.positionAfterSkippingLeadingTrivia.utf8Offset) to UTF-16-based position" |
| 128 | + ) |
| 129 | + return nil |
| 130 | + } |
| 131 | + let symbolInformation = SymbolInformation( |
| 132 | + name: function.name.text, |
| 133 | + kind: .method, |
| 134 | + location: Location(uri: snapshot.uri, range: Range(position)), |
| 135 | + containerName: containerName |
| 136 | + ) |
| 137 | + return WorkspaceSymbolItem.symbolInformation(symbolInformation) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind { |
| 142 | + guard let inheritedTypes = node.inheritanceClause?.inheritedTypes, let superclass = inheritedTypes.first else { |
| 143 | + // The class has no superclass and thus can't inherit from XCTestCase. |
| 144 | + // Continue scanning its children in case it has a nested subclass that inherits from XCTestCase. |
| 145 | + return .visitChildren |
| 146 | + } |
| 147 | + if let superclassIdentifier = superclass.type.as(IdentifierTypeSyntax.self), |
| 148 | + Self.knownNonXCTestSubclasses.contains(superclassIdentifier.name.text) |
| 149 | + { |
| 150 | + // We know that the class can't be an subclass of `XCTestCase` so don't visit it. |
| 151 | + // We can't explicitly check for the `XCTestCase` superclass because the class might inherit from a class that in |
| 152 | + // turn inherits from `XCTestCase`. Resolving that inheritance hierarchy would be semantic. |
| 153 | + return .visitChildren |
| 154 | + } |
| 155 | + let testMethods = findTestMethods(in: node.memberBlock.members, containerName: node.name.text) |
| 156 | + guard !testMethods.isEmpty else { |
| 157 | + // Don't report a test class if it doesn't contain any test methods. |
| 158 | + return .visitChildren |
| 159 | + } |
| 160 | + guard let position = snapshot.position(of: node.name.positionAfterSkippingLeadingTrivia) else { |
| 161 | + logger.fault( |
| 162 | + "Failed to convert offset \(node.name.positionAfterSkippingLeadingTrivia.utf8Offset) to UTF-16-based position" |
| 163 | + ) |
| 164 | + return .visitChildren |
| 165 | + } |
| 166 | + let testClassSymbolInformation = SymbolInformation( |
| 167 | + name: node.name.text, |
| 168 | + kind: .class, |
| 169 | + location: Location(uri: snapshot.uri, range: Range(position)), |
| 170 | + containerName: nil |
| 171 | + ) |
| 172 | + result.append(.symbolInformation(testClassSymbolInformation)) |
| 173 | + result += testMethods |
| 174 | + return .visitChildren |
| 175 | + } |
| 176 | + |
| 177 | + override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind { |
| 178 | + result += findTestMethods(in: node.memberBlock.members, containerName: node.extendedType.trimmedDescription) |
| 179 | + return .visitChildren |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +extension SwiftLanguageService { |
| 184 | + public func syntacticDocumentTests(for uri: DocumentURI) async throws -> [WorkspaceSymbolItem]? { |
| 185 | + let snapshot = try documentManager.latestSnapshot(uri) |
| 186 | + return await SyntacticSwiftXCTestScanner.findTestSymbols(in: snapshot, syntaxTreeManager: syntaxTreeManager) |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +extension ClangLanguageService { |
| 191 | + public func syntacticDocumentTests(for uri: DocumentURI) async -> [WorkspaceSymbolItem]? { |
| 192 | + return nil |
62 | 193 | }
|
63 | 194 | }
|
0 commit comments