import * as vscode from 'vscode' ; import { DefaultClient , workspaceReferences , ReferencesCancellationState , RenameParams , CancelReferencesNotification } from '../client' ; import * as refs from '../references' ; import { CppSettings } from '../settings' ; import { Position } from 'vscode-languageclient' ; import * as nls from 'vscode-nls' ; import * as util from '../../common' ; nls . config ( { messageFormat : nls . MessageFormat . bundle , bundleFormat : nls . BundleFormat . standalone } ) ( ) ; const localize = nls . loadMessageBundle ( ) ; export class RenameProvider implements vscode . RenameProvider { private client : DefaultClient ; constructor ( client ) { this . client = client ; } public async provideRenameEdits ( document , position , newName , token ) { const settings = new CppSettings ( ) ; if ( settings . renameRequiresIdentifier && ! util . isValidIdentifier ( newName ) ) { vscode . window . showErrorMessage ( localize ( "invalid.identifier.for.rename" , "Invalid identifier provided for the Rename Symbol operation." ) ) ; const workspaceEdit = new vscode . WorkspaceEdit ( ) ; return workspaceEdit ; } DefaultClient . renamePending = true ; ++ DefaultClient . renameRequestsPending ; return new Promise < vscode . WorkspaceEdit > ( ( resolve , reject ) => { const callback = async ( ) => { const params = { newName : newName , position : Position . create ( position . line , position . character ) , textDocument : this . client . languageClient . code2ProtocolConverter . asTextDocumentIdentifier ( document ) } ; DefaultClient . referencesParams = params ; await this . client . awaitUntilLanguageClientReady ( ) ; if ( params !== DefaultClient . referencesParams ) { if ( -- DefaultClient . renameRequestsPending === 0 ) { DefaultClient . renamePending = false ; } const workspaceEdit = new vscode . WorkspaceEdit ( ) ; resolve ( workspaceEdit ) ; return ; } DefaultClient . referencesRequestPending = true ; workspaceReferences . setResultsCallback ( ( referencesResult , doResolve ) => { DefaultClient . referencesRequestPending = false ; -- DefaultClient . renameRequestsPending ; const workspaceEdit = new vscode . WorkspaceEdit ( ) ; const cancelling = DefaultClient . referencesPendingCancellations . length > 0 ; if ( cancelling ) { while ( DefaultClient . referencesPendingCancellations . length > 1 ) { const pendingCancel = DefaultClient . referencesPendingCancellations [ 0 ] ; DefaultClient . referencesPendingCancellations . pop ( ) ; pendingCancel . reject ( ) ; } const pendingCancel = DefaultClient . referencesPendingCancellations [ 0 ] ; DefaultClient . referencesPendingCancellations . pop ( ) ; pendingCancel . callback ( ) ; } else { if ( DefaultClient . renameRequestsPending === 0 ) { DefaultClient . renamePending = false ; } if ( referencesResult ) { for ( const reference of referencesResult . referenceInfos ) { const uri = vscode . Uri . file ( reference . file ) ; const range = new vscode . Range ( reference . position . line , reference . position . character , reference . position . line , reference . position . character + referencesResult . text . length ) ; const metadata = { needsConfirmation : reference . type !== refs . ReferenceType . Confirmed , label : refs . getReferenceTagString ( reference . type , false , true ) , iconPath : refs . getReferenceItemIconPath ( reference . type , false ) } ; workspaceEdit . replace ( uri , range , newName , metadata ) ; } } } if ( referencesResult && ( referencesResult . referenceInfos === null || referencesResult . referenceInfos . length === 0 ) ) { vscode . window . showErrorMessage ( localize ( "unable.to.locate.selected.symbol" , "A definition for the selected symbol could not be located." ) ) ; } resolve ( workspaceEdit ) ; } ) ; workspaceReferences . startRename ( params ) ; } ; if ( DefaultClient . referencesRequestPending || workspaceReferences . symbolSearchInProgress ) { const cancelling = DefaultClient . referencesPendingCancellations . length > 0 ; DefaultClient . referencesPendingCancellations . push ( { reject : ( ) => { -- DefaultClient . renameRequestsPending ; const workspaceEdit = new vscode . WorkspaceEdit ( ) ; resolve ( workspaceEdit ) ; } , callback } ) ; if ( ! cancelling ) { workspaceReferences . referencesCanceled = true ; if ( ! DefaultClient . referencesRequestPending ) { workspaceReferences . referencesCanceledWhilePreviewing = true ; } this . client . languageClient . sendNotification ( CancelReferencesNotification ) ; } } else { callback ( ) ; } } ) ; } }