Skip to content

Commit

Permalink
Clean up nested action support a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
DustinCampbell committed Feb 5, 2017
1 parent cad9e20 commit a25c1a1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
12 changes: 0 additions & 12 deletions src/OmniSharp.Roslyn.CSharp/Extensions/CodeActionExtensions.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System.Collections.Immutable;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;

namespace OmniSharp.Roslyn.CSharp.Services.Refactoring.V2
{
public class CodeActionAndParent
public class AvailableCodeAction
{
public CodeAction CodeAction { get; }
public CodeAction ParentCodeAction { get; }

public CodeActionAndParent(CodeAction codeAction, CodeAction parentCodeAction = null)
public AvailableCodeAction(CodeAction codeAction, CodeAction parentCodeAction = null)
{
if (codeAction == null)
{
Expand All @@ -19,11 +22,21 @@ public CodeActionAndParent(CodeAction codeAction, CodeAction parentCodeAction =
this.ParentCodeAction = parentCodeAction;
}

public string GetIdentifier()
{
return CodeAction.EquivalenceKey ?? GetTitle();
}

public string GetTitle()
{
return ParentCodeAction != null
? $"{ParentCodeAction.Title} -> {CodeAction.Title}"
: CodeAction.Title;
}

public Task<ImmutableArray<CodeActionOperation>> GetOperationsAsync(CancellationToken cancellationToken)
{
return CodeAction.GetOperationsAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ protected BaseCodeActionService(OmniSharpWorkspace workspace, CodeActionHelper h

public abstract Task<TResponse> Handle(TRequest request);

protected async Task<IEnumerable<CodeActionAndParent>> GetActionsAsync(ICodeActionRequest request)
protected async Task<IEnumerable<AvailableCodeAction>> GetAvailableCodeActions(ICodeActionRequest request)
{
var originalDocument = this.Workspace.GetDocument(request.FileName);
if (originalDocument == null)
{
return Array.Empty<CodeActionAndParent>();
return Array.Empty<AvailableCodeAction>();
}

var actions = new List<CodeAction>();
Expand Down Expand Up @@ -185,9 +185,9 @@ private async Task CollectRefactoringActions(CodeRefactoringContext context)
}
}

private IEnumerable<CodeActionAndParent> ConvertToCodeActionAndParents(IEnumerable<CodeAction> actions)
private IEnumerable<AvailableCodeAction> ConvertToCodeActionAndParents(IEnumerable<CodeAction> actions)
{
var codeActions = new List<CodeActionAndParent>();
var codeActions = new List<AvailableCodeAction>();

foreach (var action in actions)
{
Expand All @@ -200,15 +200,15 @@ private IEnumerable<CodeActionAndParent> ConvertToCodeActionAndParents(IEnumerab
{
foreach (var nestedAction in nestedActions)
{
codeActions.Add(new CodeActionAndParent(nestedAction, action));
codeActions.Add(new AvailableCodeAction(nestedAction, action));
}

handledNestedActions = true;
}

if (!handledNestedActions)
{
codeActions.Add(new CodeActionAndParent(action));
codeActions.Add(new AvailableCodeAction(action));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.Logging;
using OmniSharp.Mef;
using OmniSharp.Models.V2;
using OmniSharp.Roslyn.CSharp.Extensions;
using OmniSharp.Roslyn.CSharp.Services.CodeActions;
using OmniSharp.Services;

Expand All @@ -27,17 +26,17 @@ public GetCodeActionsService(

public override async Task<GetCodeActionsResponse> Handle(GetCodeActionsRequest request)
{
var actions = await GetActionsAsync(request);
var availableActions = await GetAvailableCodeActions(request);

return new GetCodeActionsResponse
{
CodeActions = actions.Select(ConvertToOmniSharpCodeAction)
CodeActions = availableActions.Select(ConvertToOmniSharpCodeAction)
};
}

private static OmniSharpCodeAction ConvertToOmniSharpCodeAction(CodeActionAndParent codeActionAndParent)
private static OmniSharpCodeAction ConvertToOmniSharpCodeAction(AvailableCodeAction availableAction)
{
return new OmniSharpCodeAction(codeActionAndParent.CodeAction.GetIdentifier(), codeActionAndParent.GetTitle());
return new OmniSharpCodeAction(availableAction.GetIdentifier(), availableAction.GetTitle());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using Microsoft.Extensions.Logging;
using OmniSharp.Mef;
using OmniSharp.Models;
using OmniSharp.Roslyn.CSharp.Extensions;
using OmniSharp.Roslyn.CSharp.Services.CodeActions;
using OmniSharp.Services;

Expand All @@ -33,16 +32,16 @@ public RunCodeActionService(

public override async Task<RunCodeActionResponse> Handle(RunCodeActionRequest request)
{
var codeActionAndParents = await GetActionsAsync(request);
var codeActionAndParent = codeActionAndParents.FirstOrDefault(a => a.CodeAction.GetIdentifier().Equals(request.Identifier));
if (codeActionAndParent == null)
var availableActions = await GetAvailableCodeActions(request);
var availableAction = availableActions.FirstOrDefault(a => a.GetIdentifier().Equals(request.Identifier));
if (availableAction == null)
{
return new RunCodeActionResponse();
}

Logger.LogInformation($"Applying code action: {codeActionAndParent.GetTitle()}");
Logger.LogInformation($"Applying code action: {availableAction.GetTitle()}");

var operations = await codeActionAndParent.CodeAction.GetOperationsAsync(CancellationToken.None);
var operations = await availableAction.GetOperationsAsync(CancellationToken.None);

var solution = this.Workspace.CurrentSolution;
var changes = new List<ModifiedFileResponse>();
Expand Down

0 comments on commit a25c1a1

Please sign in to comment.