Example #1
0
        // Finds first contiguous line that has the same prefix as the current line.
        // If the prefix is not a comment start prefix, then this function stops at
        // the first line with triple quotes or that is empty.  It returns the point
        // positioned at the start of the first line.
        private SnapshotPoint FindParagraphStart(SnapshotPoint point, FillPrefix prefix)
        {
            var buf = point.Snapshot.TextBuffer;

            Regex regexp = null;

            if (prefix.IsDocString)
            {
                regexp = new Regex(prefix + "('''|\"\"\")");
                // Check for edge case of being on first line of docstring with quotes.
                if (regexp.Match(point.GetContainingLine().GetText()).Success)
                {
                    return(point.GetContainingLine().Start);
                }
            }

            var line   = point.GetContainingLine();
            var ln_num = line.LineNumber;

            var prev_num = ln_num - 1;

            if (prev_num < 0)
            {
                return(line.Start);
            }
            var prev_txt = point.Snapshot.GetLineFromLineNumber(prev_num).GetText();

            while (PrevNotParaStart(prefix, prev_num, ln_num, prev_txt, regexp))
            {
                ln_num = prev_num;
                prev_num--;
                if (prev_num < 0)
                {
                    return(new SnapshotPoint(point.Snapshot, 0));
                }
                prev_txt = point.Snapshot.GetLineFromLineNumber(prev_num).GetText();
            }

            SnapshotPoint res;

            if (!prefix.IsDocString ||
                string.IsNullOrEmpty(prev_txt) ||
                _startDocStringRegex.IsMatch(prev_txt))
            {
                // Normally ln is the start for filling, prev line stopped the loop.
                res = point.Snapshot.GetLineFromLineNumber(ln_num).Start;
            }
            else
            {
                // If we're in a doc string, and prev is not just the triple quotes
                // line, and prev is not empty, then we use prev line because it
                // has text on it we need to fill.  Point is already on prev line.
                res = point.Snapshot.GetLineFromLineNumber(prev_num).Start;
            }
            return(res);
        }
Example #2
0
        // Finds last contiguous line that has the same prefix as the current line. If
        // the prefix is not a comment start prefix, then this function stops at the
        // last line with triple quotes or that is empty.  It returns the point
        // positioned at the start of the last line.
        private SnapshotPoint FindParagraphEnd(SnapshotPoint point, FillPrefix prefix)
        {
            Regex regexp = null;

            if (prefix.IsDocString)
            {
                regexp = _endDocStringRegex;
                // Check for edge case of being on last line of doc string with quotes.
                if (regexp.Match(point.GetContainingLine().GetText()).Success)
                {
                    return(point.GetContainingLine().Start);
                }
            }

            var line   = point.GetContainingLine();
            var ln_num = line.LineNumber;

            var next_num = ln_num + 1;

            if (next_num >= point.Snapshot.LineCount)
            {
                return(line.End);
            }

            var next_txt = point.Snapshot.GetLineFromLineNumber(next_num).GetText();

            while (NextNotParaEnd(prefix, next_num, ln_num, next_txt, regexp))
            {
                ln_num = next_num;
                next_num++;
                if (next_num == point.Snapshot.LineCount)
                {
                    break;
                }
                next_txt = point.Snapshot.GetLineFromLineNumber(next_num).GetText();
            }

            SnapshotPoint res;

            if (!prefix.IsDocString ||
                string.IsNullOrEmpty(next_txt) ||
                _startDocStringRegex.IsMatch(next_txt))
            {
                // Normally ln is the last line to fill, next line stopped the loop.
                res = point.Snapshot.GetLineFromLineNumber(ln_num).End;
            }
            else
            {
                // If we're in a doc string, and next is not just the triple quotes
                // line, and next is not empty, then we use next line because it has
                // text on it we need to fill.  Point is on next line.
                res = point.Snapshot.GetLineFromLineNumber(next_num).End;
            }
            return(res);
        }
