-
Notifications
You must be signed in to change notification settings - Fork 418
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
Added support for 'extract base class' #1969
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9b7f6ca
added support for Extract Class with some reasonable defaults
filipw 8cd47bb
added extract class tests
filipw 78ee64e
added comments
filipw a9da2fc
added note about Roslyn update
filipw e79d1e4
Apply suggestions from code review
filipw 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
19 changes: 19 additions & 0 deletions
19
src/OmniSharp.Roslyn/WorkspaceServices/ExtractClassOptionsServiceWorkspaceServiceFactory.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,19 @@ | ||
using System.Composition; | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis.Host; | ||
using Microsoft.CodeAnalysis.Host.Mef; | ||
|
||
namespace OmniSharp | ||
{ | ||
[Shared] | ||
[ExportWorkspaceServiceFactoryWithAssemblyQualifiedName("Microsoft.CodeAnalysis.Features", "Microsoft.CodeAnalysis.ExtractClass.IExtractClassOptionsService")] | ||
public class ExtractClassOptionsServiceWorkspaceServiceFactory : IWorkspaceServiceFactory | ||
{ | ||
public IWorkspaceService CreateService(HostWorkspaceServices workspaceServices) | ||
{ | ||
// Generates proxy class to get around issue that IExtractClassOptionsService is internal at this point. | ||
var internalType = Assembly.Load("Microsoft.CodeAnalysis.Features").GetType("Microsoft.CodeAnalysis.ExtractClass.IExtractClassOptionsService"); | ||
return (IWorkspaceService)typeof(DispatchProxy).GetMethod(nameof(DispatchProxy.Create)).MakeGenericMethod(internalType, typeof(ExtractClassWorkspaceService)).Invoke(null, null); | ||
} | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/OmniSharp.Roslyn/WorkspaceServices/ExtractClassWorkspaceService.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,86 @@ | ||
using System; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace OmniSharp | ||
{ | ||
public class ExtractClassWorkspaceService : DispatchProxy | ||
{ | ||
protected override object Invoke(MethodInfo targetMethod, object[] args) | ||
{ | ||
// the args correspond to the following interface method on IExtractClassOptionsService | ||
// http://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/ExtractClass/IExtractClassOptionsService.cs,30b2d7f792fbcc68 | ||
// internal interface IExtractClassOptionsService : IWorkspaceService | ||
// { | ||
// Task<ExtractClassOptions?> GetExtractClassOptionsAsync(Document document, INamedTypeSymbol originalType, ISymbol? selectedMember); | ||
// } | ||
// if it changes, this implementation must be changed accordingly | ||
|
||
var featuresAssembly = Assembly.Load("Microsoft.CodeAnalysis.Features"); | ||
var extractClassOptionsType = featuresAssembly.GetType("Microsoft.CodeAnalysis.ExtractClass.ExtractClassOptions"); | ||
var extractClassMemberAnalysisResultType = featuresAssembly.GetType("Microsoft.CodeAnalysis.ExtractClass.ExtractClassMemberAnalysisResult"); | ||
|
||
// we need to create Enumerable.Cast<ExtractClassMemberAnalysisResult>() where ExtractClassMemberAnalysisResult is not accessible publicly | ||
var genericCast = typeof(Enumerable).GetMethod("Cast").MakeGenericMethod(extractClassMemberAnalysisResultType); | ||
|
||
var originalType = (INamedTypeSymbol)args[1]; | ||
var selectedSymbol = (ISymbol)args[2]; | ||
|
||
var symbolsToUse = selectedSymbol == null ? originalType.GetMembers().Where(member => member switch | ||
{ | ||
IMethodSymbol methodSymbol => methodSymbol.MethodKind == MethodKind.Ordinary, | ||
IFieldSymbol fieldSymbol => !fieldSymbol.IsImplicitlyDeclared, | ||
_ => member.Kind == SymbolKind.Property || member.Kind == SymbolKind.Event | ||
}).ToArray() : new ISymbol[1] { selectedSymbol }; | ||
|
||
// we need to create ImmutableArray.CreateRange<ExtractClassMemberAnalysisResult>() where ExtractClassMemberAnalysisResult is not accessible publicly | ||
var extractClassMemberAnalysisResultImmutableArray = typeof(ImmutableArray).GetMethods() | ||
.Where(x => x.Name == "CreateRange") | ||
.Select(method => new | ||
{ | ||
method, | ||
parameters = method.GetParameters(), | ||
genericArguments = method.GetGenericArguments() | ||
}) | ||
.Where(method => method.genericArguments.Length == 1 && method.parameters.Length == 1) | ||
.Select(x => x.method) | ||
.First() | ||
.MakeGenericMethod(extractClassMemberAnalysisResultType).Invoke(null, new object[] | ||
{ | ||
// at this point we have IEnumerable<object> and need to cast to IEnumerable<ExtractClassMemberAnalysisResult> | ||
// which we can then pass to ImmutableArray.CreateRange<ExtractClassMemberAnalysisResult>() | ||
genericCast.Invoke(null, new object[] | ||
{ | ||
// this constructor corresponds to | ||
// http://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/ExtractClass/ExtractClassOptions.cs,ced9042e0a010e24 | ||
// public ExtractClassMemberAnalysisResult(ISymbol member,bool makeAbstract) | ||
// if it changes, this implementation must be changed accordingly | ||
symbolsToUse.Select(symbol => Activator.CreateInstance(extractClassMemberAnalysisResultType, new object[] | ||
{ | ||
symbol, | ||
false | ||
})) | ||
}) | ||
}); | ||
|
||
const string name = "NewBaseType"; | ||
|
||
// this constructor corresponds to | ||
// http://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/ExtractClass/ExtractClassOptions.cs,6f65491c71285819,references | ||
// public ExtractClassOptions(string fileName, string typeName, bool sameFile, ImmutableArray<ExtractClassMemberAnalysisResult> memberAnalysisResults) | ||
// if it changes, this implementation must be changed accordingly | ||
var resultObject = Activator.CreateInstance(extractClassOptionsType, new object[] { | ||
$"{name}.cs", | ||
name, | ||
true, | ||
extractClassMemberAnalysisResultImmutableArray | ||
}); | ||
|
||
// the return type is Task<ExtractClassOptions> | ||
return typeof(Task).GetMethod("FromResult").MakeGenericMethod(extractClassOptionsType).Invoke(null, new[] { resultObject }); | ||
} | ||
} | ||
} |
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
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.
Thanks for documenting this!
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.
for the next lucky winner that updates Roslyn 😀