static SnapshotSpan AsSnapshotSpan(CommentRegion region, ITextSnapshot snapshot) { var startLine = snapshot.GetLineFromLineNumber(region.StartLine); var endLine = snapshot.GetLineFromLineNumber(region.EndLine); return(new SnapshotSpan(startLine.Start + region.StartOffset, endLine.End + region.EndOffset)); }
static IReadOnlyCollection <CommentRegion> GetComments(ITextSnapshot snapshot) { var results = new List <CommentRegion>(); CommentRegion current = null; foreach (var line in snapshot.Lines) { var text = line.GetText(); for (int i = 0; i < text.Length; i++) { char c = text[i]; if (c == '/') { // already in comment, this won't break us out until we get a * if (current != null) { continue; } // eager scan ahead by 1+ character (if not a match, won't matter anyway if ((i + 1) < text.Length) { char n = text[++i]; // double forward slash means we can stop attempting to parse if (n == '/') { break; } if (n == '*') { current = new CommentRegion { StartLine = line.LineNumber, StartOffset = i - 1 // offset the ++ }; } } } else if (c == '*') { // not in a comment, this won't matter if (current == null) { continue; } if ((i + 1) < text.Length) { char n = text[++i]; if (n == '/') { // capture ending information, add to results and reset current.EndLine = line.LineNumber; current.EndOffset = i - 1; // offset the ++ results.Add(current); current = null; } } } } } if (current != null) { var lastLine = snapshot.Lines.Last(); current.EndLine = lastLine.LineNumber; current.EndOffset = lastLine.End.Position; results.Add(current); current = null; } return(results); }
private static SnapshotSpan AsSnapshotSpan(CommentRegion region, ITextSnapshot snapshot) { return(new SnapshotSpan(snapshot, region.Start, region.Length)); }