license import * as vscode from 'coc.nvim' ; 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 ( ) : 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 . showPrompt ( `Angular server logging is off. Please set 'angular.log' and restart the server.` ) ; if ( selection ) { const isGlobalConfig = false ; vscode . workspace . getConfiguration ( ) . update ( 'angular.log' , 'verbose' , isGlobalConfig ) ; } return ; } await vscode . workspace . openResource ( serverOptions . logFile ) ; } , } ; } param ngClient param context function getTemplateTcb ( ngClient ) { const tcbProvider = new TcbContentProvider ( ) ; return { id : 'angular.getTemplateTcb' , isTextEditorCommand : true , async execute ( ) { const response = await ngClient . getTcbUnderCursor ( ) ; if ( response === undefined ) { return undefined ; } const tcbUri = response . uri . with ( { scheme : ANGULAR_SCHEME , } ) ; await tcbProvider . update ( tcbUri , response . content ) ; await tcbProvider . show ( response . selections ) ; } } ; } param ngClient function goToComponentWithTemplateFile ( ngClient ) { return { id : 'angular.goToComponentWithTemplateFile' , isTextEditorCommand : true , async execute ( ) { const document = await vscode . workspace . document ; if ( ! document || ! document . textDocument ) { return ; } const componentLocations = await ngClient . getComponentsForOpenExternalTemplate ( document . textDocument ) ; if ( componentLocations === undefined ) { return ; } const locations = componentLocations . map ( location => vscode . Location . create ( location . uri , location . range ) ) ; if ( locations . length > 1 ) { vscode . commands . executeCommand ( 'editor.action.showReferences' , document . textDocument . uri , undefined , locations ) ; } else if ( locations [ 0 ] ) { await vscode . workspace . openResource ( locations [ 0 ] . uri ) ; await vscode . window . moveTo ( locations [ 0 ] . range . start ) ; } } , } ; } param ngClient function goToTemplateForComponent ( ngClient ) { return { id : 'angular.goToTemplateForComponent' , isTextEditorCommand : true , async execute ( ) { const document = await vscode . workspace . document ; const location = await ngClient . getTemplateLocationForComponent ( document ) ; if ( location === null ) { return ; } await vscode . workspace . openResource ( location . uri ) ; } , } ; } param client param context export function registerCommands ( client , context ) { const commands = [ restartNgServer ( client ) , openLogFile ( client ) , getTemplateTcb ( client ) , goToComponentWithTemplateFile ( client ) , goToTemplateForComponent ( client ) , ] ; for ( const command of commands ) { const disposable = command . isTextEditorCommand ? vscode . commands . registerCommand ( command . id , async ( ) => { command . execute ( ) ; } ) : vscode . commands . registerCommand ( command . id , command . execute ) ; context . subscriptions . push ( disposable ) ; } }