private void PropertyMapperDialog_Shown(object sender, EventArgs e)
        {
            CodeParameter2 fromParameter = m_currentMethod.Parameters.Item(1) as CodeParameter2;
            string         returnType    = m_currentMethod.Type.AsFullName;
            string         fromType      = fromParameter.Type.AsFullName;

            lblFromClass.Text = fromType;
            lblToClass.Text   = returnType;
            try
            {
                InitListView(lstFromProperties, CodeModelUtils.GetPropertiesFromType(fromParameter.Type.CodeType));
            }
            catch (NotImplementedException)
            {
                AddInUtils.ShowError("The method parameter '" + fromParameter.Type.AsString + "' could not be found in the code model.  Please be sure the type exists and the project has been successfully built.");
                this.DialogResult = DialogResult.Cancel;
            }
            try
            {
                InitListView(lstToProperties, CodeModelUtils.GetPropertiesFromType(m_currentMethod.Type.CodeType));
            }
            catch (NotImplementedException)
            {
                AddInUtils.ShowError("The method return type '" + m_currentMethod.Type.AsString + "' could not be found in the code model.  Please be sure the type exists and the project has been successfully built.");
                this.DialogResult = DialogResult.Cancel;
            }

            //InitTreeView(treeView1, CodeModelUtils.GetPropertiesFromType(fromParameter.Type.CodeType));
            RefreshView();
            if (m_addInSettings.PropertyMapperAutoMapOnOpen)
            {
                AutoMap();
            }
        }
