@@ -689,7 +689,8 @@ final class CodeActionTests: XCTestCase {
689
689
try await assertCodeActions (
690
690
"""
691
691
let x = 1️⃣12️⃣63️⃣
692
- """
692
+ """ ,
693
+ ranges: [ ( " 1️⃣ " , " 2️⃣ " ) , ( " 1️⃣ " , " 3️⃣ " ) ]
693
694
) { uri, positions in
694
695
[
695
696
CodeAction (
@@ -728,6 +729,7 @@ final class CodeActionTests: XCTestCase {
728
729
"""
729
730
let x = 1️⃣# " Hello 2️⃣world " #3️⃣
730
731
""" ,
732
+ ranges: [ ( " 1️⃣ " , " 3️⃣ " ) ] ,
731
733
exhaustive: false
732
734
) { uri, positions in
733
735
[
@@ -749,6 +751,7 @@ final class CodeActionTests: XCTestCase {
749
751
##"""
750
752
let x = 1️⃣#"Hello 2️⃣\#(name)"#3️⃣
751
753
"""## ,
754
+ ranges: [ ( " 1️⃣ " , " 3️⃣ " ) ] ,
752
755
exhaustive: false
753
756
) { uri, positions in
754
757
[
@@ -787,9 +790,10 @@ final class CodeActionTests: XCTestCase {
787
790
func testMigrateIfLetSyntax( ) async throws {
788
791
try await assertCodeActions (
789
792
##"""
790
- 1️⃣if 2️⃣let 3️⃣foo = 4️⃣foo {}9️⃣
793
+ 1️⃣if 2️⃣let 3️⃣foo = 4️⃣foo {}5️⃣
791
794
"""## ,
792
- markers: [ " 1️⃣ " , " 2️⃣ " , " 3️⃣ " , " 4️⃣ " ]
795
+ markers: [ " 1️⃣ " , " 2️⃣ " , " 3️⃣ " , " 4️⃣ " ] ,
796
+ ranges: [ ( " 1️⃣ " , " 4️⃣ " ) , ( " 1️⃣ " , " 5️⃣ " ) ]
793
797
) { uri, positions in
794
798
[
795
799
CodeAction (
@@ -800,7 +804,7 @@ final class CodeActionTests: XCTestCase {
800
804
changes: [
801
805
uri: [
802
806
TextEdit (
803
- range: positions [ " 1️⃣ " ] ..< positions [ " 9️⃣ " ] ,
807
+ range: positions [ " 1️⃣ " ] ..< positions [ " 5️⃣ " ] ,
804
808
newText: " if let foo {} "
805
809
)
806
810
]
@@ -827,9 +831,10 @@ final class CodeActionTests: XCTestCase {
827
831
func testOpaqueParameterToGeneric( ) async throws {
828
832
try await assertCodeActions (
829
833
##"""
830
- 1️⃣func 2️⃣someFunction(_ 3️⃣input: some4️⃣ Value) {}9️⃣
834
+ 1️⃣func 2️⃣someFunction(_ 3️⃣input: some4️⃣ Value) {}5️⃣
831
835
"""## ,
832
836
markers: [ " 1️⃣ " , " 2️⃣ " , " 3️⃣ " , " 4️⃣ " ] ,
837
+ ranges: [ ( " 1️⃣ " , " 2️⃣ " ) , ( " 1️⃣ " , " 5️⃣ " ) ] ,
833
838
exhaustive: false
834
839
) { uri, positions in
835
840
[
@@ -841,7 +846,7 @@ final class CodeActionTests: XCTestCase {
841
846
changes: [
842
847
uri: [
843
848
TextEdit (
844
- range: positions [ " 1️⃣ " ] ..< positions [ " 9️⃣ " ] ,
849
+ range: positions [ " 1️⃣ " ] ..< positions [ " 5️⃣ " ] ,
845
850
newText: " func someFunction<T1: Value>(_ input: T1) {} "
846
851
)
847
852
]
@@ -875,6 +880,7 @@ final class CodeActionTests: XCTestCase {
875
880
}5️⃣
876
881
877
882
"""## ,
883
+ ranges: [ ( " 1️⃣ " , " 5️⃣ " ) ] ,
878
884
exhaustive: false
879
885
) { uri, positions in
880
886
[
@@ -903,9 +909,54 @@ final class CodeActionTests: XCTestCase {
903
909
}
904
910
}
905
911
912
+ func testAddDocumentationRefactorSingleParameter( ) async throws {
913
+ try await assertCodeActions (
914
+ """
915
+ 1️⃣func 2️⃣refactor(3️⃣syntax: 4️⃣Decl5️⃣Syntax)6️⃣ { }7️⃣
916
+ """ ,
917
+ ranges: [ ( " 1️⃣ " , " 2️⃣ " ) , ( " 1️⃣ " , " 6️⃣ " ) , ( " 1️⃣ " , " 7️⃣ " ) ] ,
918
+ exhaustive: false
919
+ ) { uri, positions in
920
+ [
921
+ CodeAction (
922
+ title: " Add documentation " ,
923
+ kind: . refactorInline,
924
+ diagnostics: nil ,
925
+ edit: WorkspaceEdit (
926
+ changes: [
927
+ uri: [
928
+ TextEdit (
929
+ range: Range ( positions [ " 1️⃣ " ] ) ,
930
+ newText: """
931
+ /// A description
932
+ /// - Parameter syntax:
933
+ \( " " )
934
+ """
935
+ )
936
+ ]
937
+ ]
938
+ ) ,
939
+ command: nil
940
+ )
941
+ ]
942
+ }
943
+ }
944
+
945
+ /// Retrieves the code action at a set of markers and asserts that it matches a list of expected code actions.
946
+ ///
947
+ /// - Parameters:
948
+ /// - markedText: The source file input to get the code actions for.
949
+ /// - markers: The list of markers to retrieve code actions at. If `nil` code actions will be retrieved for all
950
+ /// markers in `markedText`
951
+ /// - ranges: If specified, code actions are also requested for selection ranges between these markers.
952
+ /// - exhaustive: Whether `expected` is expected to be a subset of the returned code actions or whether it is
953
+ /// expected to exhaustively match all code actions.
954
+ /// - expected: A closure that returns the list of expected code actions, given the URI of the test document and the
955
+ /// marker positions within.
906
956
private func assertCodeActions(
907
957
_ markedText: String ,
908
958
markers: [ String ] ? = nil ,
959
+ ranges: [ ( String , String ) ] = [ ] ,
909
960
exhaustive: Bool = true ,
910
961
expected: ( _ uri: DocumentURI , _ positions: DocumentPositions ) -> [ CodeAction ] ,
911
962
file: StaticString = #filePath,
@@ -915,31 +966,38 @@ final class CodeActionTests: XCTestCase {
915
966
let uri = DocumentURI . for ( . swift)
916
967
let positions = testClient. openDocument ( markedText, uri: uri)
917
968
918
- for marker in markers ?? extractMarkers ( markedText) . markers. map ( \. key) {
969
+ var ranges = ranges
970
+ if let markers {
971
+ ranges += markers. map { ( $0, $0) }
972
+ } else {
973
+ ranges += extractMarkers ( markedText) . markers. map ( \. key) . map { ( $0, $0) }
974
+ }
975
+
976
+ for (startMarker, endMarker) in ranges {
919
977
let result = try await testClient. send (
920
978
CodeActionRequest (
921
- range: Range ( positions [ marker ] ) ,
979
+ range: positions [ startMarker ] ..< positions [ endMarker ] ,
922
980
context: . init( ) ,
923
981
textDocument: TextDocumentIdentifier ( uri)
924
982
)
925
983
)
926
984
guard case . codeActions( let codeActions) = result else {
927
- XCTFail ( " Expected code actions at marker \( marker ) " , file: file, line: line)
985
+ XCTFail ( " Expected code actions at range \( startMarker ) - \( endMarker ) " , file: file, line: line)
928
986
return
929
987
}
930
988
if exhaustive {
931
989
XCTAssertEqual (
932
990
codeActions,
933
991
expected ( uri, positions) ,
934
- " Found unexpected code actions at \( marker ) " ,
992
+ " Found unexpected code actions at range \( startMarker ) - \( endMarker ) " ,
935
993
file: file,
936
994
line: line
937
995
)
938
996
} else {
939
997
XCTAssert (
940
998
codeActions. contains ( expected ( uri, positions) ) ,
941
999
"""
942
- Code actions did not contain expected at \( marker ) :
1000
+ Code actions did not contain expected at range \( startMarker ) - \( endMarker ) :
943
1001
\( codeActions)
944
1002
""" ,
945
1003
file: file,
0 commit comments