예제 #1
0
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            if (FunctionToChange.Name == NewName)
            {
                return(new ActionResult());
            }

            // Search FunctionToChange TextRange for name
            int    searchStart = FunctionToChange.TextRange.StartOffset;
            int    searchEnd   = FunctionToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            int nameIndex = InsertionHelpers.GetFunctionNameIndex(text, FunctionToChange, searchStart, searchEnd);

            // The last "word" between the start of the property and the name is the type.
            var substring = text.Substring(nameIndex, searchEnd - nameIndex);
            var name      = Regex.Split(substring, @"\s+").Where(s => !string.IsNullOrEmpty(s)).FirstOrDefault();

            if (string.IsNullOrEmpty(name))
            {
                log.ErrorFormat("Could not find name of function {0} to change.", FunctionToChange.Name);
                return(new ActionResult());
            }

            // Find the index of the existing type
            int nameLength = name.Length;

            // Replace the old type with the new one.
            sb.Replace(name, NewName, nameIndex, nameLength);
            FunctionToChange.Name = NewName;

            return(new ActionResult(searchStart + nameIndex, NewName.Length - nameLength, null));
        }
예제 #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 }));
        }
        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 }));
        }
        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 }));
        }
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            int    searchStart = FunctionToChange.TextRange.StartOffset;
            int    searchEnd   = FunctionToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            if (InsertAtStart)
            {
                sb.Insert(searchStart, NewModifier + " ");
                FunctionToChange.Modifiers.Insert(0, NewModifier);
                return(new ActionResult(searchStart, NewModifier.Length + 1, null));
            }

            int nameIndex = InsertionHelpers.GetFunctionNameIndex(text, FunctionToChange, searchStart, searchEnd);

            // Search Function TextRange for class keyword
            // The last "word" between the start of the function and the name is the return-type.
            var    substring = text.Substring(0, nameIndex);
            string typeName  = InsertionHelpers.GetLastWord(substring);

            if (typeName == null)
            {
                log.ErrorFormat("Could not find return-type of function {0} to change, so can't insert modifier before it.", FunctionToChange.Name);
                return(new ActionResult());
            }

            // Find the index of the existing type
            int typeIndex = substring.LastIndexOf(typeName);

            //Insert the new modifier just before the class keyword
            sb.Insert(typeIndex, NewModifier + " ");
            FunctionToChange.Modifiers.Add(NewModifier);

            return(new ActionResult(typeIndex, NewModifier.Length + 1, null));
        }
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            int    searchStart = FieldToChange.TextRange.StartOffset;
            int    searchEnd   = FieldToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            int nameIndex      = InsertionHelpers.GetFieldNameIndex(text, FieldToChange, searchStart, searchEnd);
            int endOfNameIndex = nameIndex + FieldToChange.Name.Length + 1;

            // Find the old initial value and remove it, if it exists
            int lengthRemoved = searchEnd - endOfNameIndex;

            sb.Remove(endOfNameIndex, lengthRemoved);
            string textToInsert = "";

            if (!string.IsNullOrWhiteSpace(NewInitialValue))
            {
                textToInsert = string.Format(" = {0};", NewInitialValue);
                sb.Insert(endOfNameIndex + 1, textToInsert);
            }
            // Add the new modifier only if it is not null
            if (string.IsNullOrEmpty(NewInitialValue))
            {
                FieldToChange.InitialValue = "";
                return(new ActionResult(searchStart, -lengthRemoved, null));
            }

            sb.Insert(searchStart, NewInitialValue + " ");
            FieldToChange.InitialValue = NewInitialValue;
            return(new ActionResult(searchStart, textToInsert.Length + 1 - lengthRemoved, null));
        }
        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 }));
        }
예제 #8
0
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            int    searchStart = ClassToChange.TextRange.StartOffset;
            int    searchEnd   = ClassToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            if (InsertAtStart)
            {
                sb.Insert(searchStart, NewModifier + " ");
                ClassToChange.Modifiers.Insert(0, NewModifier);
                return(new ActionResult(searchStart, NewModifier.Length + 1, null));
            }

            // Search Class TextRange for class keyword
            int classIndex = InsertionHelpers.GetClassKeywordIndex(text, searchStart, searchEnd);

            //Insert the new modifier just before the class keyword
            sb.Insert(classIndex, NewModifier + " ");
            ClassToChange.Modifiers.Add(NewModifier);

            return(new ActionResult(classIndex, NewModifier.Length + 1, null));
        }
예제 #9
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));
        }
        public override ActionResult ApplyActionTo(StringBuilder sb)
        {
            if (FieldToChange.DataType.ToString() == NewType.ToString())
            {
                return(new ActionResult());
            }

            // Search FieldToChange TextRange for name
            int    searchStart = FieldToChange.TextRange.StartOffset;
            int    searchEnd   = FieldToChange.TextRange.EndOffset;
            string text        = sb.ToString();

            int nameIndex = InsertionHelpers.GetFieldNameIndex(text, FieldToChange, searchStart, searchEnd);

            // The last "word" between the start of the field and the name is the type.
            var substring     = text.Substring(searchStart, nameIndex - searchStart);
            int dataTypeStart = substring.IndexOf(FieldToChange.DataType.ToString());
            var typeName      = substring.Substring(dataTypeStart).TrimEnd();       // FieldToChange.DataType.ToString();

            if (typeName == null)
            {
                log.ErrorFormat("Could not find type of field {0} to change.", FieldToChange.Name);
                return(new ActionResult());
            }

            // Find the index of the existing type
            int typeIndex  = substring.LastIndexOf(typeName);
            int typeLength = typeName.Length;

            // Replace the old type with the new one.
            var newTypeName = NewType.ToString();

            sb.Replace(typeName, newTypeName, searchStart + typeIndex, typeLength);
            FieldToChange.DataType = NewType;

            return(new ActionResult(searchStart + typeIndex, newTypeName.Length - typeLength, null));
        }