import * as vscode from 'vscode' ; import { BaseWebViewEditor } from "./baseWebViewEditor" ; import { DevToolsExtensionContext } from '../devToolsExtensionContext' ; import { StringHelper } from '../tools/stringHelper' ; import { getVSCodeDownloadUrl } from 'vscode-test/out/util' ; import { EditorsService } from '../services/editorsService' ; export class TextDocumentWebViewEditor extends BaseWebViewEditor { public document : vscode . TextDocument | undefined ; protected _devToolsContext : DevToolsExtensionContext ; protected _inUpdateMode : boolean ; constructor ( devToolsContext , title ) { super ( devToolsContext . vscodeExtensionContext , title ) ; this . _devToolsContext = devToolsContext ; this . document = undefined ; this . _inUpdateMode = false ; } public resolveCustomTextEditor ( document , webviewPanel ) { this . document = document ; this . attachToWebView ( webviewPanel ) ; this . _disposables . push ( vscode . workspace . onDidChangeTextDocument ( e => { if ( ( this . document ) && ( e . document . uri . toString ( ) === this . document . uri . toString ( ) ) ) { if ( ! this . _inUpdateMode ) this . onTextDocumentChanged ( ) ; } } ) ) ; } protected onTextDocumentChanged ( ) { } protected async updateTextDocument ( newText ) { let success = true ; if ( this . document ) { const edit = new vscode . WorkspaceEdit ( ) ; let docText = this . document . getText ( ) ; let docLen = docText . length ; let newLen = newText . length ; let startEQ = StringHelper . equalStartLength ( docText , newText ) ; let endEQ = StringHelper . equalEndLength ( docText , newText ) ; if ( ( startEQ + endEQ ) > docLen ) endEQ = docLen - startEQ ; if ( ( startEQ + endEQ ) > newLen ) endEQ = newLen - startEQ ; if ( ( startEQ + endEQ ) == docLen ) { if ( docLen != newLen ) edit . insert ( this . document . uri , this . document . positionAt ( startEQ ) , newText . substr ( startEQ , newLen - startEQ - endEQ ) ) ; } else if ( ( startEQ + endEQ ) == newLen ) { edit . delete ( this . document . uri , new vscode . Range ( this . document . positionAt ( startEQ ) , this . document . positionAt ( docLen - endEQ ) ) ) ; } else { edit . replace ( this . document . uri , new vscode . Range ( this . document . positionAt ( startEQ ) , this . document . positionAt ( docLen - endEQ ) ) , newText . substr ( startEQ , newLen - startEQ - endEQ ) ) ; } this . _inUpdateMode = true ; success = await vscode . workspace . applyEdit ( edit ) ; this . _inUpdateMode = false ; } return success ; } protected getTextDocumentAsJson ( ) { if ( ! this . document ) return undefined ; const text = this . document . getText ( ) ; if ( text . trim ( ) . length === 0 ) return { } ; return JSON . parse ( text ) ; } protected updateTextDocumentFromJson ( json ) { this . updateTextDocument ( JSON . stringify ( json , null , 2 ) ) ; } }