Example #3
0
        private bool NextNotParaEnd(FillPrefix prefix, int next_num, int ln_num, string next_txt, Regex regexp)
        {
            var notBufLastLn = next_num != ln_num;
            var notFileDocStrAndHasPrefix       = !string.IsNullOrEmpty(prefix.Prefix) && next_txt.StartsWith(prefix.Prefix);
            var isFileDocStrAndNotEmptyLine     = string.IsNullOrEmpty(prefix.Prefix) && string.IsNullOrEmpty(next_txt);
            var notDocStringOrNoTripleQuotesYet = !(prefix.IsDocString && regexp.Match(next_txt).Success);

            return(notBufLastLn &&
                   (notFileDocStrAndHasPrefix || isFileDocStrAndNotEmptyLine) &&
                   notDocStringOrNoTripleQuotesYet);
        }
Example #4
0
        private bool NextNotParaEnd(FillPrefix prefix, int next_num, int ln_num, string next_txt, Regex regexp)
        {
            var notBufLastLn = next_num != ln_num;
            var notFileDocStrAndHasPrefix       = prefix.Prefix != "" && next_txt.IndexOf(prefix.Prefix) == 0;
            var isFileDocStrAndNotEmptyLine     = prefix.Prefix == "" && next_txt != "";
            var notDocStringOrNoTripleQuotesYet = !(prefix.IsDocString && regexp.Match(next_txt).Success);

            return(notBufLastLn &&
                   (notFileDocStrAndHasPrefix || isFileDocStrAndNotEmptyLine) &&
                   notDocStringOrNoTripleQuotesYet);
        }
Example #5
0
        private bool PrevNotParaStart(FillPrefix prefix, int prev_num, int ln_num, string prev_txt, Regex regexp)
        {
            var notBufFirstLn                   = prev_num != ln_num;
            var notFileDocStrAndHasPrefix       = !string.IsNullOrEmpty(prefix.Prefix) && prev_txt.StartsWith(prefix.Prefix);
            var isFileDocStrAndNotEmptyLine     = string.IsNullOrEmpty(prefix.Prefix) && !string.IsNullOrEmpty(prev_txt);
            var notDocStringOrNoTripleQuotesYet = !(prefix.IsDocString && regexp.Match(prev_txt).Success);

            return(notBufFirstLn &&
                   (notFileDocStrAndHasPrefix || isFileDocStrAndNotEmptyLine) &&
                   notDocStringOrNoTripleQuotesYet);
        }
Example #6
0
 private bool PrevNotParaStart(FillPrefix prefix, int prev_num, int ln_num, string prev_txt, Regex regexp)
 {
     var notBufFirstLn = prev_num != ln_num;
     var notFileDocStrAndHasPrefix = prefix.Prefix != "" && prev_txt.IndexOf(prefix.Prefix) == 0;
     var isFileDocStrAndNotEmptyLine = prefix.Prefix == "" && prev_txt != "";
     var notDocStringOrNoTripleQuotesYet = !(prefix.IsDocString && regexp.Match(prev_txt).Success);
     return (notBufFirstLn &&
             (notFileDocStrAndHasPrefix || isFileDocStrAndNotEmptyLine) &&
             notDocStringOrNoTripleQuotesYet);
 }
Example #7
0
        // Finds first contiguous line that has the same prefix as the current line.
        // If the prefix is not a comment start prefix, then this function stops at
        // the first line with triple quotes or that is empty.  It returns the point
        // positioned at the start of the first line.
        private SnapshotPoint FindParagraphStart(SnapshotPoint point, FillPrefix prefix)
        {
            var buf = point.Snapshot.TextBuffer;

            Regex regexp = null;
            if (prefix.IsDocString) {
                regexp = new Regex(prefix + "('''|\"\"\")");
                // Check for edge case of being on first line of docstring with quotes.
                if (regexp.Match(point.GetContainingLine().GetText()).Success) {
                    return point.GetContainingLine().Start;
                }
            }

            var line = point.GetContainingLine();
            var ln_num = line.LineNumber;

            var prev_num = ln_num - 1;
            if (prev_num < 0) {
                return line.Start;
            }
            var prev_txt = point.Snapshot.GetLineFromLineNumber(prev_num).GetText();

            while (PrevNotParaStart(prefix, prev_num, ln_num, prev_txt, regexp)) {
                ln_num = prev_num;
                prev_num--;
                if (prev_num < 0) {
                    return new SnapshotPoint(point.Snapshot, 0);
                }
                prev_txt = point.Snapshot.GetLineFromLineNumber(prev_num).GetText();
            }

            SnapshotPoint res;
            if (!prefix.IsDocString ||
                _startDocStringRegex.Match(prev_txt).Success ||
                prev_txt == "") {
                // Normally ln is the start for filling, prev line stopped the loop.
                res = point.Snapshot.GetLineFromLineNumber(ln_num).Start;
            } else {
                // If we're in a doc string, and prev is not just the triple quotes
                // line, and prev is not empty, then we use prev line because it
                // has text on it we need to fill.  Point is already on prev line.
                res = point.Snapshot.GetLineFromLineNumber(prev_num).Start;
            }
            return res;
        }
