-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5 Was been extracted BaseReturnsMethodProvider
- Loading branch information
1 parent
874180d
commit 11a4e86
Showing
3 changed files
with
64 additions
and
67 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Abc.MoqComplete/Abc.MoqComplete/CompletionProvider/AutoMockerReturnsMethodProvider.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 Abc.MoqComplete.Services; | ||
using JetBrains.ProjectModel; | ||
using JetBrains.ReSharper.Psi; | ||
using JetBrains.ReSharper.Psi.CSharp; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
|
||
namespace Abc.MoqComplete.CompletionProvider | ||
{ | ||
[Language(typeof(CSharpLanguage))] | ||
public class AutoMockerReturnsMethodProvider : BaseReturnsMethodProvider | ||
{ | ||
/// <inheritdoc /> | ||
protected override IMethod GetMockedMethodFromSetupMethod(ISolution solution, IInvocationExpression invocation) | ||
{ | ||
var methodProvider = solution.GetComponent<IAutoMockerMockedMethodProvider>(); | ||
|
||
return methodProvider.GetMockedMethodFromSetupMethod(invocation); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override IEnumerable<string> GetMockedMethodParameterTypes(ISolution solution, IInvocationExpression invocation) | ||
{ | ||
var methodProvider = solution.GetComponent<IAutoMockerMockedMethodProvider>(); | ||
|
||
return methodProvider.GetMockedMethodParameterTypes(invocation); | ||
} | ||
} | ||
} |
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
79 changes: 21 additions & 58 deletions
79
Abc.MoqComplete/Abc.MoqComplete/CompletionProvider/ReturnsMethodProvider.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,66 +1,29 @@ | ||
using System.Linq; | ||
using Abc.MoqComplete.Extensions; | ||
using System.Collections.Generic; | ||
using Abc.MoqComplete.Services; | ||
using JetBrains.ProjectModel; | ||
using JetBrains.ReSharper.Feature.Services.CodeCompletion; | ||
using JetBrains.ReSharper.Feature.Services.CodeCompletion.Infrastructure; | ||
using JetBrains.ReSharper.Feature.Services.CodeCompletion.Infrastructure.AspectLookupItems.BaseInfrastructure; | ||
using JetBrains.ReSharper.Feature.Services.CodeCompletion.Infrastructure.AspectLookupItems.Info; | ||
using JetBrains.ReSharper.Feature.Services.CodeCompletion.Infrastructure.LookupItems; | ||
using JetBrains.ReSharper.Feature.Services.CSharp.CodeCompletion.Infrastructure; | ||
using JetBrains.ReSharper.Features.Intellisense.CodeCompletion.CSharp; | ||
using JetBrains.ReSharper.Psi; | ||
using JetBrains.ReSharper.Psi.CSharp; | ||
using JetBrains.ReSharper.Psi.CSharp.Tree; | ||
using JetBrains.ReSharper.Psi.ExpectedTypes; | ||
using JetBrains.ReSharper.Psi.Resources; | ||
using JetBrains.ReSharper.Psi.Tree; | ||
|
||
namespace Abc.MoqComplete.CompletionProvider | ||
{ | ||
[Language(typeof(CSharpLanguage))] | ||
public class ReturnsMethodProvider : ItemsProviderOfSpecificContext<CSharpCodeCompletionContext> | ||
{ | ||
protected override bool IsAvailable(CSharpCodeCompletionContext context) | ||
{ | ||
var codeCompletionType = context.BasicContext.CodeCompletionType; | ||
return codeCompletionType == CodeCompletionType.SmartCompletion || codeCompletionType == CodeCompletionType.BasicCompletion; | ||
} | ||
|
||
protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector) | ||
{ | ||
var methodIdentifier = context.BasicContext.Solution.GetComponent<IMoqMethodIdentifier>(); | ||
var mockedMethodProvider = context.BasicContext.Solution.GetComponent<IMockedMethodProvider>(); | ||
var identifier = context.TerminatedContext.TreeNode as IIdentifier; | ||
var expression = identifier.GetParentSafe<IReferenceExpression>(); | ||
|
||
if (expression == null) | ||
return false; | ||
|
||
if (!(expression.ConditionalQualifier is IInvocationExpression invocation)) | ||
return false; | ||
|
||
if (methodIdentifier.IsMoqCallbackMethod(invocation)) | ||
invocation = invocation.InvokedExpression?.FirstChild as IInvocationExpression; | ||
|
||
var mockedMethod = mockedMethodProvider.GetMockedMethodFromSetupMethod(invocation); | ||
|
||
if (mockedMethod == null || mockedMethod.Parameters.Count == 0) | ||
return false; | ||
|
||
var types = mockedMethodProvider.GetMockedMethodParameterTypes(invocation); | ||
var variablesName = mockedMethod.Parameters.Select(p => p.ShortName); | ||
var proposedCallback = $"Returns<{string.Join(",", types)}>(({string.Join(",", variablesName)}) => )"; | ||
var item = CSharpLookupItemFactory.Instance.CreateKeywordLookupItem(context, proposedCallback, TailType.None, PsiSymbolsThemedIcons.Method.Id) as LookupItem<TextualInfo>; | ||
if (item == null) | ||
return false; | ||
|
||
item.SetInsertCaretOffset(-1); | ||
item.SetReplaceCaretOffset(-1); | ||
item.WithInitializedRanges(context.CompletionRanges, context.BasicContext); | ||
item.SetTopPriority(); | ||
collector.Add(item); | ||
return true; | ||
} | ||
} | ||
} | ||
[Language(typeof(CSharpLanguage))] | ||
public class ReturnsMethodProvider : BaseReturnsMethodProvider | ||
{ | ||
/// <inheritdoc /> | ||
protected override IMethod GetMockedMethodFromSetupMethod(ISolution solution, IInvocationExpression invocation) | ||
{ | ||
var methodProvider = solution.GetComponent<IMockedMethodProvider>(); | ||
|
||
return methodProvider.GetMockedMethodFromSetupMethod(invocation); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override IEnumerable<string> GetMockedMethodParameterTypes(ISolution solution, IInvocationExpression invocation) | ||
{ | ||
var methodProvider = solution.GetComponent<IMockedMethodProvider>(); | ||
|
||
return methodProvider.GetMockedMethodParameterTypes(invocation); | ||
} | ||
} | ||
} |