11using System . Linq ;
22using System . Collections . Generic ;
3- using System . Diagnostics ;
43using System . Threading ;
54using System . Threading . Tasks ;
6- using MediatR ;
7- using Microsoft . CodeAnalysis ;
85using Newtonsoft . Json . Linq ;
96using OmniSharp . Extensions . JsonRpc ;
10- using OmniSharp . Extensions . LanguageServer . Protocol ;
117using OmniSharp . Extensions . LanguageServer . Protocol . Client . Capabilities ;
128using OmniSharp . Extensions . LanguageServer . Protocol . Models ;
139using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
1410using OmniSharp . Models . V2 . CodeActions ;
1511using OmniSharp . Extensions . LanguageServer . Protocol . Document ;
16- using OmniSharp . Extensions . LanguageServer . Protocol . Workspace ;
17- using OmniSharp . Models ;
1812using Diagnostic = OmniSharp . Extensions . LanguageServer . Protocol . Models . Diagnostic ;
1913
2014namespace OmniSharp . LanguageServerProtocol . Handlers
2115{
22- internal sealed class OmniSharpCodeActionHandler : CodeActionHandlerBase , IExecuteCommandHandler
16+ internal sealed class OmniSharpCodeActionHandler : CodeActionHandlerBase
2317 {
2418 public static IEnumerable < IJsonRpcHandler > Enumerate (
2519 RequestHandlers handlers ,
26- ISerializer serializer ,
2720 ILanguageServer mediator ,
2821 DocumentVersions versions )
2922 {
3023 foreach ( var ( selector , getActionsHandler , runActionHandler ) in handlers
3124 . OfType < Mef . IRequestHandler < GetCodeActionsRequest , GetCodeActionsResponse > ,
3225 Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > > ( ) )
3326 {
34- yield return new OmniSharpCodeActionHandler ( getActionsHandler , runActionHandler , selector , serializer , mediator , versions ) ;
27+ yield return new OmniSharpCodeActionHandler ( getActionsHandler , runActionHandler , selector , mediator , versions ) ;
3528 }
3629 }
3730
3831 private readonly Mef . IRequestHandler < GetCodeActionsRequest , GetCodeActionsResponse > _getActionsHandler ;
39- private readonly ExecuteCommandRegistrationOptions _executeCommandRegistrationOptions ;
40- private ExecuteCommandCapability _executeCommandCapability ;
41- private Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > _runActionHandler ;
32+ private readonly Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > _runActionHandler ;
4233 private readonly DocumentSelector _documentSelector ;
43- private readonly ISerializer _serializer ;
4434 private readonly ILanguageServer _server ;
4535 private readonly DocumentVersions _documentVersions ;
4636
4737 public OmniSharpCodeActionHandler (
4838 Mef . IRequestHandler < GetCodeActionsRequest , GetCodeActionsResponse > getActionsHandler ,
4939 Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > runActionHandler ,
5040 DocumentSelector documentSelector ,
51- ISerializer serializer ,
5241 ILanguageServer server ,
5342 DocumentVersions documentVersions )
5443 {
5544 _getActionsHandler = getActionsHandler ;
5645 _runActionHandler = runActionHandler ;
5746 _documentSelector = documentSelector ;
58- _serializer = serializer ;
5947 _server = server ;
6048 _documentVersions = documentVersions ;
61- _executeCommandRegistrationOptions = new ExecuteCommandRegistrationOptions ( )
62- {
63- Commands = new Container < string > ( "omnisharp/executeCodeAction" ) ,
64- } ;
6549 }
6650
6751 public override async Task < CommandOrCodeActionContainer > Handle ( CodeActionParams request , CancellationToken cancellationToken )
@@ -93,34 +77,28 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
9377 Title = ca . Name ,
9478 Kind = kind ,
9579 Diagnostics = new Container < Diagnostic > ( ) ,
96- Edit = new WorkspaceEdit ( ) ,
97- Command = Command . Create ( "omnisharp/executeCodeAction" )
98- . WithArguments ( new CommandData ( )
80+ Edit = null ,
81+ Data = JObject . FromObject (
82+ new CodeActionCommandData ( )
9983 {
10084 Uri = request . TextDocument . Uri ,
10185 Identifier = ca . Identifier ,
10286 Name = ca . Name ,
10387 Range = request . Range ,
10488 } )
105- with { Title = ca . Name }
10689 } ) ;
10790 }
10891
10992 return new CommandOrCodeActionContainer (
11093 codeActions . Select ( ca => new CommandOrCodeAction ( ca ) ) ) ;
11194 }
11295
113- public override Task < CodeAction > Handle ( CodeAction request , CancellationToken cancellationToken )
114- {
115- return Task . FromResult ( request ) ;
116- }
117-
118- public async Task < Unit > Handle ( ExecuteCommandParams request , CancellationToken cancellationToken )
96+ public override async Task < CodeAction > Handle ( CodeAction request , CancellationToken cancellationToken )
11997 {
120- Debug . Assert ( request . Command == "omnisharp/executeCodeAction" ) ;
121- var data = request . ExtractArguments < CommandData > ( _serializer ) ;
98+ var data = request . Data . ToObject < CodeActionCommandData > ( ) ;
12299
123- var omnisharpCaRequest = new RunCodeActionRequest {
100+ var omnisharpCaRequest = new RunCodeActionRequest
101+ {
124102 Identifier = data . Identifier ,
125103 FileName = data . Uri . GetFileSystemPath ( ) ,
126104 Column = data . Range . Start . Character ,
@@ -139,33 +117,16 @@ public async Task<Unit> Handle(ExecuteCommandParams request, CancellationToken c
139117 _server . ClientSettings . Capabilities . Workspace ! . WorkspaceEdit . Value ,
140118 _documentVersions
141119 ) ;
142- ;
143120
144- await _server . Workspace . ApplyWorkspaceEdit ( new ApplyWorkspaceEditParams ( )
121+ return new CodeAction
145122 {
146- Label = data . Name ,
147- Edit = edit
148- } , cancellationToken ) ;
149-
150- // Do something with response?
151- //if (response.Applied)
123+ Edit = edit ,
124+ } ;
125+ }
126+ else
127+ {
128+ return new CodeAction ( ) ;
152129 }
153-
154- return Unit . Value ;
155- }
156-
157- class CommandData
158- {
159- public DocumentUri Uri { get ; set ; }
160- public string Identifier { get ; set ; }
161- public string Name { get ; set ; }
162- public Range Range { get ; set ; }
163- }
164-
165- ExecuteCommandRegistrationOptions IRegistration < ExecuteCommandRegistrationOptions , ExecuteCommandCapability > . GetRegistrationOptions ( ExecuteCommandCapability capability , ClientCapabilities clientCapabilities )
166- {
167- _executeCommandCapability = capability ;
168- return _executeCommandRegistrationOptions ;
169130 }
170131
171132 protected override CodeActionRegistrationOptions CreateRegistrationOptions ( CodeActionCapability capability , ClientCapabilities clientCapabilities )
@@ -177,6 +138,7 @@ protected override CodeActionRegistrationOptions CreateRegistrationOptions(CodeA
177138 CodeActionKind . SourceOrganizeImports ,
178139 CodeActionKind . Refactor ,
179140 CodeActionKind . RefactorExtract ) ,
141+ ResolveProvider = true ,
180142 } ;
181143 }
182144 }
0 commit comments