public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            int    searchStart = PropertyToChange.TextRange.StartOffset;
            int    searchEnd   = PropertyToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            // Find the opening brace.
            var endOfOpeningBraceIndex = text.IndexOf("{", searchStart, searchEnd - searchStart) + 1;

            int numTabs = InsertionHelpers.GetIndentationInFrontOf(text, searchStart) + 1;

            NewAccessor.Controller.IndentLevel = numTabs;

            string newAccessorText = Helper.RemoveTrailingLineBreaks(NewAccessor.ToString());

            // The "+ numTabs + 2" is account for the /r/n and the tabs that are at the start
            // of the new text. If we don't trim those off the text range, then it will
            // include the whitespace before the actual property, which the objects which have actually
            // been parsed will not have. If this number is wrong, the next object to be placed after it
            // will not be able to get the tabs correctly.
            NewAccessor.TextRange.StartOffset = endOfOpeningBraceIndex + numTabs + 2;
            NewAccessor.TextRange.EndOffset   = endOfOpeningBraceIndex + newAccessorText.Length - (numTabs + 2);
            NewAccessor.Index = endOfOpeningBraceIndex + numTabs + 2;

            sb.Insert(endOfOpeningBraceIndex, newAccessorText);

            // Add accessor to the property
            PropertyToChange.AddChild(NewAccessor);
            NewAccessor.Parent     = PropertyToChange;
            NewAccessor.Controller = PropertyToChange.Controller;

            //return new ActionResult(endOfOpeningBraceIndex, newAccessorText.Length, new[] { NewAccessor });
            return(new ActionResult(NewAccessor.TextRange.StartOffset, newAccessorText.Length, new[] { NewAccessor }));
        }
