@@ -3,7 +3,7 @@ namespace ts.refactor {
3
3
const refactorName = "Move to a new file" ;
4
4
registerRefactor ( refactorName , {
5
5
getAvailableActions ( context ) : ApplicableRefactorInfo [ ] | undefined {
6
- if ( ! context . preferences . allowTextChangesInNewFiles || getFirstAndLastStatementToMove ( context ) === undefined ) return undefined ;
6
+ if ( ! context . preferences . allowTextChangesInNewFiles || getStatementsToMove ( context ) === undefined ) return undefined ;
7
7
const description = getLocaleSpecificMessage ( Diagnostics . Move_to_a_new_file ) ;
8
8
return [ { name : refactorName , description, actions : [ { name : refactorName , description } ] } ] ;
9
9
} ,
@@ -15,7 +15,7 @@ namespace ts.refactor {
15
15
}
16
16
} ) ;
17
17
18
- function getFirstAndLastStatementToMove ( context : RefactorContext ) : { readonly first : number , readonly afterLast : number } | undefined {
18
+ function getRangeToMove ( context : RefactorContext ) : ReadonlyArray < Statement > | undefined {
19
19
const { file } = context ;
20
20
const range = createTextRangeFromSpan ( getRefactorContextSpan ( context ) ) ;
21
21
const { statements } = file ;
@@ -25,7 +25,7 @@ namespace ts.refactor {
25
25
26
26
const startStatement = statements [ startNodeIndex ] ;
27
27
if ( isNamedDeclaration ( startStatement ) && startStatement . name && rangeContainsRange ( startStatement . name , range ) ) {
28
- return { first : startNodeIndex , afterLast : startNodeIndex + 1 } ;
28
+ return [ statements [ startNodeIndex ] ] ;
29
29
}
30
30
31
31
// Can't only partially include the start node or be partially into the next node
@@ -34,7 +34,7 @@ namespace ts.refactor {
34
34
// Can't be partially into the next node
35
35
if ( afterEndNodeIndex !== - 1 && ( afterEndNodeIndex === 0 || statements [ afterEndNodeIndex ] . getStart ( file ) < range . end ) ) return undefined ;
36
36
37
- return { first : startNodeIndex , afterLast : afterEndNodeIndex === - 1 ? statements . length : afterEndNodeIndex } ;
37
+ return statements . slice ( startNodeIndex , afterEndNodeIndex === - 1 ? statements . length : afterEndNodeIndex ) ;
38
38
}
39
39
40
40
function doChange ( oldFile : SourceFile , program : Program , toMove : ToMove , changes : textChanges . ChangeTracker , host : LanguageServiceHost ) : void {
@@ -63,16 +63,15 @@ namespace ts.refactor {
63
63
64
64
// Filters imports out of the range of statements to move. Imports will be copied to the new file anyway, and may still be needed in the old file.
65
65
function getStatementsToMove ( context : RefactorContext ) : ToMove | undefined {
66
- const { statements } = context . file ;
67
- const { first , afterLast } = getFirstAndLastStatementToMove ( context ) ! ;
66
+ const rangeToMove = getRangeToMove ( context ) ;
67
+ if ( rangeToMove === undefined ) return undefined ;
68
68
const all : Statement [ ] = [ ] ;
69
69
const ranges : StatementRange [ ] = [ ] ;
70
- const rangeToMove = statements . slice ( first , afterLast ) ;
71
70
getRangesWhere ( rangeToMove , s => ! isPureImport ( s ) , ( start , afterEnd ) => {
72
71
for ( let i = start ; i < afterEnd ; i ++ ) all . push ( rangeToMove [ i ] ) ;
73
72
ranges . push ( { first : rangeToMove [ start ] , last : rangeToMove [ afterEnd - 1 ] } ) ;
74
73
} ) ;
75
- return { all, ranges } ;
74
+ return all . length === 0 ? undefined : { all, ranges } ;
76
75
}
77
76
78
77
function isPureImport ( node : Node ) : boolean {
0 commit comments