-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
GH1205: Add support for code actions in Cake
- Loading branch information
Showing
8 changed files
with
364 additions
and
3 deletions.
There are no files selected for viewing
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
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
32 changes: 32 additions & 0 deletions
32
src/OmniSharp.Cake/Services/RequestHandlers/Refactoring/V2/BaseCodeActionHandler.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,32 @@ | ||
using System.Composition; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Cake.Utilities; | ||
using OmniSharp.Mef; | ||
using OmniSharp.Models.V2; | ||
|
||
namespace OmniSharp.Cake.Services.RequestHandlers.Refactoring.V2 | ||
{ | ||
public abstract class BaseCodeActionsHandler<TRequest, TResponse> : CakeRequestHandler<TRequest, TResponse> | ||
where TRequest : ICodeActionRequest | ||
{ | ||
protected BaseCodeActionsHandler( | ||
OmniSharpWorkspace workspace) | ||
: base(workspace) | ||
{ | ||
} | ||
|
||
protected override async Task<TRequest> TranslateRequestAsync(TRequest request) | ||
{ | ||
if (request.Selection != null) | ||
{ | ||
var startLine = await LineIndexHelper.TranslateToGenerated(request.FileName, request.Selection.Start.Line, Workspace); | ||
request.Selection.End.Line = request.Selection.Start.Line != request.Selection.End.Line | ||
? await LineIndexHelper.TranslateToGenerated(request.FileName, request.Selection.End.Line, Workspace) | ||
: startLine; | ||
request.Selection.Start.Line = startLine; | ||
} | ||
|
||
return await base.TranslateRequestAsync(request); | ||
} | ||
} | ||
} |
22 changes: 21 additions & 1 deletion
22
src/OmniSharp.Cake/Services/RequestHandlers/Refactoring/V2/GetCodeActionsHandler.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 |
---|---|---|
@@ -1,17 +1,37 @@ | ||
using System.Composition; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Cake.Utilities; | ||
using OmniSharp.Mef; | ||
using OmniSharp.Models.V2; | ||
|
||
namespace OmniSharp.Cake.Services.RequestHandlers.Refactoring.V2 | ||
{ | ||
[OmniSharpHandler(OmniSharpEndpoints.V2.GetCodeActions, Constants.LanguageNames.Cake), Shared] | ||
public class GetCodeActionsHandler : CakeRequestHandler<GetCodeActionsRequest, GetCodeActionsResponse> | ||
public class GetCodeActionsHandler : BaseCodeActionsHandler<GetCodeActionsRequest, GetCodeActionsResponse> | ||
{ | ||
[ImportingConstructor] | ||
public GetCodeActionsHandler( | ||
OmniSharpWorkspace workspace) | ||
: base(workspace) | ||
{ | ||
} | ||
|
||
protected override Task<GetCodeActionsResponse> TranslateResponse(GetCodeActionsResponse response, GetCodeActionsRequest request) | ||
{ | ||
if (response?.CodeActions == null) | ||
{ | ||
return Task.FromResult(response); | ||
} | ||
|
||
// At this point, we remove the "Rename file to.." code actions as these will | ||
// return the buffer as known to Roslyn to the client. Currently we don't store | ||
// the buffer as seen by the client and it's next to impossible to reverse it. | ||
response.CodeActions = response.CodeActions | ||
.Where(x => !x.Identifier.StartsWith("Rename file to")) | ||
.ToList(); | ||
|
||
return Task.FromResult(response); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/OmniSharp.Cake/Services/RequestHandlers/Refactoring/V2/RunCodeActionsHandler.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,29 @@ | ||
using System.Collections.Generic; | ||
using System.Composition; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Cake.Extensions; | ||
using OmniSharp.Mef; | ||
using OmniSharp.Models; | ||
using OmniSharp.Models.FileOpen; | ||
using OmniSharp.Models.V2; | ||
|
||
namespace OmniSharp.Cake.Services.RequestHandlers.Refactoring.V2 | ||
{ | ||
[OmniSharpHandler(OmniSharpEndpoints.V2.RunCodeAction, Constants.LanguageNames.Cake), Shared] | ||
public class RunCodeActionsHandler : BaseCodeActionsHandler<RunCodeActionRequest, RunCodeActionResponse> | ||
{ | ||
[ImportingConstructor] | ||
public RunCodeActionsHandler( | ||
OmniSharpWorkspace workspace) | ||
: base(workspace) | ||
{ | ||
} | ||
|
||
protected override bool IsValid(RunCodeActionRequest request) => request.WantsTextChanges; | ||
|
||
protected override Task<RunCodeActionResponse> TranslateResponse(RunCodeActionResponse response, RunCodeActionRequest request) | ||
{ | ||
return response.TranslateAsync(Workspace); | ||
} | ||
} | ||
} |
Oops, something went wrong.