-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Xaml command hanler for CreateEventHandler command (#51670)
* Add Xaml command handler for CreateEventHandler command * code review comments * address cr comments * Move command title to const string
- Loading branch information
1 parent
3d685b3
commit b8f5462
Showing
8 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/VisualStudio/Xaml/Impl/Features/Commands/IXamlCommandService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Host; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.Xaml.Features.Commands | ||
{ | ||
internal interface IXamlCommandService : ILanguageService | ||
{ | ||
/// <summary> | ||
/// Execute the <paramref name="command"/> with the <paramref name="commandArguments"/> | ||
/// </summary> | ||
/// <param name="document">TextDocument command was triggered on</param> | ||
/// <param name="command">The command that will be executed</param> | ||
/// <param name="commandArguments">The arguments need by the command</param> | ||
/// <param name="cancellationToken">cancellationToken</param> | ||
/// <returns>True if the command has been executed, otherwise false</returns> | ||
Task<bool> ExecuteCommandAsync(TextDocument document, string command, object[]? commandArguments, CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/VisualStudio/Xaml/Impl/Features/Completion/XamlEventDescription.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// 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.Collections.Immutable; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.Xaml.Features.Completion | ||
{ | ||
internal struct XamlEventDescription | ||
{ | ||
public string ClassName { get; set; } | ||
public string EventName { get; set; } | ||
public string ReturnType { get; set; } | ||
public ImmutableArray<(string Name, string ParameterType, string Modifier)> Parameters { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...dio/Xaml/Impl/Implementation/LanguageServer/Handler/Commands/CreateEventCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// 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.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis.Editor.Xaml; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler.Commands; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using Microsoft.VisualStudio.LanguageServices.Xaml.Features.Commands; | ||
using Microsoft.VisualStudio.LanguageServices.Xaml.Features.Completion; | ||
using Newtonsoft.Json.Linq; | ||
using Roslyn.Utilities; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.Xaml.Implementation.LanguageServer.Handler.Commands | ||
{ | ||
/// <summary> | ||
/// Handle the command that adds an event handler method in code | ||
/// </summary> | ||
internal class CreateEventCommandHandler : AbstractExecuteWorkspaceCommandHandler | ||
{ | ||
public override string Command => StringConstants.CreateEventHandlerCommand; | ||
|
||
public override bool MutatesSolutionState => false; | ||
|
||
public override bool RequiresLSPSolution => true; | ||
|
||
public override TextDocumentIdentifier? GetTextDocumentIdentifier(ExecuteCommandParams request) | ||
=> ((JToken)request.Arguments.First()).ToObject<TextDocumentIdentifier>(); | ||
|
||
public override async Task<object> HandleRequestAsync(ExecuteCommandParams request, RequestContext context, CancellationToken cancellationToken) | ||
{ | ||
Contract.ThrowIfNull(request.Arguments); | ||
|
||
var document = context.Document; | ||
if (document == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var commandService = document.Project.LanguageServices.GetService<IXamlCommandService>(); | ||
if (commandService == null) | ||
{ | ||
return false; | ||
} | ||
|
||
// request.Arguments has two argument for CreateEventHandlerCommand | ||
// Arguments[0]: TextDocumentIdentifier | ||
// Arguments[1]: XamlEventDescription | ||
var arguments = new object[] { ((JToken)request.Arguments[1]).ToObject<XamlEventDescription>() }; | ||
return await commandService.ExecuteCommandAsync(document, request.Command, arguments, cancellationToken).ConfigureAwait(false); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../Impl/Implementation/LanguageServer/Handler/Commands/CreateEventCommandHandlerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// 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.Collections.Immutable; | ||
using System.Composition; | ||
using Microsoft.CodeAnalysis.Editor.Xaml; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler; | ||
using Microsoft.CodeAnalysis.LanguageServer.Handler.Commands; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServices.Xaml.Implementation.LanguageServer.Handler.Commands | ||
{ | ||
[ExportLspRequestHandlerProvider(StringConstants.XamlLanguageName), Shared] | ||
[ProvidesCommand(StringConstants.CreateEventHandlerCommand)] | ||
internal class CreateEventCommandHandlerProvider : AbstractRequestHandlerProvider | ||
{ | ||
[ImportingConstructor] | ||
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] | ||
public CreateEventCommandHandlerProvider() | ||
{ | ||
} | ||
|
||
public override ImmutableArray<IRequestHandler> CreateRequestHandlers() | ||
{ | ||
return ImmutableArray.Create<IRequestHandler>(new CreateEventCommandHandler()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters