-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1078 from akshita31/order_codeProviders
Order code actions
- Loading branch information
Showing
4 changed files
with
271 additions
and
33 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
85 changes: 85 additions & 0 deletions
85
src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/CodeActionsOrder.Graph.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,85 @@ | ||
// Adapted from ExtensionOrderer in Roslyn | ||
using System.Collections.Generic; | ||
|
||
namespace OmniSharp.Roslyn.CSharp.Services.Refactoring.V2 | ||
{ | ||
internal class Graph<T> | ||
{ | ||
//Dictionary to map between nodes and the names | ||
private Dictionary<string, ProviderNode<T>> Nodes { get; } | ||
private List<ProviderNode<T>> AllNodes { get; } | ||
private Graph(List<ProviderNode<T>> nodesList) | ||
{ | ||
Nodes = new Dictionary<string, ProviderNode<T>>(); | ||
AllNodes = nodesList; | ||
} | ||
internal static Graph<T> GetGraph(List<ProviderNode<T>> nodesList) | ||
{ | ||
var graph = new Graph<T>(nodesList); | ||
|
||
foreach (ProviderNode<T> node in graph.AllNodes) | ||
{ | ||
graph.Nodes[node.ProviderName] = node; | ||
} | ||
|
||
foreach (ProviderNode<T> node in graph.AllNodes) | ||
{ | ||
foreach (var before in node.Before) | ||
{ | ||
if (graph.Nodes.ContainsKey(before)) | ||
{ | ||
var beforeNode = graph.Nodes[before]; | ||
beforeNode.NodesBeforeMeSet.Add(node); | ||
} | ||
} | ||
|
||
foreach (var after in node.After) | ||
{ | ||
if (graph.Nodes.ContainsKey(after)) | ||
{ | ||
var afterNode = graph.Nodes[after]; | ||
node.NodesBeforeMeSet.Add(afterNode); | ||
} | ||
} | ||
} | ||
|
||
return graph; | ||
} | ||
|
||
public bool HasCycles() | ||
{ | ||
foreach (var node in this.AllNodes) | ||
{ | ||
if (node.CheckForCycles()) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public List<T> TopologicalSort() | ||
{ | ||
List<T> result = new List<T>(); | ||
var seenNodes = new HashSet<ProviderNode<T>>(); | ||
|
||
foreach (var node in AllNodes) | ||
{ | ||
Visit(node, result, seenNodes); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
private void Visit(ProviderNode<T> node, List<T> result, HashSet<ProviderNode<T>> seenNodes) | ||
{ | ||
if (seenNodes.Add(node)) | ||
{ | ||
foreach (var before in node.NodesBeforeMeSet) | ||
{ | ||
Visit(before, result, seenNodes); | ||
} | ||
|
||
result.Add(node.Provider); | ||
} | ||
} | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/OmniSharp.Roslyn.CSharp/Services/Refactoring/V2/CodeActionsOrder.ProviderNode.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,85 @@ | ||
// Adapted from ExtensionOrderer in Roslyn | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CodeRefactorings; | ||
|
||
namespace OmniSharp.Roslyn.CSharp.Services.Refactoring.V2 | ||
{ | ||
internal class ProviderNode<TProvider> | ||
{ | ||
public string ProviderName { get; set; } | ||
public List<string> Before { get; set; } | ||
public List<string> After { get; set; } | ||
public TProvider Provider { get; set; } | ||
public HashSet<ProviderNode<TProvider>> NodesBeforeMeSet { get; set; } | ||
|
||
public static ProviderNode<TProvider> From(TProvider provider) | ||
{ | ||
string providerName = ""; | ||
if (provider is CodeFixProvider) | ||
{ | ||
var exportAttribute = provider.GetType().GetCustomAttribute(typeof(ExportCodeFixProviderAttribute)); | ||
if (exportAttribute is ExportCodeFixProviderAttribute fixAttribute && fixAttribute.Name != null) | ||
{ | ||
providerName = fixAttribute.Name; | ||
} | ||
} | ||
else | ||
{ | ||
var exportAttribute = provider.GetType().GetCustomAttribute(typeof(ExportCodeRefactoringProviderAttribute)); | ||
if (exportAttribute is ExportCodeRefactoringProviderAttribute refactoringAttribute && refactoringAttribute.Name != null) | ||
{ | ||
providerName = refactoringAttribute.Name; | ||
} | ||
} | ||
|
||
var orderAttributes = provider.GetType().GetCustomAttributes(typeof(ExtensionOrderAttribute), true).Select(attr => (ExtensionOrderAttribute)attr).ToList(); | ||
return new ProviderNode<TProvider>(provider, providerName, orderAttributes); | ||
} | ||
|
||
private ProviderNode(TProvider provider, string providerName, List<ExtensionOrderAttribute> orderAttributes) | ||
{ | ||
Provider = provider; | ||
ProviderName = providerName; | ||
Before = new List<string>(); | ||
After = new List<string>(); | ||
NodesBeforeMeSet = new HashSet<ProviderNode<TProvider>>(); | ||
orderAttributes.ForEach(attr => AddAttribute(attr)); | ||
} | ||
|
||
private void AddAttribute(ExtensionOrderAttribute attribute) | ||
{ | ||
if (attribute.Before != null) | ||
Before.Add(attribute.Before); | ||
if (attribute.After != null) | ||
After.Add(attribute.After); | ||
} | ||
|
||
internal bool CheckForCycles() | ||
{ | ||
return CheckForCycles(new HashSet<ProviderNode<TProvider>>()); | ||
} | ||
|
||
private bool CheckForCycles(HashSet<ProviderNode<TProvider>> seenNodes) | ||
{ | ||
if (!seenNodes.Add(this)) | ||
{ | ||
//Cycle detected | ||
return true; | ||
} | ||
|
||
foreach (var before in this.NodesBeforeMeSet) | ||
{ | ||
if (before.CheckForCycles(seenNodes)) | ||
return true; | ||
} | ||
|
||
seenNodes.Remove(this); | ||
return false; | ||
} | ||
} | ||
} |
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