Example #1
0
        /// <summary>
        /// Implements the mock variable.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        /// <param name="mockVariableDeclaration">The mock variable declaration.</param>
        /// <returns>The code variable.</returns>
        public static CodeVariable ImplementMockVariable(
            this CodeClass instance,
            string name,
            string type,
            string mockVariableDeclaration)
        {
            TraceService.WriteLine("CodeClassExtensions::ImplementMockVariable file=" + instance.Name);

            CodeVariable codeVariable = instance.AddVariable(name, type, 0, vsCMAccess.vsCMAccessPrivate, 0);

            string typeDescriptor = name.Substring(4, 1).ToUpper() + name.Substring(5);

            codeVariable.DocComment = "<doc><summary>\r\nMock " + typeDescriptor + ".\r\n</summary></doc>";

            EditPoint startPoint = codeVariable.StartPoint.CreateEditPoint();
            EditPoint endPoint   = codeVariable.EndPoint.CreateEditPoint();

            //// if we are Moq then we change the variable declaration.
            if (string.IsNullOrEmpty(mockVariableDeclaration) == false)
            {
                string substitution = mockVariableDeclaration.Replace("%TYPE%", type);

                string text    = startPoint.GetText(endPoint);
                string newText = text.Replace("private " + type, "private " + substitution);
                startPoint.ReplaceText(endPoint, newText, 0);
            }

            // get the new endpoint before inserting new line.
            endPoint = codeVariable.EndPoint.CreateEditPoint();
            endPoint.InsertNewLine();

            return(codeVariable);
        }
        private void AddConstructorToRequestHandler(CodeClass codeClass, CreateMessage model)
        {
            var constructor = codeClass.AddFunction(
                Name: codeClass.Name,
                Kind: vsCMFunction.vsCMFunctionConstructor,
                Type: vsCMTypeRef.vsCMTypeRefVoid,
                Position: 0,
                Access: vsCMAccess.vsCMAccessPublic);

            var constructorParameters = model.ConstructorParameters
                                        .Select(x => {
                x.Type = x.Type.Replace("$self$", model.MessageHandlerName.Name);
                return(x);
            })
                                        .ToList();

            constructorParameters.ForEach(x => constructor.AddParameter(x.Name, x.Type, -1));
            constructorParameters.ForEach(x => {
                var variable = codeClass.AddVariable(x.Name, x.Type, 0);
                variable.StartPoint.CreateEditPoint().Insert(model.ServicesAcces);
            });

            var constructorEditPoit = constructor.GetStartPoint(vsCMPart.vsCMPartBody).CreateEditPoint();

            constructorParameters.ForEach(x => constructorEditPoit.Insert(model.RequestHandlerIndent + "this." + x.Name + " = " + x.Name + ";" + Environment.NewLine));
        }
Example #3
0
        /// <summary>
        /// Implements the mock variable.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        /// <returns>The code variable.</returns>
        public static CodeVariable ImplementMockVariable(
            this CodeClass instance,
            string name,
            string type)
        {
            TraceService.WriteLine("CodeClassExtensions::ImplementMockVariable file=" + instance.Name);

            CodeVariable codeVariable = instance.AddVariable(name, type, 0, vsCMAccess.vsCMAccessPrivate, 0);

            string typeDescriptor = name.Substring(4, 1).ToUpper() + name.Substring(5);

            codeVariable.DocComment = "<doc><summary>\r\nMock " + typeDescriptor + ".\r\n</summary></doc>";


            EditPoint startPoint = codeVariable.StartPoint.CreateEditPoint();
            EditPoint endPoint   = codeVariable.EndPoint.CreateEditPoint();

            string text    = startPoint.GetText(endPoint);
            string newText = text.Replace(type, "Mock<" + type + ">");

            startPoint.ReplaceText(endPoint, newText, 0);

            // get the new endpoint before inserting new line.
            endPoint = codeVariable.EndPoint.CreateEditPoint();
            endPoint.InsertNewLine();

            return(codeVariable);
        }
Example #4
0
        /// <summary>
        /// Implements the variable.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        /// <param name="isReadOnly">if set to <c>true</c> [is read only].</param>
        /// <returns>The Code Variable. </returns>
        public static CodeVariable ImplementVariable(
            this CodeClass instance,
            string name,
            string type,
            bool isReadOnly)
        {
            TraceService.WriteLine("CodeClassExtensions::ImplementVariable name=" + name + " type=" + type);

            CodeVariable codeVariable = instance.AddVariable(name, type, 0, vsCMAccess.vsCMAccessPrivate, 0);

            codeVariable.DocComment = "<doc><summary>\r\nBacking field for " + name + ".\r\n</summary></doc>";
            codeVariable.GetEndPoint().CreateEditPoint().InsertNewLine();

            if (isReadOnly)
            {
                CodeVariable2 codeVariable2 = codeVariable as CodeVariable2;

                if (codeVariable2 != null)
                {
                    codeVariable2.ConstKind = vsCMConstKind.vsCMConstKindReadOnly;
                }
            }

            return(codeVariable);
        }
