public override bool IsAvailable(IUserDataHolder cache)
        {
            var testProjectProvider = ComponentResolver.GetComponent <ITestProjectProvider>(_dataProvider);

            _selectedElement      = _dataProvider.GetSelectedElement <IObjectCreationExpression>(false, false);
            _csharpMemberProvider = ComponentResolver.GetComponent <ICsharpMemberProvider>(_dataProvider);
            if (!(_selectedElement?.TypeReference?.Resolve().DeclaredElement is IClass c))
            {
                return(false);
            }

            var parameterCount = _selectedElement.ArgumentList?.Arguments.Count(x => x.Kind != ParameterKind.UNKNOWN);

            _constructor = c.Constructors.FirstOrDefault(x => !x.IsParameterless &&
                                                         x.Parameters.Count > parameterCount &&
                                                         x.Parameters.All(_csharpMemberProvider.IsAbstractOrInterface));
            if (_constructor == null)
            {
                return(false);
            }

            var isAvailable = testProjectProvider.IsTestProject(_dataProvider.PsiModule) && _selectedElement != null && _selectedElement.Arguments.Count == 0;

            if (isAvailable)
            {
                cache.PutKey(AnchorKey.FillWithMockContextActionKey);
            }

            return(isAvailable);
        }
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            var csharpMemberProvider = ComponentResolver.GetComponent <ICsharpMemberProvider>(_dataProvider);
            var selectedElement      = _dataProvider.GetSelectedElement <IObjectCreationExpression>(false, false);
            var argumentList         = selectedElement.ArgumentList;
            var @class                 = (IClass)selectedElement.TypeReference?.Resolve().DeclaredElement;
            var parameterCount         = selectedElement.ArgumentList?.Arguments.Count(x => x.Kind != ParameterKind.UNKNOWN);
            var constructor            = @class.Constructors.ToArray().FirstOrDefault(x => !x.IsParameterless && x.Parameters.Count > parameterCount);
            var parameters             = csharpMemberProvider.GetConstructorParameters(constructor.ToString()).ToArray();
            var parameterNumber        = csharpMemberProvider.GetCurrentParameterNumber(selectedElement, _dataProvider);
            var shortName              = constructor.Parameters[parameterNumber].ShortName;
            var currentParam           = parameters[parameterNumber];
            var block                  = _dataProvider.GetSelectedElement <IBlock>();
            var localVariableStatement = _dataProvider.ElementFactory.CreateStatement("var $0 = new Mock<$1>();", (object)shortName, currentParam);

            block.AddStatementBefore(localVariableStatement, selectedElement.GetContainingStatement());

            return(csharpMemberProvider.FillCurrentParameterWithMock(shortName, argumentList, selectedElement, parameterNumber, _dataProvider));
        }
        public override bool IsAvailable(IUserDataHolder cache)
        {
            var testProjectProvider = ComponentResolver.GetComponent <ITestProjectProvider>(_dataProvider);

            if (!testProjectProvider.IsTestProject(_dataProvider.PsiModule))
            {
                return(false);
            }

            _csharpMemberProvider = ComponentResolver.GetComponent <ICsharpMemberProvider>(_dataProvider);
            _selectedElement      = _dataProvider.GetSelectedElement <IObjectCreationExpression>(false, false);
            _block            = _dataProvider.GetSelectedElement <IBlock>();
            _classBody        = _dataProvider.GetSelectedElement <IClassBody>();
            _classDeclaration = _classBody?.GetContainingTypeDeclaration() as IClassLikeDeclaration;

            if (_classDeclaration == null || _block == null || _selectedElement == null)
            {
                return(false);
            }

            if (!(_selectedElement.TypeReference?.Resolve().DeclaredElement is IClass c))
            {
                return(false);
            }

            var parameterCount = _selectedElement.ArgumentList?.Arguments.Count(x => x.Kind != ParameterKind.UNKNOWN);

            _constructor = c.Constructors.ToArray().FirstOrDefault(x => !x.IsParameterless && x.Parameters.Count > parameterCount);
            if (_constructor == null)
            {
                return(false);
            }

            var previousTokenType = _dataProvider.TokenBeforeCaret?.NodeType as ITokenNodeType;
            var nextTokenType     = _dataProvider.TokenAfterCaret?.NodeType as ITokenNodeType;

            if (previousTokenType == null || nextTokenType == null)
            {
                return(false);
            }

            if (previousTokenType.TokenRepresentation == " ")
            {
                previousTokenType = _dataProvider.PsiFile.FindTokenAt(_dataProvider.TokenBeforeCaret.GetTreeStartOffset() - 1)?.NodeType as ITokenNodeType;
            }

            if (nextTokenType.TokenRepresentation == " ")
            {
                nextTokenType = _dataProvider.PsiFile.FindTokenAt(_dataProvider.TokenBeforeCaret.GetTreeEndOffset() + 1)?.NodeType as ITokenNodeType;
            }

            if (previousTokenType == null || nextTokenType == null)
            {
                return(false);
            }

            var isAvailable = false;

            if (previousTokenType.TokenRepresentation == "(")
            {
                isAvailable = nextTokenType.TokenRepresentation == ")" || nextTokenType.TokenRepresentation == ",";
            }

            else if (previousTokenType.TokenRepresentation == ",")
            {
                isAvailable = !nextTokenType.IsIdentifier;
            }

            if (!isAvailable)
            {
                return(false);
            }

            _parameterNumber = _csharpMemberProvider.GetCurrentParameterNumber(_selectedElement, _dataProvider);
            var parameter = _constructor.Parameters[_parameterNumber];

            if (!_csharpMemberProvider.IsAbstractOrInterface(parameter))
            {
                return(false);
            }

            cache.PutKey(AnchorKey.FillParamWithMockContextActionKey);

            return(true);
        }
Пример #4
0
 public override bool IsAvailable(IUserDataHolder cache)
 {
     _csharpMemberProvider = ComponentResolver.GetComponent <ICsharpMemberProvider>(_dataProvider);
     return(cache.HasKey(AnchorKey.FillWithMockContextActionKey));
 }