public static Position CalculatePosition(SourceFile source, int pos) { if (pos >= source.Content.Length) { throw new InvalidOperationException ( "The character position specified is not inside the provided content."); } var result = new Position () { FileName = source.FileName, Line = 1, Column = 1 }; for (var i = 0; i <= pos; i++) { var ch = source.Content [i]; if (ch == '\n') { result.Line ++; result.Column = 1; } if (ch != '\n' && ch != '\r') { result.Column ++; } } result.Column --; return result; }
internal static List<string> ExtractRelevantSource( string[] sourceLines, Position startPos, Position endPos, string linePrefix = "") { var fileName = startPos.FileName; var startLine = startPos.Line; var endLine = endPos.Line; var addEllipsis = endLine > startLine; var firstLine = sourceLines[startLine - 1]; List<string> result = new List<string>(); StringBuilder sb1 = new StringBuilder(); sb1.AppendFormat("{0}file '{1}' line {2}: {3}", linePrefix, fileName, startLine, firstLine); if (addEllipsis) { sb1.Append(" [...content snipped...]"); } result.Add(sb1.ToString()); var underline = Utils.Underline( Math.Max(startPos.Column, Utils.FirstNonBlank(firstLine)), Math.Min(firstLine.Length, addEllipsis ? firstLine.Length : endPos.Column) ); var underlinedLine = String.Format( "{0}file '{1}' line {2}: {3}", linePrefix, fileName, startLine, underline); result.Add(underlinedLine); return result; }