Example #5
0
        public bool CreateSynchClass()
        {
            CodeClass synchClass = _cc.AddClass("Synch" + _cc.Name, -1, 0,
                                                0, EnvDTE.vsCMAccess.vsCMAccessPrivate);

            synchClass.AddBase(_cc, -1);

            //member variables
            synchClass.AddVariable("_root", "System.Object", -1, EnvDTE.vsCMAccess.vsCMAccessPrivate, null);
            synchClass.AddVariable("_parent", _cc.FullName, -1, EnvDTE.vsCMAccess.vsCMAccessPrivate, null);

            //constructor - add function can't handle this at the moment
            EditPoint classEndPt = synchClass.EndPoint.CreateEditPoint();

            classEndPt.StartOfLine();
            classEndPt.Insert("\n");

            EditPoint editPt = synchClass.StartPoint.CreateEditPoint();

            editPt.LineDown(3);
            editPt.EndOfLine();
            editPt.Insert("\ninternal " + synchClass.Name + "(" + _cc.Name +
                          " parent){\n_parent = parent;\n_root = parent.SyncRoot;\n}\n");
            editPt.MoveToPoint(synchClass.StartPoint);
            editPt.SmartFormat(synchClass.EndPoint);

            //functions, properties and indexers
            for (CodeType ct = (CodeType)_cc; ct != null;)
            {
                if (!AddMemberElementsFromType(ct, synchClass))
                {
                    return(false);
                }
                if (ct.Bases.Count != 0)
                {
                    ct = (CodeType)(ct.Bases.Item(1));
                    if (ct.Name == "Object")
                    {
                        break;
                    }
                }
            }

            synchClass.StartPoint.CreateEditPoint().SmartFormat(synchClass.EndPoint);

            return(true);
        }
        public void AddPublicProperty(string propertyName, string typeName)
        {
            var prop        = _codeClass.AddVariable(propertyName, typeName, -1, vsCMAccess.vsCMAccessPublic);
            var startPoint  = prop.StartPoint;
            int linelength  = startPoint.LineLength;
            var edit        = startPoint.CreateEditPoint();
            var currentLine = edit.GetLines(edit.Line, edit.Line + 1).TrimStart();
            var newLine     = currentLine.Replace(";", " { get; set; }");

            edit.Delete(currentLine.Length);
            edit.Insert(newLine);
        }
Example #7
0
        /// <summary>
        /// Implements the variable.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <param name="name">The name.</param>
        /// <param name="type">The type.</param>
        /// <param name="isReadOnly">if set to <c>true</c> [is read only].</param>
        /// <returns>The Code Variable. </returns>
        public static CodeVariable ImplementVariable(
            this CodeClass instance,
            string name,
            string type,
            bool isReadOnly)
        {
            TraceService.WriteLine("CodeClassExtensions::ImplementVariable name=" + name + " type=" + type);

            string variableName = name;

            bool placeHolderVariable = false;

            //// are we a place holder variable (used in snippets!)
            if (variableName.Contains("%"))
            {
                variableName        = variableName.Replace("%", string.Empty);
                placeHolderVariable = true;
            }

            CodeVariable codeVariable = instance.AddVariable(variableName, type, 0, vsCMAccess.vsCMAccessPrivate, 0);

            codeVariable.DocComment = "<doc><summary>" + "\r\nBacking field for " + name + ".\r\n</summary></doc>";
            codeVariable.GetEndPoint().CreateEditPoint().InsertNewLine();

            if (isReadOnly)
            {
                CodeVariable2 codeVariable2 = codeVariable as CodeVariable2;

                if (codeVariable2 != null)
                {
                    codeVariable2.ConstKind = vsCMConstKind.vsCMConstKindReadOnly;
                }
            }

            if (placeHolderVariable)
            {
                EditPoint startPoint = codeVariable.StartPoint.CreateEditPoint();
                EditPoint endPoint   = codeVariable.EndPoint.CreateEditPoint();
                string    text       = startPoint.GetText(endPoint);
                string    newText    = text.Replace(variableName, name);
                startPoint.ReplaceText(endPoint, newText, 0);
            }

            return(codeVariable);
        }
        public void DefinePublicProperty(string propertyName, string typeName)
        {
            FileCodeModel fileCodeModel = _projectItem.FileCodeModel;

            CodeElement   fileCodeElement      = fileCodeModel.CodeElements.Item(1);
            CodeNamespace fileNamespace        = (CodeNamespace)fileCodeElement;
            CodeElement   fileNamespaceElement = fileNamespace.Members.Item(1);
            CodeClass     codeClass            = (CodeClass)fileNamespaceElement;

            var l = codeClass.Language;

            codeClass.AddProperty(propertyName, propertyName, typeName, 0, vsCMAccess.vsCMAccessPublic);

            codeClass.AddVariable(propertyName, typeName, -1, vsCMAccess.vsCMAccessPublic);



            //fileCodeModel.AddVariable(propertyName, typeName, -1, vsCMAccess.vsCMAccessPublic);



            //CodeElement fileCodeElement = fileCodeModel.CodeElements.Item(1);
            //CodeNamespace fileNamespace = (CodeNamespace)fileCodeElement;
            //CodeElement fileNamespaceElement = fileNamespace.Members.Item(1);
            //CodeClass codeClass = (CodeClass)fileNamespaceElement;


            //var prop = codeClass.AddVariable(propertyName, typeName, -1, vsCMAccess.vsCMAccessPublic);
            //var startPoint = prop.StartPoint;
            //int linelength = startPoint.LineLength;
            //var edit = startPoint.CreateEditPoint();
            //var currentLine = edit.GetLines(edit.Line, edit.Line + 1).TrimStart();
            //var newLine = currentLine.Replace(";", " { get; set; }");
            //edit.Delete(currentLine.Length);
            //edit.Insert(newLine);
        }
