Skip to content

Commit 0bb605d

Browse files
committed
Remove a few workarounds that should no longer be needed
1 parent 50a28bb commit 0bb605d

File tree

12 files changed

+77
-82
lines changed

12 files changed

+77
-82
lines changed

Sources/LanguageServerProtocol/Error.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ public struct ErrorCode: RawRepresentable, Codable, Hashable, Sendable {
8989
public struct ResponseError: Error, Codable, Hashable {
9090
public var code: ErrorCode
9191
public var message: String
92-
// FIXME: data
92+
public var data: LSPAny?
9393

94-
public init(code: ErrorCode, message: String) {
94+
public init(code: ErrorCode, message: String, data: LSPAny? = nil) {
9595
self.code = code
9696
self.message = message
97+
self.data = data
9798
}
9899
}
99100

Sources/SKTestSupport/SkipUnless.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,37 @@ package actor SkipUnless {
493493
}
494494
}
495495
}
496+
497+
/// Checks if sourcekitd contains https://github.com/swiftlang/swift/pull/71049
498+
package static func solverBasedCursorInfoWorksForMemoryOnlyFiles(
499+
file: StaticString = #filePath,
500+
line: UInt = #line
501+
) async throws {
502+
struct ExpectedLocationsResponse: Error {}
503+
504+
return try await shared.skipUnlessSupportedByToolchain(swiftVersion: SwiftVersion(6, 0), file: file, line: line) {
505+
let testClient = try await TestSourceKitLSPClient()
506+
let uri = DocumentURI(for: .swift)
507+
let positions = testClient.openDocument(
508+
"""
509+
func foo() -> Int { 1 }
510+
func foo() -> String { "" }
511+
func test() {
512+
_ = 3️⃣foo()
513+
}
514+
""",
515+
uri: uri
516+
)
517+
518+
let response = try await testClient.send(
519+
DefinitionRequest(textDocument: TextDocumentIdentifier(uri), position: positions["3️⃣"])
520+
)
521+
guard case .locations(let locations) = response else {
522+
throw ExpectedLocationsResponse()
523+
}
524+
return locations.count > 0
525+
}
526+
}
496527
}
497528

498529
// MARK: - Parsing Swift compiler version

Sources/SKTestSupport/SwiftPMTestProject.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,7 @@ package class SwiftPMTestProject: MultiFileTestProject {
227227
"-Xswiftc", "-module-cache-path", "-Xswiftc", globalModuleCache.path,
228228
]
229229
}
230-
var environment = ProcessEnv.block
231-
// FIXME: SwiftPM does not index-while-building on non-Darwin platforms for C-family files (rdar://117744039).
232-
// Force-enable index-while-building with the environment variable.
233-
environment["SWIFTPM_ENABLE_CLANG_INDEX_STORE"] = "1"
234-
try await Process.checkNonZeroExit(arguments: arguments, environmentBlock: environment)
230+
try await Process.checkNonZeroExit(arguments: arguments)
235231
}
236232

237233
/// Resolve package dependencies for the package at `path`.

