-
Notifications
You must be signed in to change notification settings - Fork 314
Support cross-file rename for clang languages #1031
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
Sources/LanguageServerProtocol/Requests/IndexedRenameRequest.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Rename all occurrences of a symbol named `oldName` to `newName` at the | ||
/// given `positions`. | ||
/// | ||
/// The use case of this method is for when the positions to rename are already | ||
/// known, eg. from an index lookup outside of clangd's built-in index. In | ||
/// particular, it determines the edits necessary to rename multi-piece | ||
/// Objective-C selector names. | ||
/// | ||
/// `textDocument` is used to determine the language options for the symbol to | ||
/// rename, eg. to decide whether `oldName` and `newName` are Objective-C | ||
/// selectors or normal identifiers. | ||
/// | ||
/// This is a clangd extension. | ||
public struct IndexedRenameRequest: TextDocumentRequest, Hashable { | ||
public static let method: String = "workspace/indexedRename" | ||
public typealias Response = WorkspaceEdit? | ||
|
||
/// The document in which the declaration to rename is declared. Its compiler | ||
/// arguments are used to infer language settings for the rename. | ||
public var textDocument: TextDocumentIdentifier | ||
|
||
/// The old name of the symbol. | ||
public var oldName: String | ||
|
||
/// The new name of the symbol. | ||
public var newName: String | ||
|
||
/// The positions at which the symbol is known to appear and that should be | ||
/// renamed. The key is a document URI | ||
public var positions: [DocumentURI: [Position]] | ||
|
||
public init( | ||
textDocument: TextDocumentIdentifier, | ||
oldName: String, | ||
newName: String, | ||
positions: [DocumentURI: [Position]] | ||
) { | ||
self.textDocument = textDocument | ||
self.oldName = oldName | ||
self.newName = newName | ||
self.positions = positions | ||
} | ||
} | ||
|
||
// Workaround for Codable not correctly encoding dictionaries whose keys aren't strings. | ||
extension IndexedRenameRequest: Codable { | ||
private enum CodingKeys: CodingKey { | ||
case textDocument | ||
case oldName | ||
case newName | ||
case positions | ||
} | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
self.textDocument = try container.decode( | ||
TextDocumentIdentifier.self, | ||
forKey: IndexedRenameRequest.CodingKeys.textDocument | ||
) | ||
self.oldName = try container.decode(String.self, forKey: IndexedRenameRequest.CodingKeys.oldName) | ||
self.newName = try container.decode(String.self, forKey: IndexedRenameRequest.CodingKeys.newName) | ||
self.positions = try container.decode([String: [Position]].self, forKey: .positions).mapKeys(DocumentURI.init) | ||
} | ||
|
||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
|
||
try container.encode(self.textDocument, forKey: IndexedRenameRequest.CodingKeys.textDocument) | ||
try container.encode(self.oldName, forKey: IndexedRenameRequest.CodingKeys.oldName) | ||
try container.encode(self.newName, forKey: IndexedRenameRequest.CodingKeys.newName) | ||
try container.encode(self.positions.mapKeys(\.stringValue), forKey: IndexedRenameRequest.CodingKeys.positions) | ||
|
||
} | ||
} | ||
|
||
fileprivate extension Dictionary { | ||
func mapKeys<NewKeyType: Hashable>(_ transform: (Key) -> NewKeyType) -> [NewKeyType: Value] { | ||
return [NewKeyType: Value](uniqueKeysWithValues: self.map { (transform($0.key), $0.value) }) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we add a cross-language test? Or multiple even, eg. swift <-> objc, swift <-> c, and swift <-> c++?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ha, no. This PR is about adding global rename for clang languages, not doing cross-language rename yet. For that we need name translation, which I’ll add in a follow-up PR (and which is currently blocked by rdar://120066209).
rdar://118996461 is tracking the addition of cross-language rename.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC only ObjC is blocked by that right 😅? But cool, didn't realize we didn't have name translation yet, thanks 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but ObjC is really the interesting candidate for name translation. And it’s easiest to design it with all name translations in mind. There will be a follow-up PR soon-ish.