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 ;
107using OmniSharp . Extensions . LanguageServer . Protocol ;
118using OmniSharp . Extensions . LanguageServer . Protocol . Client . Capabilities ;
129using OmniSharp . Extensions . LanguageServer . Protocol . Models ;
1310using OmniSharp . Extensions . LanguageServer . Protocol . Server ;
14- using 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 ;
13+ using OmniSharp . Models . V2 . CodeActions ;
1914
2015namespace OmniSharp . LanguageServerProtocol . Handlers
2116{
22- internal sealed class OmniSharpCodeActionHandler : CodeActionHandlerBase , IExecuteCommandHandler
17+ internal sealed class OmniSharpCodeActionHandler : CodeActionHandlerBase
2318 {
2419 public static IEnumerable < IJsonRpcHandler > Enumerate (
2520 RequestHandlers handlers ,
26- ISerializer serializer ,
2721 ILanguageServer mediator ,
2822 DocumentVersions versions )
2923 {
3024 foreach ( var ( selector , getActionsHandler , runActionHandler ) in handlers
3125 . OfType < Mef . IRequestHandler < GetCodeActionsRequest , GetCodeActionsResponse > ,
3226 Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > > ( ) )
3327 {
34- yield return new OmniSharpCodeActionHandler ( getActionsHandler , runActionHandler , selector , serializer , mediator , versions ) ;
28+ yield return new OmniSharpCodeActionHandler ( getActionsHandler , runActionHandler , selector , mediator , versions ) ;
3529 }
3630 }
3731
3832 private readonly Mef . IRequestHandler < GetCodeActionsRequest , GetCodeActionsResponse > _getActionsHandler ;
39- private readonly ExecuteCommandRegistrationOptions _executeCommandRegistrationOptions ;
40- private ExecuteCommandCapability _executeCommandCapability ;
41- private Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > _runActionHandler ;
33+ private readonly Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > _runActionHandler ;
4234 private readonly DocumentSelector _documentSelector ;
43- private readonly ISerializer _serializer ;
4435 private readonly ILanguageServer _server ;
4536 private readonly DocumentVersions _documentVersions ;
4637
4738 public OmniSharpCodeActionHandler (
4839 Mef . IRequestHandler < GetCodeActionsRequest , GetCodeActionsResponse > getActionsHandler ,
4940 Mef . IRequestHandler < RunCodeActionRequest , RunCodeActionResponse > runActionHandler ,
5041 DocumentSelector documentSelector ,
51- ISerializer serializer ,
5242 ILanguageServer server ,
5343 DocumentVersions documentVersions )
5444 {
5545 _getActionsHandler = getActionsHandler ;
5646 _runActionHandler = runActionHandler ;
5747 _documentSelector = documentSelector ;
58- _serializer = serializer ;
5948 _server = server ;
6049 _documentVersions = documentVersions ;
61- _executeCommandRegistrationOptions = new ExecuteCommandRegistrationOptions ( )
62- {
63- Commands = new Container < string > ( "omnisharp/executeCodeAction" ) ,
64- } ;
6550 }
6651
6752 public override async Task < CommandOrCodeActionContainer > Handle ( CodeActionParams request , CancellationToken cancellationToken )
6853 {
54+ var codeActionCaps = _server . ClientSettings . Capabilities . TextDocument . CodeAction . Value ;
55+ bool clientCanResolveEditProp = codeActionCaps . ResolveSupport ? . Properties . Contains ( "edit" ) ?? false ;
56+
6957 var omnisharpRequest = new GetCodeActionsRequest
7058 {
7159 FileName = Helpers . FromUri ( request . TextDocument . Uri ) ,
@@ -87,40 +75,44 @@ public override async Task<CommandOrCodeActionContainer> Handle(CodeActionParams
8775 else if ( ca . Identifier . StartsWith ( "Change " ) ) { kind = CodeActionKind . QuickFix ; }
8876 else { kind = CodeActionKind . Refactor ; }
8977
90- codeActions . Add (
91- new CodeAction
92- {
93- Title = ca . Name ,
94- Kind = kind ,
95- Diagnostics = new Container < Diagnostic > ( ) ,
96- Edit = new WorkspaceEdit ( ) ,
97- Command = Command . Create ( "omnisharp/executeCodeAction" )
98- . WithArguments ( new CommandData ( )
99- {
100- Uri = request . TextDocument . Uri ,
101- Identifier = ca . Identifier ,
102- Name = ca . Name ,
103- Range = request . Range ,
104- } )
105- with { Title = ca . Name }
106- } ) ;
78+ var codeAction = new CodeAction {
79+ Title = ca . Name ,
80+ Kind = kind ,
81+ Diagnostics = new Container < Diagnostic > ( ) ,
82+ Edit = null ,
83+ Data = JObject . FromObject (
84+ new CommandData ( )
85+ {
86+ Uri = request . TextDocument . Uri ,
87+ Identifier = ca . Identifier ,
88+ Name = ca . Name ,
89+ Range = request . Range ,
90+ } )
91+ } ;
92+
93+ if ( ! clientCanResolveEditProp )
94+ {
95+ var codeActionResolution = await this . Handle ( codeAction , cancellationToken ) ;
96+
97+ codeAction = codeAction with {
98+ Edit = codeActionResolution . Edit ,
99+ Data = null ,
100+ } ;
101+ }
102+
103+ codeActions . Add ( codeAction ) ;
107104 }
108105
109106 return new CommandOrCodeActionContainer (
110107 codeActions . Select ( ca => new CommandOrCodeAction ( ca ) ) ) ;
111108 }
112109
113- public override Task < CodeAction > Handle ( CodeAction request , CancellationToken cancellationToken )
110+ public override async Task < CodeAction > Handle ( CodeAction request , CancellationToken cancellationToken )
114111 {
115- return Task . FromResult ( request ) ;
116- }
112+ var data = request . Data . ToObject < CommandData > ( ) ;
117113
118- public async Task < Unit > Handle ( ExecuteCommandParams request , CancellationToken cancellationToken )
119- {
120- Debug . Assert ( request . Command == "omnisharp/executeCodeAction" ) ;
121- var data = request . ExtractArguments < CommandData > ( _serializer ) ;
122-
123- var omnisharpCaRequest = new RunCodeActionRequest {
114+ var omnisharpCaRequest = new RunCodeActionRequest
115+ {
124116 Identifier = data . Identifier ,
125117 FileName = data . Uri . GetFileSystemPath ( ) ,
126118 Column = data . Range . Start . Character ,
@@ -139,19 +131,16 @@ public async Task<Unit> Handle(ExecuteCommandParams request, CancellationToken c
139131 _server . ClientSettings . Capabilities . Workspace ! . WorkspaceEdit . Value ,
140132 _documentVersions
141133 ) ;
142- ;
143134
144- await _server . Workspace . ApplyWorkspaceEdit ( new ApplyWorkspaceEditParams ( )
135+ return new CodeAction
145136 {
146- Label = data . Name ,
147- Edit = edit
148- } , cancellationToken ) ;
149-
150- // Do something with response?
151- //if (response.Applied)
137+ Edit = edit ,
138+ } ;
139+ }
140+ else
141+ {
142+ return new CodeAction ( ) ;
152143 }
153-
154- return Unit . Value ;
155144 }
156145
157146 class CommandData
@@ -162,12 +151,6 @@ class CommandData
162151 public Range Range { get ; set ; }
163152 }
164153
165- ExecuteCommandRegistrationOptions IRegistration < ExecuteCommandRegistrationOptions , ExecuteCommandCapability > . GetRegistrationOptions ( ExecuteCommandCapability capability , ClientCapabilities clientCapabilities )
166- {
167- _executeCommandCapability = capability ;
168- return _executeCommandRegistrationOptions ;
169- }
170-
171154 protected override CodeActionRegistrationOptions CreateRegistrationOptions ( CodeActionCapability capability , ClientCapabilities clientCapabilities )
172155 {
173156 return new CodeActionRegistrationOptions ( )
@@ -177,6 +160,7 @@ protected override CodeActionRegistrationOptions CreateRegistrationOptions(CodeA
177160 CodeActionKind . SourceOrganizeImports ,
178161 CodeActionKind . Refactor ,
179162 CodeActionKind . RefactorExtract ) ,
163+ ResolveProvider = true ,
180164 } ;
181165 }
182166 }
0 commit comments