Files
vscode-extension-samples/lsp-embedded-request-forwarding/server/src/server.ts

113 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-03-25 12:01:54 -07:00
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import {
CompletionList,
createConnection,
Diagnostic,
InitializeParams,
ProposedFeatures,
2020-04-01 11:54:04 -07:00
TextDocuments,
TextDocumentSyncKind
2020-03-25 12:01:54 -07:00
} from 'vscode-languageserver';
import { getLanguageModes, LanguageModes } from './languageModes';
2020-04-01 11:54:04 -07:00
import { TextDocument } from 'vscode-languageserver-textdocument';
2020-03-25 12:01:54 -07:00
// Create a connection for the server. The connection uses Node's IPC as a transport.
// Also include all preview / proposed LSP features.
let connection = createConnection(ProposedFeatures.all);
// Create a simple text document manager. The text document manager
// supports full document sync only
2020-04-01 11:54:04 -07:00
let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
2020-03-25 12:01:54 -07:00
let languageModes: LanguageModes;
connection.onInitialize((_params: InitializeParams) => {
languageModes = getLanguageModes();
documents.onDidClose(e => {
languageModes.onDocumentRemoved(e.document);
});
connection.onShutdown(() => {
languageModes.dispose();
});
return {
capabilities: {
2020-04-01 11:54:04 -07:00
textDocumentSync: TextDocumentSyncKind.Full,
2020-03-25 12:01:54 -07:00
// Tell the client that the server supports code completion
completionProvider: {
resolveProvider: false
}
}
};
});
connection.onInitialized(() => {});
connection.onDidChangeConfiguration(_change => {
// Revalidate all open text documents
documents.all().forEach(validateTextDocument);
});
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidChangeContent(change => {
validateTextDocument(change.document);
});
async function validateTextDocument(textDocument: TextDocument) {
try {
const version = textDocument.version;
const diagnostics: Diagnostic[] = [];
2020-04-07 12:47:46 -07:00
if (textDocument.languageId === 'html1') {
2020-03-25 12:01:54 -07:00
const modes = languageModes.getAllModesInDocument(textDocument);
const latestTextDocument = documents.get(textDocument.uri);
if (latestTextDocument && latestTextDocument.version === version) {
// check no new version has come in after in after the async op
modes.forEach(mode => {
if (mode.doValidation) {
mode.doValidation(latestTextDocument).forEach(d => {
diagnostics.push(d);
});
}
});
connection.sendDiagnostics({ uri: latestTextDocument.uri, diagnostics });
}
}
} catch (e) {
connection.console.error(`Error while validating ${textDocument.uri}`);
connection.console.error(e);
}
}
connection.onDidChangeWatchedFiles(_change => {
// Monitored files have change in VSCode
connection.console.log('We received an file change event');
});
// This handler provides the initial list of the completion items.
connection.onCompletion(async (textDocumentPosition, token) => {
const document = documents.get(textDocumentPosition.textDocument.uri);
if (!document) {
return null;
}
const mode = languageModes.getModeAtPosition(document, textDocumentPosition.position);
if (!mode || !mode.doComplete) {
return CompletionList.create();
}
const doComplete = mode.doComplete!;
return doComplete(document, textDocumentPosition.position);
});
// Make the text document manager listen on the connection
// for open, change and close text document events
documents.listen(connection);
// Listen on the connection
connection.listen();