Ejemplo n.º 2
0
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            int    startSearch = PropertyToAddTo.TextRange.StartOffset;
            string text        = sb.ToString();

            // Put the new Attribute just in front of the property declaration.
            int insertionPoint = text.LastIndexOf("\n", startSearch) + 1;

            AttributeSection section = new AttributeSection(AttributeToAdd.Controller);

            section.AddAttribute(AttributeToAdd);
            section.PreceedingBlankLines = -1;
            var indentLevel = InsertionHelpers.GetIndentationInFrontOf(text, startSearch);

            section.Controller.IndentLevel = indentLevel;

            string newText = Helper.StandardizeLineBreaks(section.ToString(), "\n") + "\n";

            // Calculate the Attribute's Text Range
            // The start index is the insertion point + the number of tabs in front of the attribute, +1 for the \n
            AttributeToAdd.TextRange.StartOffset = insertionPoint + indentLevel + 1;
            AttributeToAdd.TextRange.EndOffset   = AttributeToAdd.TextRange.StartOffset + (newText.Trim()).Length;

            sb.Insert(insertionPoint, newText);

            PropertyToAddTo.AttributeSections.Add(section);

            //return new ActionResult(insertionPoint, newText.Length, new[] { AttributeToAdd });
            return(new ActionResult(AttributeToAdd.TextRange.StartOffset, newText.Length, new[] { AttributeToAdd }));
        }
        protected ActionResult InsertAtStartOfParent(StringBuilder sb)
        {
            int searchStart = ClassToAddTo.TextRange.StartOffset;
            int searchEnd   = ClassToAddTo.TextRange.EndOffset;
            var text        = sb.ToString();

            // Figure out where the first NewLine is after the class's opening brace
            int braceIndex = text.IndexOf("{", searchStart, searchEnd - searchStart);

            // Find the first NewLine after the brace
            int newLineIndex = text.IndexOf("\n", braceIndex, searchEnd - braceIndex);

            // Indent one level further than the Class we are adding to.
            int numTabs = InsertionHelpers.GetIndentationInFrontOf(text, braceIndex) + 1;

            ConstructToAdd.Controller.IndentLevel = numTabs;
            ConstructToAdd.PreceedingBlankLines   = -1;
            string newPropertyText = ConstructToAdd.ToString();

            int insertionIndex = newLineIndex + 1;

            ConstructToAdd.TextRange.StartOffset = insertionIndex + numTabs;
            ConstructToAdd.TextRange.EndOffset   = insertionIndex + (newPropertyText).Length - numTabs;
            ConstructToAdd.Index = insertionIndex + numTabs;

            BeforeInsert(insertionIndex, newPropertyText);
            sb.Insert(insertionIndex, newPropertyText);

            //return new ActionResult(insertionIndex, newPropertyText.Length, new[] { ConstructToAdd });
            return(new ActionResult(ConstructToAdd.TextRange.StartOffset, newPropertyText.Length, new[] { ConstructToAdd }));
        }
        protected ActionResult InsertAtEndOfParent(StringBuilder sb, IBaseConstruct insertAfter)
        {
            if (insertAfter == null)
            {
                return(InsertAtStartOfParent(sb));
            }

            int    lastSiblingStartOffset = insertAfter.TextRange.StartOffset;
            int    lastSiblingEndOffset   = insertAfter.TextRange.EndOffset;
            int    endIndex = ClassToAddTo.TextRange.EndOffset;
            string text     = sb.ToString();

            if (lastSiblingEndOffset < 0 || string.IsNullOrWhiteSpace(text))
            {
                return(InsertAtStartOfParent(sb));
            }

            int firstNewLine;

            if (endIndex > lastSiblingEndOffset)
            {
                firstNewLine = text.IndexOf('\n', lastSiblingEndOffset, (endIndex - lastSiblingEndOffset));
            }
            else
            {
                firstNewLine = text.IndexOf('\n', lastSiblingEndOffset);
            }

            int newStartIndex = firstNewLine + 1;
            int numTabs       = InsertionHelpers.GetIndentationInFrontOf(sb, lastSiblingStartOffset);

            ConstructToAdd.Controller.IndentLevel = numTabs;
            ConstructToAdd.PreceedingBlankLines   = -1;
            string newPropertyText = ConstructToAdd.ToString();

            // The "+ numTabs" is account for the tabs that are at the start
            // of the new text. If we don't trim those off the text range, then it will
            // include the whitespace before the actual property, which the objects which have actually
            // been parsed will not have. If this number is wrong, the next object to be placed after it
            // will not be able to get the tabs correctly.
            ConstructToAdd.TextRange.StartOffset = newStartIndex + numTabs;
            ConstructToAdd.TextRange.EndOffset   = newStartIndex + numTabs + newPropertyText.Trim().Length;
            ConstructToAdd.Index = newStartIndex + numTabs;

            BeforeInsert(newStartIndex, newPropertyText);
            sb.Insert(newStartIndex, newPropertyText);

            return(new ActionResult(ConstructToAdd.TextRange.StartOffset, newPropertyText.Length, new[] { ConstructToAdd }));
        }
Ejemplo n.º 5
0
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            int    searchStart = ClassToChange.TextRange.StartOffset;
            int    searchEnd   = ClassToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            // Find the :
            int colonIndex = text.IndexOf(":", searchStart, searchEnd - searchStart);

            // Find the first brace after that
            int firstBraceIndex = text.IndexOf("{", colonIndex, searchEnd - colonIndex);

            // Trim to just the text between the : and {
            text = text.Substring(colonIndex, firstBraceIndex - colonIndex);

            // Remove each instance of the implement text between the : and {
            // as well as the whitespace and comma that comes just after it.
            Regex removalRegex = new Regex(@"[\s,]+" + ImplementToRemove);
            var   newText      = removalRegex.Replace(text, "");

            // Remove the implement from the class object.
            ClassToChange.BaseNames.RemoveAll(s => s == ImplementToRemove);

            if (ClassToChange.BaseNames.Count == 0)
            {
                // Remove the colon as well by moving the start of the removal
                // to just before it.
                colonIndex--;
                int numTabs = InsertionHelpers.GetIndentationInFrontOf(sb, searchStart);
                newText = Environment.NewLine + new string('\t', numTabs);
            }

            sb.Remove(colonIndex, firstBraceIndex - colonIndex);
            sb.Insert(colonIndex, newText);

            return(new ActionResult(colonIndex, newText.Length - (firstBraceIndex - colonIndex), null));
        }