|
| 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 | + |
| 6 | +import { RoslynLanguageServer } from './roslynLanguageServer'; |
| 7 | +import * as vscode from 'vscode'; |
| 8 | +import { |
| 9 | + DidChangeTextDocumentNotification, |
| 10 | + DidCloseTextDocumentNotification, |
| 11 | + DidCloseTextDocumentParams, |
| 12 | + DidChangeTextDocumentParams, |
| 13 | + DocumentDiagnosticParams, |
| 14 | + RequestType, |
| 15 | + DocumentDiagnosticRequest, |
| 16 | + DocumentDiagnosticReport, |
| 17 | + CancellationToken, |
| 18 | + CodeAction, |
| 19 | + CodeActionParams, |
| 20 | + CodeActionRequest, |
| 21 | + CodeActionResolveRequest, |
| 22 | + CompletionParams, |
| 23 | + CompletionRequest, |
| 24 | + CompletionResolveRequest, |
| 25 | + CompletionItem, |
| 26 | +} from 'vscode-languageclient/node'; |
| 27 | +import SerializableSimplifyMethodParams from '../razor/src/simplify/serializableSimplifyMethodParams'; |
| 28 | +import { TextEdit } from 'vscode-html-languageservice'; |
| 29 | + |
| 30 | +// These are commands that are invoked by the Razor extension, and are used to send LSP requests to the Roslyn LSP server |
| 31 | +export const roslynDidChangeCommand = 'roslyn.changeRazorCSharp'; |
| 32 | +export const roslynDidCloseCommand = 'roslyn.closeRazorCSharp'; |
| 33 | +export const roslynPullDiagnosticCommand = 'roslyn.pullDiagnosticRazorCSharp'; |
| 34 | +export const provideCodeActionsCommand = 'roslyn.provideCodeActions'; |
| 35 | +export const resolveCodeActionCommand = 'roslyn.resolveCodeAction'; |
| 36 | +export const provideCompletionsCommand = 'roslyn.provideCompletions'; |
| 37 | +export const resolveCompletionsCommand = 'roslyn.resolveCompletion'; |
| 38 | +export const roslynSimplifyMethodCommand = 'roslyn.simplifyMethod'; |
| 39 | +export const razorInitializeCommand = 'razor.initialize'; |
| 40 | + |
| 41 | +export function registerRazorCommands(context: vscode.ExtensionContext, languageServer: RoslynLanguageServer) { |
| 42 | + // Razor will call into us (via command) for generated file didChange/didClose notifications. We'll then forward these |
| 43 | + // notifications along to Roslyn. didOpen notifications are handled separately via the vscode.openTextDocument method. |
| 44 | + context.subscriptions.push( |
| 45 | + vscode.commands.registerCommand(roslynDidChangeCommand, async (notification: DidChangeTextDocumentParams) => { |
| 46 | + await languageServer.sendNotification(DidChangeTextDocumentNotification.method, notification); |
| 47 | + }) |
| 48 | + ); |
| 49 | + context.subscriptions.push( |
| 50 | + vscode.commands.registerCommand(roslynDidCloseCommand, async (notification: DidCloseTextDocumentParams) => { |
| 51 | + await languageServer.sendNotification(DidCloseTextDocumentNotification.method, notification); |
| 52 | + }) |
| 53 | + ); |
| 54 | + context.subscriptions.push( |
| 55 | + vscode.commands.registerCommand(roslynPullDiagnosticCommand, async (request: DocumentDiagnosticParams) => { |
| 56 | + const diagnosticRequestType = new RequestType<DocumentDiagnosticParams, DocumentDiagnosticReport, any>( |
| 57 | + DocumentDiagnosticRequest.method |
| 58 | + ); |
| 59 | + return await languageServer.sendRequest(diagnosticRequestType, request, CancellationToken.None); |
| 60 | + }) |
| 61 | + ); |
| 62 | + context.subscriptions.push( |
| 63 | + vscode.commands.registerCommand( |
| 64 | + roslynSimplifyMethodCommand, |
| 65 | + async (request: SerializableSimplifyMethodParams) => { |
| 66 | + const simplifyMethodRequestType = new RequestType<SerializableSimplifyMethodParams, TextEdit[], any>( |
| 67 | + 'roslyn/simplifyMethod' |
| 68 | + ); |
| 69 | + return await languageServer.sendRequest(simplifyMethodRequestType, request, CancellationToken.None); |
| 70 | + } |
| 71 | + ) |
| 72 | + ); |
| 73 | + |
| 74 | + // The VS Code API for code actions (and the vscode.CodeAction type) doesn't support everything that LSP supports, |
| 75 | + // namely the data property, which Razor needs to identify which code actions are on their allow list, so we need |
| 76 | + // to expose a command for them to directly invoke our code actions LSP endpoints, rather than use built-in commands. |
| 77 | + context.subscriptions.push( |
| 78 | + vscode.commands.registerCommand(provideCodeActionsCommand, async (request: CodeActionParams) => { |
| 79 | + return await languageServer.sendRequest(CodeActionRequest.type, request, CancellationToken.None); |
| 80 | + }) |
| 81 | + ); |
| 82 | + context.subscriptions.push( |
| 83 | + vscode.commands.registerCommand(resolveCodeActionCommand, async (request: CodeAction) => { |
| 84 | + return await languageServer.sendRequest(CodeActionResolveRequest.type, request, CancellationToken.None); |
| 85 | + }) |
| 86 | + ); |
| 87 | + |
| 88 | + context.subscriptions.push( |
| 89 | + vscode.commands.registerCommand(provideCompletionsCommand, async (request: CompletionParams) => { |
| 90 | + return await languageServer.sendRequest(CompletionRequest.type, request, CancellationToken.None); |
| 91 | + }) |
| 92 | + ); |
| 93 | + context.subscriptions.push( |
| 94 | + vscode.commands.registerCommand(resolveCompletionsCommand, async (request: CompletionItem) => { |
| 95 | + return await languageServer.sendRequest(CompletionResolveRequest.type, request, CancellationToken.None); |
| 96 | + }) |
| 97 | + ); |
| 98 | + |
| 99 | + // Roslyn is responsible for producing a json file containing information for Razor, that comes from the compilation for |
| 100 | + // a project. We want to defer this work until necessary, so this command is called by the Razor document manager to tell |
| 101 | + // us when they need us to initialize the Razor things. |
| 102 | + context.subscriptions.push( |
| 103 | + vscode.commands.registerCommand(razorInitializeCommand, async () => { |
| 104 | + await languageServer.sendNotification('razor/initialize', {}); |
| 105 | + }) |
| 106 | + ); |
| 107 | +} |
0 commit comments