Example #8
0
        // Finds last contiguous line that has the same prefix as the current line. If
        // the prefix is not a comment start prefix, then this function stops at the
        // last line with triple quotes or that is empty.  It returns the point
        // positioned at the start of the last line.
        private SnapshotPoint FindParagraphEnd(SnapshotPoint point, FillPrefix prefix)
        {
            Regex regexp = null;
            if (prefix.IsDocString) {
                regexp = _endDocStringRegex;
                // Check for edge case of being on last line of doc string with quotes.
                if (regexp.Match(point.GetContainingLine().GetText()).Success) {
                    return point.GetContainingLine().Start;
                }
            }

            var line = point.GetContainingLine();
            var ln_num = line.LineNumber;

            var next_num = ln_num + 1;
            if (next_num >= point.Snapshot.LineCount) {
                return line.End;
            }

            var next_txt = point.Snapshot.GetLineFromLineNumber(next_num).GetText();

            while (NextNotParaEnd(prefix, next_num, ln_num, next_txt, regexp)) {
                ln_num = next_num;
                next_num++;
                next_txt = point.Snapshot.GetLineFromLineNumber(next_num).GetText();
            }

            SnapshotPoint res;
            if (!prefix.IsDocString ||
                _startDocStringRegex.Match(next_txt).Success ||
                next_txt == "") {
                // Normally ln is the last line to fill, next line stopped the loop.
                res = point.Snapshot.GetLineFromLineNumber(ln_num).End;
            } else {
                // If we're in a doc string, and next is not just the triple quotes
                // line, and next is not empty, then we use next line because it has
                // text on it we need to fill.  Point is on next line.
                res = point.Snapshot.GetLineFromLineNumber(next_num).End;
            }
            return res;
        }
Example #9
0
 private bool NextNotParaEnd(FillPrefix prefix, int next_num, int ln_num, string next_txt, Regex regexp) {
     var notBufLastLn = next_num != ln_num;
     var notFileDocStrAndHasPrefix = !string.IsNullOrEmpty(prefix.Prefix) && next_txt.StartsWith(prefix.Prefix);
     var isFileDocStrAndNotEmptyLine = string.IsNullOrEmpty(prefix.Prefix) && string.IsNullOrEmpty(next_txt);
     var notDocStringOrNoTripleQuotesYet = !(prefix.IsDocString && regexp.Match(next_txt).Success);
     return (notBufLastLn &&
             (notFileDocStrAndHasPrefix || isFileDocStrAndNotEmptyLine) &&
             notDocStringOrNoTripleQuotesYet);
 }
Example #10
0
 private bool PrevNotParaStart(FillPrefix prefix, int prev_num, int ln_num, string prev_txt, Regex regexp) {
     var notBufFirstLn = prev_num != ln_num;
     var notFileDocStrAndHasPrefix = !string.IsNullOrEmpty(prefix.Prefix) && prev_txt.StartsWith(prefix.Prefix);
     var isFileDocStrAndNotEmptyLine = string.IsNullOrEmpty(prefix.Prefix) && !string.IsNullOrEmpty(prev_txt);
     var notDocStringOrNoTripleQuotesYet = !(prefix.IsDocString && regexp.Match(prev_txt).Success);
     return (notBufFirstLn &&
             (notFileDocStrAndHasPrefix || isFileDocStrAndNotEmptyLine) &&
             notDocStringOrNoTripleQuotesYet);
 }