@@ -52,9 +52,11 @@ import {
52
52
getNewLineKind ,
53
53
getNewLineOrDefaultFromHost ,
54
54
getNodeId ,
55
+ getOriginalNode ,
55
56
getPrecedingNonSpaceCharacterPosition ,
56
57
getScriptKindFromFileName ,
57
58
getShebang ,
59
+ getSourceFileOfNode ,
58
60
getStartPositionOfLine ,
59
61
getTokenAtPosition ,
60
62
getTouchingToken ,
@@ -1238,9 +1240,12 @@ namespace changesToText {
1238
1240
1239
1241
const textChanges = mapDefined ( normalized , c => {
1240
1242
const span = createTextSpanFromRange ( c . range ) ;
1241
- const newText = computeNewText ( c , sourceFile , newLineCharacter , formatContext , validate ) ;
1243
+ const targetSourceFile = c . kind === ChangeKind . ReplaceWithSingleNode ? getSourceFileOfNode ( getOriginalNode ( c . node ) ) ?? c . sourceFile :
1244
+ c . kind === ChangeKind . ReplaceWithMultipleNodes ? getSourceFileOfNode ( getOriginalNode ( c . nodes [ 0 ] ) ) ?? c . sourceFile :
1245
+ c . sourceFile ;
1246
+ const newText = computeNewText ( c , targetSourceFile , sourceFile , newLineCharacter , formatContext , validate ) ;
1242
1247
// Filter out redundant changes.
1243
- if ( span . length === newText . length && stringContainsAt ( sourceFile . text , newText , span . start ) ) {
1248
+ if ( span . length === newText . length && stringContainsAt ( targetSourceFile . text , newText , span . start ) ) {
1244
1249
return undefined ;
1245
1250
}
1246
1251
@@ -1264,7 +1269,7 @@ namespace changesToText {
1264
1269
return applyChanges ( nonFormattedText , changes ) + newLineCharacter ;
1265
1270
}
1266
1271
1267
- function computeNewText ( change : Change , sourceFile : SourceFile , newLineCharacter : string , formatContext : formatting . FormatContext , validate : ValidateNonFormattedText | undefined ) : string {
1272
+ function computeNewText ( change : Change , targetSourceFile : SourceFile , sourceFile : SourceFile , newLineCharacter : string , formatContext : formatting . FormatContext , validate : ValidateNonFormattedText | undefined ) : string {
1268
1273
if ( change . kind === ChangeKind . Remove ) {
1269
1274
return "" ;
1270
1275
}
@@ -1273,26 +1278,26 @@ namespace changesToText {
1273
1278
}
1274
1279
1275
1280
const { options = { } , range : { pos } } = change ;
1276
- const format = ( n : Node ) => getFormattedTextOfNode ( n , sourceFile , pos , options , newLineCharacter , formatContext , validate ) ;
1281
+ const format = ( n : Node ) => getFormattedTextOfNode ( n , targetSourceFile , sourceFile , pos , options , newLineCharacter , formatContext , validate ) ;
1277
1282
const text = change . kind === ChangeKind . ReplaceWithMultipleNodes
1278
1283
? change . nodes . map ( n => removeSuffix ( format ( n ) , newLineCharacter ) ) . join ( change . options ?. joiner || newLineCharacter )
1279
1284
: format ( change . node ) ;
1280
1285
// strip initial indentation (spaces or tabs) if text will be inserted in the middle of the line
1281
- const noIndent = ( options . indentation !== undefined || getLineStartPositionForPosition ( pos , sourceFile ) === pos ) ? text : text . replace ( / ^ \s + / , "" ) ;
1286
+ const noIndent = ( options . indentation !== undefined || getLineStartPositionForPosition ( pos , targetSourceFile ) === pos ) ? text : text . replace ( / ^ \s + / , "" ) ;
1282
1287
return ( options . prefix || "" ) + noIndent
1283
1288
+ ( ( ! options . suffix || endsWith ( noIndent , options . suffix ) )
1284
1289
? "" : options . suffix ) ;
1285
1290
}
1286
1291
1287
1292
/** Note: this may mutate `nodeIn`. */
1288
- function getFormattedTextOfNode ( nodeIn : Node , sourceFile : SourceFile , pos : number , { indentation, prefix, delta } : InsertNodeOptions , newLineCharacter : string , formatContext : formatting . FormatContext , validate : ValidateNonFormattedText | undefined ) : string {
1289
- const { node, text } = getNonformattedText ( nodeIn , sourceFile , newLineCharacter ) ;
1293
+ function getFormattedTextOfNode ( nodeIn : Node , targetSourceFile : SourceFile , sourceFile : SourceFile , pos : number , { indentation, prefix, delta } : InsertNodeOptions , newLineCharacter : string , formatContext : formatting . FormatContext , validate : ValidateNonFormattedText | undefined ) : string {
1294
+ const { node, text } = getNonformattedText ( nodeIn , targetSourceFile , newLineCharacter ) ;
1290
1295
if ( validate ) validate ( node , text ) ;
1291
- const formatOptions = getFormatCodeSettingsForWriting ( formatContext , sourceFile ) ;
1296
+ const formatOptions = getFormatCodeSettingsForWriting ( formatContext , targetSourceFile ) ;
1292
1297
const initialIndentation =
1293
1298
indentation !== undefined
1294
1299
? indentation
1295
- : formatting . SmartIndenter . getIndentation ( pos , sourceFile , formatOptions , prefix === newLineCharacter || getLineStartPositionForPosition ( pos , sourceFile ) === pos ) ;
1300
+ : formatting . SmartIndenter . getIndentation ( pos , sourceFile , formatOptions , prefix === newLineCharacter || getLineStartPositionForPosition ( pos , targetSourceFile ) === pos ) ;
1296
1301
if ( delta === undefined ) {
1297
1302
delta = formatting . SmartIndenter . shouldIndentChildNode ( formatOptions , nodeIn ) ? ( formatOptions . indentSize || 0 ) : 0 ;
1298
1303
}
@@ -1303,7 +1308,7 @@ namespace changesToText {
1303
1308
return getLineAndCharacterOfPosition ( this , pos ) ;
1304
1309
}
1305
1310
} ;
1306
- const changes = formatting . formatNodeGivenIndentation ( node , file , sourceFile . languageVariant , initialIndentation , delta , { ...formatContext , options : formatOptions } ) ;
1311
+ const changes = formatting . formatNodeGivenIndentation ( node , file , targetSourceFile . languageVariant , initialIndentation , delta , { ...formatContext , options : formatOptions } ) ;
1307
1312
return applyChanges ( text , changes ) ;
1308
1313
}
1309
1314
0 commit comments