Example #9
0
        /// <summary>
        /// Selects the introduction page html to be at the top of the windows
        /// </summary>
        public override void Execute()
        {
            DTE         vs = GetService <DTE>(true);
            ProjectItem actionClassFile = (ProjectItem)DteHelper.GetTarget(vs);

            if ((actionClassFile == null) || (actionClassFile.FileCodeModel == null) ||
                (actionClassFile.FileCodeModel.CodeElements == null) ||
                (actionClassFile.FileCodeModel.CodeElements.Count == 0))
            {
                return;
            }

            string fieldName = "";

            if ((ParameterName.Length > 1) && (ParameterName[0] >= 'A') && (ParameterName[0] <= 'Z'))
            {
                fieldName = char.ToLower(parameterName[0], CultureInfo.CurrentCulture) +
                            parameterName.Substring(1);
            }
            else
            {
                fieldName = "_" + parameterName;
            }

            string attribute;

            if (ParameterIsOutput)
            {
                attribute = "Output";
            }
            else
            {
                attribute = "Input";
            }
            bool addedProperty = false;

            foreach (CodeElement element in actionClassFile.FileCodeModel.CodeElements)
            {
                if (element.Kind == vsCMElement.vsCMElementNamespace)
                {
                    foreach (CodeElement elementNamespace in ((CodeNamespace)element).Members)
                    {
                        if (elementNamespace.Kind == vsCMElement.vsCMElementClass)
                        {
                            #region Look where to insert the property
                            object    whereToInsert = null;
                            CodeClass classAction   = (CodeClass)elementNamespace;

                            CodeProperty foundProperty = null;
                            foreach (CodeElement classElement in classAction.Members)
                            {
                                if (foundProperty != null)
                                {
                                    if (classElement.Kind == vsCMElement.vsCMElementVariable)
                                    {
                                        // Then insert after this declaration of variable that was after the property
                                        whereToInsert = classElement;
                                    }
                                }
                                if (classElement.Kind == vsCMElement.vsCMElementProperty)
                                {
                                    foundProperty = (CodeProperty)classElement;
                                    bool hasAttribute = false;
                                    for (int i = 1; i <= foundProperty.Attributes.Count; i++)
                                    {
                                        if (foundProperty.Attributes.Item(i).Name == attribute)
                                        {
                                            hasAttribute = true;
                                            break;
                                        }
                                    }
                                    if (!hasAttribute)
                                    {
                                        foundProperty = null;
                                    }
                                }
                            }
                            if (foundProperty != null)
                            {
                                if (whereToInsert == null)
                                {
                                    whereToInsert = foundProperty;
                                }
                            }
                            else if (whereToInsert == null)
                            {
                                whereToInsert = 0;
                            }

                            #endregion

                            CodeProperty prop = classAction.AddProperty(ParameterName, ParameterName, ParameterType, whereToInsert, vsCMAccess.vsCMAccessPublic, actionClassFile.Name);

                            TextPoint getTextTP = prop.Getter.GetStartPoint(vsCMPart.vsCMPartBody);
                            EditPoint getText   = getTextTP.CreateEditPoint();
                            getText.ReplaceText(prop.Getter.GetEndPoint(vsCMPart.vsCMPartBody),
                                                string.Format(CultureInfo.InvariantCulture, "return {0};", fieldName), (int)vsEPReplaceTextOptions.vsEPReplaceTextNormalizeNewlines);
                            getText.SmartFormat(getTextTP);

                            TextPoint setTextTP = prop.Setter.GetStartPoint(vsCMPart.vsCMPartBody);
                            EditPoint setText   = setTextTP.CreateEditPoint();
                            setText.ReplaceText(0, string.Format(CultureInfo.InvariantCulture, "{0}=value;", fieldName), 0);
                            setText.SmartFormat(setTextTP);

                            if (ParameterIsOutput)
                            {
                                prop.AddAttribute(attribute, "", 0);
                            }
                            else
                            {
                                prop.AddAttribute(attribute, "true", 0);
                            }
                            classAction.AddVariable(fieldName, ParameterType, prop, vsCMAccess.vsCMAccessPrivate, actionClassFile.Name);

                            // Stop adding property, just the first class found
                            addedProperty = true;
                            break;
                        }
                    }
                    if (addedProperty)
                    {
                        break;
                    }
                }
            }
        }