@@ -2950,9 +2950,7 @@ Actual: ${stringify(fullActual)}`);
2950
2950
}
2951
2951
2952
2952
public verifyApplicableRefactorAvailableAtMarker ( negative : boolean , markerName : string ) {
2953
- const marker = this . getMarkerByName ( markerName ) ;
2954
- const applicableRefactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , marker . position , ts . defaultPreferences ) ;
2955
- const isAvailable = applicableRefactors && applicableRefactors . length > 0 ;
2953
+ const isAvailable = this . getApplicableRefactors ( this . getMarkerByName ( markerName ) . position ) . length > 0 ;
2956
2954
if ( negative && isAvailable ) {
2957
2955
this . raiseError ( `verifyApplicableRefactorAvailableAtMarker failed - expected no refactor at marker ${ markerName } but found some.` ) ;
2958
2956
}
@@ -2969,9 +2967,7 @@ Actual: ${stringify(fullActual)}`);
2969
2967
}
2970
2968
2971
2969
public verifyRefactorAvailable ( negative : boolean , name : string , actionName ?: string ) {
2972
- const selection = this . getSelection ( ) ;
2973
-
2974
- let refactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , selection , ts . defaultPreferences ) || [ ] ;
2970
+ let refactors = this . getApplicableRefactors ( this . getSelection ( ) ) ;
2975
2971
refactors = refactors . filter ( r => r . name === name && ( actionName === undefined || r . actions . some ( a => a . name === actionName ) ) ) ;
2976
2972
const isAvailable = refactors . length > 0 ;
2977
2973
@@ -2991,10 +2987,7 @@ Actual: ${stringify(fullActual)}`);
2991
2987
}
2992
2988
2993
2989
public verifyRefactor ( { name, actionName, refactors } : FourSlashInterface . VerifyRefactorOptions ) {
2994
- const selection = this . getSelection ( ) ;
2995
-
2996
- const actualRefactors = ( this . languageService . getApplicableRefactors ( this . activeFile . fileName , selection , ts . defaultPreferences ) || ts . emptyArray )
2997
- . filter ( r => r . name === name && r . actions . some ( a => a . name === actionName ) ) ;
2990
+ const actualRefactors = this . getApplicableRefactors ( this . getSelection ( ) ) . filter ( r => r . name === name && r . actions . some ( a => a . name === actionName ) ) ;
2998
2991
this . assertObjectsEqual ( actualRefactors , refactors ) ;
2999
2992
}
3000
2993
@@ -3004,8 +2997,7 @@ Actual: ${stringify(fullActual)}`);
3004
2997
throw new Error ( "Exactly one refactor range is allowed per test." ) ;
3005
2998
}
3006
2999
3007
- const applicableRefactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , ts . first ( ranges ) , ts . defaultPreferences ) ;
3008
- const isAvailable = applicableRefactors && applicableRefactors . length > 0 ;
3000
+ const isAvailable = this . getApplicableRefactors ( ts . first ( ranges ) ) . length > 0 ;
3009
3001
if ( negative && isAvailable ) {
3010
3002
this . raiseError ( `verifyApplicableRefactorAvailableForRange failed - expected no refactor but found some.` ) ;
3011
3003
}
@@ -3016,7 +3008,7 @@ Actual: ${stringify(fullActual)}`);
3016
3008
3017
3009
public applyRefactor ( { refactorName, actionName, actionDescription, newContent : newContentWithRenameMarker } : FourSlashInterface . ApplyRefactorOptions ) {
3018
3010
const range = this . getSelection ( ) ;
3019
- const refactors = this . languageService . getApplicableRefactors ( this . activeFile . fileName , range , ts . defaultPreferences ) ;
3011
+ const refactors = this . getApplicableRefactors ( range ) ;
3020
3012
const refactorsWithName = refactors . filter ( r => r . name === refactorName ) ;
3021
3013
if ( refactorsWithName . length === 0 ) {
3022
3014
this . raiseError ( `The expected refactor: ${ refactorName } is not available at the marker location.\nAvailable refactors: ${ refactors . map ( r => r . name ) } ` ) ;
@@ -3062,7 +3054,38 @@ Actual: ${stringify(fullActual)}`);
3062
3054
return { renamePosition, newContent } ;
3063
3055
}
3064
3056
}
3057
+ }
3058
+
3059
+ public moveToNewFile ( options : FourSlashInterface . MoveToNewFileOptions ) : void {
3060
+ assert ( this . getRanges ( ) . length === 1 ) ;
3061
+ const range = this . getRanges ( ) [ 0 ] ;
3062
+ const refactor = ts . find ( this . getApplicableRefactors ( range ) , r => r . name === "Move to new file" ) ;
3063
+ assert ( refactor . actions . length === 1 ) ;
3064
+ const action = ts . first ( refactor . actions ) ;
3065
+ assert ( action . name === "Move to new file" && action . description === "Move to new file" ) ;
3066
+
3067
+ const editInfo = this . languageService . getEditsForRefactor ( this . activeFile . fileName , this . formatCodeSettings , range , refactor . name , action . name , ts . defaultPreferences ) ;
3068
+ for ( const edit of editInfo . edits ) {
3069
+ const newContent = options . newFileContents [ edit . fileName ] ;
3070
+ if ( newContent === undefined ) {
3071
+ this . raiseError ( `There was an edit in ${ edit . fileName } but new content was not specified.` ) ;
3072
+ }
3073
+ if ( this . testData . files . some ( f => f . fileName === edit . fileName ) ) {
3074
+ this . applyEdits ( edit . fileName , edit . textChanges , /*isFormattingEdit*/ false ) ;
3075
+ this . openFile ( edit . fileName ) ;
3076
+ this . verifyCurrentFileContent ( newContent ) ;
3077
+ }
3078
+ else {
3079
+ assert ( edit . textChanges . length === 1 ) ;
3080
+ const change = ts . first ( edit . textChanges ) ;
3081
+ assert . deepEqual ( change . span , ts . createTextSpan ( 0 , 0 ) ) ;
3082
+ assert . equal ( change . newText , newContent , `Content for ${ edit . fileName } ` ) ;
3083
+ }
3084
+ }
3065
3085
3086
+ for ( const fileName in options . newFileContents ) {
3087
+ assert ( editInfo . edits . some ( e => e . fileName === fileName ) ) ;
3088
+ }
3066
3089
}
3067
3090
3068
3091
public verifyFileAfterApplyingRefactorAtMarker (
@@ -3278,6 +3301,10 @@ Actual: ${stringify(fullActual)}`);
3278
3301
this . verifyCurrentFileContent ( options . newFileContents [ fileName ] ) ;
3279
3302
}
3280
3303
}
3304
+
3305
+ private getApplicableRefactors ( positionOrRange : number | ts . TextRange ) : ReadonlyArray < ts . ApplicableRefactorInfo > {
3306
+ return this . languageService . getApplicableRefactors ( this . activeFile . fileName , positionOrRange , ts . defaultPreferences ) || ts . emptyArray ;
3307
+ }
3281
3308
}
3282
3309
3283
3310
export function runFourSlashTest ( basePath : string , testType : FourSlashTestType , fileName : string ) {
@@ -4373,6 +4400,10 @@ namespace FourSlashInterface {
4373
4400
public getEditsForFileRename ( options : GetEditsForFileRenameOptions ) {
4374
4401
this . state . getEditsForFileRename ( options ) ;
4375
4402
}
4403
+
4404
+ public moveToNewFile ( options : MoveToNewFileOptions ) : void {
4405
+ this . state . moveToNewFile ( options ) ;
4406
+ }
4376
4407
}
4377
4408
4378
4409
export class Edit {
@@ -4721,4 +4752,8 @@ namespace FourSlashInterface {
4721
4752
readonly newPath : string ;
4722
4753
readonly newFileContents : { readonly [ fileName : string ] : string } ;
4723
4754
}
4755
+
4756
+ export interface MoveToNewFileOptions {
4757
+ readonly newFileContents : { readonly [ fileName : string ] : string } ;
4758
+ }
4724
4759
}
0 commit comments