예제 #1
0
        internal static Match LineMatchesRegex(EditPoint point, Regex regex)
        {
            var line  = point.GetLine();
            var match = regex.Match(line);

            return(match);
        }
예제 #2
0
        /// <summary>
        /// Expand a textpoint to the full comment, in the direction specified by the <paramref name="foundAction"/>.
        /// </summary>
        /// <param name="point">The initial starting point for the expansion.</param>
        /// <param name="foundAction">An action which advances the search either up or down.</param>
        /// <returns>
        /// The endpoint of the comment, or <c>null</c> if the expansion did not find a valid comment.
        /// </returns>
        private EditPoint Expand(TextPoint point, Action <EditPoint> foundAction)
        {
            EditPoint current = point.CreateEditPoint();
            EditPoint result  = null;
            string    prefix  = null;

            do
            {
                var line = current.Line;
                var text = current.GetLine();

                var match = _commentLineRegex.Match(text);
                if (match.Success)
                {
                    // Cancel the expansion if the prefix does not match. This takes priority over
                    // the initial spacer check to allow formatting comments adjacent to Stylecop
                    // SA1626 style commented code.
                    var currentPrefix = match.Groups["prefix"].Value.TrimStart();
                    if (prefix != null && !string.Equals(prefix, currentPrefix))
                    {
                        break;
                    }
                    else
                    {
                        prefix = currentPrefix;
                    }

                    // The initial spacer is required, otherwise we assume this is commented out
                    // code and do not format.
                    if (match.Groups["initialspacer"].Success)
                    {
                        result = current.CreateEditPoint();
                        foundAction(current);

                        // If result and iterator line are the same, the found action (move line up or
                        // down) did nothing. This means we're at the start or end of the file, and
                        // there is no point to keep searching, it would create an infinite loop.
                        if (result.Line == current.Line)
                        {
                            break;
                        }
                    }
                    else
                    {
                        // Did not succesfully match the intial spacer, we have to assume this is
                        // code and cancel all formatting.
                        result  = null;
                        current = null;
                    }
                }
                else
                {
                    current = null;
                }
            } while (current != null);

            return(result);
        }
예제 #3
0
        private EditPoint Expand(TextPoint point, Action <EditPoint> foundAction)
        {
            EditPoint current = point.CreateEditPoint();
            EditPoint result  = null;
            string    prefix  = null;

            do
            {
                var line = current.Line;
                var text = current.GetLine();

                var match = _commentLineRegex.Match(text);
                if (match.Success)
                {
                    // The initial spacer is required, otherwise we assume this is commented out
                    // code and do not format.
                    if (match.Groups["initialspacer"].Success)
                    {
                        // Get the comment prefix for the current line.
                        var currentPrefix = match.Groups["prefix"].Value.TrimStart();
                        if (prefix == null)
                        {
                            prefix = currentPrefix;
                        }

                        // Cancel the expanding if the prefix does not match.
                        if (prefix != currentPrefix)
                        {
                            break;
                        }

                        result = current.CreateEditPoint();
                        foundAction(current);

                        // If result and iterator line are the same, the found action (move line up or
                        // down) did nothing. This means we're at the start or end of the file, and
                        // there is no point to keep searching, it would create an infinite loop.
                        if (result.Line == current.Line)
                        {
                            break;
                        }
                    }
                    else
                    {
                        // This is code! Set to null to abandon loop.
                        result  = null;
                        current = null;
                    }
                }
                else
                {
                    current = null;
                }
            } while (current != null);

            return(result);
        }
예제 #4
0
        /// <summary>
        /// Inserts a blank line after the specified point except where adjacent to a brace.
        /// </summary>
        /// <param name="point">The point.</param>
        internal static void InsertBlankLineAfterPoint(EditPoint point)
        {
            if (point.AtEndOfDocument)
            {
                return;
            }

            point.LineDown(1);
            point.StartOfLine();

            string text = point.GetLine();

            if (RegexNullSafe.IsMatch(text, @"^\s*[^\s\}]"))
            {
                point.Insert(Environment.NewLine);
            }
        }
예제 #5
0
        /// <summary>
        /// Inserts a blank line before the specified point except where adjacent to a brace.
        /// </summary>
        /// <param name="point">The point.</param>
        internal static void InsertBlankLineBeforePoint(EditPoint point)
        {
            if (point.Line <= 1)
            {
                return;
            }

            point.LineUp(1);
            point.StartOfLine();

            string text = point.GetLine();

            if (RegexNullSafe.IsMatch(text, @"^\s*[^\s\{]")) // If it is not a scope boundary, insert newline.
            {
                point.EndOfLine();
                point.Insert(Environment.NewLine);
            }
        }
예제 #6
0
        private EditPoint Expand(TextPoint point, Action <EditPoint> foundAction)
        {
            EditPoint i      = point.CreateEditPoint();
            EditPoint result = null;

            do
            {
                var line = i.Line;
                var text = i.GetLine();

                if (CodeCommentHelper.LineMatchesRegex(i, _commentLineRegex).Success)
                {
                    result = i.CreateEditPoint();
                    foundAction(i);

                    // If result and iterator line are the same, the found action (move line up or
                    // down) did nothing. This means there is no point to keep searching, it would
                    // create an infinite loop.
                    if (result.Line == i.Line)
                    {
                        break;
                    }
                }
                else
                {
                    if (i != null && result != null && CodeCommentHelper.LineMatchesRegex(i, _codeLineRegex).Success)
                    {
                        result = null;
                    }

                    i = null;
                }
            } while (i != null);

            return(result);
        }
예제 #7
0
 internal static Match LineMatchesRegex(EditPoint point, Regex regex)
 {
     var line = point.GetLine();
     var match = regex.Match(line);
     return match;
 }
예제 #8
0
        /// <summary>
        /// Inserts a blank line after the specified point except where adjacent to a brace.
        /// </summary>
        /// <param name="point">The point.</param>
        internal static void InsertBlankLineAfterPoint(EditPoint point)
        {
            if (point.AtEndOfDocument) return;

            point.LineDown(1);
            point.StartOfLine();

            string text = point.GetLine();
            if (Regex.IsMatch(text, @"^\s*[^\s\}]"))
            {
                point.Insert(Environment.NewLine);
            }
        }
예제 #9
0
        /// <summary>
        /// Inserts a blank line before the specified point except where adjacent to a brace.
        /// </summary>
        /// <param name="point">The point.</param>
        internal static void InsertBlankLineBeforePoint(EditPoint point)
        {
            if (point.Line <= 1) return;

            point.LineUp(1);
            point.StartOfLine();

            string text = point.GetLine();
            if (Regex.IsMatch(text, @"^\s*[^\s\{]")) // If it is not a scope boundary, insert newline.
            {
                point.EndOfLine();
                point.Insert(Environment.NewLine);
            }
        }
예제 #10
0
 internal static Match LineMatchesRegex(EditPoint point, Regex regex)
 {
     return regex.Match(point.GetLine());
 }
예제 #11
0
 internal static Match LineMatchesRegex(EditPoint point, Regex regex)
 {
     return(regex.Match(point.GetLine()));
 }