Exemple #1
0
        /// <summary>
        /// Put the selected lines of code inside a check step.
        /// This method does all modification of the text in the editor, with one string substitution
        /// </summary>
        /// <param name="document"></param>
        /// <param name="selection"></param>
        private void SubstituteStringFromSelection_RemoveCheckSteps(EnvDTE.TextDocument document)
        {
            EnvDTE.EditPoint earlierPoint = null, laterPoint = null;
            EditingTools.Instance.GetEditPointsForLinesToCheck(document, out earlierPoint, out laterPoint);
            string indentSpaces = EditingTools.Instance.GetIndentSpaces(document);
            string linesNoTabs  = earlierPoint.GetText(laterPoint).Replace("\t", indentSpaces);

            linesNoTabs = linesNoTabs.TrimEnd(new char[] { '\r', '\n' });

            string[] lines = linesNoTabs.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);


            Stack <int[]> checkStepOpenings = new Stack <int[]>();

            // use for loop instead of foreach because we might have to look with multiple lines
            for (int lineIndex = 0; lineIndex < lines.Length; lineIndex++)
            {
                // Iterate through lines, pushing opening line(s) for Check.Step on stack in string[]
                if (LinesMatchCheckStepOpening(new string[] { lines[lineIndex] }))
                {
                    checkStepOpenings.Push(new int[] { lineIndex });
                }
                else if ((lineIndex + 1 < lines.Length) && LinesMatchCheckStepOpening(new string[] { lines[lineIndex], lines[lineIndex + 1] }))
                {
                    checkStepOpenings.Push(new int[] { lineIndex, lineIndex + 1 });
                    lineIndex++;
                }
                else if (this.LineMatchesCheckStepClosing(lines[lineIndex]))
                {
                    // Remove check step
                    int[] openingLines = checkStepOpenings.Pop();
                    lines = RemoveCheckStepDefinition(openingLines, lineIndex, lines, indentSpaces);
                }
            }

            StringBuilder resultingText = new StringBuilder();

            foreach (string line in lines)
            {
                if (line != null)
                {
                    resultingText.Append(line);
                    resultingText.Append(Environment.NewLine);
                }
            }

            earlierPoint.ReplaceText(laterPoint, resultingText.ToString(), (int)EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
        }
Exemple #2
0
        /// <summary>
        /// Put the selected lines of code inside a check step.
        /// This method does all modification of the text in the editor, with one string substitution
        /// </summary>
        /// <param name="document"></param>
        /// <param name="selection"></param>
        private void SubstituteStringFromSelection_AddCheckStep(EnvDTE.TextDocument document)
        {
            EnvDTE.EditPoint earlierPoint = null, laterPoint = null;
            EditingTools.Instance.GetEditPointsForLinesToCheck(document, out earlierPoint, out laterPoint);
            string indentSpaces = EditingTools.Instance.GetIndentSpaces(document);
            string linesNoTabs  = earlierPoint.GetText(laterPoint).Replace("\t", indentSpaces);

            string[]      lines          = linesNoTabs.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
            StringBuilder resultingText  = new StringBuilder();
            bool          firstLine      = true;
            string        originalIndent = string.Empty;

            foreach (string line in lines)
            {
                if (!firstLine)
                {
                    resultingText.Append(Environment.NewLine);
                }

                firstLine = false;

                if (!String.IsNullOrWhiteSpace(line))
                {
                    string totalIndentSpaces   = "";
                    int    tabbedSpacesCounter = 0;

                    // Found how many tabbed spaces precede the line text
                    while ((line.Length >= ((tabbedSpacesCounter + 1) * document.TabSize)) && (line.Substring(tabbedSpacesCounter * document.TabSize, document.TabSize) == indentSpaces))
                    {
                        totalIndentSpaces += indentSpaces;
                        tabbedSpacesCounter++;
                    }

                    // find the minimum tab count, but skipping any zero-tab line
                    if (originalIndent.Length == 0)
                    {
                        originalIndent = totalIndentSpaces;
                    }
                    else
                    {
                        if (totalIndentSpaces.Length < originalIndent.Length)
                        {
                            originalIndent = totalIndentSpaces;
                        }
                    }

                    // Start the resulting line with a tab
                    resultingText.Append(indentSpaces);
                }

                // Add the rest of the text
                resultingText.Append(line);
            }

            StringBuilder stringToInsert = new StringBuilder();

            // strings that come before user-selected lines
            foreach (string beforeSelectedCode in StringResources.Instance.StringsBeforeSelectedCode)
            {
                stringToInsert.Append(originalIndent);
                stringToInsert.Append(beforeSelectedCode);
                stringToInsert.Append(Environment.NewLine);
            }

            stringToInsert.Append(resultingText);

            // strings that come after user-selected lines
            foreach (string afterSelectedCode in StringResources.Instance.StringsAfterSelectedCode)
            {
                stringToInsert.Append(originalIndent);
                stringToInsert.Append(afterSelectedCode);
                stringToInsert.Append(Environment.NewLine);
            }

            // Do replacement in Visual Studio text buffer
            earlierPoint.ReplaceText(laterPoint, stringToInsert.ToString(), (int)EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
        }