/******************************************************************************
 * Copyright 2021 TypeFox GmbH
 * This program and the accompanying materials are made available under the
 * terms of the MIT License, which is available in the project root.
 ******************************************************************************/ import { URI , Utils } from 'vscode-uri' ; import { LangiumServices } from './services' ; /**
 * The service registry provides access to the language-specific services. These are resolved
 * via the URI of a text document.
 */ export interface ServiceRegistry { /**
     * Register a language via its injected services.
     */ register ( language ) : void ; /**
     * Retrieve the language-specific services for the given URI. In case only one language is
     * registered, it may be used regardless of the URI format.
     */ getServices ( uri ) : LangiumServices ; /**
     * The full set of registered language services.
     */ readonly all : readonly LangiumServices [ ] ; } export class DefaultServiceRegistry implements ServiceRegistry { protected singleton ? : LangiumServices ; protected map ? : Record < string , LangiumServices > ; register ( language ) { if ( ! this . singleton && ! this . map ) { this . singleton = language ; return ; } if ( ! this . map ) { this . map = { } ; if ( this . singleton ) { for ( const ext of this . singleton . LanguageMetaData . fileExtensions ) { this . map [ ext ] = this . singleton ; } this . singleton = undefined ; } } for ( const ext of language . LanguageMetaData . fileExtensions ) { if ( this . map [ ext ] !== undefined && this . map [ ext ] !== language ) { console . warn ( `The file extension ${ ext } is used by multiple languages. It is now assigned to '${ language . LanguageMetaData . languageId }'.` ) ; } this . map [ ext ] = language ; } } getServices ( uri ) { if ( this . singleton !== undefined ) { return this . singleton ; } if ( this . map === undefined ) { throw new Error ( 'The service registry is empty. Use `register` to register the services of a language.' ) ; } const ext = Utils . extname ( uri ) ; const services = this . map [ ext ] ; if ( ! services ) { throw new Error ( `The service registry contains no services for the extension '${ ext }'.` ) ; } return services ; } get all ( ) : readonly LangiumServices [ ] { if ( this . singleton !== undefined ) { return [ this . singleton ] ; } if ( this . map !== undefined ) { return Object . values ( this . map ) ; } return [ ] ; } }