-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Order code actions #1078
Merged
Merged
Order code actions #1078
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4dabba9
Test
akshita31 653e4be
Order CodeProviders using Generics
akshita31 82c3a2c
Changes to incorporate providers with no name attribute
akshita31 14b1499
Changes for cases where the provider doesn't have a name
akshita31 2fb5746
Code to check for cycles in providers
akshita31 2e2b5c7
Created fields for storing the orderedProviders
akshita31 b8eefcf
Added comment for adaptation from roslyn
akshita31 751051c
Tests passing
akshita31 20d35af
Added test for ordered code action
akshita31 0467d17
Removed exception
akshita31 81b0935
Implemented list using Lazy
akshita31 b1a92b6
Merge branch 'master' into order_codeProviders
akshita31 9a7fb14
Added code for exportCodeRefactoring attribute
akshita31 d83bb8d
Merge branch 'order_codeProviders' of https://github.com/akshita31/om…
akshita31 7ba0db9
Merge branch 'master' into order_codeProviders
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this adapted from Roslyn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is taken from Extension Orderer used by Roslyn and modified a bit for the current case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, we might want to add a comment stating as such.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DustinCampbell can we get that API opened up publicly? 😀