mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 02:58:56 +01:00
Contribute to json language server with a custom language. (#198583)
* Contribute to json language server with a custom language. * Add `snippets` to `"activationEvents"` * Remove hardcoded `snippets` from `documentSettings` * Fix wrong variable in `!isEqualSet()` * Use `extensions.allAcrossExtensionHosts` instead of `extensions.all` * enable `"enabledApiProposals"` for `extensions.allAcrossExtensionHosts` * Fix error: `Property 'allAcrossExtensionHosts' does not exist on type 'typeof extensions'` * Remove `snippets`
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import { DocumentSelector } from 'vscode-languageclient';
|
||||
import { Event, EventEmitter, extensions } from 'vscode';
|
||||
|
||||
/**
|
||||
* JSON language participant contribution.
|
||||
*/
|
||||
interface LanguageParticipantContribution {
|
||||
/**
|
||||
* The id of the language which participates with the JSON language server.
|
||||
*/
|
||||
languageId: string;
|
||||
/**
|
||||
* true if the language allows comments and false otherwise.
|
||||
* TODO: implement server side setting
|
||||
*/
|
||||
comments?: boolean;
|
||||
}
|
||||
|
||||
export interface LanguageParticipants {
|
||||
readonly onDidChange: Event<void>;
|
||||
readonly documentSelector: DocumentSelector;
|
||||
hasLanguage(languageId: string): boolean;
|
||||
useComments(languageId: string): boolean;
|
||||
dispose(): void;
|
||||
}
|
||||
|
||||
export function getLanguageParticipants(): LanguageParticipants {
|
||||
const onDidChangeEmmiter = new EventEmitter<void>();
|
||||
let languages = new Set<string>();
|
||||
let comments = new Set<string>();
|
||||
|
||||
function update() {
|
||||
const oldLanguages = languages, oldComments = comments;
|
||||
|
||||
languages = new Set();
|
||||
languages.add('json');
|
||||
languages.add('jsonc');
|
||||
comments = new Set();
|
||||
comments.add('jsonc');
|
||||
|
||||
for (const extension of extensions.allAcrossExtensionHosts) {
|
||||
const jsonLanguageParticipants = extension.packageJSON?.contributes?.jsonLanguageParticipants as LanguageParticipantContribution[];
|
||||
if (Array.isArray(jsonLanguageParticipants)) {
|
||||
for (const jsonLanguageParticipant of jsonLanguageParticipants) {
|
||||
const languageId = jsonLanguageParticipant.languageId;
|
||||
if (typeof languageId === 'string') {
|
||||
languages.add(languageId);
|
||||
if (jsonLanguageParticipant.comments === true) {
|
||||
comments.add(languageId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return !isEqualSet(languages, oldLanguages) || !isEqualSet(comments, oldComments);
|
||||
}
|
||||
update();
|
||||
|
||||
const changeListener = extensions.onDidChange(_ => {
|
||||
if (update()) {
|
||||
onDidChangeEmmiter.fire();
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
onDidChange: onDidChangeEmmiter.event,
|
||||
get documentSelector() { return Array.from(languages); },
|
||||
hasLanguage(languageId: string) { return languages.has(languageId); },
|
||||
useComments(languageId: string) { return comments.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;
|
||||
}
|
||||
Reference in New Issue
Block a user