Remove middleware

This commit is contained in:
Dirk Baeumer
2018-06-01 16:39:40 +02:00
parent 4fc9232e88
commit 57d8227721
2 changed files with 12 additions and 76 deletions

View File

@ -6,67 +6,14 @@
import * as path from 'path';
import { workspace, ExtensionContext, WorkspaceConfiguration, Disposable } from 'vscode';
import { workspace, ExtensionContext } from 'vscode';
import {
LanguageClient, LanguageClientOptions, ServerOptions, TransportKind, CancellationToken, Middleware,
DidChangeConfigurationNotification, ConfigurationParams
LanguageClient, LanguageClientOptions, ServerOptions, TransportKind
} from 'vscode-languageclient';
// The example settings
interface MultiRootExampleSettings {
maxNumberOfProblems: number;
}
let client: LanguageClient;
namespace Configuration {
let configurationListener: Disposable;
// Convert VS Code specific settings to a format acceptable by the server. Since
// both client and server do use JSON the conversion is trivial.
export function computeConfiguration(params: ConfigurationParams, _token: CancellationToken, _next: Function): any[] {
if (!params.items) {
return null;
}
let result: (MultiRootExampleSettings | null)[] = [];
for (let item of params.items) {
// The server asks the client for configuration settings without a section
// If a section is present we return null to indicate that the configuration
// is not supported.
if (item.section) {
result.push(null);
continue;
}
let config: WorkspaceConfiguration;
if (item.scopeUri) {
config = workspace.getConfiguration('languageServerExample', client.protocol2CodeConverter.asUri(item.scopeUri));
} else {
config = workspace.getConfiguration('languageServerExample');
}
result.push({
maxNumberOfProblems: config.get('maxNumberOfProblems')
});
}
return result;
}
export function initialize() {
// VS Code currently doesn't sent fine grained configuration changes. So we
// listen to any change. However this will change in the near future.
configurationListener = workspace.onDidChangeConfiguration(() => {
client.sendNotification(DidChangeConfigurationNotification.type, { settings: null });
});
}
export function dispose() {
if (configurationListener) {
configurationListener.dispose();
}
}
}
export function activate(context: ExtensionContext) {
// The server is implemented in node
@ -81,12 +28,6 @@ export function activate(context: ExtensionContext) {
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
}
let middleware: Middleware = {
workspace: {
configuration: Configuration.computeConfiguration
}
};
// Options to control the language client
let clientOptions: LanguageClientOptions = {
// Register the server for plain text documents
@ -94,12 +35,7 @@ export function activate(context: ExtensionContext) {
synchronize: {
// Notify the server about file changes to '.clientrc files contain in the workspace
fileEvents: workspace.createFileSystemWatcher('**/.clientrc'),
// In the past this told the client to actively synchronize settings. Since the
// client now supports 'getConfiguration' requests this active synchronization is not
// necessary anymore.
// configurationSection: [ 'lspMultiRootSample' ]
},
middleware: middleware
}
}
// Create the language client and start the client.
@ -113,6 +49,5 @@ export function deactivate(): Thenable<void> {
if (!client) {
return undefined;
}
Configuration.dispose();
return client.stop();
}

View File

@ -40,6 +40,7 @@ connection.onInitialize((params: InitializeParams) => {
connection.onInitialized(() => {
if (hasConfigurationCapability) {
// Register for all conifiguration changes.
connection.client.register(DidChangeConfigurationNotification.type, undefined);
}
if (hasWorkspaceFolderCapability) {
@ -50,38 +51,38 @@ connection.onInitialized(() => {
});
// The example settings
interface MultiRootExampleSettings {
interface Settings {
maxNumberOfProblems: number;
}
// The global settings, used when the `workspace/configuration` request is not supported by the client.
// Please note that this is not the case when using this server with the client provided in this example
// but could happen with other clients.
const defaultSettings: MultiRootExampleSettings = { maxNumberOfProblems: 1000 };
let globalSettings: MultiRootExampleSettings = defaultSettings;
const defaultSettings: Settings = { maxNumberOfProblems: 1000 };
let globalSettings: Settings = defaultSettings;
// Cache the settings of all open documents
let documentSettings: Map<string, Thenable<MultiRootExampleSettings>> = new Map();
let documentSettings: Map<string, Thenable<Settings>> = new Map();
connection.onDidChangeConfiguration(change => {
if (hasConfigurationCapability) {
// Reset all cached document settings
documentSettings.clear();
} else {
globalSettings = <MultiRootExampleSettings>(change.settings.lspMultiRootSample || defaultSettings);
globalSettings = <Settings>(change.settings.lspMultiRootSample || defaultSettings);
}
// Revalidate all open text documents
documents.all().forEach(validateTextDocument);
});
function getDocumentSettings(resource: string): Thenable<MultiRootExampleSettings> {
function getDocumentSettings(resource: string): Thenable<Settings> {
if (!hasConfigurationCapability) {
return Promise.resolve(globalSettings);
}
let result = documentSettings.get(resource);
if (!result) {
result = connection.workspace.getConfiguration({ scopeUri: resource });
result = connection.workspace.getConfiguration({ scopeUri: resource, section: 'languageServerExample' });
documentSettings.set(resource, result);
}
return result;