@@ -1514,16 +1514,18 @@ namespace ts.server {
15141514 }
15151515
15161516 if ( simplifiedResult ) {
1517- const file = result . renameFilename ;
1518- let location : protocol . Location | undefined ;
1519- if ( file !== undefined && result . renameLocation !== undefined ) {
1520- const renameScriptInfo = project . getScriptInfoForNormalizedPath ( toNormalizedPath ( file ) ) ;
1521- location = renameScriptInfo . positionToLineOffset ( result . renameLocation ) ;
1517+ const { renameFilename, renameLocation, edits } = result ;
1518+ let mappedRenameLocation : protocol . Location | undefined ;
1519+ if ( renameFilename !== undefined && renameLocation !== undefined ) {
1520+ const renameScriptInfo = project . getScriptInfoForNormalizedPath ( toNormalizedPath ( renameFilename ) ) ;
1521+ const snapshot = renameScriptInfo . getSnapshot ( ) ;
1522+ const oldText = snapshot . getText ( 0 , snapshot . getLength ( ) ) ;
1523+ mappedRenameLocation = getLocationInNewDocument ( oldText , renameFilename , renameLocation , edits ) ;
15221524 }
15231525 return {
1524- renameLocation : location ,
1525- renameFilename : file ,
1526- edits : result . edits . map ( change => this . mapTextChangesToCodeEdits ( project , change ) )
1526+ renameLocation : mappedRenameLocation ,
1527+ renameFilename,
1528+ edits : edits . map ( change => this . mapTextChangesToCodeEdits ( project , change ) )
15271529 } ;
15281530 }
15291531 else {
@@ -2040,4 +2042,26 @@ namespace ts.server {
20402042 response ?: { } ;
20412043 responseRequired ?: boolean ;
20422044 }
2045+
2046+ /* @internal */ // Exported only for tests
2047+ export function getLocationInNewDocument ( oldText : string , renameFilename : string , renameLocation : number , edits : ReadonlyArray < FileTextChanges > ) : protocol . Location {
2048+ const newText = applyEdits ( oldText , renameFilename , edits ) ;
2049+ const { line, character } = computeLineAndCharacterOfPosition ( computeLineStarts ( newText ) , renameLocation ) ;
2050+ return { line : line + 1 , offset : character + 1 } ;
2051+ }
2052+
2053+ function applyEdits ( text : string , textFilename : string , edits : ReadonlyArray < FileTextChanges > ) : string {
2054+ for ( const { fileName, textChanges } of edits ) {
2055+ if ( fileName !== textFilename ) {
2056+ continue ;
2057+ }
2058+
2059+ for ( let i = textChanges . length - 1 ; i >= 0 ; i -- ) {
2060+ const { newText, span : { start, length } } = textChanges [ i ] ;
2061+ text = text . slice ( 0 , start ) + newText + text . slice ( start + length ) ;
2062+ }
2063+ }
2064+
2065+ return text ;
2066+ }
20432067}
0 commit comments