protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var block = _dataProvider.GetSelectedElement <IBlock>(); var classBody = _dataProvider.GetSelectedElement <IClassBody>(); var classDeclaration = classBody?.GetContainingTypeDeclaration() as IClassLikeDeclaration; if (classDeclaration == null || block == null) { return(null); } var parameters = _csharpMemberProvider.GetConstructorParameters(_constructor.ToString()).ToArray(); var naming = _dataProvider.PsiServices.Naming; var mockFieldsByType = _csharpMemberProvider.GetClassFields(classBody, _selectedElement.Language); for (int i = 0; i < _constructor.Parameters.Count; i++) { var shortName = _constructor.Parameters[i].ShortName; var typeString = _csharpMemberProvider.GetGenericMock(parameters[i]); if (!mockFieldsByType.TryGetValue(typeString, out var name)) { var mockType = TypeFactory.CreateTypeByCLRName(typeString, _dataProvider.PsiModule); var field = _dataProvider.ElementFactory.CreateTypeMemberDeclaration("private $0 $1;", (object)mockType, (object)shortName); var options = new SuggestionOptions(defaultName: shortName); name = naming.Suggestion.GetDerivedName(field.DeclaredElement, NamedElementKinds.PrivateInstanceFields, ScopeKind.Common, _selectedElement.Language, options, _dataProvider.SourceFile); field.SetName(name); classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)field); var statement = _dataProvider.ElementFactory.CreateStatement("$0 = new Mock<$1>();", (object)name, (object)parameters[i]); block.AddStatementBefore(statement, _selectedElement.GetContainingStatement()); } var argument = _dataProvider.ElementFactory.CreateArgument(ParameterKind.VALUE, _dataProvider.ElementFactory.CreateExpression($"{name}.Object")); _selectedElement.AddArgumentBefore(argument, null); } return(null); }
protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress) { var argumentList = _selectedElement.ArgumentList; var parameters = _csharpMemberProvider.GetConstructorParameters(_constructor.ToString()).ToArray(); var mockFieldsByType = _csharpMemberProvider.GetClassFields(_classBody, _selectedElement.Language); var shortName = _constructor.Parameters[_parameterNumber].ShortName; var currentParam = parameters[_parameterNumber]; var typeString = _csharpMemberProvider.GetGenericMock(currentParam); if (!mockFieldsByType.TryGetValue(typeString, out var name)) { var mockType = TypeFactory.CreateTypeByCLRName(typeString, _dataProvider.PsiModule); var field = _dataProvider.ElementFactory.CreateTypeMemberDeclaration("private $0 $1;", (object)mockType, (object)shortName); var options = new SuggestionOptions(defaultName: shortName); name = _dataProvider.PsiServices.Naming.Suggestion.GetDerivedName(field.DeclaredElement, NamedElementKinds.PrivateInstanceFields, ScopeKind.Common, _selectedElement.Language, options, _dataProvider.SourceFile); field.SetName(name); _classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)field); var statement = _dataProvider.ElementFactory.CreateStatement("$0 = new Mock<$1>();", (object)name, (object)currentParam); _block.AddStatementBefore(statement, _selectedElement.GetContainingStatement()); } return(_csharpMemberProvider.FillCurrentParameterWithMock(name, argumentList, _selectedElement, _parameterNumber, _dataProvider)); }