@@ -2354,7 +2354,8 @@ namespace FourSlash {
2354
2354
private applyCodeAction ( fileName : string , actions : ts . CodeAction [ ] , index ?: number ) : void {
2355
2355
if ( index === undefined ) {
2356
2356
if ( ! ( actions && actions . length === 1 ) ) {
2357
- this . raiseError ( `Should find exactly one codefix, but ${ actions ? actions . length : "none" } found.` ) ;
2357
+ const actionText = ( actions && actions . length ) ? JSON . stringify ( actions ) : "none" ;
2358
+ this . raiseError ( `Should find exactly one codefix, but found ${ actionText } ` ) ;
2358
2359
}
2359
2360
index = 0 ;
2360
2361
}
@@ -2708,6 +2709,60 @@ namespace FourSlash {
2708
2709
}
2709
2710
}
2710
2711
2712
+ public verifyApplicableRefactorAvailableAtMarker ( negative : boolean , markerName : string ) {
2713
+ const marker = this . getMarkerByName ( markerName ) ;
2714
+ const applicableRefactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , marker . position ) ;
2715
+ const isAvailable = applicableRefactors && applicableRefactors . length > 0 ;
2716
+ if ( negative && isAvailable ) {
2717
+ this . raiseError ( `verifyApplicableRefactorAvailableAtMarker failed - expected no refactor at marker ${ markerName } but found some.` ) ;
2718
+ }
2719
+ if ( ! negative && ! isAvailable ) {
2720
+ this . raiseError ( `verifyApplicableRefactorAvailableAtMarker failed - expected a refactor at marker ${ markerName } but found none.` ) ;
2721
+ }
2722
+ }
2723
+
2724
+ public verifyApplicableRefactorAvailableForRange ( negative : boolean ) {
2725
+ const ranges = this . getRanges ( ) ;
2726
+ if ( ! ( ranges && ranges . length === 1 ) ) {
2727
+ throw new Error ( "Exactly one refactor range is allowed per test." ) ;
2728
+ }
2729
+
2730
+ const applicableRefactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , { pos : ranges [ 0 ] . start , end : ranges [ 0 ] . end } ) ;
2731
+ const isAvailable = applicableRefactors && applicableRefactors . length > 0 ;
2732
+ if ( negative && isAvailable ) {
2733
+ this . raiseError ( `verifyApplicableRefactorAvailableForRange failed - expected no refactor but found some.` ) ;
2734
+ }
2735
+ if ( ! negative && ! isAvailable ) {
2736
+ this . raiseError ( `verifyApplicableRefactorAvailableForRange failed - expected a refactor but found none.` ) ;
2737
+ }
2738
+ }
2739
+
2740
+ public verifyFileAfterApplyingRefactorAtMarker (
2741
+ markerName : string ,
2742
+ expectedContent : string ,
2743
+ refactorNameToApply : string ,
2744
+ formattingOptions ?: ts . FormatCodeSettings ) {
2745
+
2746
+ formattingOptions = formattingOptions || this . formatCodeSettings ;
2747
+ const markerPos = this . getMarkerByName ( markerName ) . position ;
2748
+
2749
+ const applicableRefactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , markerPos ) ;
2750
+ const applicableRefactorToApply = ts . find ( applicableRefactors , refactor => refactor . name === refactorNameToApply ) ;
2751
+
2752
+ if ( ! applicableRefactorToApply ) {
2753
+ this . raiseError ( `The expected refactor: ${ refactorNameToApply } is not available at the marker location.` ) ;
2754
+ }
2755
+
2756
+ const codeActions = this . languageService . getRefactorCodeActions ( this . activeFile . fileName , formattingOptions , markerPos , refactorNameToApply ) ;
2757
+
2758
+ this . applyCodeAction ( this . activeFile . fileName , codeActions ) ;
2759
+ const actualContent = this . getFileContent ( this . activeFile . fileName ) ;
2760
+
2761
+ if ( this . normalizeNewlines ( actualContent ) !== this . normalizeNewlines ( expectedContent ) ) {
2762
+ this . raiseError ( `verifyFileAfterApplyingRefactors failed: expected:\n${ expectedContent } \nactual:\n${ actualContent } ` ) ;
2763
+ }
2764
+ }
2765
+
2711
2766
public printAvailableCodeFixes ( ) {
2712
2767
const codeFixes = this . getCodeFixActions ( this . activeFile . fileName ) ;
2713
2768
Harness . IO . log ( stringify ( codeFixes ) ) ;
@@ -3521,6 +3576,14 @@ namespace FourSlashInterface {
3521
3576
public codeFixAvailable ( ) {
3522
3577
this . state . verifyCodeFixAvailable ( this . negative ) ;
3523
3578
}
3579
+
3580
+ public applicableRefactorAvailableAtMarker ( markerName : string ) {
3581
+ this . state . verifyApplicableRefactorAvailableAtMarker ( this . negative , markerName ) ;
3582
+ }
3583
+
3584
+ public applicableRefactorAvailableForRange ( ) {
3585
+ this . state . verifyApplicableRefactorAvailableForRange ( this . negative ) ;
3586
+ }
3524
3587
}
3525
3588
3526
3589
export class Verify extends VerifyNegatable {
@@ -3735,6 +3798,10 @@ namespace FourSlashInterface {
3735
3798
this . state . verifyRangeAfterCodeFix ( expectedText , includeWhiteSpace , errorCode , index ) ;
3736
3799
}
3737
3800
3801
+ public fileAfterApplyingRefactorAtMarker ( markerName : string , expectedContent : string , refactorNameToApply : string , formattingOptions ?: ts . FormatCodeSettings ) : void {
3802
+ this . state . verifyFileAfterApplyingRefactorAtMarker ( markerName , expectedContent , refactorNameToApply , formattingOptions ) ;
3803
+ }
3804
+
3738
3805
public importFixAtPosition ( expectedTextArray : string [ ] , errorCode ?: number ) : void {
3739
3806
this . state . verifyImportFixAtPosition ( expectedTextArray , errorCode ) ;
3740
3807
}
0 commit comments