Path: blob/main/extensions/json-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* JSON language participant contribution.9*/10interface LanguageParticipantContribution {11/**12* The id of the language which participates with the JSON language server.13*/14languageId: string;15/**16* true if the language allows comments and false otherwise.17* TODO: implement server side setting18*/19comments?: boolean;20}2122export interface LanguageParticipants {23readonly onDidChange: Event<void>;24readonly documentSelector: string[];25hasLanguage(languageId: string): boolean;26useComments(languageId: string): boolean;27dispose(): void;28}2930export function getLanguageParticipants(): LanguageParticipants {31const onDidChangeEmmiter = new EventEmitter<void>();32let languages = new Set<string>();33let comments = new Set<string>();3435function update() {36const oldLanguages = languages, oldComments = comments;3738languages = new Set();39languages.add('json');40languages.add('jsonc');41languages.add('snippets');42comments = new Set();43comments.add('jsonc');44comments.add('snippets');4546for (const extension of extensions.allAcrossExtensionHosts) {47const jsonLanguageParticipants = extension.packageJSON?.contributes?.jsonLanguageParticipants as LanguageParticipantContribution[];48if (Array.isArray(jsonLanguageParticipants)) {49for (const jsonLanguageParticipant of jsonLanguageParticipants) {50const languageId = jsonLanguageParticipant.languageId;51if (typeof languageId === 'string') {52languages.add(languageId);53if (jsonLanguageParticipant.comments === true) {54comments.add(languageId);55}56}57}58}59}60return !isEqualSet(languages, oldLanguages) || !isEqualSet(comments, oldComments);61}62update();6364const changeListener = extensions.onDidChange(_ => {65if (update()) {66onDidChangeEmmiter.fire();67}68});6970return {71onDidChange: onDidChangeEmmiter.event,72get documentSelector() { return Array.from(languages); },73hasLanguage(languageId: string) { return languages.has(languageId); },74useComments(languageId: string) { return comments.has(languageId); },75dispose: () => changeListener.dispose()76};77}7879function isEqualSet<T>(s1: Set<T>, s2: Set<T>) {80if (s1.size !== s2.size) {81return false;82}83for (const e of s1) {84if (!s2.has(e)) {85return false;86}87}88return true;89}909192