@@ -1058,13 +1058,20 @@ extension SwiftLanguageServer {
1058
1058
// Once we have lexical scope lookup in swift-syntax, this can be a purely syntactic rename.
1059
1059
// We know that the parameters are variables and thus there can't be overloads that need to be resolved by the
1060
1060
// type checker.
1061
- let relatedIdentifiersResponse = try await self . relatedIdentifiers (
1061
+ let relatedIdentifiers = try await self . relatedIdentifiers (
1062
1062
at: parameterPosition,
1063
1063
in: snapshot,
1064
1064
includeNonEditableBaseNames: false
1065
1065
)
1066
1066
1067
- let parameterRenameLocations = relatedIdentifiersResponse. renameLocations ( in: snapshot)
1067
+ // Exclude the edit that renames the parameter itself. The parameter gets renamed as part of the function
1068
+ // declaration.
1069
+ let filteredRelatedIdentifiers = RelatedIdentifiersResponse (
1070
+ relatedIdentifiers: relatedIdentifiers. relatedIdentifiers. filter { !$0. range. contains ( parameterPosition) } ,
1071
+ name: relatedIdentifiers. name
1072
+ )
1073
+
1074
+ let parameterRenameLocations = filteredRelatedIdentifiers. renameLocations ( in: snapshot)
1068
1075
1069
1076
return try await editsToRename (
1070
1077
locations: parameterRenameLocations,
@@ -1076,9 +1083,7 @@ extension SwiftLanguageServer {
1076
1083
guard let parameterRenameEdits else {
1077
1084
continue
1078
1085
}
1079
- // Exclude the edit that renames the parameter itself. The parameter gets renamed as part of the function
1080
- // declaration.
1081
- edits += parameterRenameEdits. filter { !$0. range. contains ( parameterPosition) }
1086
+ edits += parameterRenameEdits
1082
1087
}
1083
1088
return edits
1084
1089
}
@@ -1162,6 +1167,8 @@ extension SwiftLanguageServer {
1162
1167
)
1163
1168
}
1164
1169
1170
+ let tree = await syntaxTreeManager. syntaxTree ( for: snapshot)
1171
+
1165
1172
let compoundRenameRanges = try await getSyntacticRenameRanges (
1166
1173
renameLocations: renameLocations,
1167
1174
oldName: oldNameString,
@@ -1187,6 +1194,19 @@ extension SwiftLanguageServer {
1187
1194
}
1188
1195
return compoundRenameRange. pieces. compactMap { ( piece) -> TextEdit ? in
1189
1196
if piece. kind == . baseName {
1197
+ if let absolutePiecePosition = snapshot. absolutePosition ( of: piece. range. lowerBound) ,
1198
+ let firstNameToken = tree. token ( at: absolutePiecePosition) ,
1199
+ firstNameToken. keyPathInParent == \FunctionParameterSyntax . firstName,
1200
+ let parameterSyntax = firstNameToken. parent ( as: FunctionParameterSyntax . self) ,
1201
+ parameterSyntax. secondName == nil , // Should always be true because otherwise decl would be second name
1202
+ let firstNameEndPos = snapshot. position ( of: firstNameToken. endPositionBeforeTrailingTrivia)
1203
+ {
1204
+ // We are renaming a function parameter from inside the function body.
1205
+ // This should be a local rename and it shouldn't affect all the callers of the function. Introduce the new
1206
+ // name as a second name.
1207
+ return TextEdit ( range: firstNameEndPos..< firstNameEndPos, newText: " " + newName. baseName)
1208
+ }
1209
+
1190
1210
return TextEdit ( range: piece. range, newText: newName. baseName)
1191
1211
} else if piece. kind == . keywordBaseName {
1192
1212
// Keyword base names can't be renamed
0 commit comments