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

            _parameterProvider = ComponentResolver.GetComponent <IParameterProvider>(_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);
            }

            _parameterNumber = _selectedElement.ArgumentList.Arguments.Count(x => x.Kind != ParameterKind.UNKNOWN);
            _constructor     = c.Constructors.ToArray().FirstOrDefault(x => !x.IsParameterless && x.Parameters.Count > _parameterNumber);
            if (_constructor == null)
            {
                return(false);
            }

            return(testProjectProvider.IsTestProject(_dataProvider.PsiModule));
        }
        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);
        }