Skip to content

Commit 05d8d02

Browse files
author
Wang Lun
committed
fixed various test issues, format
1 parent b372182 commit 05d8d02

File tree

2 files changed

+56
-69
lines changed

2 files changed

+56
-69
lines changed

Sources/SourceKitLSP/SourceKitLSPServer.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,7 @@ extension SourceKitLSPServer {
18541854
// For module symbols, prefer using systemModule information if available
18551855
let moduleName: String
18561856
let groupName: String?
1857-
1857+
18581858
if let systemModule = symbol.systemModule {
18591859
moduleName = systemModule.moduleName
18601860
groupName = systemModule.groupName
@@ -1864,7 +1864,7 @@ extension SourceKitLSPServer {
18641864
} else {
18651865
return []
18661866
}
1867-
1867+
18681868
let interfaceLocation = try await self.definitionInInterface(
18691869
moduleName: moduleName,
18701870
groupName: groupName,
@@ -2067,10 +2067,12 @@ extension SourceKitLSPServer {
20672067
) async throws -> Location {
20682068
// Check if we're already in the target interface with the same module/group/symbol
20692069
if case .generatedInterface(let interfaceData) = try? ReferenceDocumentURL(from: originatorUri),
2070-
interfaceData.moduleName == moduleName && interfaceData.groupName == groupName {
2070+
interfaceData.moduleName == moduleName && interfaceData.groupName == groupName
2071+
{
20712072
// If we have a specific symbol USR, try to find its position in the current interface
20722073
if let symbolUSR = symbolUSR,
2073-
let swiftLanguageService = languageService as? SwiftLanguageService {
2074+
let swiftLanguageService = languageService as? SwiftLanguageService
2075+
{
20742076
do {
20752077
let position = try await swiftLanguageService.generatedInterfaceManager.position(
20762078
ofUsr: symbolUSR,

Tests/SourceKitLSPTests/SwiftInterfaceTests.swift

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ final class SwiftInterfaceTests: XCTestCase {
153153
public init() {}
154154
}
155155
""",
156-
"Exec/main.swift": "import 1️⃣MyLibrary"
156+
"Exec/main.swift": "import 1️⃣MyLibrary",
157157
],
158158
manifest: """
159159
let package = Package(
@@ -196,13 +196,21 @@ final class SwiftInterfaceTests: XCTestCase {
196196
func testSemanticFunctionalityInGeneratedInterface() async throws {
197197
let project = try await SwiftPMTestProject(
198198
files: [
199-
"MyLibrary/MyLibrary.swift": "public struct Lib { public func foo() -> String {} }",
200-
"Exec/main.swift": "import 1️⃣MyLibrary"
199+
"MyLibrary/MyLibrary.swift": """
200+
public struct Lib {
201+
public func foo() -> String {}
202+
public init() {}
203+
}
204+
""",
205+
"Exec/main.swift": "import 1️⃣MyLibrary",
201206
],
202207
manifest: """
203208
let package = Package(
204209
name: "MyLibrary",
205-
targets: [.target(name: "MyLibrary"), .executableTarget(name: "Exec", dependencies: ["MyLibrary"])]
210+
targets: [
211+
.target(name: "MyLibrary"),
212+
.executableTarget(name: "Exec", dependencies: ["MyLibrary"])
213+
]
206214
)
207215
""",
208216
capabilities: ClientCapabilities(experimental: [
@@ -217,21 +225,28 @@ final class SwiftInterfaceTests: XCTestCase {
217225
)
218226
let referenceDocumentUri = try XCTUnwrap(response?.locations?.only).uri
219227
let referenceDocument = try await project.testClient.send(GetReferenceDocumentRequest(uri: referenceDocumentUri))
220-
228+
221229
// Test hover functionality in the interface
222-
if let stringIndex = referenceDocument.content.firstRange(of: "-> String") {
223-
let (line, column) = LineTable(referenceDocument.content)
224-
.lineAndUTF16ColumnOf(referenceDocument.content.index(stringIndex.lowerBound, offsetBy: 3))
225-
project.testClient.send(
226-
DidOpenTextDocumentNotification(
227-
textDocument: TextDocumentItem(uri: referenceDocumentUri, language: .swift, version: 0, text: referenceDocument.content)
230+
let stringIndex = try XCTUnwrap(referenceDocument.content.firstRange(of: "-> String"))
231+
let (line, column) = LineTable(referenceDocument.content)
232+
.lineAndUTF16ColumnOf(referenceDocument.content.index(stringIndex.lowerBound, offsetBy: 3))
233+
project.testClient.send(
234+
DidOpenTextDocumentNotification(
235+
textDocument: TextDocumentItem(
236+
uri: referenceDocumentUri,
237+
language: .swift,
238+
version: 0,
239+
text: referenceDocument.content
228240
)
229241
)
230-
let hover = try await project.testClient.send(
231-
HoverRequest(textDocument: TextDocumentIdentifier(referenceDocumentUri), position: Position(line: line, utf16index: column))
242+
)
243+
let hover = try await project.testClient.send(
244+
HoverRequest(
245+
textDocument: TextDocumentIdentifier(referenceDocumentUri),
246+
position: Position(line: line, utf16index: column)
232247
)
233-
XCTAssertNotNil(hover)
234-
}
248+
)
249+
XCTAssertNotNil(hover)
235250
}
236251

237252
func testJumpToSynthesizedExtensionMethodInSystemModuleWithoutIndex() async throws {
@@ -313,13 +328,11 @@ final class SwiftInterfaceTests: XCTestCase {
313328
GetReferenceDocumentRequest.method: .dictionary(["supported": .bool(true)])
314329
])
315330
)
316-
let uri = DocumentURI(for: .swiftinterface)
331+
let uri = DocumentURI(for: .swift)
317332

318333
let positions = testClient.openDocument(
319334
"""
320-
func test(x: 1️⃣String) {
321-
let a: 2️⃣Int = 5
322-
}
335+
func test(x: 1️⃣String) {}
323336
""",
324337
uri: uri
325338
)
@@ -331,25 +344,13 @@ final class SwiftInterfaceTests: XCTestCase {
331344
let interfaceUri = try XCTUnwrap(stringDefinition?.locations?.only?.uri)
332345
let interfaceContents = try await testClient.send(GetReferenceDocumentRequest(uri: interfaceUri))
333346
// Open the interface document
334-
testClient.send(
335-
DidOpenTextDocumentNotification(
336-
textDocument: TextDocumentItem(
337-
uri: interfaceUri,
338-
language: .swift,
339-
version: 0,
340-
text: interfaceContents.content
341-
)
342-
)
343-
)
347+
testClient.openDocument(interfaceContents.content, uri: interfaceUri, language: .swift)
344348

345349
// Find a symbol within the interface (e.g., "init" method in String)
346-
guard let initRange = interfaceContents.content.range(of: "public init()") else {
347-
XCTFail("Could not find 'public init()' in String interface")
348-
return
349-
}
350+
let initRange = try XCTUnwrap(interfaceContents.content.range(of: "public init()"))
350351
let lineTable = LineTable(interfaceContents.content)
351352
let (line, column) = lineTable.lineAndUTF16ColumnOf(initRange.lowerBound)
352-
let initPosition = Position(line: line, utf16index: column + 7) // Position on "init"
353+
let initPosition = Position(line: line, utf16index: column + 7) // Position on "init"
353354

354355
// Test definition request within the interface
355356
let internalDefinition = try await testClient.send(
@@ -359,18 +360,9 @@ final class SwiftInterfaceTests: XCTestCase {
359360
)
360361
)
361362

362-
// The definition should either:
363-
// 1. Return a position within the same interface (internal navigation)
364-
// 2. Return the same position if it's already at the definition
365-
// 3. Return nil if no definition is available
366-
if let location = internalDefinition?.locations?.first {
367-
// If we get a location, it should be in the same interface or a related one
368-
XCTAssertTrue(
369-
location.uri.pseudoPath.hasSuffix(".swiftinterface") ||
370-
location.uri == interfaceUri,
371-
"Internal navigation should stay within interface files, got: \(location.uri.pseudoPath)"
372-
)
373-
}
363+
let location = try XCTUnwrap(internalDefinition?.locations?.first)
364+
XCTAssertTrue(location.uri.pseudoPath.hasSuffix(".swiftinterface"))
365+
XCTAssertTrue(location.uri == interfaceUri)
374366
}
375367

376368
func testFoundationImportNavigation() async throws {
@@ -385,7 +377,8 @@ final class SwiftInterfaceTests: XCTestCase {
385377
"""
386378
import 1️⃣Foundation
387379
""",
388-
uri: uri
380+
uri: uri,
381+
language: .swift
389382
)
390383

391384
// Test navigation to Foundation module
@@ -394,11 +387,8 @@ final class SwiftInterfaceTests: XCTestCase {
394387
)
395388
let foundationLocation = try XCTUnwrap(foundationDefinition?.locations?.only)
396389
// Verify it's a swiftinterface file (can be either file:// or sourcekit-lsp:// scheme)
397-
XCTAssertTrue(
398-
foundationLocation.uri.pseudoPath.hasSuffix(".swiftinterface") ||
399-
(foundationLocation.uri.scheme == "sourcekit-lsp" &&
400-
foundationLocation.uri.pseudoPath.contains("Foundation.swiftinterface"))
401-
)
390+
XCTAssertTrue(foundationLocation.uri.scheme == "sourcekit-lsp")
391+
XCTAssertTrue(foundationLocation.uri.pseudoPath.hasSuffix("Foundation.swiftinterface"))
402392
}
403393

404394
func testFoundationSubmoduleNavigation() async throws {
@@ -419,23 +409,18 @@ final class SwiftInterfaceTests: XCTestCase {
419409
let foundationDefinition = try await testClient.send(
420410
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
421411
)
422-
if let foundationLocation = foundationDefinition?.locations?.only {
423-
XCTAssertTrue(
424-
foundationLocation.uri.pseudoPath.contains("Foundation.swiftinterface") ||
425-
foundationLocation.uri.scheme == "sourcekit-lsp"
426-
)
427-
}
412+
let foundationLocation = try XCTUnwrap(foundationDefinition?.locations?.only)
413+
XCTAssertTrue(foundationLocation.uri.pseudoPath.contains("Foundation.swiftinterface"))
414+
XCTAssertTrue(foundationLocation.uri.scheme == "sourcekit-lsp")
415+
428416
// Test navigation to NSAffineTransform
429417
let transformDefinition = try await testClient.send(
430418
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["2️⃣"])
431419
)
432-
if let transformLocation = transformDefinition?.locations?.only {
433-
// Verify we can identify this as a swiftinterface file
434-
XCTAssertTrue(
435-
transformLocation.uri.pseudoPath.contains("Foundation.NSAffineTransform.swiftinterface") ||
436-
transformLocation.uri.scheme == "sourcekit-lsp"
437-
)
438-
}
420+
let transformLocation = try XCTUnwrap(transformDefinition?.locations?.only)
421+
// Verify we can identify this as a swiftinterface file
422+
XCTAssertTrue(transformLocation.uri.pseudoPath.contains("Foundation.NSAffineTransform.swiftinterface"))
423+
XCTAssertTrue(transformLocation.uri.scheme == "sourcekit-lsp")
439424
}
440425
}
441426

0 commit comments

Comments
 (0)