-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathindex.ts
More file actions
301 lines (245 loc) · 9.03 KB
/
index.ts
File metadata and controls
301 lines (245 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* index.ts
*
* Copyright (C) 2022 by Posit Software, PBC
*
* Unless you have received this program directly from Posit Software pursuant
* to the terms of a commercial license agreement with Posit Software, then
* this program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
import path from "path";
import {
ClientCapabilities,
Definition,
DocumentLink,
DocumentSymbol,
FoldingRange,
InitializeParams,
ProposedFeatures,
SelectionRange,
TextDocuments,
TextDocumentSyncKind,
WorkspaceSymbol
} from "vscode-languageserver";
import { CompletionItem, Hover, Location } from "vscode-languageserver-types";
import { createConnection } from "vscode-languageserver/node";
import { URI } from "vscode-uri";
import { TextDocument } from "vscode-languageserver-textdocument";
import { registerCustomMethods } from "./custom";
import { isWindows, LspConnection } from "core-node";
import { initQuartoContext, Document, markdownitParser, LspInitializationOptions } from "quarto-core";
import { ConfigurationManager, lsConfiguration } from "./config";
import { Logger } from "./logging";
import { languageServiceWorkspace } from "./workspace";
import { middlewareCapabilities, middlewareRegister } from "./middleware";
import { createLanguageService, IMdLanguageService } from "./service";
import { initializeQuarto } from "./quarto";
import { registerDiagnostics } from "./diagnostics";
// Create a connection for the server. The connection uses Node's IPC as a transport.
// Also include all preview / proposed LSP features.
const connection = createConnection(ProposedFeatures.all);
// Initialize logger
const logger = new Logger(console.log.bind(console));
// Create text document manager
const documents: TextDocuments<Document> = new TextDocuments(TextDocument);
documents.listen(connection);
// Configuration
const configManager = new ConfigurationManager(connection, logger);
const config = lsConfiguration(configManager);
// Capabilities
let capabilities: ClientCapabilities | undefined;
// Initialization options
let initializationOptions: LspInitializationOptions | undefined;
// Markdown language service
let mdLs: IMdLanguageService | undefined;
connection.onInitialize((params: InitializeParams) => {
// Set log level from initialization options if provided so that we use the
// expected level as soon as possible
const initLogLevel = Logger.parseLogLevel(
params.initializationOptions?.logLevel ?? "warn"
);
logger.init(initLogLevel);
configManager.init(initLogLevel);
// We're connected, log messages via LSP
logger.setConnection(connection);
logger.logRequest('initialize');
// alias options and capabilities
initializationOptions = params.initializationOptions;
capabilities = params.capabilities;
connection.onCompletion(async (params, token): Promise<CompletionItem[]> => {
logger.logRequest('completion');
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return mdLs?.getCompletionItems(document, params.position, params.context, config, token) || [];
});
connection.onHover(async (params, token): Promise<Hover | null | undefined> => {
logger.logRequest('hover');
const document = documents.get(params.textDocument.uri);
if (!document) {
return null;
}
return mdLs?.getHover(document, params.position, config, token);
});
connection.onDocumentLinks(async (params, token): Promise<DocumentLink[]> => {
logger.logRequest('documentLinks');
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return mdLs?.getDocumentLinks(document, token) || [];
});
connection.onDocumentLinkResolve(async (link, token): Promise<DocumentLink | undefined> => {
logger.logRequest('documentLinksResolve');
return mdLs?.resolveDocumentLink(link, token);
});
connection.onDocumentSymbol(async (params, token): Promise<DocumentSymbol[]> => {
logger.logRequest('documentSymbol');
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return mdLs?.getDocumentSymbols(document, { includeLinkDefinitions: true }, token) || [];
});
connection.onFoldingRanges(async (params, token): Promise<FoldingRange[]> => {
logger.logRequest('foldingRanges');
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return mdLs?.getFoldingRanges(document, token) || [];
});
connection.onSelectionRanges(async (params, token): Promise<SelectionRange[] | undefined> => {
logger.logRequest('selectionRanges');
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return mdLs?.getSelectionRanges(document, params.positions, token);
});
connection.onWorkspaceSymbol(async (params, token): Promise<WorkspaceSymbol[]> => {
logger.logRequest('workspaceSymbol');
return mdLs?.getWorkspaceSymbols(params.query, token) || [];
});
connection.onReferences(async (params, token): Promise<Location[]> => {
logger.logRequest('references');
const document = documents.get(params.textDocument.uri);
if (!document) {
return [];
}
return mdLs?.getReferences(document, params.position, params.context, token) || [];
});
connection.onDefinition(async (params, token): Promise<Definition | undefined> => {
logger.logRequest('definition');
const document = documents.get(params.textDocument.uri);
if (!document) {
return undefined;
}
return mdLs?.getDefinition(document, params.position, token);
});
// register no-op methods to enable client middleware
middlewareRegister(connection);
return {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Incremental,
completionProvider: {
resolveProvider: false,
// register a superset of all trigger characters for embedded languages
// (languages are responsible for declaring which one they support if any)
triggerCharacters: [".", "$", "@", ":", "\\", "=", "/", "#"],
},
hoverProvider: true,
definitionProvider: true,
documentLinkProvider: { resolveProvider: true },
documentSymbolProvider: true,
foldingRangeProvider: true,
referencesProvider: true,
selectionRangeProvider: true,
workspaceSymbolProvider: true,
workspace: {
workspaceFolders: {
supported: true,
changeNotifications: true,
},
},
...middlewareCapabilities()
},
};
});
// listen for color theme changes from the client
connection.onNotification("quarto/didChangeActiveColorTheme", (params: { kind: 'light' | 'dark'; }) => {
configManager.setComputedThemeKind(params.kind);
});
// further config dependent initialization
connection.onInitialized(async () => {
logger.logNotification('initialized');
// sync config if possible
if (capabilities?.workspace?.configuration) {
await configManager.subscribe();
logger.setConfigurationManager(configManager);
}
// initialize connection to quarto
const workspaceFolders = await connection.workspace.getWorkspaceFolders();
const workspaceDir = workspaceFolders?.length
? URI.parse(workspaceFolders[0].uri).fsPath
: undefined;
// if we were passed a quarto bin path then use that
let quartoBinPath: string | undefined;
if (initializationOptions?.quartoBinPath) {
quartoBinPath = path.join(initializationOptions?.quartoBinPath, isWindows() ? "quarto.exe" : "quarto");
}
// initialize quarto
const quartoContext = initQuartoContext(
quartoBinPath || configManager.getSettings().quarto.path,
workspaceDir
);
const quarto = await initializeQuarto(quartoContext);
// initialize workspace
const workspace = languageServiceWorkspace(
workspaceFolders?.map(value => URI.parse(value.uri)) || [],
documents,
connection,
capabilities!,
config,
logger
);
// initialize parser
const parser = markdownitParser();
// create language service
// note that `mdLs` is referenced in `connection.onInitialize` above
mdLs = createLanguageService({
config,
quarto,
workspace,
documents,
parser,
logger
});
// dynamic diagnostics registration
registerDiagnostics(
connection,
workspace,
documents,
mdLs,
configManager,
logger
);
// create lsp connection (jsonrpc bridge)
const lspConnection: LspConnection = {
onRequest(method: string, handler: (params: unknown[]) => Promise<unknown>) {
return connection.onRequest(method, handler);
}
};
// register custom methods
registerCustomMethods(quarto, lspConnection, documents);
});
// ensure that the deno runtime won't exit b/c of the event queue being empty
setInterval(() => { /* */ }, 1000);
// listen
connection.listen();