提交 7c90bdba 编写于 作者: A Allison Chou

Preliminary implementation - need to figure out exception

上级 d04f5551
......@@ -32,7 +32,7 @@
<CodeStyleLayerCodeAnalysisVersion>3.6.0-2.final</CodeStyleLayerCodeAnalysisVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.1-beta1.20210.2</MicrosoftCodeAnalysisTestingVersion>
<CodeStyleAnalyzerVersion>3.7.0-3.20271.4</CodeStyleAnalyzerVersion>
<VisualStudioEditorPackagesVersion>16.4.280</VisualStudioEditorPackagesVersion>
<VisualStudioEditorPackagesVersion>16.4.248</VisualStudioEditorPackagesVersion>
<ILToolsPackageVersion>5.0.0-alpha1.19409.1</ILToolsPackageVersion>
<MicrosoftVisualStudioLanguageServerProtocolPackagesVersion>16.7.61</MicrosoftVisualStudioLanguageServerProtocolPackagesVersion>
</PropertyGroup>
......@@ -97,7 +97,7 @@
<MicrosoftNetSdkVersion>2.0.0-alpha-20170405-2</MicrosoftNetSdkVersion>
<MicrosoftNuGetBuildTasksVersion>0.1.0</MicrosoftNuGetBuildTasksVersion>
<MicrosoftPortableTargetsVersion>0.1.2-dev</MicrosoftPortableTargetsVersion>
<MicrosoftServiceHubClientVersion>2.6.44</MicrosoftServiceHubClientVersion>
<MicrosoftServiceHubClientVersion>2.4.227</MicrosoftServiceHubClientVersion>
<MicrosoftServiceHubFrameworkVersion>2.4.227</MicrosoftServiceHubFrameworkVersion>
<MicrosoftVisualBasicVersion>10.1.0</MicrosoftVisualBasicVersion>
<MicrosoftVisualStudioCallHierarchyPackageDefinitionsVersion>15.8.27812-alpha</MicrosoftVisualStudioCallHierarchyPackageDefinitionsVersion>
......@@ -123,22 +123,22 @@
<MicrosoftVisualStudioLanguageNavigateToInterfacesVersion>16.0.467</MicrosoftVisualStudioLanguageNavigateToInterfacesVersion>
<MicrosoftVisualStudioLanguageServerProtocolVersion>$(MicrosoftVisualStudioLanguageServerProtocolPackagesVersion)</MicrosoftVisualStudioLanguageServerProtocolVersion>
<MicrosoftVisualStudioLanguageServerProtocolExtensionsVersion>$(MicrosoftVisualStudioLanguageServerProtocolPackagesVersion)</MicrosoftVisualStudioLanguageServerProtocolExtensionsVersion>
<MicrosoftVisualStudioLanguageServerClientVersion>16.7.61</MicrosoftVisualStudioLanguageServerClientVersion>
<MicrosoftVisualStudioLanguageServerClientVersion>16.6.47</MicrosoftVisualStudioLanguageServerClientVersion>
<MicrosoftVisualStudioLanguageStandardClassificationVersion>$(VisualStudioEditorPackagesVersion)</MicrosoftVisualStudioLanguageStandardClassificationVersion>
<MicrosoftVisualStudioLiveShareLanguageServicesGuestVersion>2.0.1</MicrosoftVisualStudioLiveShareLanguageServicesGuestVersion>
<MicrosoftVisualStudioLiveShareWebEditorsVersion>2.2.0-preview1-t001</MicrosoftVisualStudioLiveShareWebEditorsVersion>
<MicrosoftVisualStudioOLEInteropVersion>7.10.6072</MicrosoftVisualStudioOLEInteropVersion>
<MicrosoftVisualStudioOLEInteropVersion>7.10.6071</MicrosoftVisualStudioOLEInteropVersion>
<MicrosoftVisualStudioPlatformVSEditorVersion>$(VisualStudioEditorPackagesVersion)</MicrosoftVisualStudioPlatformVSEditorVersion>
<MicrosoftVisualStudioProgressionCodeSchemaVersion>15.8.27812-alpha</MicrosoftVisualStudioProgressionCodeSchemaVersion>
<MicrosoftVisualStudioProgressionCommonVersion>15.8.27812-alpha</MicrosoftVisualStudioProgressionCommonVersion>
<MicrosoftVisualStudioProgressionInterfacesVersion>15.8.27812-alpha</MicrosoftVisualStudioProgressionInterfacesVersion>
<MicrosoftVisualStudioProjectSystemVersion>16.2.133-pre</MicrosoftVisualStudioProjectSystemVersion>
<MicrosoftVisualStudioProjectSystemManagedVersion>2.3.6152103</MicrosoftVisualStudioProjectSystemManagedVersion>
<MicrosoftVisualStudioRemoteControlVersion>16.3.23</MicrosoftVisualStudioRemoteControlVersion>
<MicrosoftVisualStudioRemoteControlVersion>16.3.4</MicrosoftVisualStudioRemoteControlVersion>
<MicrosoftVisualStudioSDKAnalyzersVersion>16.3.14</MicrosoftVisualStudioSDKAnalyzersVersion>
<MicrosoftVisualStudioSetupConfigurationInteropVersion>1.16.30</MicrosoftVisualStudioSetupConfigurationInteropVersion>
<MicrosoftVisualStudioShell150Version>16.6.29925.50-pre</MicrosoftVisualStudioShell150Version>
<MicrosoftVisualStudioShellFrameworkVersion>16.7.30204.53-pre</MicrosoftVisualStudioShellFrameworkVersion>
<MicrosoftVisualStudioShellFrameworkVersion>16.6.29925.50-pre</MicrosoftVisualStudioShellFrameworkVersion>
<MicrosoftVisualStudioShellDesignVersion>15.7.27703</MicrosoftVisualStudioShellDesignVersion>
<MicrosoftVisualStudioShellImmutable100Version>15.0.25415</MicrosoftVisualStudioShellImmutable100Version>
<MicrosoftVisualStudioShellImmutable110Version>15.0.25415</MicrosoftVisualStudioShellImmutable110Version>
......@@ -149,7 +149,7 @@
<MicrosoftVisualStudioShellInterop157DesignTimeVersion>15.7.1</MicrosoftVisualStudioShellInterop157DesignTimeVersion>
<MicrosoftVisualStudioShellInterop80Version>8.0.50728</MicrosoftVisualStudioShellInterop80Version>
<MicrosoftVisualStudioShellInterop90Version>9.0.30730</MicrosoftVisualStudioShellInterop90Version>
<MicrosoftVisualStudioTelemetryVersion>16.3.58</MicrosoftVisualStudioTelemetryVersion>
<MicrosoftVisualStudioTelemetryVersion>16.3.36</MicrosoftVisualStudioTelemetryVersion>
<MicrosoftVisualStudioTemplateWizardInterfaceVersion>8.0.0.0-alpha</MicrosoftVisualStudioTemplateWizardInterfaceVersion>
<MicrosoftVisualStudioTextDataVersion>$(VisualStudioEditorPackagesVersion)</MicrosoftVisualStudioTextDataVersion>
<MicrosoftVisualStudioTextInternalVersion>$(VisualStudioEditorPackagesVersion)</MicrosoftVisualStudioTextInternalVersion>
......@@ -163,7 +163,7 @@
<MicrosoftVisualStudioTextManagerInterop160DesignTimeVersion>16.0.0</MicrosoftVisualStudioTextManagerInterop160DesignTimeVersion>
<MicrosoftVisualStudioThreadingAnalyzersVersion>16.6.13</MicrosoftVisualStudioThreadingAnalyzersVersion>
<MicrosoftVisualStudioThreadingVersion>16.6.13</MicrosoftVisualStudioThreadingVersion>
<MicrosoftVisualStudioUtilitiesVersion>16.7.30203.189-pre</MicrosoftVisualStudioUtilitiesVersion>
<MicrosoftVisualStudioUtilitiesVersion>16.6.29925.50</MicrosoftVisualStudioUtilitiesVersion>
<MicrosoftVisualStudioValidationVersion>15.5.31</MicrosoftVisualStudioValidationVersion>
<MicrosoftVisualStudioVsInteractiveWindowVersion>2.8.0</MicrosoftVisualStudioVsInteractiveWindowVersion>
<MicrosoftVisualStudioWorkspaceVSIntegrationVersion>16.3.43</MicrosoftVisualStudioWorkspaceVSIntegrationVersion>
......@@ -243,7 +243,7 @@
create a test insertion in Visual Studio to validate.
-->
<NewtonsoftJsonVersion>12.0.2</NewtonsoftJsonVersion>
<StreamJsonRpcVersion>2.4.48</StreamJsonRpcVersion>
<StreamJsonRpcVersion>2.4.34</StreamJsonRpcVersion>
<SystemCollectionsImmutableVersion>1.5.0</SystemCollectionsImmutableVersion>
<SystemReflectionMetadataVersion>1.6.0</SystemReflectionMetadataVersion>
<MicrosoftVisualStudioStaticReviewsEmbeddableVersion>0.1.102-alpha</MicrosoftVisualStudioStaticReviewsEmbeddableVersion>
......@@ -266,4 +266,4 @@
-->
<UsingToolMicrosoftNetCompilers Condition="'$(BootstrapBuildPath)' == ''">true</UsingToolMicrosoftNetCompilers>
</PropertyGroup>
</Project>
</Project>
\ No newline at end of file
......@@ -30,7 +30,7 @@ public CodeRefactoring(CodeRefactoringProvider provider, ImmutableArray<(CodeAct
Provider = provider;
CodeActions = actions.NullToEmpty();
if (CodeActions.Length == 0)
if (CodeActions.IsEmpty)
{
throw new ArgumentException(FeaturesResources.Actions_can_not_be_empty, nameof(actions));
}
......
......@@ -5,6 +5,7 @@
#nullable enable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Composition;
......@@ -15,8 +16,9 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.LanguageServer.CustomProtocol;
using Microsoft.CodeAnalysis.LanguageServer.Handler.CodeActions;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.CodeAnalysis.LanguageServer.Handler
......@@ -24,9 +26,8 @@ namespace Microsoft.CodeAnalysis.LanguageServer.Handler
/// <summary>
/// Handles the get code actions command.
/// </summary>
[Shared]
[ExportLspMethod(LSP.Methods.TextDocumentCodeActionName)]
internal class CodeActionsHandler : AbstractRequestHandler<LSP.CodeActionParams, LSP.SumType<LSP.Command, LSP.CodeAction>[]>
[ExportLspMethod(LSP.Methods.TextDocumentCodeActionName), Shared]
internal class CodeActionsHandler : AbstractRequestHandler<LSP.CodeActionParams, LSP.VSCodeAction[]>
{
private readonly ICodeFixService _codeFixService;
private readonly ICodeRefactoringService _codeRefactoringService;
......@@ -35,15 +36,21 @@ internal class CodeActionsHandler : AbstractRequestHandler<LSP.CodeActionParams,
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CodeActionsHandler(ICodeFixService codeFixService, ICodeRefactoringService codeRefactoringService, ILspSolutionProvider solutionProvider)
public CodeActionsHandler(
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
ILspSolutionProvider solutionProvider)
: base(solutionProvider)
{
_codeFixService = codeFixService;
_codeRefactoringService = codeRefactoringService;
}
public override async Task<LSP.SumType<LSP.Command, LSP.CodeAction>[]> HandleRequestAsync(LSP.CodeActionParams request, LSP.ClientCapabilities clientCapabilities,
string? clientName, CancellationToken cancellationToken)
public override async Task<LSP.VSCodeAction[]> HandleRequestAsync(
LSP.CodeActionParams request,
LSP.ClientCapabilities clientCapabilities,
string? clientName,
CancellationToken cancellationToken)
{
var document = SolutionProvider.GetDocument(request.TextDocument, clientName);
var codeActions = await GetCodeActionsAsync(document,
......@@ -53,47 +60,49 @@ public CodeActionsHandler(ICodeFixService codeFixService, ICodeRefactoringServic
cancellationToken).ConfigureAwait(false);
// Filter out code actions with options since they'll show dialogs and we can't remote the UI and the options.
codeActions = codeActions.Where(c => !(c is CodeActionWithOptions));
codeActions = codeActions.Where(c => !(c.Key is CodeActionWithOptions));
var result = new ArrayBuilder<LSP.SumType<LSP.Command, LSP.CodeAction>>();
var results = new List<VSCodeAction>();
foreach (var codeAction in codeActions)
{
// Always return the Command instead of a precalculated set of workspace edits.
// The edits will be calculated when the code action is either previewed or
// invoked.
// It's safe for the client to pass back the range/filename in the command to run
// on the server because the client will always re-issue a get code actions request
// before invoking a preview or running the command on the server.
result.Add(
new LSP.Command
{
CommandIdentifier = RunCodeActionCommandName,
Title = codeAction.Title,
Arguments = new object[]
{
new RunCodeActionParams
{
CodeActionParams = request,
Title = codeAction.Title
}
}
});
results.Add(GenerateVSCodeAction(request, codeAction.Key, codeAction.Value));
}
return result.ToArrayAndFree();
return results.ToArray();
static VSCodeAction GenerateVSCodeAction(
CodeActionParams request,
CodeAnalysis.CodeActions.CodeAction codeAction,
CodeActionKind codeActionKind)
{
var nestedActions = new List<VSCodeAction>();
foreach (var action in codeAction.NestedCodeActions)
{
nestedActions.Add(GenerateVSCodeAction(request, action, codeActionKind));
}
return new VSCodeAction
{
Title = codeAction.Title,
Kind = codeActionKind,
Diagnostics = request.Context.Diagnostics,
Children = nestedActions.ToArray(),
Data = new CodeActionResolveData { CodeActionParams = request }
};
}
}
internal static async Task<IEnumerable<CodeAction>> GetCodeActionsAsync(Document? document,
internal static async Task<IEnumerable<KeyValuePair<CodeAnalysis.CodeActions.CodeAction, CodeActionKind>>> GetCodeActionsAsync(
Document? document,
ICodeFixService codeFixService,
ICodeRefactoringService codeRefactoringService,
LSP.Range selection,
CancellationToken cancellationToken)
{
var actions = new Dictionary<CodeAnalysis.CodeActions.CodeAction, CodeActionKind>();
if (document == null)
{
return ImmutableArray<CodeAction>.Empty;
return actions;
}
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
......@@ -102,14 +111,17 @@ public CodeActionsHandler(ICodeFixService codeFixService, ICodeRefactoringServic
var codeFixCollections = await codeFixService.GetFixesAsync(document, textSpan, true, cancellationToken).ConfigureAwait(false);
var codeRefactorings = await codeRefactoringService.GetRefactoringsAsync(document, textSpan, cancellationToken).ConfigureAwait(false);
var codeActions = codeFixCollections.SelectMany(c => c.Fixes.Select(f => f.Action)).Concat(
codeRefactorings.SelectMany(r => r.CodeActions.Select(ca => ca.action)));
foreach (var fix in codeFixCollections.SelectMany(codeFix => codeFix.Fixes))
{
actions.Add(fix.Action, CodeActionKind.QuickFix);
}
// Flatten out the nested codeactions.
var nestedCodeActions = codeActions.Where(c => c is CodeAction.CodeActionWithNestedActions nc && nc.IsInlinable).SelectMany(nc => nc.NestedCodeActions);
codeActions = codeActions.Where(c => !(c is CodeAction.CodeActionWithNestedActions)).Concat(nestedCodeActions);
foreach (var (action, _) in codeRefactorings.SelectMany(codeRefactoring => codeRefactoring.CodeActions))
{
actions.Add(action, CodeActionKind.Refactor);
}
return codeActions;
return actions;
}
}
}
......@@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Completion;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.CodeAnalysis.LanguageServer.Handler
......@@ -44,6 +45,8 @@ public Task<LSP.InitializeResult> HandleRequestAsync(LSP.InitializeParams reques
DefinitionProvider = true,
RenameProvider = true,
ImplementationProvider = true,
CodeActionProvider = new LSP.CodeActionOptions { CodeActionKinds = new[] { CodeActionKind.QuickFix, CodeActionKind.Refactor } },
CodeActionsResolveProvider = true,
CompletionProvider = new LSP.CompletionOptions { ResolveProvider = true, TriggerCharacters = triggerCharacters },
SignatureHelpProvider = new LSP.SignatureHelpOptions { TriggerCharacters = new[] { "(", "," } },
DocumentSymbolProvider = true,
......
......@@ -150,6 +150,11 @@ public Task<VSReferenceItem[]> GetTextDocumentReferencesAsync(ReferenceParams re
=> _protocol.ExecuteRequestAsync<ReferenceParams, VSReferenceItem[]>(Methods.TextDocumentReferencesName,
referencesParams, _clientCapabilities, _clientName, cancellationToken);
[JsonRpcMethod(Methods.TextDocumentCodeActionName, UseSingleObjectParameterDeserialization = true)]
public Task<VSCodeAction[]> GetTextDocumentCodeActionsAsync(CodeActionParams codeActionParams, CancellationToken cancellationToken)
=> _protocol.ExecuteRequestAsync<CodeActionParams, VSCodeAction[]>(Methods.TextDocumentCodeActionName,
codeActionParams, _clientCapabilities, _clientName, cancellationToken);
[JsonRpcMethod(Methods.TextDocumentCompletionName, UseSingleObjectParameterDeserialization = true)]
public async Task<SumType<CompletionList, CompletionItem[]>> GetTextDocumentCompletionAsync(CompletionParams completionParams, CancellationToken cancellationToken)
// Convert to sumtype before reporting to work around https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1107698
......@@ -206,6 +211,11 @@ public Task<SymbolInformation[]> GetWorkspaceSymbolsAsync(WorkspaceSymbolParams
=> _protocol.ExecuteRequestAsync<WorkspaceSymbolParams, SymbolInformation[]>(Methods.WorkspaceSymbolName,
workspaceSymbolParams, _clientCapabilities, _clientName, cancellationToken);
[JsonRpcMethod(MSLSPMethods.TextDocumentCodeActionResolveName, UseSingleObjectParameterDeserialization = true)]
public Task<VSCodeAction> ResolveCodeActionAsync(VSCodeAction vsCodeAction, CancellationToken cancellationToken)
=> _protocol.ExecuteRequestAsync<VSCodeAction, VSCodeAction>(MSLSPMethods.TextDocumentCodeActionResolveName,
vsCodeAction, _clientCapabilities, _clientName, cancellationToken);
[JsonRpcMethod(MSLSPMethods.ProjectContextsName, UseSingleObjectParameterDeserialization = true)]
public Task<ActiveProjectContexts?> GetProjectContextsAsync(GetTextDocumentWithContextParams textDocumentWithContextParams, CancellationToken cancellationToken)
=> _protocol.ExecuteRequestAsync<GetTextDocumentWithContextParams, ActiveProjectContexts?>(MSLSPMethods.ProjectContextsName,
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Composition;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Host.Mef;
namespace Microsoft.VisualStudio.LanguageServices.LiveShare.Client.CodeActions
{
[Shared]
[ExportCodeRefactoringProvider(StringConstants.CSharpLspLanguageName)]
internal class CSharpLspCodeActionProvider : RoslynCodeActionProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpLspCodeActionProvider(CSharpLspClientServiceFactory csharpLspClientServiceFactory, IDiagnosticAnalyzerService diagnosticAnalyzerService)
: base(csharpLspClientServiceFactory, diagnosticAnalyzerService)
{
}
}
[Shared]
[ExportCodeRefactoringProvider(StringConstants.VBLspLanguageName)]
internal class VBLspCodeActionProvider : RoslynCodeActionProvider
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VBLspCodeActionProvider(VisualBasicLspClientServiceFactory vbLspClientServiceFactory, IDiagnosticAnalyzerService diagnosticAnalyzerService)
: base(vbLspClientServiceFactory, diagnosticAnalyzerService)
{
}
}
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.VisualStudio.LanguageServices.LiveShare.Protocol;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
namespace Microsoft.VisualStudio.LanguageServices.LiveShare.Client.CodeActions
{
internal class RoslynCodeActionProvider : CodeRefactoringProvider
{
private readonly AbstractLspClientServiceFactory _roslynLspClientServiceFactory;
private readonly IDiagnosticAnalyzerService _diagnosticAnalyzerService;
public RoslynCodeActionProvider(AbstractLspClientServiceFactory roslynLspClientServiceFactory, IDiagnosticAnalyzerService diagnosticAnalyzerService)
{
_roslynLspClientServiceFactory = roslynLspClientServiceFactory ?? throw new ArgumentNullException(nameof(roslynLspClientServiceFactory));
_diagnosticAnalyzerService = diagnosticAnalyzerService ?? throw new ArgumentNullException(nameof(diagnosticAnalyzerService));
}
public override async Task ComputeRefactoringsAsync(CodeRefactoringContext context)
{
// This provider is exported for all workspaces - so limit it to just our workspace.
var (document, span, cancellationToken) = context;
if (document.Project.Solution.Workspace.Kind != WorkspaceKind.AnyCodeRoslynWorkspace)
{
return;
}
var lspClient = _roslynLspClientServiceFactory.ActiveLanguageServerClient;
if (lspClient == null)
{
return;
}
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
var codeActionParams = new LSP.CodeActionParams
{
TextDocument = ProtocolConversions.DocumentToTextDocumentIdentifier(document),
Range = ProtocolConversions.TextSpanToRange(span, text)
};
var results = await lspClient.RequestAsync(LSP.Methods.TextDocumentCodeAction.ToLSRequest(), codeActionParams, cancellationToken).ConfigureAwait(false);
if (results == null)
{
return;
}
foreach (var result in results)
{
if (result.Value is LSP.Command lspCommand)
{
// The command can either wrap a Command or a CodeAction.
// If a Command, leave it unchanged; we want to dispatch it to the host to execute.
// If a CodeAction, unwrap the CodeAction so the guest can run it locally.
var commandArguments = lspCommand.Arguments.Single();
if (LanguageServicesUtils.TryParseJson(commandArguments, out LSP.CodeAction lspCodeAction))
{
context.RegisterRefactoring(new RoslynRemoteCodeAction(document, lspCodeAction.Command, lspCodeAction.Edit, lspCodeAction.Title, lspClient));
}
else
{
context.RegisterRefactoring(new RoslynRemoteCodeAction(document, lspCommand, lspCommand?.Title, lspClient));
}
}
}
}
}
}
......@@ -95,7 +95,6 @@ public Task<ICollaborationService> CreateServiceAsync(CollaborationSession colla
JObject.FromObject(new ServerCapabilities
{
// Uses Roslyn client.
CodeActionProvider = true,
ExecuteCommandProvider = new ExecuteCommandOptions(),
})));
......
......@@ -22,7 +22,7 @@ internal class LiveShareInitializeHandler : ILspRequestHandler<InitializeParams,
{
Capabilities = new ServerCapabilities
{
CodeActionProvider = true,
CodeActionProvider = false,
ExecuteCommandProvider = new ExecuteCommandOptions(),
ReferencesProvider = true,
RenameProvider = false,
......
......@@ -13,7 +13,6 @@
using Microsoft.CodeAnalysis.LanguageServer.CustomProtocol;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.LiveShare.LanguageServices;
using LSP = Microsoft.VisualStudio.LanguageServer.Protocol;
......@@ -42,11 +41,12 @@ public async Task<LSP.TextEdit[]> HandleAsync(RunCodeActionParams request, Reque
request.CodeActionParams.Range,
cancellationToken).ConfigureAwait(false);
var actionToRun = codeActions?.FirstOrDefault(a => a.Title == request.Title);
var actionToRun = codeActions?.FirstOrDefault(a => a.Key.Title == request.Title);
if (actionToRun != null)
{
var operations = await actionToRun.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
// add check here
var operations = await actionToRun.Value.Key.GetOperationsAsync(cancellationToken).ConfigureAwait(false);
var applyChangesOperation = operations.OfType<ApplyChangesOperation>().FirstOrDefault();
var text = await document.GetTextAsync(cancellationToken).ConfigureAwait(false);
......
......@@ -44,7 +44,7 @@ protected RunCodeActionsHandler(ICodeFixService codeFixService, ICodeRefactoring
public async Task<object> HandleAsync(LSP.ExecuteCommandParams request, RequestContext<Solution> requestContext, CancellationToken cancellationToken)
{
// Unwrap the command to get the RunCodeActions command.
if (request.Command.StartsWith(CodeActionsHandlerShim.RemoteCommandNamePrefix))
if (request.Command.StartsWith("_liveshare.remotecommand"))
{
var command = ((JObject)request.Arguments[0]).ToObject<LSP.Command>();
request.Command = command.CommandIdentifier;
......@@ -58,11 +58,12 @@ public async Task<object> HandleAsync(LSP.ExecuteCommandParams request, RequestC
var codeActions = await CodeActionsHandler.GetCodeActionsAsync(document, _codeFixService, _codeRefactoringService,
runRequest.CodeActionParams.Range, cancellationToken).ConfigureAwait(false);
var actionToRun = codeActions?.FirstOrDefault(a => a.Title == runRequest.Title);
var actionToRun = codeActions?.FirstOrDefault(a => a.Key.Title == runRequest.Title);
if (actionToRun != null)
{
foreach (var operation in await actionToRun.GetOperationsAsync(cancellationToken).ConfigureAwait(false))
// add check here
foreach (var operation in await actionToRun.Value.Key.GetOperationsAsync(cancellationToken).ConfigureAwait(false))
{
// TODO - This UI thread dependency should be removed.
// https://github.com/dotnet/roslyn/projects/45#card-20619668
......
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CodeRefactorings;
using Microsoft.CodeAnalysis.LanguageServer.Handler;
using Microsoft.CodeAnalysis.PooledObjects;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.LanguageServices.LiveShare.Protocol;
using Microsoft.VisualStudio.LiveShare.LanguageServices;
using Microsoft.CodeAnalysis.LanguageServer;
namespace Microsoft.VisualStudio.LanguageServices.LiveShare
{
internal abstract class CodeActionsHandlerShim : CodeActionsHandler, ILspRequestHandler<CodeActionParams, SumType<Command, CodeAction>[], Solution>
{
public const string RemoteCommandNamePrefix = "_liveshare.remotecommand";
protected const string ProviderName = "Roslyn";
[Obsolete(MefConstruction.ImportingConstructorMessage, true)]
public CodeActionsHandlerShim(ILspSolutionProvider solutionProvider, ICodeFixService codeFixService, ICodeRefactoringService codeRefactoringService)
: base(codeFixService, codeRefactoringService, solutionProvider)
{
}
/// <summary>
/// Handle a <see cref="Methods.TextDocumentCodeActionName"/> by delegating to the base LSP implementation
/// from <see cref="CodeActionsHandler"/>.
///
/// We need to return a command that is a generic wrapper that VS Code can execute.
/// The argument to this wrapper will either be a RunCodeAction command which will carry
/// enough information to run the command or a CodeAction with the edits.
/// There are server and client side dependencies on this shape in liveshare.
/// </summary>
/// <param name="param"></param>
/// <param name="requestContext"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<SumType<Command, CodeAction>[]> HandleAsync(CodeActionParams param, RequestContext<Solution> requestContext, CancellationToken cancellationToken)
{
var result = await base.HandleRequestAsync(param, requestContext.GetClientCapabilities(), null, cancellationToken).ConfigureAwait(false);
var commands = new ArrayBuilder<SumType<Command, CodeAction>>();
foreach (var resultObj in result)
{
var commandArguments = resultObj;
var title = resultObj.Value is CodeAction codeAction ? codeAction.Title : ((Command)resultObj).Title;
commands.Add(new Command
{
Title = title,
// Overwrite the command identifier to match the expected liveshare remote command identifier.
CommandIdentifier = $"{RemoteCommandNamePrefix}.{ProviderName}",
Arguments = new object[] { commandArguments }
});
}
return commands.ToArrayAndFree();
}
}
[ExportLspRequestHandler(LiveShareConstants.RoslynContractName, Methods.TextDocumentCodeActionName)]
[Obsolete("Used for backwards compatibility with old liveshare clients.")]
internal class RoslynCodeActionsHandlerShim : CodeActionsHandlerShim
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public RoslynCodeActionsHandlerShim(ILspSolutionProvider solutionProvider, ICodeFixService codeFixService, ICodeRefactoringService codeRefactoringService)
: base(solutionProvider, codeFixService, codeRefactoringService)
{
}
}
[ExportLspRequestHandler(LiveShareConstants.CSharpContractName, Methods.TextDocumentCodeActionName)]
internal class CSharpCodeActionsHandlerShim : CodeActionsHandlerShim
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public CSharpCodeActionsHandlerShim(ILspSolutionProvider solutionProvider, ICodeFixService codeFixService, ICodeRefactoringService codeRefactoringService)
: base(solutionProvider, codeFixService, codeRefactoringService)
{
}
}
[ExportLspRequestHandler(LiveShareConstants.VisualBasicContractName, Methods.TextDocumentCodeActionName)]
internal class VisualBasicCodeActionsHandlerShim : CodeActionsHandlerShim
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public VisualBasicCodeActionsHandlerShim(ILspSolutionProvider solutionProvider, ICodeFixService codeFixService, ICodeRefactoringService codeRefactoringService)
: base(solutionProvider, codeFixService, codeRefactoringService)
{
}
}
[ExportLspRequestHandler(LiveShareConstants.TypeScriptContractName, Methods.TextDocumentCodeActionName)]
internal class TypeScriptCodeActionsHandlerShim : CodeActionsHandlerShim
{
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public TypeScriptCodeActionsHandlerShim(ILspSolutionProvider solutionProvider, ICodeFixService codeFixService, ICodeRefactoringService codeRefactoringService)
: base(solutionProvider, codeFixService, codeRefactoringService)
{
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册