import * as fs from 'fs' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { BUILDER_VIEW_TYPE , HTML_FILE , SOQL_BUILDER_UI_PATH , SOQL_BUILDER_WEB_ASSETS_PATH } from '../constants' ; import { nls } from '../messages' ; import { channelService , isDefaultOrgSet } from '../sfdx' ; import { HtmlUtils } from './htmlUtils' ; import { SOQLEditorInstance } from './soqlEditorInstance' ; export class SOQLEditorProvider implements vscode . CustomTextEditorProvider { public static register ( context ) { const provider = new SOQLEditorProvider ( context ) ; const providerRegistration = vscode . window . registerCustomEditorProvider ( BUILDER_VIEW_TYPE , provider ) ; return providerRegistration ; } private instances : SOQLEditorInstance [ ] = [ ] ; constructor ( private readonly context ) { } public async resolveCustomTextEditor ( document , webviewPanel , _token ) { webviewPanel . webview . options = { enableScripts : true , localResourceRoots : [ vscode . Uri . file ( path . join ( this . context . extensionPath , SOQL_BUILDER_WEB_ASSETS_PATH ) ) ] } ; webviewPanel . webview . html = this . getWebViewContent ( webviewPanel . webview ) ; const instance = new SOQLEditorInstance ( document , webviewPanel , _token ) ; this . instances . push ( instance ) ; instance . onDispose ( this . disposeInstance . bind ( this ) ) ; this . context . subscriptions . push ( ... instance . subscriptions ) ; if ( ! isDefaultOrgSet ( ) ) { const message = nls . localize ( 'info_no_default_org' ) ; channelService . appendLine ( message ) ; vscode . window . showInformationMessage ( message ) ; } } private getWebViewContent ( webview ) { const pathToLwcDist = path . join ( this . context . extensionPath , SOQL_BUILDER_UI_PATH ) ; const pathToHtml = path . join ( pathToLwcDist , HTML_FILE ) ; let html = fs . readFileSync ( pathToHtml ) . toString ( ) ; html = HtmlUtils . transformHtml ( html , pathToLwcDist , webview ) ; return html ; } private disposeInstance ( instance ) { const found = this . instances . findIndex ( storedInstance => { return storedInstance === instance ; } ) ; if ( found > - 1 ) { this . instances . splice ( found , 1 ) ; } } }