license import * as vscode from 'vscode' ; import { ServerOptions } from '../common/initialize' ; import { AngularLanguageClient } from './client' ; import { ANGULAR_SCHEME , TcbContentProvider } from './providers' ; /**
 * Represent a vscode command with an ID and an impl function `execute`.
 */ type Command = { id : string ; isTextEditorCommand : false ; execute ( ) : Promise < unknown > ; } | { id : string ; isTextEditorCommand : true ; execute ( textEditor ) : Promise < unknown > ; } ; param client param context function restartNgServer ( client ) { return { id : 'angular.restartNgServer' , isTextEditorCommand : false , async execute ( ) { await client . stop ( ) ; await client . start ( ) ; } , } ; } /**
 * Open the current server log file in a new editor.
 */ function openLogFile ( client ) { return { id : 'angular.openLogFile' , isTextEditorCommand : false , async execute ( ) { const serverOptions = client . initializeResult ?. serverOptions ; if ( ! serverOptions ?. logFile ) { const selection = await vscode . window . showErrorMessage ( `Angular server logging is off. Please set 'angular.log' and restart the server.` , 'Enable logging and restart server' ) ; if ( selection ) { const isGlobalConfig = false ; await vscode . workspace . getConfiguration ( ) . update ( 'angular.log' , 'verbose' , isGlobalConfig ) ; } return ; } const document = await vscode . workspace . openTextDocument ( serverOptions . logFile ) ; return vscode . window . showTextDocument ( document ) ; } , } ; } param ngClient param context function getTemplateTcb ( ngClient , context ) { const TCB_HIGHLIGHT_DECORATION = vscode . window . createTextEditorDecorationType ( { backgroundColor : new vscode . ThemeColor ( 'editor.selectionHighlightBackground' ) , } ) ; const tcbProvider = new TcbContentProvider ( ) ; const disposable = vscode . workspace . registerTextDocumentContentProvider ( ANGULAR_SCHEME , tcbProvider ) ; context . subscriptions . push ( disposable ) ; return { id : 'angular.getTemplateTcb' , isTextEditorCommand : true , async execute ( textEditor ) { tcbProvider . clear ( ) ; const response = await ngClient . getTcbUnderCursor ( textEditor ) ; if ( response === undefined ) { return undefined ; } const tcbUri = response . uri . with ( { scheme : ANGULAR_SCHEME , } ) ; tcbProvider . update ( tcbUri , response . content ) ; const editor = await vscode . window . showTextDocument ( tcbUri , { viewColumn : vscode . ViewColumn . Beside , preserveFocus : true , } ) ; editor . setDecorations ( TCB_HIGHLIGHT_DECORATION , response . selections ) ; } } ; } param ngClient function goToComponentWithTemplateFile ( ngClient ) { return { id : 'angular.goToComponentWithTemplateFile' , isTextEditorCommand : true , async execute ( textEditor ) { const locations = await ngClient . getComponentsForOpenExternalTemplate ( textEditor ) ; if ( locations === undefined ) { return ; } vscode . commands . executeCommand ( 'editor.action.goToLocations' , textEditor . document . uri , textEditor . selection . active , locations , 'peek' ) ; } , } ; } param ngClient function goToTemplateForComponent ( ngClient ) { return { id : 'angular.goToTemplateForComponent' , isTextEditorCommand : true , async execute ( textEditor ) { const location = await ngClient . getTemplateLocationForComponent ( textEditor ) ; if ( location === null ) { return ; } vscode . commands . executeCommand ( 'editor.action.goToLocations' , textEditor . document . uri , textEditor . selection . active , [ location ] , 'goto' ) ; } , } ; } param client param context export function registerCommands ( client , context ) { const commands = [ restartNgServer ( client ) , openLogFile ( client ) , getTemplateTcb ( client , context ) , goToComponentWithTemplateFile ( client ) , goToTemplateForComponent ( client ) , ] ; for ( const command of commands ) { const disposable = command . isTextEditorCommand ? vscode . commands . registerTextEditorCommand ( command . id , command . execute ) : vscode . commands . registerCommand ( command . id , command . execute ) ; context . subscriptions . push ( disposable ) ; } }