Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/html-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
* HTML language participant contribution.
10
*/
11
interface LanguageParticipantContribution {
12
/**
13
* The id of the language which participates with the HTML language server.
14
*/
15
languageId: string;
16
/**
17
* true if the language activates the auto insertion and false otherwise.
18
*/
19
autoInsert?: boolean;
20
}
21
22
export interface LanguageParticipants {
23
readonly onDidChange: Event<void>;
24
readonly documentSelector: string[];
25
hasLanguage(languageId: string): boolean;
26
useAutoInsert(languageId: string): boolean;
27
dispose(): void;
28
}
29
30
export function getLanguageParticipants(): LanguageParticipants {
31
const onDidChangeEmmiter = new EventEmitter<void>();
32
let languages = new Set<string>();
33
let autoInsert = new Set<string>();
34
35
function update() {
36
const oldLanguages = languages, oldAutoInsert = autoInsert;
37
38
languages = new Set();
39
languages.add('html');
40
autoInsert = new Set();
41
autoInsert.add('html');
42
43
for (const extension of extensions.allAcrossExtensionHosts) {
44
const htmlLanguageParticipants = extension.packageJSON?.contributes?.htmlLanguageParticipants as LanguageParticipantContribution[];
45
if (Array.isArray(htmlLanguageParticipants)) {
46
for (const htmlLanguageParticipant of htmlLanguageParticipants) {
47
const languageId = htmlLanguageParticipant.languageId;
48
if (typeof languageId === 'string') {
49
languages.add(languageId);
50
if (htmlLanguageParticipant.autoInsert !== false) {
51
autoInsert.add(languageId);
52
}
53
}
54
}
55
}
56
}
57
return !isEqualSet(languages, oldLanguages) || !isEqualSet(autoInsert, oldAutoInsert);
58
}
59
update();
60
61
const changeListener = extensions.onDidChange(_ => {
62
if (update()) {
63
onDidChangeEmmiter.fire();
64
}
65
});
66
67
return {
68
onDidChange: onDidChangeEmmiter.event,
69
get documentSelector() { return Array.from(languages); },
70
hasLanguage(languageId: string) { return languages.has(languageId); },
71
useAutoInsert(languageId: string) { return autoInsert.has(languageId); },
72
dispose: () => changeListener.dispose()
73
};
74
}
75
76
function isEqualSet<T>(s1: Set<T>, s2: Set<T>) {
77
if (s1.size !== s2.size) {
78
return false;
79
}
80
for (const e of s1) {
81
if (!s2.has(e)) {
82
return false;
83
}
84
}
85
return true;
86
}
87
88