Skip to content

Commit 8d0af97

Browse files
committed
Migrate Rename.swift to use DocumentURI instead of URL
1 parent 7dd84ee commit 8d0af97

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

Sources/SourceKitLSP/IndexStoreDB+MainFilesProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extension UncheckedIndex {
2424
mainFiles = Set(
2525
mainFilePaths
2626
.filter { FileManager.default.fileExists(atPath: $0) }
27-
.map({ DocumentURI(URL(fileURLWithPath: $0, isDirectory: false)) })
27+
.map({ DocumentURI(filePath: $0, isDirectory: false) })
2828
)
2929
} else {
3030
mainFiles = []

Sources/SourceKitLSP/Rename.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -730,24 +730,27 @@ extension SourceKitLSPServer {
730730

731731
// If we have a USR + old name, perform an index lookup to find workspace-wide symbols to rename.
732732
// First, group all occurrences of that USR by the files they occur in.
733-
var locationsByFile: [URL: [RenameLocation]] = [:]
733+
var locationsByFile: [DocumentURI: [RenameLocation]] = [:]
734734

735735
actor LanguageServerTypesCache {
736736
let index: UncheckedIndex
737-
var languageServerTypesCache: [URL: LanguageServerType?] = [:]
737+
var languageServerTypesCache: [DocumentURI: LanguageServerType?] = [:]
738738

739739
init(index: UncheckedIndex) {
740740
self.index = index
741741
}
742742

743-
func languageServerType(for url: URL) -> LanguageServerType? {
744-
if let cachedValue = languageServerTypesCache[url] {
743+
func languageServerType(for uri: DocumentURI) -> LanguageServerType? {
744+
if let cachedValue = languageServerTypesCache[uri] {
745745
return cachedValue
746746
}
747-
let serverType = LanguageServerType(
748-
symbolProvider: index.symbolProvider(for: url.path)
749-
)
750-
languageServerTypesCache[url] = serverType
747+
let serverType: LanguageServerType? =
748+
if let fileURL = uri.fileURL {
749+
LanguageServerType(symbolProvider: index.symbolProvider(for: fileURL.path))
750+
} else {
751+
nil
752+
}
753+
languageServerTypesCache[uri] = serverType
751754
return serverType
752755
}
753756
}
@@ -757,17 +760,17 @@ extension SourceKitLSPServer {
757760
let usrsToRename = overridingAndOverriddenUsrs(of: usr, index: index)
758761
let occurrencesToRename = usrsToRename.flatMap { index.occurrences(ofUSR: $0, roles: renameRoles) }
759762
for occurrence in occurrencesToRename {
760-
let url = URL(fileURLWithPath: occurrence.location.path)
763+
let uri = occurrence.location.documentUri
761764

762765
// Determine whether we should add the location produced by the index to those that will be renamed, or if it has
763766
// already been handled by the set provided by the AST.
764-
if changes[DocumentURI(url)] != nil {
767+
if changes[uri] != nil {
765768
if occurrence.symbol.usr == usr {
766769
// If the language server's rename function already produced AST-based locations for this symbol, no need to
767770
// perform an indexed rename for it.
768771
continue
769772
}
770-
switch await languageServerTypesCache.languageServerType(for: url) {
773+
switch await languageServerTypesCache.languageServerType(for: uri) {
771774
case .swift:
772775
// sourcekitd only produces AST-based results for the direct calls to this USR. This is because the Swift
773776
// AST only has upwards references to superclasses and overridden methods, not the other way round. It is
@@ -788,17 +791,16 @@ extension SourceKitLSPServer {
788791
utf8Column: occurrence.location.utf8Column,
789792
usage: RenameLocation.Usage(roles: occurrence.roles)
790793
)
791-
locationsByFile[url, default: []].append(renameLocation)
794+
locationsByFile[uri, default: []].append(renameLocation)
792795
}
793796

794797
// Now, call `editsToRename(locations:in:oldName:newName:)` on the language service to convert these ranges into
795798
// edits.
796799
let urisAndEdits =
797800
await locationsByFile
798-
.concurrentMap { (url: URL, renameLocations: [RenameLocation]) -> (DocumentURI, [TextEdit])? in
799-
let uri = DocumentURI(url)
801+
.concurrentMap { (uri: DocumentURI, renameLocations: [RenameLocation]) -> (DocumentURI, [TextEdit])? in
800802
let language: Language
801-
switch await languageServerTypesCache.languageServerType(for: url) {
803+
switch await languageServerTypesCache.languageServerType(for: uri) {
802804
case .clangd:
803805
// Technically, we still don't know the language of the source file but defaulting to C is sufficient to
804806
// ensure we get the clang toolchain language server, which is all we care about.

Sources/SourceKitLSP/SymbolLocation+DocumentURI.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ import LanguageServerProtocol
1515

1616
extension SymbolLocation {
1717
var documentUri: DocumentURI {
18-
return DocumentURI(URL(fileURLWithPath: self.path))
18+
return DocumentURI(filePath: self.path, isDirectory: false)
1919
}
2020
}

0 commit comments

Comments
 (0)