Sources/SourceKitD/DynamicallyLoadedSourceKitD.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,8 @@ package actor DynamicallyLoadedSourceKitD: SourceKitD {
8383
deinit {
8484
self.api.set_notification_handler(nil)
8585
self.api.shutdown()
86-
// FIXME: is it safe to dlclose() sourcekitd? If so, do that here. For now, let the handle leak.
87-
Task.detached(priority: .background) { [dylib] in
88-
dylib.leak()
86+
Task.detached(priority: .background) { [dylib, path] in
87+
orLog("Closing dylib \(path)") { try dylib.close() }
8988
}
9089
}
9190

Sources/SourceKitLSP/Clang/ClangLanguageService.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,15 @@ extension ClangLanguageService {
486486
let clangBuildSettings = await self.buildSettings(for: uri)
487487

488488
// The compile command changed, send over the new one.
489-
// FIXME: what should we do if we no longer have valid build settings?
490489
if let compileCommand = clangBuildSettings?.compileCommand,
491490
let pathString = (try? AbsolutePath(validating: url.path))?.pathString
492491
{
493492
let notification = DidChangeConfigurationNotification(
494-
settings: .clangd(
495-
ClangWorkspaceSettings(
496-
compilationDatabaseChanges: [pathString: compileCommand])
497-
)
493+
settings: .clangd(ClangWorkspaceSettings(compilationDatabaseChanges: [pathString: compileCommand]))
498494
)
499495
clangd.send(notification)
496+
} else {
497+
logger.error("No longer have build settings for \(url.path) but can't send null build settings to clangd")
500498
}
501499
}
502500

Sources/SourceKitLSP/DocumentManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,15 @@ package final class DocumentManager: InMemoryDocumentManager, Sendable {
191191
}
192192
}
193193

194+
/// Returns the latest open snapshot of `uri` or, if no document with that URI is open, reads the file contents of
195+
/// that file from disk.
196+
package func latestSnapshotOrDisk(_ uri: DocumentURI, language: Language) -> DocumentSnapshot? {
197+
if let snapshot = try? self.latestSnapshot(uri) {
198+
return snapshot
199+
}
200+
return try? DocumentSnapshot(withContentsFromDisk: uri, language: language)
201+
}
202+
194203
package func fileHasInMemoryModifications(_ uri: DocumentURI) -> Bool {
195204
guard let document = try? latestSnapshot(uri), let fileURL = uri.fileURL else {
196205
return false

Sources/SourceKitLSP/Rename.swift

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -482,14 +482,6 @@ package struct CrossLanguageName: Sendable {
482482
/// The kinds of symbol occurrence roles that should be renamed.
483483
fileprivate let renameRoles: SymbolRole = [.declaration, .definition, .reference]
484484

485-
extension DocumentManager {
486-
/// Returns the latest open snapshot of `uri` or, if no document with that URI is open, reads the file contents of
487-
/// that file from disk.
488-
fileprivate func latestSnapshotOrDisk(_ uri: DocumentURI, language: Language) -> DocumentSnapshot? {
489-
return (try? self.latestSnapshot(uri)) ?? (try? DocumentSnapshot(withContentsFromDisk: uri, language: language))
490-
}
491-
}
492-
493485
extension SourceKitLSPServer {
494486
/// Returns a `DocumentSnapshot`, a position and the corresponding language service that references
495487
/// `usr` from a Swift file. If `usr` is not referenced from Swift, returns `nil`.
@@ -564,22 +556,6 @@ extension SourceKitLSPServer {
564556
return nil
565557
}
566558

567-
// FIXME: (async-workaround): Needed to work around rdar://127977642
568-
private func translateClangNameToSwift(
569-
_ swiftLanguageService: SwiftLanguageService,
570-
at symbolLocation: SymbolLocation,
571-
in snapshot: DocumentSnapshot,
572-
isObjectiveCSelector: Bool,
573-
name: String
574-
) async throws -> String {
575-
return try await swiftLanguageService.translateClangNameToSwift(
576-
at: symbolLocation,
577-
in: snapshot,
578-
isObjectiveCSelector: isObjectiveCSelector,
579-
name: name
580-
)
581-
}
582-
583559
private func getCrossLanguageName(
584560
forDefinitionOccurrence definitionOccurrence: SymbolOccurrence,
585561
overrideName: String? = nil,
@@ -614,8 +590,7 @@ extension SourceKitLSPServer {
614590
let swiftName: String?
615591
if let swiftReference = await getReferenceFromSwift(usr: usr, index: index, workspace: workspace) {
616592
let isObjectiveCSelector = definitionLanguage == .objective_c && definitionSymbol.kind.isMethod
617-
swiftName = try await self.translateClangNameToSwift(
618-
swiftReference.swiftLanguageService,
593+
swiftName = try await swiftReference.swiftLanguageService.translateClangNameToSwift(
619594
at: swiftReference.location,
620595
in: swiftReference.snapshot,
621596
isObjectiveCSelector: isObjectiveCSelector,

Sources/SourceKitLSP/Swift/CodeActions/ConvertIntegerLiteral.swift

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,8 @@ struct ConvertIntegerLiteral: SyntaxCodeActionProvider {
4040
continue
4141
}
4242

43-
//TODO: Add this to swift-syntax?
44-
let prefix: String
45-
switch radix {
46-
case .binary:
47-
prefix = "0b"
48-
case .octal:
49-
prefix = "0o"
50-
case .hex:
51-
prefix = "0x"
52-
case .decimal:
53-
prefix = ""
54-
#if RESILIENT_LIBRARIES
55-
@unknown default:
56-
fatalError("Unknown case")
57-
#endif
58-
}
59-
6043
let convertedValue: ExprSyntax =
61-
"\(raw: prefix)\(raw: String(integerValue, radix: radix.size))"
44+
"\(raw: radix.literalPrefix)\(raw: String(integerValue, radix: radix.size))"
6245
let edit = TextEdit(
6346
range: scope.snapshot.range(of: integerExpr),
6447
newText: convertedValue.description

Sources/SourceKitLSP/Swift/CursorInfo.swift

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import LanguageServerProtocol
14+
import SKLogging
1415
import SourceKitD
1516

1617
/// Detailed information about a symbol under the cursor.
@@ -57,6 +58,7 @@ struct CursorInfo {
5758

5859
init?(
5960
_ dict: SKDResponseDictionary,
61+
documentManager: DocumentManager,
6062
sourcekitd: some SourceKitD
6163
) {
6264
let keys = sourcekitd.keys
@@ -70,12 +72,14 @@ struct CursorInfo {
7072
let line: Int = dict[keys.line],
7173
let column: Int = dict[keys.column]
7274
{
73-
let position = Position(
74-
line: line - 1,
75-
// FIXME: we need to convert the utf8/utf16 column, which may require reading the file!
76-
utf16index: column - 1
77-
)
78-
location = Location(uri: DocumentURI(filePath: filepath, isDirectory: false), range: Range(position))
75+
let uri = DocumentURI(filePath: filepath, isDirectory: false)
76+
if let snapshot = documentManager.latestSnapshotOrDisk(uri, language: .swift) {
77+
let position = snapshot.positionOf(zeroBasedLine: line - 1, utf8Column: column - 1)
78+
location = Location(uri: uri, range: Range(position))
79+
} else {
80+
logger.error("Failed to get snapshot for \(uri.forLogging) to convert position")
81+
location = nil
82+
}
7983
} else {
8084
location = nil
8185
}
@@ -142,6 +146,7 @@ extension SwiftLanguageService {
142146
_ range: Range<Position>,
143147
additionalParameters appendAdditionalParameters: ((SKDRequestDictionary) -> Void)? = nil
144148
) async throws -> (cursorInfo: [CursorInfo], refactorActions: [SemanticRefactorCommand]) {
149+
let documentManager = try self.documentManager
145150
let snapshot = try documentManager.latestSnapshot(uri)
146151

147152
let offsetRange = snapshot.utf8OffsetRange(of: range)
@@ -162,10 +167,12 @@ extension SwiftLanguageService {
162167
let dict = try await sendSourcekitdRequest(skreq, fileContents: snapshot.text)
163168

164169
var cursorInfoResults: [CursorInfo] = []
165-
if let cursorInfo = CursorInfo(dict, sourcekitd: sourcekitd) {
170+
if let cursorInfo = CursorInfo(dict, documentManager: documentManager, sourcekitd: sourcekitd) {
166171
cursorInfoResults.append(cursorInfo)
167172
}
168-
cursorInfoResults += dict[keys.secondarySymbols]?.compactMap { CursorInfo($0, sourcekitd: sourcekitd) } ?? []
173+
cursorInfoResults +=
174+
dict[keys.secondarySymbols]?
175+
.compactMap { CursorInfo($0, documentManager: documentManager, sourcekitd: sourcekitd) } ?? []
169176
let refactorActions =
170177
[SemanticRefactorCommand](
171178
array: dict[keys.refactorActions],

Sources/SourceKitLSP/Swift/Diagnostic.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,8 @@ extension DiagnosticRelatedInformation {
340340
let snapshot: DocumentSnapshot
341341
if filePath == primaryDocumentSnapshot.uri.pseudoPath {
342342
snapshot = primaryDocumentSnapshot
343-
} else if let inMemorySnapshot = try? documentManager.latestSnapshot(uri) {
344-
snapshot = inMemorySnapshot
345-
} else if let snapshotFromDisk = try? DocumentSnapshot(withContentsFromDisk: uri, language: .swift) {
346-
snapshot = snapshotFromDisk
343+
} else if let loadedSnapshot = documentManager.latestSnapshotOrDisk(uri, language: .swift) {
344+
snapshot = loadedSnapshot
347345
} else {
348346
return nil
349347
}

0 commit comments

Comments
 (0)