'use strict' ; import * as vscode from 'vscode' ; import * as path from 'path' ; import { BaseWebViewEditor } from '../../webviews/baseWebViewEditor' ; import { DevToolsExtensionContext } from '../../devToolsExtensionContext' ; import { ALObjectWizardSettings } from './alObjectWizardSettings' ; import { ICRSExtensionPublicApi } from 'crs-al-language-extension-api' ; import { CRSALLangExtHelper } from '../../crsAlLangExtHelper' ; import { FileBuilder } from '../fileBuilder' ; import { ToolsGetProjectSettingsRequest } from '../../langserver/toolsGetProjectSettingsRequest' ; import { ToolsGetProjectSettingsResponse } from '../../langserver/toolsGetProjectSettingsResponse' ; export class ProjectItemWizardPage extends BaseWebViewEditor { protected _toolsExtensionContext : DevToolsExtensionContext ; protected _settings : ALObjectWizardSettings ; constructor ( toolsExtensionContext , title , settings ) { super ( toolsExtensionContext . vscodeExtensionContext , title ) ; this . _toolsExtensionContext = toolsExtensionContext ; this . _settings = settings ; } protected processWebViewMessage ( message ) { if ( super . processWebViewMessage ( message ) ) return true ; switch ( message . command ) { case 'finishClick' : this . onFinish ( message . data ) ; return true ; case 'cancelClick' : this . onCancel ( ) ; return true ; } return false ; } protected async finishWizard ( data ) { return false ; } protected async onFinish ( data ) { if ( await this . finishWizard ( data ) ) this . close ( ) ; } protected onCancel ( ) { this . close ( ) ; } protected async getObjectFileName ( objectType , objectId , objectName ) { let crsExtensionApi = await CRSALLangExtHelper . GetCrsAlLangExt ( ) ; if ( crsExtensionApi ) return crsExtensionApi . ObjectNamesApi . GetObjectFileName ( objectType , objectId , objectName ) ; else return objectName + ".al" ; } protected async getExtObjectFileName ( objectType , objectId , objectName , extendedObjectName ) { let crsExtensionApi = await CRSALLangExtHelper . GetCrsAlLangExt ( ) ; if ( crsExtensionApi ) return crsExtensionApi . ObjectNamesApi . GetObjectExtensionFileName ( objectType , objectId , objectName , "" , extendedObjectName ) ; else return objectName + ".al" ; } protected async createObjectFile ( objectType , objectId , objectName , content ) { let fileName = await this . getObjectFileName ( objectType , objectId , objectName ) ; let destPath = this . getDestFilePath ( this . _settings . destDirectoryPath , objectType ) ; if ( destPath ) { let fullPath = FileBuilder . generateObjectFileInDir ( destPath , fileName , content ) ; if ( fullPath ) FileBuilder . showFile ( fullPath ) ; } } protected async createObjectExtensionFile ( objectType , objectId , objectName , extendedObjectName , content ) { let fileName = await this . getExtObjectFileName ( objectType , objectId , objectName , extendedObjectName ) ; let destPath = this . getDestFilePath ( this . _settings . destDirectoryPath , objectType ) ; if ( destPath ) { let fullPath = FileBuilder . generateObjectFileInDir ( destPath , fileName , content ) ; if ( fullPath ) FileBuilder . showFile ( fullPath ) ; } } protected getDestFilePath ( targetPath , objectType ) { if ( targetPath ) return targetPath ; let workspacePathSelected = false ; if ( ! targetPath ) { targetPath = this . _toolsExtensionContext . alLangProxy . getCurrentWorkspaceFolderPath ( ) ; if ( ! targetPath ) return undefined ; workspacePathSelected = true ; } let settings = vscode . workspace . getConfiguration ( 'CRS' , vscode . Uri . file ( targetPath ) ) ; let saveFileAction = settings . get < string > ( 'OnSaveAlFileAction' ) ; if ( ( ! saveFileAction ) || ( saveFileAction . toLowerCase ( ) != 'reorganize' ) ) return targetPath ; if ( ! workspacePathSelected ) targetPath = this . _toolsExtensionContext . alLangProxy . getCurrentWorkspaceFolderPath ( ) ; if ( ! targetPath ) return undefined ; let alPath = settings . get < string > ( 'AlSubFolderName' ) ; if ( alPath ) targetPath = path . join ( targetPath , alPath ) ; targetPath = path . join ( targetPath , objectType . toLowerCase ( ) ) ; return targetPath ; } protected async getProjectSettings ( ) { let uri = this . _settings . getDestDirectoryUri ( ) ; if ( ! uri ) { let folders = vscode . workspace . workspaceFolders ; if ( ( folders ) && ( folders . length > 0 ) ) uri = folders [ 0 ] . uri ; } if ( uri ) return await this . _toolsExtensionContext . toolsLangServerClient . getProjectSettings ( new ToolsGetProjectSettingsRequest ( uri . fsPath ) ) ; return undefined ; } }