forked from redhat-developer/vscode-xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlanguageParticipants.ts
89 lines (79 loc) · 2.56 KB
/
languageParticipants.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Event, EventEmitter, extensions } from 'vscode';
import { DocumentFilter, DocumentSelector } from 'vscode-languageclient';
/**
* XML language participant contribution.
*/
interface LanguageParticipantContribution {
/**
* The id of the language which participates with the XML language server.
*/
languageId: string;
}
export interface LanguageParticipants {
readonly onDidChange: Event<void>;
readonly documentSelector: DocumentSelector;
hasLanguage(languageId: string): boolean;
dispose(): void;
}
export function getLanguageParticipants(): LanguageParticipants {
const onDidChangeEmmiter = new EventEmitter<void>();
let languages = new Set<string>();
function update() {
const oldLanguages = languages;
languages = new Set<string>();
languages.add('xml');
languages.add('xsl');
languages.add('dtd');
languages.add('svg');
for (const extension of extensions.all /*extensions.allAcrossExtensionHosts*/) {
const xmlLanguageParticipants = extension.packageJSON?.contributes?.xmlLanguageParticipants as LanguageParticipantContribution[];
if (Array.isArray(xmlLanguageParticipants)) {
for (const xmlLanguageParticipant of xmlLanguageParticipants) {
const languageId = xmlLanguageParticipant.languageId;
if (typeof languageId === 'string') {
languages.add(languageId);
}
}
}
}
return !isEqualSet(languages, oldLanguages);
}
update();
const changeListener = extensions.onDidChange(_ => {
if (update()) {
onDidChangeEmmiter.fire();
}
});
return {
onDidChange: onDidChangeEmmiter.event,
get documentSelector() {
return Array.from(languages) //
.flatMap(language => {
return [{
language,
scheme: 'file',
}, {
language,
scheme: 'untitled',
}] as DocumentFilter[];
});
},
hasLanguage(languageId: string) { return languages.has(languageId); },
dispose: () => changeListener.dispose()
};
}
function isEqualSet<T>(s1: Set<T>, s2: Set<T>) {
if (s1.size !== s2.size) {
return false;
}
for (const e of s1) {
if (!s2.has(e)) {
return false;
}
}
return true;
}