diff options
Diffstat (limited to 'extensions/markdown-language-features/server/src/server.ts')
-rw-r--r-- | extensions/markdown-language-features/server/src/server.ts | 156 |
1 files changed, 136 insertions, 20 deletions
diff --git a/extensions/markdown-language-features/server/src/server.ts b/extensions/markdown-language-features/server/src/server.ts index ad2491d9688..b5891712533 100644 --- a/extensions/markdown-language-features/server/src/server.ts +++ b/extensions/markdown-language-features/server/src/server.ts @@ -3,48 +3,92 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { Connection, InitializeParams, InitializeResult, TextDocuments } from 'vscode-languageserver'; +import { CancellationToken, Connection, InitializeParams, InitializeResult, NotebookDocuments, TextDocuments } from 'vscode-languageserver'; import { TextDocument } from 'vscode-languageserver-textdocument'; import * as lsp from 'vscode-languageserver-types'; import * as md from 'vscode-markdown-languageservice'; +import { URI } from 'vscode-uri'; +import { getLsConfiguration } from './config'; import { LogFunctionLogger } from './logging'; -import { parseRequestType } from './protocol'; +import * as protocol from './protocol'; import { VsCodeClientWorkspace } from './workspace'; -declare const TextDecoder: any; - -export function startServer(connection: Connection) { +export async function startServer(connection: Connection) { const documents = new TextDocuments(TextDocument); - documents.listen(connection); + const notebooks = new NotebookDocuments(documents); + + connection.onInitialize((params: InitializeParams): InitializeResult => { + const parser = new class implements md.IMdParser { + slugifier = md.githubSlugifier; + + async tokenize(document: md.ITextDocument): Promise<md.Token[]> { + return await connection.sendRequest(protocol.parseRequestType, { uri: document.uri.toString() }); + } + }; + + const config = getLsConfiguration({ + markdownFileExtensions: params.initializationOptions.markdownFileExtensions, + }); + + const workspace = new VsCodeClientWorkspace(connection, config, documents, notebooks); + const logger = new LogFunctionLogger(connection.console.log.bind(connection.console)); + provider = md.createLanguageService({ + workspace, + parser, + logger, + markdownFileExtensions: config.markdownFileExtensions, + }); - connection.onInitialize((_params: InitializeParams): InitializeResult => { + workspace.workspaceFolders = (params.workspaceFolders ?? []).map(x => URI.parse(x.uri)); return { capabilities: { + completionProvider: { triggerCharacters: ['.', '/', '#'] }, + definitionProvider: true, + documentLinkProvider: { resolveProvider: true }, documentSymbolProvider: true, foldingRangeProvider: true, + renameProvider: { prepareProvider: true, }, selectionRangeProvider: true, workspaceSymbolProvider: true, + workspace: { + workspaceFolders: { + supported: true, + changeNotifications: true, + }, + } } }; }); - const parser = new class implements md.IMdParser { - slugifier = md.githubSlugifier; - async tokenize(document: md.ITextDocument): Promise<md.Token[]> { - return await connection.sendRequest(parseRequestType, { uri: document.uri.toString() }); + let provider: md.IMdLanguageService | undefined; + + connection.onDocumentLinks(async (params, token): Promise<lsp.DocumentLink[]> => { + try { + const document = documents.get(params.textDocument.uri); + if (document) { + return await provider!.getDocumentLinks(document, token); + } + } catch (e) { + console.error(e.stack); } - }; + return []; + }); - const workspace = new VsCodeClientWorkspace(connection, documents); - const logger = new LogFunctionLogger(connection.console.log.bind(connection.console)); - const provider = md.createLanguageService({ workspace, parser, logger }); + connection.onDocumentLinkResolve(async (link, token): Promise<lsp.DocumentLink | undefined> => { + try { + return await provider!.resolveDocumentLink(link, token); + } catch (e) { + console.error(e.stack); + } + return undefined; + }); connection.onDocumentSymbol(async (params, token): Promise<lsp.DocumentSymbol[]> => { try { const document = documents.get(params.textDocument.uri); if (document) { - return await provider.provideDocumentSymbols(document, token); + return await provider!.getDocumentSymbols(document, token); } } catch (e) { console.error(e.stack); @@ -56,7 +100,7 @@ export function startServer(connection: Connection) { try { const document = documents.get(params.textDocument.uri); if (document) { - return await provider.provideFoldingRanges(document, token); + return await provider!.getFoldingRanges(document, token); } } catch (e) { console.error(e.stack); @@ -68,7 +112,7 @@ export function startServer(connection: Connection) { try { const document = documents.get(params.textDocument.uri); if (document) { - return await provider.provideSelectionRanges(document, params.positions, token); + return await provider!.getSelectionRanges(document, params.positions, token); } } catch (e) { console.error(e.stack); @@ -78,13 +122,85 @@ export function startServer(connection: Connection) { connection.onWorkspaceSymbol(async (params, token): Promise<lsp.WorkspaceSymbol[]> => { try { - return await provider.provideWorkspaceSymbols(params.query, token); + return await provider!.getWorkspaceSymbols(params.query, token); + } catch (e) { + console.error(e.stack); + } + return []; + }); + + connection.onCompletion(async (params, token): Promise<lsp.CompletionItem[]> => { + try { + const document = documents.get(params.textDocument.uri); + if (document) { + return await provider!.getCompletionItems(document, params.position, params.context!, token); + } + } catch (e) { + console.error(e.stack); + } + return []; + }); + + connection.onReferences(async (params, token): Promise<lsp.Location[]> => { + try { + const document = documents.get(params.textDocument.uri); + if (document) { + return await provider!.getReferences(document, params.position, params.context, token); + } } catch (e) { console.error(e.stack); } return []; }); + connection.onDefinition(async (params, token): Promise<lsp.Definition | undefined> => { + try { + const document = documents.get(params.textDocument.uri); + if (document) { + return await provider!.getDefinition(document, params.position, token); + } + } catch (e) { + console.error(e.stack); + } + return undefined; + }); + + connection.onPrepareRename(async (params, token) => { + try { + const document = documents.get(params.textDocument.uri); + if (document) { + return await provider!.prepareRename(document, params.position, token); + } + } catch (e) { + console.error(e.stack); + } + return undefined; + }); + + connection.onRenameRequest(async (params, token) => { + try { + const document = documents.get(params.textDocument.uri); + if (document) { + const edit = await provider!.getRenameEdit(document, params.position, params.newName, token); + console.log(JSON.stringify(edit)); + return edit; + } + } catch (e) { + console.error(e.stack); + } + return undefined; + }); + + connection.onRequest(protocol.getReferencesToFileInWorkspace, (async (params: { uri: string }, token: CancellationToken) => { + try { + return await provider!.getFileReferences(URI.parse(params.uri), token); + } catch (e) { + console.error(e.stack); + } + return undefined; + })); + + documents.listen(connection); + notebooks.listen(connection); connection.listen(); } - |