Path: blob/main/extensions/html-language-features/client/src/languageParticipants.ts
3320 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Event, EventEmitter, extensions } from 'vscode';67/**8* HTML language participant contribution.9*/10interface LanguageParticipantContribution {11/**12* The id of the language which participates with the HTML language server.13*/14languageId: string;15/**16* true if the language activates the auto insertion and false otherwise.17*/18autoInsert?: boolean;19}2021export interface LanguageParticipants {22readonly onDidChange: Event<void>;23readonly documentSelector: string[];24hasLanguage(languageId: string): boolean;25useAutoInsert(languageId: string): boolean;26dispose(): void;27}2829export function getLanguageParticipants(): LanguageParticipants {30const onDidChangeEmmiter = new EventEmitter<void>();31let languages = new Set<string>();32let autoInsert = new Set<string>();3334function update() {35const oldLanguages = languages, oldAutoInsert = autoInsert;3637languages = new Set();38languages.add('html');39autoInsert = new Set();40autoInsert.add('html');4142for (const extension of extensions.allAcrossExtensionHosts) {43const htmlLanguageParticipants = extension.packageJSON?.contributes?.htmlLanguageParticipants as LanguageParticipantContribution[];44if (Array.isArray(htmlLanguageParticipants)) {45for (const htmlLanguageParticipant of htmlLanguageParticipants) {46const languageId = htmlLanguageParticipant.languageId;47if (typeof languageId === 'string') {48languages.add(languageId);49if (htmlLanguageParticipant.autoInsert !== false) {50autoInsert.add(languageId);51}52}53}54}55}56return !isEqualSet(languages, oldLanguages) || !isEqualSet(autoInsert, oldAutoInsert);57}58update();5960const changeListener = extensions.onDidChange(_ => {61if (update()) {62onDidChangeEmmiter.fire();63}64});6566return {67onDidChange: onDidChangeEmmiter.event,68get documentSelector() { return Array.from(languages); },69hasLanguage(languageId: string) { return languages.has(languageId); },70useAutoInsert(languageId: string) { return autoInsert.has(languageId); },71dispose: () => changeListener.dispose()72};73}7475function isEqualSet<T>(s1: Set<T>, s2: Set<T>) {76if (s1.size !== s2.size) {77return false;78}79for (const e of s1) {80if (!s2.has(e)) {81return false;82}83}84return true;85}868788