Example #2
0
        public static bool IsConstructorDefined(CodeClass2 codeClass, CodeVariable2[] codeVariables)
        {
            List <CodeFunction2> constructors = GetConstructors(codeClass);

            foreach (CodeFunction2 constructor in constructors)
            {
                if (constructor.Parameters.Count == codeVariables.Length)
                {
                    bool areEqual = true;
                    foreach (CodeElement codeElement in constructor.Parameters)
                    {
                        CodeParameter2 codeParameter = (CodeParameter2)codeElement;
                        if (!ContainsType(codeVariables, codeParameter.Type.AsFullName))
                        {
                            areEqual = false;
                            break;
                        }
                    }

                    if (areEqual)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #3
0
 private string GetKindString(CodeParameter2 codeParameter)
 {
     return(Switch.Into <string>().From(codeParameter.ParameterKind)
            .Case(vsCMParameterKind.vsCMParameterKindOut, "out ")
            .Case(vsCMParameterKind.vsCMParameterKindRef, "ref ")
            .Default(""));
 }
Example #4
0
        /// <summary>
        /// 克隆一个方法
        /// </summary>
        /// <param name="cloneFunction">要克隆的方法</param>
        /// <param name="codeClass">添加到类中</param>
        private void CloneFunction(CodeFunction cloneFunction, CodeClass codeClass)
        {
            CodeFunction codeFunction = codeClass.AddFunction(cloneFunction.Name, cloneFunction.FunctionKind,
                                                              cloneFunction.Type, -1, cloneFunction.Access, null);

            //添加参数
            for (int i = 1; i <= cloneFunction.Parameters.Count; i++)
            {
                CodeParameter2 parameter      = cloneFunction.Parameters.Item(i) as CodeParameter2;
                CodeParameter2 cloneParameter = codeFunction.AddParameter(parameter.FullName, parameter.Type.AsFullName, i) as CodeParameter2;
                cloneParameter.DefaultValue  = parameter.DefaultValue;
                cloneParameter.ParameterKind = parameter.ParameterKind;
            }

            //添加属性
            for (int i = 1; i <= cloneFunction.Attributes.Count; i++)
            {
                CodeAttribute attribute = cloneFunction.Attributes.Item(i) as CodeAttribute;
                codeFunction.AddAttribute(attribute.Name, attribute.Value, i);
            }

            //方法注释
            codeFunction.Comment = cloneFunction.Comment;
            //方法说明
            codeFunction.DocComment = cloneFunction.DocComment;
            //静态修饰
            codeFunction.IsShared = cloneFunction.IsShared;
            //抽象修饰
            codeFunction.MustImplement = cloneFunction.MustImplement;
            //重载修饰
            codeFunction.CanOverride = cloneFunction.CanOverride;
        }
Example #5
0
        public void initialize_constructor()
        {
            TextSelection selection    = studio.ActiveDocument.Selection as TextSelection;
            CodeClass2    class_object = (CodeClass2)selection.ActivePoint.get_CodeElement(vsCMElement.vsCMElementClass);
            CodeFunction2 constructor  = class_object.AddFunction(class_object.Name, vsCMFunction.vsCMFunctionConstructor,
                                                                  vsCMTypeRef.vsCMTypeRefVoid, -1, vsCMAccess.vsCMAccessPublic, 0) as CodeFunction2;

            string text = "";

            foreach (CodeElement2 member in class_object.Members)
            {
                if (member.Kind == vsCMElement.vsCMElementVariable)
                {
                    CodeVariable2  variable  = member as CodeVariable2;
                    CodeParameter2 parameter = constructor.AddParameter("new_" + variable.Name, variable.Type, -1) as CodeParameter2;
                    text += "\r\n" + variable.Name + " = " + parameter.Name + ";";
                }
                else if (member.Kind == vsCMElement.vsCMElementProperty)
                {
                    var variable = member as CodeProperty;
                    // CodeTypeRef new_type =
                    CodeParameter2 parameter = constructor.AddParameter("new_" + variable.Name, variable.Type, -1) as CodeParameter2;
                    text += "\r\n" + variable.Name + " = " + parameter.Name + ";";
                }
            }

            EditPoint2 point = constructor.EndPoint.CreateEditPoint() as EditPoint2;

            point.LineUp(1);
            point.Insert(text);
            selection.MoveToPoint(constructor.StartPoint, false);
            selection.MoveToPoint(constructor.EndPoint, true);
            selection.SmartFormat();
            selection.MoveToPoint(point, false);
        }
        public void Parameters_MethodHasOneParameter_ReturnsOneCodeParameter2()
        {
            AddParameterToMethod("test");
            CreatePublicFunction("MyClass.MyMethod");

            CodeParameter2 parameter = codeFunction.Parameters.FirstCodeParameter2OrDefault();

            Assert.AreEqual("test", parameter.Name);
        }
 /// <summary>
 /// constructor
 /// </summary>
 public MethodParamInfo(CodeFunctionInfo parent, CodeParameter2 item, string comment)
 {
     this.parent = parent;
     this.item = item;
     this.Comment = comment;
     Name = item.Name;
     Type = TypeInfo.Create(item.Type);
     DefaultValue = item.DefaultValue;
 }
 /// <summary>
 /// constructor
 /// </summary>
 public ParamInfo(CodePropertyInfo parent, CodeParameter2 item, int position, string comment)
 {
     this.parent = parent;
     this.item = item;
     this.Comment = comment;
     Name = item.Name;
     Type = TypeInfo.Create(item.Type);
     DefaultValue = item.DefaultValue;
     Position = position;
 }
Example #9
0
        public void Parameters_MethodHasOneParameter_ReturnsOneCodeParameter2()
        {
            CreateFunction(
                "public class MyClass {\r\n" +
                "    public void MyMethod(int test) {}\r\n" +
                "}");

            CodeParameter2 parameter = codeFunction.Parameters.FirstCodeParameter2OrDefault();

            Assert.AreEqual("test", parameter.Name);
        }
        /// <summary>
        /// Obtains a reflection wrapper for a parameter.
        /// </summary>
        /// <param name="target">The parameter, or null if none.</param>
        /// <returns>The reflection wrapper, or null if none.</returns>
        public StaticParameterWrapper Wrap(CodeParameter2 target)
        {
            if (target == null)
            {
                return(null);
            }

            StaticMemberWrapper member = Wrap(GetContainingFunction((CodeElement)target));

            return(new StaticParameterWrapper(this, target, member));
        }
        /// <inheritdoc />
        protected override IEnumerable <StaticAttributeWrapper> GetParameterCustomAttributes(StaticParameterWrapper parameter)
        {
            if (parameter.Handle is CodeTypeRef)
            {
                return(EmptyArray <StaticAttributeWrapper> .Instance);
            }

            CodeParameter2 parameterHandle = (CodeParameter2)parameter.Handle;

            return(WrapAttributes(parameterHandle.Attributes));
        }
        /// <inheritdoc />
        protected override string GetParameterName(StaticParameterWrapper parameter)
        {
            if (parameter.Handle is CodeTypeRef)
            {
                return(null);
            }

            CodeParameter2 parameterHandle = (CodeParameter2)parameter.Handle;

            return(parameterHandle.Name);
        }
Example #13
0
 private static string GetParameterName(CodeParameter2 param)
 {
     var typeString = param.Type.AsFullName;
     GetTypeName(param.Type);
     if (param.Type.TypeKind == vsCMTypeRef.vsCMTypeRefArray)
         typeString = getArray(param.Type);
     typeString = GenericNameMangler.MangleParameterName(typeString);
     if (param.ParameterKind == vsCMParameterKind.vsCMParameterKindOut || param.ParameterKind == vsCMParameterKind.vsCMParameterKindRef)
     {
         typeString += "&";
     }
     return typeString;
 }
        public static void PrintParameter2Info(CodeParameter2 item)
        {
            return;

            Debug.WriteLine("--------PrintParameter2Info--------");
            Debug.WriteLine("Rename Method.FullName:" + item.FullName);
            Debug.WriteLine("Rename Method.DefaultValue  :" + item.DefaultValue);
            Debug.WriteLine("Rename Method.DocComment   :" + item.DocComment);
            Debug.WriteLine("Rename Method.ParameterKind   :" + item.ParameterKind);
            Debug.WriteLine("Rename Method.Type   :" + item.Type);
            Debug.WriteLine("Rename Method.Name  :" + item.Name);
            Debug.WriteLine("-----------------------------------");
        }
Example #15
0
        void CreateParameter(string code)
        {
            AddCodeFile("class.cs", code);
            IMethod method = assemblyModel
                             .TopLevelTypeDefinitions
                             .First()
                             .Members
                             .First()
                             .Resolve() as IMethod;

            IParameter member = method.Parameters.First();

            parameter = new CodeParameter2(codeModelContext, member);
        }
Example #16
0
            private void RepairPageMethod(CodeFunction2 method)
            {
                if (method == null || method.Reference == null)
                {
                    return;
                }

                // Tweak the signature so it's appropriate
                method.IsShared = true;
                method.Access   = vsCMAccess.vsCMAccessPublic;
                int i = 0;

                ParameterInfo[] parameters = _signature.GetParameters();
                foreach (object p in method.Parameters)
                {
                    CodeParameter2 parameter = new CodeParameter2(p);
                    if (parameter.Reference != null)
                    {
                        parameter.Name = parameters[i++].Name;
                    }
                }

                // Add the necessary attributes
                bool hasWebMethod    = false;
                bool hasScriptMethod = false;

                foreach (object attr in method.Attributes)
                {
                    CodeAttribute2 attribute = new CodeAttribute2(attr);
                    if (attribute.Reference == null)
                    {
                        continue;
                    }

                    hasWebMethod    |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("WebMethod");
                    hasScriptMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("ScriptMethod");
                    if (hasWebMethod && hasScriptMethod)
                    {
                        break;
                    }
                }
                if (!hasWebMethod)
                {
                    method.AddAttribute(typeof(WebMethodAttribute).FullName, "", -1);
                }
                if (!hasScriptMethod)
                {
                    method.AddAttribute(typeof(ScriptMethodAttribute).FullName, "", -1);
                }
            }
Example #17
0
            private bool PageMethodNeedsRepair(CodeFunction2 method)
            {
                if (method == null || method.Reference == null)
                {
                    return(false);
                }

                ParameterInfo[] parameters = _signature.GetParameters();

                // Tweak the signature so it's appropriate
                if (!method.IsShared)
                {
                    return(true);
                }
                if (method.Access != vsCMAccess.vsCMAccessPublic)
                {
                    return(true);
                }

                int i = 0;

                foreach (object p in method.Parameters)
                {
                    CodeParameter2 parameter = new CodeParameter2(p);
                    if ((parameter.Reference == null) || (string.Compare(parameter.Name, parameters[i++].Name, StringComparison.Ordinal) != 0))
                    {
                        return(true);
                    }
                }

                // Add the necessary attributes
                bool hasWebMethod    = false;
                bool hasScriptMethod = false;

                foreach (object attr in method.Attributes)
                {
                    CodeAttribute2 attribute = new CodeAttribute2(attr);
                    if (attribute.Reference == null)
                    {
                        continue;
                    }
                    hasWebMethod    |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("WebMethod");
                    hasScriptMethod |= !string.IsNullOrEmpty(attribute.Name) && attribute.Name.Contains("ScriptMethod");
                    if (hasWebMethod && hasScriptMethod)
                    {
                        break;
                    }
                }
                return(!hasWebMethod || !hasScriptMethod);
            }
        private static string GetParameterName(CodeParameter2 param)
        {
            var typeString = param.Type.AsFullName;

            GetTypeName(param.Type);
            if (param.Type.TypeKind == vsCMTypeRef.vsCMTypeRefArray)
            {
                typeString = getArray(param.Type);
            }
            typeString = GenericNameMangler.MangleParameterName(typeString);
            if (param.ParameterKind == vsCMParameterKind.vsCMParameterKindOut || param.ParameterKind == vsCMParameterKind.vsCMParameterKindRef)
            {
                typeString += "&";
            }
            return(typeString);
        }
Example #19
0
        private bool IsCommandAvailable(CodeFunction2 currentMethod, bool fullCheck)
        {
            if (
                // Ensure we found the current method
                currentMethod != null &&
                // That it has one parameter
                currentMethod.Parameters.Count == 1 &&

                /*
                 * // That it is an override
                 * currentMethod.OverrideKind == vsCMOverrideKind.vsCMOverrideKindOverride &&
                 */
                // That it is function (method)
                currentMethod.FunctionKind == vsCMFunction.vsCMFunctionFunction &&
                // That it exists inside a project
                currentMethod.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject &&
                // It's return type is not void
                currentMethod.Type.AsString != "void" &&
                // The return type does not equal the parameter type
                ((CodeParameter2)currentMethod.Parameters.Item(1)).Type.AsFullName != currentMethod.Type.AsFullName &&
                // And it's name is either ends with ToEntity or begins with To
                ((currentMethod.Name.EndsWith("ToEntity") || currentMethod.Name.StartsWith("To")) ||
                 !m_addInSettings.OnlyEnableCodeGenOnConversionMethods)
                )
            {
                if (!fullCheck)
                {
                    return(true);
                }

                CodeParameter2 param           = currentMethod.Parameters.Item(1) as CodeParameter2;
                CodeClass2     containingClass = currentMethod.Parent as CodeClass2;
                if (// Ensure we found the parameter and the parent class
                    param != null &&
                    containingClass != null &&

                    (!m_addInSettings.OnlyEnableCodeGenInDaoImpl ||
                     // Ensure we are in a DaoImpl class
                     containingClass.Name.Contains("DaoImpl"))

                    )
                {
                    return(true);
                }
            }
            return(false);
        }
Example #20
0
            private static CodeFunction2 FindMethod(CodeClass2 classModel, string name, MethodInfo signature)
            {
                ParameterInfo[] parameters = signature.GetParameters();

                // Check each of the overloads to see if we have a matching signature
                foreach (CodeFunction2 method in FindMethods(classModel, name))
                {
                    if (method == null || method.Reference == null)
                    {
                        continue;
                    }

                    // Check the return type
                    if (!AreSameType(method.Type, signature.ReturnType))
                    {
                        continue;
                    }

                    // Check the number of parameters
                    if (method.Parameters.Count != parameters.Length)
                    {
                        continue;
                    }

                    // Check the types of each parameter
                    bool failed = false;
                    int  i      = 0;
                    foreach (object p in method.Parameters)
                    {
                        CodeParameter2 parameter = new CodeParameter2(p);
                        if ((parameter.Reference == null) || !AreSameType(parameter.Type, parameters[i++].ParameterType))
                        {
                            failed = true;
                            break;
                        }
                    }
                    if (!failed)
                    {
                        return(method);
                    }
                }

                // Return null if no matching signature was found
                return(null);
            }
        /// <inheritdoc />
        protected override ParameterAttributes GetParameterAttributes(StaticParameterWrapper parameter)
        {
            if (parameter.Handle is CodeTypeRef)
            {
                return(ParameterAttributes.None);
            }

            CodeParameter2 parameterHandle = (CodeParameter2)parameter.Handle;

            vsCMParameterKind   kind  = parameterHandle.ParameterKind;
            ParameterAttributes flags = 0;

            ReflectorFlagsUtils.AddFlagIfTrue(ref flags, ParameterAttributes.HasDefault, parameterHandle.DefaultValue != null);
            ReflectorFlagsUtils.AddFlagIfTrue(ref flags, ParameterAttributes.In, (kind & vsCMParameterKind.vsCMParameterKindIn) != 0);
            ReflectorFlagsUtils.AddFlagIfTrue(ref flags, ParameterAttributes.Out, (kind & vsCMParameterKind.vsCMParameterKindOut) != 0);
            ReflectorFlagsUtils.AddFlagIfTrue(ref flags, ParameterAttributes.Optional, (kind & vsCMParameterKind.vsCMParameterKindOptional) != 0);
            return(flags);
        }
Example #22
0
        /// <summary>
        /// 重构方法
        /// </summary>
        /// <param name="replacedFunction"></param>
        /// <param name="newFunction"></param>
        private void RefactorMethod(CodeFunction replacedFunction, CodeFunction newFunction)
        {
            //清除方法参数
            while (replacedFunction.Parameters.Count > 0)
            {
                replacedFunction.RemoveParameter(replacedFunction.Parameters.Count);
            }

            //清除方法属性
            while (replacedFunction.Attributes.Count > 0)
            {
                ((CodeAttribute)replacedFunction.Attributes.Item(replacedFunction.Attributes.Count)).Delete();
            }

            //添加参数
            for (int i = 1; i <= newFunction.Parameters.Count; i++)
            {
                CodeParameter2 parameter      = newFunction.Parameters.Item(i) as CodeParameter2;
                CodeParameter2 cloneParameter = replacedFunction.AddParameter(parameter.FullName, parameter.Type.AsFullName, i) as CodeParameter2;
                cloneParameter.DefaultValue  = parameter.DefaultValue;
                cloneParameter.ParameterKind = parameter.ParameterKind;
            }

            //添加属性
            for (int i = 1; i <= newFunction.Attributes.Count; i++)
            {
                CodeAttribute2 attribute = newFunction.Attributes.Item(i) as CodeAttribute2;
                replacedFunction.AddAttribute(attribute.Name, attribute.Value, i);
            }

            //方法名
            replacedFunction.Name = newFunction.Name;
            //方法注释
            replacedFunction.Comment = newFunction.Comment;
            //方法说明
            replacedFunction.DocComment = newFunction.DocComment;
            //静态修饰
            replacedFunction.IsShared = newFunction.IsShared;
            //抽象修饰
            replacedFunction.MustImplement = newFunction.MustImplement;
            //重载修饰
            replacedFunction.CanOverride = newFunction.CanOverride;
        }
        /// <inheritdoc />
        protected override StaticTypeWrapper GetParameterType(StaticParameterWrapper parameter)
        {
            CodeTypeRef returnTypeHandle = parameter.Handle as CodeTypeRef;

            if (returnTypeHandle != null)
            {
                return(MakeType(returnTypeHandle));
            }

            CodeParameter2    parameterHandle = (CodeParameter2)parameter.Handle;
            StaticTypeWrapper parameterType   = MakeType(parameterHandle.Type);

            if ((parameterHandle.ParameterKind & (vsCMParameterKind.vsCMParameterKindRef | vsCMParameterKind.vsCMParameterKindOut)) != 0)
            {
                parameterType = parameterType.MakeByRefType();
            }

            return(parameterType);
        }
        /// <inheritdoc />
        protected override int GetParameterPosition(StaticParameterWrapper parameter)
        {
            if (parameter.Handle is CodeTypeRef)
            {
                return(-1);
            }

            CodeParameter2 parameterHandle = (CodeParameter2)parameter.Handle;

            CodeElements codeElements = parameterHandle.Collection;

            for (int i = 0; i < codeElements.Count; i++)
            {
                if (codeElements.Item(i) == parameterHandle)
                {
                    return(i);
                }
            }

            throw new InvalidOperationException("Could not obtain position of parameter.");
        }
 public CodeParameterNodeFactory(CodeParameter2 parameter) : base(parameter as CodeElement)
 {
     _parameter = parameter;
 }
Example #26
0
 internal ShellCodeParameter(CodeParameter2 parameter) : base(parameter as CodeElement2)
 {
     _parameter = parameter;
 }
 /// <summary>
 /// 
 /// </summary>
 public virtual ParamInfo CreateParameter(CodeFunctionInfo parent, CodeParameter2 item, int position, string parameterComment)
 {
     return new ParamInfo(parent, item, position, parameterComment);
 }
Example #28
0
 public CodeParameterNodeFactory(CodeParameter2 parameter) : base(parameter as CodeElement)
 {
     _parameter = parameter;
 }
        public string GenerateCode(CodeFunction2 method)
        {
            CodeParameter2 param           = method.Parameters.Item(1) as CodeParameter2;
            CodeClass2     containingClass = method.Parent as CodeClass2;

            string returnType         = method.Type.AsFullName;
            string paramType          = param.Type.AsFullName;
            string paramName          = param.Name;
            string returnVariableName = string.Empty;
            string codeToInsert       = string.Empty;

            bool convertingToEntity = CodeModelUtils.IsEntityClass(method.Type.CodeType);

            // If we are converting to an entity
            if (convertingToEntity)
            {
                returnVariableName = "entity";
                codeToInsert      += "// VO to entity conversion\n";
                // Add code to create a new entity with the Factory.NewInstance() method
                codeToInsert += returnType + " " + returnVariableName + " = " + returnType + ".Factory.NewInstance();\n\n";
            }
            else
            {
                returnVariableName = "valueObject";
                codeToInsert      += "// Entity to VO conversion\n\n";
                // Add code to create a new VO with a new statement
                codeToInsert += returnType + " " + returnVariableName + " = new " + returnType + "();\n\n";
            }

            ArrayList unmappedProperties = new ArrayList();

            foreach (Property prop in m_properties)
            {
                if (prop.SourceProperty != null)
                {
                    if (prop.IsNullableType)
                    {
                        codeToInsert += "if (" + paramName + "." + prop.Name + ".HasValue)\n{\n";
                        codeToInsert += returnVariableName + "." + prop.Name + " = " + paramName + "." + prop.SourceProperty.Name + ".Value;\n}\n";
                    }
                    else
                    {
                        codeToInsert += returnVariableName + "." + prop.Name + " = " + paramName + "." + prop.SourceProperty.Name + ";\n";
                    }
                }
                else
                {
                    unmappedProperties.Add(prop);
                }
            }

            foreach (Property unmappedProp in unmappedProperties)
            {
                codeToInsert += "// " + returnVariableName + "." + unmappedProp.Name + "\n";
            }

            // Add the return statement
            codeToInsert += "\nreturn " + returnVariableName + ";\n\n";

            return(codeToInsert);
        }
 public static ITypeMetadata FromCodeElement(CodeParameter2 codeVariable, CodeDomFileMetadata file)
 {
     return GetType(codeVariable, file);
 }
Example #31
0
 private Parameter ParseParameter(CodeParameter2 codeParameter)
 {
     var parameter = new Parameter {Name = codeParameter.Name, Type = codeParameter.Type.AsString, Modifier = codeParameter.ParameterKind.Convert()};
     WriteLine("ParseParameter" + " " + codeParameter.Name + " " + codeParameter.Type.AsString);
     return parameter;
 }
 private CodeDomParameterMetadata(CodeParameter2 codeParameter, CodeDomFileMetadata file)
 {
     this.codeParameter = codeParameter;
     this.file          = file;
 }
 private CodeDomParameterMetadata(CodeParameter2 codeParameter, CodeDomFileMetadata file)
 {
     this.codeParameter = codeParameter;
     this.file = file;
 }
Example #34
0
        public override CodeFunction GenerateTest(CodeClass unitTestCodeClass, CodeFunction originalClassCodeFuntion)
        {
            vsCMFunction functionKind = vsCMFunction.vsCMFunctionFunction;
            object       functionType = null;

            functionKind = vsCMFunction.vsCMFunctionSub;
            functionType = vsCMTypeRef.vsCMTypeRefVoid;

            string nextAvailableName = originalClassCodeFuntion.Name;

            if (!CodeSelectionHandler.CanGenerateHandleCodeFunction(unitTestCodeClass,
                                                                    nextAvailableName))
            {
                nextAvailableName = GetNextAvailableCopyName(unitTestCodeClass, ref nextAvailableName, unitTestCodeClass.ProjectItem.ContainingProject);
            }

            CodeFunction unitTestCodeFunction = unitTestCodeClass.AddFunction(
                nextAvailableName,
                functionKind,
                functionType,
                -1,
                originalClassCodeFuntion.Access,
                -1);

            bool tvIsStatic = originalClassCodeFuntion.IsShared;

            //add the NUnit attribute to the function
            unitTestCodeFunction.AddAttribute("NUnit.Framework.Test", "", -1);

            try
            {
                unitTestCodeFunction.Comment    = originalClassCodeFuntion.Comment;
                unitTestCodeFunction.DocComment = originalClassCodeFuntion.DocComment;
            }
            catch (Exception ex)
            {
                //ignore, for some reason the doc throws in vb
                Logger.LogException(ex);
            }

            string tvFunctionCallTemplate   = string.Empty; //"iv{0}Type.{1}(";
            string tvFunctionReturnTemplate = string.Empty; //"{0} iv{1}Return = ";

            tvFunctionCallTemplate = "iv{0}Type.{1}(";

            if (tvIsStatic)
            {
                tvFunctionCallTemplate = "{0}.{1}(";
            }

            tvFunctionReturnTemplate = "Dim iv{1}Return As {0} = ";

            string tvTempParameterList = string.Empty;
            string tvFunctionCall      = tvFunctionCallTemplate;
            string tvFunctionReturn    = tvFunctionReturnTemplate;


            if (!originalClassCodeFuntion.FunctionKind.ToString().Equals("vsCMFunctionConstructor"))
            {
                CodeElements tvParameters = originalClassCodeFuntion.Parameters;

                foreach (CodeElement tvCodeElement in tvParameters)
                {
                    if (!tvFunctionCall.Equals(tvFunctionCallTemplate))
                    {
                        tvFunctionCall += ", ";
                    }

                    CodeParameter2 tvCodeParameter = (CodeParameter2)tvCodeElement;

                    string parameterName = tvCodeParameter.Name;

                    CodeTypeRef tvParameterType = tvCodeParameter.Type;

                    vsCMParameterKind tvParameterKind = tvCodeParameter.ParameterKind;

                    string parameterTypeAsString = tvParameterType.AsString;

                    tvTempParameterList += "Dim " + parameterName + " As " + parameterTypeAsString + " = New " + parameterTypeAsString + "()" + Environment.NewLine + StringHelper.GetTabString();

                    if (tvParameterKind == vsCMParameterKind.vsCMParameterKindRef)
                    {
                        tvFunctionCall += parameterName;
                    }
                    else if (tvParameterKind == vsCMParameterKind.vsCMParameterKindOut)
                    {
                        tvFunctionCall += parameterName;
                    }
                    else if (tvParameterKind == vsCMParameterKind.vsCMParameterKindIn)
                    {
                        tvFunctionCall += parameterName;
                    }
                    else
                    {
                        tvFunctionCall += parameterName;
                    }
                }

                tvFunctionCall = string.Format(tvFunctionCall + ")" + Environment.NewLine, ((CodeClass)originalClassCodeFuntion.Parent).Name, originalClassCodeFuntion.Name);
            }

            if (originalClassCodeFuntion.Type.TypeKind != vsCMTypeRef.vsCMTypeRefVoid)
            {
                tvFunctionReturn = string.Format(tvFunctionReturn, originalClassCodeFuntion.Type.AsString, originalClassCodeFuntion.Name);
                tvFunctionCall   = tvFunctionReturn + tvFunctionCall;
            }

            TextPoint bodyStartingPoint =
                unitTestCodeFunction.GetStartPoint(vsCMPart.vsCMPartBody);

            EditPoint boydEditPoint = bodyStartingPoint.CreateEditPoint();

            if (!originalClassCodeFuntion.FunctionKind.ToString().Equals("vsCMFunctionConstructor"))
            {
                boydEditPoint.Insert("\t\t\t' TODO: Update variable/s' defaults to meet test needs" + Environment.NewLine);

                boydEditPoint.Insert(StringHelper.GetTabString() + tvTempParameterList + Environment.NewLine);
                boydEditPoint.Insert(StringHelper.GetTabString() + tvFunctionCall + Environment.NewLine);
            }

            if (originalClassCodeFuntion.Type.TypeKind != vsCMTypeRef.vsCMTypeRefVoid)
            {
                string stringHolder = "iv{0}Return";
                stringHolder = string.Format(stringHolder, originalClassCodeFuntion.Name);
                //FIX ME (tabing)
                //boydEditPoint.Insert(string.Format("\t\t\tAssert.AreEqual({0}, default({1}));\r\n", stringHolder, originalClassCodeFuntion.Type.AsString));
            }


            boydEditPoint.Insert(Environment.NewLine);
            boydEditPoint.Insert("\t\t\t'TODO: Update Assert to meet test needs" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\t'Assert.AreEqual( , )" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\t" + Environment.NewLine);
            boydEditPoint.Insert("\t\t\tThrow New Exception 'Not Implemented'" + Environment.NewLine);

            return(unitTestCodeFunction);
        }
 private string GetKindString(CodeParameter2 codeParameter)
 {
     return Switch.Into<string>().From(codeParameter.ParameterKind)
         .Case(vsCMParameterKind.vsCMParameterKindOut, "out ")
         .Case(vsCMParameterKind.vsCMParameterKindRef, "ref ")
         .Default("");
 }
 public static ITypeMetadata FromCodeElement(CodeParameter2 codeVariable, CodeDomFileMetadata file)
 {
     return(GetType(codeVariable, file));
 }
Example #37
0
 void CreateParameter()
 {
     parameter = new CodeParameter2(null, helper.Parameter);
 }
 internal ShellCodeParameter(CodeParameter2 parameter) : base(parameter as CodeElement2)
 {
     _parameter = parameter;
 }
Example #39
0
        public override void Exec(vsCommandExecOption executeOption, ref object varIn, ref object varOut)
        {
            CodeFunction2 currentMethod = CodeModelUtils.GetCurrentMethod(m_application);

            if (IsCommandAvailable(currentMethod))
            {
                try
                {
                    ConversionCodeGenerator codeGenerator = new ConversionCodeGenerator();

                    CodeParameter2 param = currentMethod.Parameters.Item(1) as CodeParameter2;

                    ArrayList toProperties;
                    ArrayList fromProperties;

                    try
                    {
                        toProperties = CodeModelUtils.GetPropertiesFromType(currentMethod.Type.CodeType);
                    }
                    catch (NotImplementedException)
                    {
                        throw new Exception("Method return type '" + currentMethod.Type.AsString + "' could not be resolved.");
                    }

                    try
                    {
                        fromProperties = CodeModelUtils.GetPropertiesFromType(param.Type.CodeType);
                    }
                    catch (NotImplementedException)
                    {
                        throw new Exception("Method parameter type '" + param.Type.AsString + "' could not be resolved.");
                    }

                    foreach (CodeProperty toProperty in toProperties)
                    {
                        bool   mapped       = false;
                        string toName       = toProperty.Name;
                        string toNameNoDots = toName.Replace(".", string.Empty);
                        string toType       = toProperty.Type.AsFullName;

                        foreach (CodeProperty fromProperty in fromProperties)
                        {
                            string fromName       = fromProperty.Name;
                            string fromNameNoDots = fromName.Replace(".", string.Empty);
                            if (fromName == toName || fromName == toNameNoDots || fromNameNoDots == toNameNoDots)
                            {
                                string fromType = fromProperty.Type.AsFullName;
                                if (fromType.Replace("?", string.Empty) == toType.Replace("?", string.Empty))
                                {
                                    codeGenerator.AddProperty(toName, toType, fromName, fromType);
                                    mapped = true;
                                    break;
                                }
                            }
                        }

                        if (!mapped)
                        {
                            codeGenerator.AddProperty(toName, toType);
                        }
                    }

                    AddInUtils.InsertCodeInMethod(currentMethod, codeGenerator.GenerateCode(currentMethod));

                    m_application.StatusBar.Text = "Android/VS: Code inserted";
                }
                catch (Exception e)
                {
                    m_application.StatusBar.Text = "Android/VS: Unable to insert code: " + e.Message;
                    m_application.StatusBar.Highlight(true);
                }
            }
            else
            {
                m_application.StatusBar.Text = "Android/VS: Unable to insert code";
            }
        }