@@ -40,7 +40,6 @@ import {
40
40
FindAllReferences ,
41
41
findIndex ,
42
42
findLast ,
43
- findLastIndex ,
44
43
firstDefined ,
45
44
flatMap ,
46
45
forEachKey ,
@@ -82,7 +81,6 @@ import {
82
81
isIdentifier ,
83
82
isImportDeclaration ,
84
83
isImportEqualsDeclaration ,
85
- isNamedDeclaration ,
86
84
isNamedExports ,
87
85
isObjectLiteralExpression ,
88
86
isOmittedExpression ,
@@ -117,7 +115,6 @@ import {
117
115
PropertyAccessExpression ,
118
116
PropertyAssignment ,
119
117
QuotePreference ,
120
- rangeContainsRange ,
121
118
RefactorContext ,
122
119
RefactorEditInfo ,
123
120
RequireOrImportCall ,
@@ -182,7 +179,7 @@ registerRefactor(refactorNameForMoveToFile, {
182
179
if ( host . fileExists ( targetFile ) && program . getSourceFile ( targetFile ) === undefined ) {
183
180
return error ( getLocaleSpecificMessage ( Diagnostics . Cannot_move_statements_to_the_selected_file ) ) ;
184
181
}
185
- const edits = textChanges . ChangeTracker . with ( context , t => doChange ( context , context . file , interactiveRefactorArguments . targetFile , program , statements , t , context . host , context . preferences ) ) ;
182
+ const edits = textChanges . ChangeTracker . with ( context , t => doChange ( context , context . file , interactiveRefactorArguments . targetFile , context . program , statements , t , context . host , context . preferences ) ) ;
186
183
return { edits, renameFilename : undefined , renameLocation : undefined } ;
187
184
}
188
185
return error ( getLocaleSpecificMessage ( Diagnostics . Cannot_move_to_file_selected_file_is_invalid ) ) ;
@@ -196,7 +193,7 @@ function error(notApplicableReason: string) {
196
193
function doChange ( context : RefactorContext , oldFile : SourceFile , targetFile : string , program : Program , toMove : ToMove , changes : textChanges . ChangeTracker , host : LanguageServiceHost , preferences : UserPreferences ) : void {
197
194
const checker = program . getTypeChecker ( ) ;
198
195
const usage = getUsageInfo ( oldFile , toMove . all , checker ) ;
199
- // For a new file
196
+ //For a new file
200
197
if ( ! host . fileExists ( targetFile ) ) {
201
198
changes . createNewFile ( oldFile , targetFile , getNewStatementsAndRemoveFromOldFile ( oldFile , targetFile , usage , changes , toMove , program , host , preferences ) ) ;
202
199
addNewFileToTsconfig ( program , changes , oldFile . fileName , targetFile , hostGetCanonicalFileName ( host ) ) ;
@@ -913,28 +910,33 @@ function getRangeToMove(context: RefactorContext): RangeToMove | undefined {
913
910
const range = createTextRangeFromSpan ( getRefactorContextSpan ( context ) ) ;
914
911
const { statements } = file ;
915
912
916
- const startNodeIndex = findIndex ( statements , s => s . end > range . pos ) ;
913
+ let startNodeIndex = findIndex ( statements , s => s . end > range . pos ) ;
917
914
if ( startNodeIndex === - 1 ) return undefined ;
918
-
919
915
const startStatement = statements [ startNodeIndex ] ;
920
- if ( isNamedDeclaration ( startStatement ) && startStatement . name && rangeContainsRange ( startStatement . name , range ) ) {
921
- return { toMove : [ statements [ startNodeIndex ] ] , afterLast : statements [ startNodeIndex + 1 ] } ;
922
- }
923
916
924
917
const overloadRangeToMove = getOverloadRangeToMove ( file , startStatement ) ;
925
918
if ( overloadRangeToMove ) {
926
- return overloadRangeToMove ;
919
+ startNodeIndex = overloadRangeToMove . start ;
927
920
}
928
921
929
- // Can't only partially include the start node or be partially into the next node
930
- if ( range . pos > startStatement . getStart ( file ) ) return undefined ;
931
- const afterEndNodeIndex = findIndex ( statements , s => s . end > range . end , startNodeIndex ) ;
932
- // Can't be partially into the next node
933
- if ( afterEndNodeIndex !== - 1 && ( afterEndNodeIndex === 0 || statements [ afterEndNodeIndex ] . getStart ( file ) < range . end ) ) return undefined ;
922
+ let endNodeIndex = findIndex ( statements , s => s . end >= range . end , startNodeIndex ) ;
923
+ /**
924
+ * [|const a = 2;
925
+ * function foo() {
926
+ * }
927
+ * |]
928
+ */
929
+ if ( endNodeIndex !== - 1 && range . end <= statements [ endNodeIndex ] . getStart ( ) ) {
930
+ endNodeIndex -- ;
931
+ }
932
+ const endingOverloadRangeToMove = getOverloadRangeToMove ( file , statements [ endNodeIndex ] ) ;
933
+ if ( endingOverloadRangeToMove ) {
934
+ endNodeIndex = endingOverloadRangeToMove . end ;
935
+ }
934
936
935
937
return {
936
- toMove : statements . slice ( startNodeIndex , afterEndNodeIndex === - 1 ? statements . length : afterEndNodeIndex ) ,
937
- afterLast : afterEndNodeIndex === - 1 ? undefined : statements [ afterEndNodeIndex ] ,
938
+ toMove : statements . slice ( startNodeIndex , endNodeIndex === - 1 ? statements . length : endNodeIndex + 1 ) ,
939
+ afterLast : endNodeIndex === - 1 ? undefined : statements [ endNodeIndex + 1 ]
938
940
} ;
939
941
}
940
942
@@ -1204,10 +1206,12 @@ function getOverloadRangeToMove(sourceFile: SourceFile, statement: Statement) {
1204
1206
if ( declarations === undefined || length ( declarations ) <= 1 || ! contains ( declarations , statement ) ) {
1205
1207
return undefined ;
1206
1208
}
1209
+ const firstDecl = declarations [ 0 ] ;
1207
1210
const lastDecl = declarations [ length ( declarations ) - 1 ] ;
1208
1211
const statementsToMove = mapDefined ( declarations , d => getSourceFileOfNode ( d ) === sourceFile && isStatement ( d ) ? d : undefined ) ;
1209
- const end = findLastIndex ( sourceFile . statements , s => s . end > lastDecl . end ) ;
1210
- return { toMove : statementsToMove , afterLast : end >= 0 ? sourceFile . statements [ end ] : undefined } ;
1212
+ const end = findIndex ( sourceFile . statements , s => s . end >= lastDecl . end ) ;
1213
+ const start = findIndex ( sourceFile . statements , s => s . end >= firstDecl . end ) ;
1214
+ return { toMove : statementsToMove , start, end } ;
1211
1215
}
1212
1216
return undefined ;
1213
1217
}
0 commit comments