UNPKG

vscode-languageclient

Version:
247 lines (246 loc) 9.57 kB
"use strict"; /* -------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. * ------------------------------------------------------------------------------------------ */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.SyncConfigurationFeature = exports.ConfigurationFeature = void 0; exports.toJSONObject = toJSONObject; const vscode_1 = require("vscode"); const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol"); const Is = __importStar(require("./utils/is")); const UUID = __importStar(require("./utils/uuid")); const features_1 = require("./features"); /** * Configuration pull model. From server to client. */ class ConfigurationFeature { _client; constructor(client) { this._client = client; } getState() { return { kind: 'static' }; } fillClientCapabilities(capabilities) { capabilities.workspace = capabilities.workspace || {}; capabilities.workspace.configuration = true; } initialize() { const client = this._client; client.onRequest(vscode_languageserver_protocol_1.ConfigurationRequest.type, (params, token) => { const configuration = (params) => { const result = []; for (const item of params.items) { const resource = item.scopeUri !== void 0 && item.scopeUri !== null ? this._client.protocol2CodeConverter.asUri(item.scopeUri) : undefined; result.push(this.getConfiguration(resource, item.section !== null ? item.section : undefined)); } return result; }; const middleware = client.middleware.workspace; return middleware && middleware.configuration ? middleware.configuration(params, token, configuration) : configuration(params, token); }); } getConfiguration(resource, section) { let result = null; if (section) { const index = section.lastIndexOf('.'); if (index === -1) { result = toJSONObject(vscode_1.workspace.getConfiguration(undefined, resource).get(section)); } else { const config = vscode_1.workspace.getConfiguration(section.substr(0, index), resource); if (config) { result = toJSONObject(config.get(section.substr(index + 1))); } } } else { const config = vscode_1.workspace.getConfiguration(undefined, resource); result = {}; for (const key of Object.keys(config)) { if (config.has(key)) { result[key] = toJSONObject(config.get(key)); } } } if (result === undefined) { result = null; } return result; } clear() { } } exports.ConfigurationFeature = ConfigurationFeature; function toJSONObject(obj) { if (obj) { if (Array.isArray(obj)) { return obj.map(toJSONObject); } else if (typeof obj === 'object') { const res = Object.create(null); for (const key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { res[key] = toJSONObject(obj[key]); } } return res; } } return obj; } class SyncConfigurationFeature { _client; isCleared; _listeners; constructor(_client) { this._client = _client; this.isCleared = false; this._listeners = new Map(); } getState() { return { kind: 'workspace', id: this.registrationType.method, registrations: this._listeners.size > 0 }; } get registrationType() { return vscode_languageserver_protocol_1.DidChangeConfigurationNotification.type; } fillClientCapabilities(capabilities) { (0, features_1.ensure)((0, features_1.ensure)(capabilities, 'workspace'), 'didChangeConfiguration').dynamicRegistration = true; } initialize() { this.isCleared = false; const section = this._client.clientOptions.synchronize?.configurationSection; if (section !== undefined) { this.register({ id: UUID.generateUuid(), registerOptions: { section: section } }); } } register(data) { const disposable = vscode_1.workspace.onDidChangeConfiguration((event) => { this.onDidChangeConfiguration(data.registerOptions.section, event); }); this._listeners.set(data.id, disposable); if (data.registerOptions.section !== undefined) { this.onDidChangeConfiguration(data.registerOptions.section, undefined); } } unregister(id) { const disposable = this._listeners.get(id); if (disposable) { this._listeners.delete(id); disposable.dispose(); } } clear() { for (const disposable of this._listeners.values()) { disposable.dispose(); } this._listeners.clear(); this.isCleared = true; } onDidChangeConfiguration(configurationSection, event) { if (this.isCleared) { return; } let sections; if (Is.string(configurationSection)) { sections = [configurationSection]; } else { sections = configurationSection; } if (sections !== undefined && event !== undefined) { const affected = sections.some((section) => event.affectsConfiguration(section)); if (!affected) { return; } } const didChangeConfiguration = async (sections) => { if (sections === undefined) { return this._client.sendNotification(vscode_languageserver_protocol_1.DidChangeConfigurationNotification.type, { settings: null }); } else { return this._client.sendNotification(vscode_languageserver_protocol_1.DidChangeConfigurationNotification.type, { settings: this.extractSettingsInformation(sections) }); } }; const middleware = this._client.middleware.workspace?.didChangeConfiguration; (middleware ? middleware(sections, didChangeConfiguration) : didChangeConfiguration(sections)).catch((error) => { this._client.error(`Sending notification ${vscode_languageserver_protocol_1.DidChangeConfigurationNotification.type.method} failed`, error); }); } extractSettingsInformation(keys) { function ensurePath(config, path) { let current = config; for (let i = 0; i < path.length - 1; i++) { let obj = current[path[i]]; if (!obj) { obj = Object.create(null); current[path[i]] = obj; } current = obj; } return current; } const resource = this._client.clientOptions.workspaceFolder ? this._client.clientOptions.workspaceFolder.uri : undefined; const result = Object.create(null); for (let i = 0; i < keys.length; i++) { const key = keys[i]; const index = key.indexOf('.'); let config = null; if (index >= 0) { config = vscode_1.workspace.getConfiguration(key.substr(0, index), resource).get(key.substr(index + 1)); } else { config = vscode_1.workspace.getConfiguration(undefined, resource).get(key); } if (config) { const path = keys[i].split('.'); ensurePath(result, path)[path[path.length - 1]] = toJSONObject(config); } } return result; } } exports.SyncConfigurationFeature = SyncConfigurationFeature;