Skip to content

Commit

Permalink
#5 Was been extracted BaseReturnsMethodProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
inyutin-maxim committed Apr 2, 2020
1 parent 874180d commit 11a4e86
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 67 deletions.
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using Abc.MoqComplete.Extensions;
using Abc.MoqComplete.Services;
using JetBrains.ProjectModel;
Expand All @@ -8,28 +9,32 @@
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 AutoMockerReturnsMethodProviderReturnsMethodProvider : ItemsProviderOfSpecificContext<CSharpCodeCompletionContext>
public abstract class BaseReturnsMethodProvider : ItemsProviderOfSpecificContext<CSharpCodeCompletionContext>
{
protected abstract IMethod GetMockedMethodFromSetupMethod(ISolution solution, IInvocationExpression invocation);

protected abstract IEnumerable<string> GetMockedMethodParameterTypes(ISolution solution, IInvocationExpression invocation);

/// <inheritdoc />
protected override bool IsAvailable(CSharpCodeCompletionContext context)
{
var codeCompletionType = context.BasicContext.CodeCompletionType;

return codeCompletionType == CodeCompletionType.SmartCompletion || codeCompletionType == CodeCompletionType.BasicCompletion;
}

/// <inheritdoc />
protected override bool AddLookupItems(CSharpCodeCompletionContext context, IItemsCollector collector)
{
var methodIdentifier = context.BasicContext.Solution.GetComponent<IMoqMethodIdentifier>();
var mockedMethodProvider = context.BasicContext.Solution.GetComponent<IAutoMockerMockedMethodProvider>();

var solution = context.BasicContext.Solution;
var identifier = context.TerminatedContext.TreeNode as IIdentifier;
var expression = identifier.GetParentSafe<IReferenceExpression>();

Expand All @@ -42,20 +47,20 @@ protected override bool AddLookupItems(CSharpCodeCompletionContext context, IIte
{
return false;
}

var methodIdentifier = solution.GetComponent<IMoqMethodIdentifier>();
if (methodIdentifier.IsMoqCallbackMethod(invocation))
{
invocation = invocation.InvokedExpression?.FirstChild as IInvocationExpression;
}

var mockedMethod = mockedMethodProvider.GetMockedMethodFromSetupMethod(invocation);
var mockedMethod = GetMockedMethodFromSetupMethod(solution, invocation);

if (mockedMethod == null || mockedMethod.Parameters.Count == 0)
{
return false;
}

var types = mockedMethodProvider.GetMockedMethodParameterTypes(invocation);
var types = GetMockedMethodParameterTypes(solution, invocation);
var variablesName = mockedMethod.Parameters.Select(p => p.ShortName);
var proposedCallback = $"Returns<{string.Join(",", types)}>(({string.Join(",", variablesName)}) => )";

Expand Down
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);
}
}
}

0 comments on commit 11a4e86

Please sign in to comment.