import * as vscode from 'vscode' ; import { DevToolsExtensionContext } from "../devToolsExtensionContext" ; import { AZSymbolInformation } from '../symbollibraries/azSymbolInformation' ; import { TextRange } from '../symbollibraries/textRange' ; import { TextEditorHelper } from '../tools/textEditorHelper' ; import { ISyntaxModifierResult } from './iSntaxModifierResult' ; export class SyntaxModifier { protected _context : DevToolsExtensionContext ; protected _showProgress : boolean ; protected _progressMessage : string ; name : string ; modifiedFilesOnly : boolean ; constructor ( context , newName ) { this . _context = context ; this . _showProgress = true ; this . _progressMessage = "Processing project files. Please wait..." ; this . name = newName ; this . modifiedFilesOnly = false ; } async runForWorkspace ( ) { let confirmation = await this . confirmRunForWorkspace ( ) ; if ( ! confirmation ) return ; let workspaceUri = TextEditorHelper . getActiveWorkspaceFolderUri ( ) ; vscode . workspace . saveAll ( ) ; if ( ! workspaceUri ) return ; let cont = await this . askForParameters ( workspaceUri ) ; if ( ! cont ) { vscode . window . showInformationMessage ( "Command cancelled" ) ; return ; } let result = await this . runForWorkspaceWithoutUI ( workspaceUri ) ; if ( ( result ) && ( result . message ) ) { if ( result . success ) vscode . window . showInformationMessage ( result . message ) ; else vscode . window . showErrorMessage ( result . message ) ; } } async runForWorkspaceWithoutUI ( workspaceUri ) { return undefined ; } protected async confirmRunForWorkspace ( ) { let confirmation = await vscode . window . showInformationMessage ( 'Do you want to run this command for all files in the current project folder?' , 'Yes' , 'No' ) ; return ( confirmation === 'Yes' ) ; } protected getParameters ( uri ) { let values = { } ; if ( this . modifiedFilesOnly ) values . modifiedFilesOnly = true ; return values ; } async runForActiveEditor ( ) { if ( ! vscode . window . activeTextEditor ) return ; await this . runForDocument ( vscode . window . activeTextEditor . document , undefined , true ) ; } async runForDocumentSymbol ( document , symbol , withUI ) { await this . runForDocument ( document , symbol . range , withUI ) ; } async runForDocument ( document , range , withUI ) { let text = document . getText ( ) ; if ( ! text ) return ; let workspaceUri = TextEditorHelper . getActiveWorkspaceFolderUri ( ) ; if ( ( ! workspaceUri ) || ( ! document . uri ) || ( ! document . uri . fsPath ) ) return ; let cont = await this . askForParameters ( document . uri ) ; if ( ! cont ) { vscode . window . showInformationMessage ( "Command cancelled" ) ; return ; } let result = await this . runForDocumentWithoutUI ( text , workspaceUri , document . uri , range ) ; if ( result ) { if ( ! result . success ) { if ( ( withUI ) && ( result . message ) ) vscode . window . showErrorMessage ( result . message ) ; } else { if ( ( result . source ) && ( result . source != text ) ) { text = result . source ; const edit = new vscode . WorkspaceEdit ( ) ; var firstLine = document . lineAt ( 0 ) ; var lastLine = document . lineAt ( document . lineCount - 1 ) ; var textRange = new vscode . Range ( 0 , firstLine . range . start . character , document . lineCount - 1 , lastLine . range . end . character ) ; edit . replace ( document . uri , textRange , text ) ; await vscode . workspace . applyEdit ( edit ) ; } if ( ( withUI ) && ( result . message ) ) vscode . window . showInformationMessage ( result . message ) ; } } else if ( withUI ) vscode . window . showInformationMessage ( 'There was nothing to change.' ) ; } protected loadDefaultParameters ( uri ) { return false ; } async runForDocumentWithoutUI ( text , workspaceUri , documentUri , range ) { return undefined ; } async loadDefaultOrAskForParameters ( uri ) { if ( this . loadDefaultParameters ( uri ) ) return true ; return await this . askForParameters ( uri ) ; } async askForParameters ( uri ) { return true ; } hideProgress ( ) { this . _showProgress = false ; } }