示例#1
0
        public Action <ITextControl> ExecutePsiTransaction(ICSharpContextActionDataProvider dataProvider, ISolution solution, IClassLikeDeclaration classDeclaration, IInterface theInterface)
        {
            string typeParameters = theInterface.TypeParameters.AggregateString(",", (builder, parameter) => builder.Append(parameter.ShortName));

            if (theInterface.TypeParameters.Count != 0)
            {
                typeParameters = "<" + typeParameters + ">";
            }
            string interfaceName = theInterface.ShortName;
            string className     = $"Mock{interfaceName.Substring(1)}";

            classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration($"private {className}(){{}}"));
            foreach (IMethod node in theInterface.Methods)
            {
                ISignature methodSig = new MockMethod(node, theInterface);
                classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(methodSig.Signature()));
                classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockClassVariable(node, theInterface).Declaration()));
            }

            IClassLikeDeclaration builderClass = (IClassLikeDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration("public class Builder {}");

            BuildMethod(dataProvider, theInterface, builderClass, className, typeParameters);

            foreach (IMethod node in theInterface.Methods)
            {
                builderClass.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockClassVariable(node, theInterface).BuilderDeclaration()));
                builderClass.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockBuilderMethod(node, theInterface).Typed()));
                builderClass.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockBuilderMethod(node, theInterface).Lambdad()));
            }
            classDeclaration.AddClassMemberDeclaration(builderClass);

            foreach (IMethod node in theInterface.Methods)
            {
                ISignature2 methodSig = MockAssertMethod(node, theInterface);
                methodSig.Signature(classDeclaration, dataProvider.ElementFactory);
            }
            return(textControl =>
            {
                IntentionTemplateUtil.ExecuteTemplate(solution, textControl, DocumentRange.InvalidRange);
            });
        }
示例#2
0
        private void ProcessInterface(ICSharpContextActionDataProvider dataProvider, IClassLikeDeclaration classDeclaration, ICollection <IInterface> interfacesArray, IClassLikeDeclaration builderClass)
        {
            if (interfacesArray.IsEmpty())
            {
                return;
            }

            foreach (IInterface theInterface in interfacesArray)
            {
                ProcessInterface(dataProvider, classDeclaration, theInterface.GetSuperTypes().Select(x => x.GetTypeElement()).OfType <IInterface>().ToArray(), builderClass);

                foreach (IEvent theEvent in theInterface.Events)
                {
                    string eventShortName           = theEvent.ShortName;
                    string eventTypePresentableName = theEvent.Type.GetPresentableName(Languages.Instance.GetLanguageByName("CSHARP"));
                    classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration($"public event {eventTypePresentableName} _{eventShortName};"));
                    if (theEvent.Type.GetPresentableName(Languages.Instance.GetLanguageByName("CSHARP")) == "EventHandler")
                    {
                        classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration($"public void Trigger{eventShortName}() => _{eventShortName}.Invoke(this, EventArgs.Empty);"));
                    }
                    else
                    {
                        //type.GetScalarType().GetTypeElement().TypeParameters[0]
                        //ITypeParameter x = theEvent.Type.GetScalarType().GetTypeElement().TypeParameters[0];
                        //DeclaredTypeBase con = theEvent.Type as DeclaredTypeFromReferenceName;
                        //classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration($"public void Trigger{eventShortName}" +
                        //$"({x.ShortName} val) => _{eventShortName}.Invoke(val);"));
                    }
                }

                foreach (IMethod node in theInterface.Methods)
                {
                    ISignature methodSig = new MockMethod(node, theInterface);
                    classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(methodSig.Signature()));
                    classDeclaration.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockClassVariable(node, theInterface).Declaration()));
                }

                foreach (IMethod node in theInterface.Methods)
                {
                    builderClass.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockClassVariable(node, theInterface).BuilderDeclaration()));
                    builderClass.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockBuilderMethod(node, theInterface).Typed()));
                    builderClass.AddClassMemberDeclaration((IClassMemberDeclaration)dataProvider.ElementFactory.CreateTypeMemberDeclaration(MockBuilderMethod(node, theInterface).Lambdad()));
                }

                foreach (IMethod node in theInterface.Methods)
                {
                    ISignature2 methodSig = MockAssertMethod(node, theInterface);
                    methodSig.Signature(classDeclaration, dataProvider.ElementFactory);
                }
            }
        }