microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e966efd30e552f4e84d5ae7224515199738ddd8e

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/compiler/server/server.ts

90lines · modecode

1import { Console } from "console";
2import { fileURLToPath } from "url";
3import { TextDocument } from "vscode-languageserver-textdocument";
4import {
5 createConnection,
6 ProposedFeatures,
7 PublishDiagnosticsParams,
8 TextDocuments,
9} from "vscode-languageserver/node.js";
10import { NodeHost } from "../core/node-host.js";
11import { cadlVersion } from "../core/util.js";
12import { createServer, Server, ServerHost } from "./serverlib.js";
13
14let server: Server | undefined = undefined;
15
16process.on("unhandledRejection", fatalError);
17try {
18 main();
19} catch (e) {
20 fatalError(e);
21}
22
23function main() {
24 // Redirect all console stdout output to stderr since LSP pipe uses stdout
25 // and writing to stdout for anything other than LSP protocol will break
26 // things badly.
27 global.console = new Console(process.stderr, process.stderr);
28
29 let clientHasWorkspaceFolderCapability = false;
30 const connection = createConnection(ProposedFeatures.all);
31 const documents = new TextDocuments(TextDocument);
32
33 const host: ServerHost = {
34 compilerHost: NodeHost,
35 sendDiagnostics(params: PublishDiagnosticsParams) {
36 connection.sendDiagnostics(params);
37 },
38 log(message: string) {
39 connection.console.log(message);
40 },
41 getDocumentByURL(url: string) {
42 return documents.get(url);
43 },
44 };
45
46 const s = createServer(host);
47 server = s;
48 s.log(`Cadl language server v${cadlVersion}`);
49 s.log("Module", fileURLToPath(import.meta.url));
50 s.log("Process ID", process.pid);
51 s.log("Command Line", process.argv);
52
53 connection.onInitialize((params) => {
54 if (params.capabilities.workspace?.workspaceFolders) {
55 clientHasWorkspaceFolderCapability = true;
56 }
57 return s.initialize(params);
58 });
59
60 connection.onInitialized((params) => {
61 if (clientHasWorkspaceFolderCapability) {
62 connection.workspace.onDidChangeWorkspaceFolders(s.workspaceFoldersChanged);
63 }
64 s.initialized(params);
65 });
66
67 connection.onDidChangeWatchedFiles(s.watchedFilesChanged);
68 connection.onDefinition(s.gotoDefinition);
69 connection.onCompletion(s.complete);
70 connection.onReferences(s.findReferences);
71 connection.onRenameRequest(s.rename);
72 connection.onPrepareRename(s.prepareRename);
73 documents.onDidChangeContent(s.checkChange);
74 documents.onDidClose(s.documentClosed);
75
76 documents.listen(connection);
77 connection.listen();
78}
79
80function fatalError(e: unknown) {
81 // If we failed to send any log messages over LSP pipe, send them to
82 // stderr before exiting.
83 for (const pending of server?.pendingMessages ?? []) {
84 // eslint-disable-next-line no-console
85 console.error(pending);
86 }
87 // eslint-disable-next-line no-console
88 console.error(e);
89 process.exit(1);
90}
91