server.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* --------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. * ------------------------------------------------------------------------------------------ */
  5. import { getLanguageService } from 'vscode-html-languageservice';
  6. import { createConnection, InitializeParams, ProposedFeatures, TextDocuments, TextDocumentSyncKind } from 'vscode-languageserver';
  7. import { TextDocument } from 'vscode-languageserver-textdocument';
  8. // Create a connection for the server. The connection uses Node's IPC as a transport.
  9. // Also include all preview / proposed LSP features.
  10. let connection = createConnection(ProposedFeatures.all);
  11. // Create a simple text document manager. The text document manager
  12. // supports full document sync only
  13. let documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
  14. const htmlLanguageService = getLanguageService();
  15. connection.onInitialize((_params: InitializeParams) => {
  16. return {
  17. capabilities: {
  18. textDocumentSync: TextDocumentSyncKind.Full,
  19. // Tell the client that the server supports code completion
  20. completionProvider: {
  21. resolveProvider: false
  22. }
  23. }
  24. };
  25. });
  26. connection.onInitialized(() => { });
  27. connection.onCompletion(async (textDocumentPosition, token) => {
  28. const document = documents.get(textDocumentPosition.textDocument.uri);
  29. if (!document) {
  30. return null;
  31. }
  32. return htmlLanguageService.doComplete(
  33. document,
  34. textDocumentPosition.position,
  35. htmlLanguageService.parseHTMLDocument(document)
  36. );
  37. });
  38. documents.listen(connection);
  39. connection.listen();