import { OptionalVersionedTextDocumentIdentifier , TextDocumentEdit , TextEdit , WorkspaceEdit } from 'vscode-languageserver' ; import { mapRangeToOriginal } from '../../../lib/documents' ; import { urlToPath } from '../../../utils' ; import { FileRename , UpdateImportsProvider } from '../../interfaces' ; import { LSAndTSDocResolver } from '../LSAndTSDocResolver' ; import { convertRange } from '../utils' ; import { SnapshotFragmentMap } from './utils' ; export class UpdateImportsProviderImpl implements UpdateImportsProvider { constructor ( private readonly lsAndTsDocResolver ) { } async updateImports ( fileRename ) { const oldPath = urlToPath ( fileRename . oldUri ) ; const newPath = urlToPath ( fileRename . newUri ) ; if ( ! oldPath || ! newPath ) { return null ; } const ls = await this . getLSForPath ( newPath ) ; const fileChanges = ls . getEditsForFileRename ( oldPath , newPath , { } , { } ) ; await this . lsAndTsDocResolver . updateSnapshotPath ( oldPath , newPath ) ; const updateImportsChanges = fileChanges . filter ( ( change ) => ! change . isNewFile || change . fileName === oldPath ) . map ( ( change ) => { change . fileName = change . fileName . replace ( oldPath , newPath ) ; return change ; } ) ; const docs = new SnapshotFragmentMap ( this . lsAndTsDocResolver ) ; const documentChanges = await Promise . all ( updateImportsChanges . map ( async ( change ) => { const fragment = await docs . retrieveFragment ( change . fileName ) ; return TextDocumentEdit . create ( OptionalVersionedTextDocumentIdentifier . create ( fragment . getURL ( ) , null ) , change . textChanges . map ( ( edit ) => { const range = mapRangeToOriginal ( fragment , convertRange ( fragment , edit . span ) ) ; return TextEdit . replace ( range , edit . newText ) ; } ) ) ; } ) ) ; return { documentChanges } ; } private async getLSForPath ( path ) { return this . lsAndTsDocResolver . getLSForPath ( path ) ; } }