Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/json-language-features/client/src/languageParticipants.ts
3320 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Event, EventEmitter, extensions } from 'vscode';
7
8
/**
9
* JSON language participant contribution.
10
*/
11
interface LanguageParticipantContribution {
12
/**
13
* The id of the language which participates with the JSON language server.
14
*/
15
languageId: string;
16
/**
17
* true if the language allows comments and false otherwise.
18
* TODO: implement server side setting
19
*/
20
comments?: boolean;
21
}
22
23
export interface LanguageParticipants {
24
readonly onDidChange: Event<void>;
25
readonly documentSelector: string[];
26
hasLanguage(languageId: string): boolean;
27
useComments(languageId: string): boolean;
28
dispose(): void;
29
}
30
31
export function getLanguageParticipants(): LanguageParticipants {
32
const onDidChangeEmmiter = new EventEmitter<void>();
33
let languages = new Set<string>();
34
let comments = new Set<string>();
35
36
function update() {
37
const oldLanguages = languages, oldComments = comments;
38
39
languages = new Set();
40
languages.add('json');
41
languages.add('jsonc');
42
languages.add('snippets');
43
comments = new Set();
44
comments.add('jsonc');
45
comments.add('snippets');
46
47
for (const extension of extensions.allAcrossExtensionHosts) {
48
const jsonLanguageParticipants = extension.packageJSON?.contributes?.jsonLanguageParticipants as LanguageParticipantContribution[];
49
if (Array.isArray(jsonLanguageParticipants)) {
50
for (const jsonLanguageParticipant of jsonLanguageParticipants) {
51
const languageId = jsonLanguageParticipant.languageId;
52
if (typeof languageId === 'string') {
53
languages.add(languageId);
54
if (jsonLanguageParticipant.comments === true) {
55
comments.add(languageId);
56
}
57
}
58
}
59
}
60
}
61
return !isEqualSet(languages, oldLanguages) || !isEqualSet(comments, oldComments);
62
}
63
update();
64
65
const changeListener = extensions.onDidChange(_ => {
66
if (update()) {
67
onDidChangeEmmiter.fire();
68
}
69
});
70
71
return {
72
onDidChange: onDidChangeEmmiter.event,
73
get documentSelector() { return Array.from(languages); },
74
hasLanguage(languageId: string) { return languages.has(languageId); },
75
useComments(languageId: string) { return comments.has(languageId); },
76
dispose: () => changeListener.dispose()
77
};
78
}
79
80
function isEqualSet<T>(s1: Set<T>, s2: Set<T>) {
81
if (s1.size !== s2.size) {
82
return false;
83
}
84
for (const e of s1) {
85
if (!s2.has(e)) {
86
return false;
87
}
88
}
89
return true;
90
}
91
92