public string ExtractMember(CodeVariable variable)
        {
            var memberStart = variable.GetStartPoint().CreateEditPoint();
            var memberText  = string.Empty;

            memberText += memberStart.GetText(variable.GetEndPoint());
            memberStart.Delete(variable.GetEndPoint());
            return(memberText);
        }
        public void GetEndPoint_VariableEndsAtColumn10_ReturnsTextPointWithLineCharOffset10()
        {
            CreatePublicVariable("MyVariable");
            VariableEndsAtColumn(10);

            TextPoint point  = codeVariable.GetEndPoint();
            int       offset = point.LineCharOffset;

            Assert.AreEqual(10, offset);
        }
        public void GetEndPoint_VariableEndsAtColumnSevenOnLineTwo_ReturnsTextPointWithLineCharOffsetSevenOnLineTwo()
        {
            CreateCodeVariable(
                "public class Foo {\r\n" +
                "int V;\r\n" +
                "}");

            global::EnvDTE.TextPoint point = codeVariable.GetEndPoint();

            Assert.AreEqual(7, point.LineCharOffset);
            Assert.AreEqual(2, point.Line);
        }
        void CreateFieldEditPoint()
        {
            var startPoint = (TextPoint)codeVariable.GetStartPoint();

            endPoint  = (TextPoint)codeVariable.GetEndPoint();
            editPoint = (EditPoint)startPoint.CreateEditPoint();
        }
示例#5
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);
        }
示例#6
0
        void CreateFieldEditPoint()
        {
            var       codeVariable = new CodeVariable(fieldHelper.Field, documentLoader);
            TextPoint startPoint   = codeVariable.GetStartPoint();

            endPoint  = codeVariable.GetEndPoint();
            editPoint = startPoint.CreateEditPoint();
        }
        public async Task GetEndPoint_WholeWithAttributes()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes);

            Assert.Equal(14, endPoint.Line);
            Assert.Equal(26, endPoint.LineCharOffset);
        }
        public async Task GetEndPoint_Navigate()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartNavigate);

            Assert.Equal(14, endPoint.Line);
            Assert.Equal(21, endPoint.LineCharOffset);
        }
        public void GetEndPoint_Navigate()
        {
            CodeVariable testObject = GetCodeVariable("A", "intC");

            TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartNavigate);

            Assert.Equal(14, endPoint.Line);
            Assert.Equal(21, endPoint.LineCharOffset);
        }
        public async Task GetEndPoint_AttributesWithDelimiter()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            TextPoint endPoint = testObject.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter);

            Assert.Equal(13, endPoint.Line);
            Assert.Equal(19, endPoint.LineCharOffset);
        }
示例#11
0
        /// <summary>
        /// Gets the declaration of the specified code field as a string.
        /// </summary>
        /// <param name="codeField">The code field.</param>
        /// <returns>The string declaration.</returns>
        internal static string GetFieldDeclaration(CodeVariable codeField)
        {
            // Get the start point at the end of the attributes if there are any (vsCMPartHeader is
            // not available for fields).
            var startPoint = codeField.Attributes.Count > 0
                ? codeField.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter)
                : codeField.StartPoint;

            return(TextDocumentHelper.GetTextToFirstMatch(startPoint, @"[,;]"));
        }
示例#12
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 AddPublicVariable_VariableTypeIsString_ReturnsCodeVariable()
        {
            CreateClass("MyClass");
            CreateCodeGenerator();
            string fileName = @"d:\projects\myproject\MyClass.cs";

            SetClassFileName(fileName);
            SetDocumentFileName(fileName);
            AddFieldToClassForReparse("MyClass.MyVariable", new DomRegion(1, 2, 1, 5));

            AddPublicVariable("MyVariable", "System.String");

            TextPoint start = codeVariable.GetStartPoint();
            TextPoint end   = codeVariable.GetEndPoint();

            Assert.AreEqual("MyVariable", codeVariable.Name);
            Assert.AreEqual(1, start.Line);
            Assert.AreEqual(2, start.LineCharOffset);
            Assert.AreEqual(1, end.Line);
            Assert.AreEqual(5, end.LineCharOffset);
        }
        public async Task GetEndPoint_Whole()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            Assert.Throws <NotImplementedException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartWhole));
        }
        public async Task GetEndPoint_HeaderWithAttributes()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            Assert.Throws <NotImplementedException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartHeaderWithAttributes));
        }
        public void GetEndPoint_BodyWithDelimiter()
        {
            CodeVariable testObject = GetCodeVariable("A", "intC");

            AssertEx.Throws <NotImplementedException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartBodyWithDelimiter));
        }
        public async Task GetEndPoint_Body()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            Assert.Throws <COMException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartBody));
        }
        public void GetEndPoint_Whole()
        {
            CodeVariable testObject = GetCodeVariable("A", "intC");

            AssertEx.Throws <NotImplementedException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartWhole));
        }
        public void GetEndPoint_HeaderWithAttributes()
        {
            CodeVariable testObject = GetCodeVariable("A", "intC");

            AssertEx.Throws <NotImplementedException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartHeaderWithAttributes));
        }
示例#20
0
        /// <summary>
        /// Gets the declaration of the specified code field as a string.
        /// </summary>
        /// <param name="codeField">The code field.</param>
        /// <returns>The string declaration.</returns>
        internal static string GetFieldDeclaration(CodeVariable codeField)
        {
            // Get the start point at the end of the attributes if there are any (vsCMPartHeader is
            // not available for fields).
            var startPoint = codeField.Attributes.Count > 0
                ? codeField.GetEndPoint(vsCMPart.vsCMPartAttributesWithDelimiter)
                : codeField.StartPoint;

            return TextDocumentHelper.GetTextToFirstMatch(startPoint, @"[,;]");
        }
        public async Task GetEndPoint_BodyWithDelimiter()
        {
            CodeVariable testObject = await GetCodeVariableAsync("A", "intC");

            Assert.Throws <NotImplementedException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartBodyWithDelimiter));
        }
        public void GetEndPoint_Body()
        {
            CodeVariable testObject = GetCodeVariable("A", "intC");

            AssertEx.Throws <COMException>(() => testObject.GetEndPoint(vsCMPart.vsCMPartBody));
        }