@@ -995,6 +995,24 @@ extension SwiftLanguageService {
995
995
return nil
996
996
}
997
997
998
+ /// Returns `true` if the given position is inside an `EnumCaseDeclSyntax`.
999
+ fileprivate func isInsideEnumCaseDecl( position: Position , snapshot: DocumentSnapshot ) async -> Bool {
1000
+ let syntaxTree = await syntaxTreeManager. syntaxTree ( for: snapshot)
1001
+ var node = Syntax ( syntaxTree. token ( at: snapshot. absolutePosition ( of: position) ) )
1002
+
1003
+ while let parent = node? . parent {
1004
+ if parent. is ( EnumCaseDeclSyntax . self) {
1005
+ return true
1006
+ }
1007
+ if parent. is ( MemberBlockItemSyntax . self) || parent. is ( CodeBlockItemSyntax . self) {
1008
+ // `MemberBlockItemSyntax` and `CodeBlockItemSyntax` can't be nested inside an EnumCaseDeclSyntax. Early exit.
1009
+ return false
1010
+ }
1011
+ node = parent
1012
+ }
1013
+ return false
1014
+ }
1015
+
998
1016
/// When the user requested a rename at `position` in `snapshot`, determine the position at which the rename should be
999
1017
/// performed internally, the USR of the symbol to rename and the range to rename that should be returned to the
1000
1018
/// editor.
@@ -1013,9 +1031,9 @@ extension SwiftLanguageService {
1013
1031
/// For example, `position` could point to the definition of a function within the file when rename was initiated on
1014
1032
/// a call.
1015
1033
///
1016
- /// If a `range ` is returned, this is an expanded range that contains both the symbol to rename as well as the
1017
- /// position at which the rename was requested. For example, when rename was initiated from the argument label of a
1018
- /// function call, the `range` will contain the entire function call from the base name to the closing `)`.
1034
+ /// If a `functionLikeRange ` is returned, this is an expanded range that contains both the symbol to rename as well
1035
+ /// as the position at which the rename was requested. For example, when rename was initiated from the argument label
1036
+ /// of a function call, the `range` will contain the entire function call from the base name to the closing `)`.
1019
1037
func symbolToRename(
1020
1038
at position: Position ,
1021
1039
in snapshot: DocumentSnapshot
@@ -1069,8 +1087,17 @@ extension SwiftLanguageService {
1069
1087
1070
1088
try Task . checkCancellation ( )
1071
1089
1090
+ var requestedNewName = request. newName
1091
+ if let openParenIndex = requestedNewName. firstIndex ( of: " ( " ) ,
1092
+ await isInsideEnumCaseDecl ( position: renamePosition, snapshot: snapshot)
1093
+ {
1094
+ // We don't support renaming enum parameter labels at the moment
1095
+ // (https://github.com/apple/sourcekit-lsp/issues/1228)
1096
+ requestedNewName = String ( requestedNewName [ ..< openParenIndex] )
1097
+ }
1098
+
1072
1099
let oldName = CrossLanguageName ( clangName: nil , swiftName: oldNameString, definitionLanguage: . swift)
1073
- let newName = CrossLanguageName ( clangName: nil , swiftName: request . newName , definitionLanguage: . swift)
1100
+ let newName = CrossLanguageName ( clangName: nil , swiftName: requestedNewName , definitionLanguage: . swift)
1074
1101
var edits = try await editsToRename (
1075
1102
locations: renameLocations,
1076
1103
in: snapshot,
@@ -1359,6 +1386,13 @@ extension SwiftLanguageService {
1359
1386
if name. hasSuffix ( " () " ) {
1360
1387
name = String ( name. dropLast ( 2 ) )
1361
1388
}
1389
+ if let openParenIndex = name. firstIndex ( of: " ( " ) ,
1390
+ await isInsideEnumCaseDecl ( position: renamePosition, snapshot: snapshot)
1391
+ {
1392
+ // We don't support renaming enum parameter labels at the moment
1393
+ // (https://github.com/apple/sourcekit-lsp/issues/1228)
1394
+ name = String ( name [ ..< openParenIndex] )
1395
+ }
1362
1396
guard let relatedIdentRange = response. relatedIdentifiers. first ( where: { $0. range. contains ( renamePosition) } ) ? . range
1363
1397
else {
1364
1398
return nil
0 commit comments