/// <summary>Calculates a line break count</summary> /// <param name="sourceCode">Source code</param> /// <param name="lineBreakCount">Number of line breaks</param> /// <param name="charRemainderCount">Number of characters left</param> public static void CalculateLineBreakCount( string sourceCode, out int lineBreakCount, out int charRemainderCount) { SourceCodeNavigator.CalculateLineBreakCount(sourceCode, 0, out lineBreakCount, out charRemainderCount); }
/// <summary>Finds a next line break</summary> /// <param name="sourceCode">Source code</param> /// <param name="startPosition">Position in the input string that defines the leftmost /// position to be searched</param> /// <param name="lineBreakPosition">Position of line break</param> /// <param name="lineBreakLength">Length of line break</param> private static void FindNextLineBreak( string sourceCode, int startPosition, out int lineBreakPosition, out int lineBreakLength) { int length = sourceCode.Length - startPosition; SourceCodeNavigator.FindNextLineBreak(sourceCode, startPosition, length, out lineBreakPosition, out lineBreakLength); }
/// <summary>Gets a source fragment</summary> /// <param name="sourceCode">Source code</param> /// <param name="nodePosition">Current node position</param> /// <param name="tabSize">Number of spaces in the tab</param> /// <param name="maxFragmentLength">Maximum length of the source fragment</param> /// <returns>Source fragment</returns> public static string GetSourceFragment( string sourceCode, int nodePosition, byte tabSize = 4, int maxFragmentLength = 95) { SourceCodeNodeCoordinates nodeCoordinates = SourceCodeNavigator.CalculateNodeCoordinates(sourceCode, nodePosition); return(SourceCodeNavigator.GetSourceFragment(sourceCode, nodeCoordinates, tabSize)); }
/// <summary>Calculates a line break count</summary> /// <param name="sourceCode">Source code</param> /// <param name="fragmentStartPosition">Start position of fragment</param> /// <param name="lineBreakCount">Number of line breaks</param> /// <param name="charRemainderCount">Number of characters left</param> public static void CalculateLineBreakCount( string sourceCode, int fragmentStartPosition, out int lineBreakCount, out int charRemainderCount) { int fragmentLength = sourceCode.Length - fragmentStartPosition; SourceCodeNavigator.CalculateLineBreakCount(sourceCode, fragmentStartPosition, fragmentLength, out lineBreakCount, out charRemainderCount); }
/// <summary>Calculates a node coordinates</summary> /// <param name="sourceCode">Source code</param> /// <param name="nodePosition">Current node position</param> /// <returns>Node coordinates</returns> public static SourceCodeNodeCoordinates CalculateNodeCoordinates( string sourceCode, int nodePosition) { if (string.IsNullOrEmpty(sourceCode) || nodePosition >= sourceCode.Length) { return(SourceCodeNodeCoordinates.Empty); } int fragmentLength = nodePosition + 1; int lineBreakCount; int charRemainderCount; SourceCodeNavigator.CalculateLineBreakCount(sourceCode, 0, fragmentLength, out lineBreakCount, out charRemainderCount); return(new SourceCodeNodeCoordinates(lineBreakCount + 1, charRemainderCount + 1)); }
/// <summary>Calculates a line break count</summary> /// <param name="sourceCode">Source code</param> /// <param name="fragmentStartPosition">Start position of fragment</param> /// <param name="fragmentLength">Length of fragment</param> /// <param name="lineBreakCount">Number of line breaks</param> /// <param name="charRemainderCount">Number of characters left</param> public static void CalculateLineBreakCount( string sourceCode, int fragmentStartPosition, int fragmentLength, out int lineBreakCount, out int charRemainderCount) { int length1 = sourceCode.Length; lineBreakCount = 0; charRemainderCount = 0; if (string.IsNullOrWhiteSpace(sourceCode)) { return; } if (fragmentStartPosition < 0) { throw new ArgumentException("", nameof(fragmentStartPosition)); } if (fragmentLength > length1 - fragmentStartPosition) { throw new ArgumentException("", nameof(fragmentLength)); } int num = fragmentStartPosition + fragmentLength - 1; int lineBreakPosition = int.MinValue; int lineBreakLength = 0; int startPosition; do { startPosition = lineBreakPosition == int.MinValue ? fragmentStartPosition : lineBreakPosition + lineBreakLength; int length2 = num - startPosition + 1; SourceCodeNavigator.FindNextLineBreak(sourceCode, startPosition, length2, out lineBreakPosition, out lineBreakLength); if (lineBreakPosition != -1) { ++lineBreakCount; } }while (lineBreakPosition != -1 && lineBreakPosition <= num); if (lineBreakCount > 0) { charRemainderCount = num - startPosition + 1; } else { charRemainderCount = fragmentLength; } }
/// <summary>Gets a current line content</summary> /// <param name="sourceCode">Source code</param> /// <param name="currentPosition">Current position</param> /// <param name="startLinePosition">Start position of line</param> /// <param name="endLinePosition">End position of line</param> /// <returns>Line content</returns> public static string GetCurrentLine( string sourceCode, int currentPosition, out int startLinePosition, out int endLinePosition) { startLinePosition = -1; endLinePosition = -1; if (string.IsNullOrEmpty(sourceCode)) { return(string.Empty); } int length1 = sourceCode.Length; if (currentPosition >= length1) { throw new ArgumentException("", nameof(currentPosition)); } switch (sourceCode[currentPosition]) { case '\n': case '\r': return(string.Empty); default: int lineBreakPosition1; int lineBreakLength; SourceCodeNavigator.FindPreviousLineBreak(sourceCode, currentPosition, out lineBreakPosition1, out lineBreakLength); startLinePosition = lineBreakPosition1 == -1 ? 0 : lineBreakPosition1 + lineBreakLength; int lineBreakPosition2; SourceCodeNavigator.FindNextLineBreak(sourceCode, currentPosition, out lineBreakPosition2, out int _); endLinePosition = lineBreakPosition2 == -1 ? length1 - 1 : lineBreakPosition2 - 1; int length2 = endLinePosition - startLinePosition + 1; return(sourceCode.Substring(startLinePosition, length2)); } }
/// <summary>Calculates a absolute node coordinates</summary> /// <param name="baseNodeCoordinates">Base node coordinates</param> /// <param name="additionalContent">Additional content</param> /// <returns>Absolute node coordinates</returns> public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates( SourceCodeNodeCoordinates baseNodeCoordinates, string additionalContent) { int lineBreakCount = 0; int charRemainderCount = 0; if (!string.IsNullOrEmpty(additionalContent)) { SourceCodeNavigator.CalculateLineBreakCount(additionalContent, out lineBreakCount, out charRemainderCount); } int lineNumber1; int columnNumber1; if (!baseNodeCoordinates.IsEmpty) { int lineNumber2 = baseNodeCoordinates.LineNumber; int columnNumber2 = baseNodeCoordinates.ColumnNumber; if (lineBreakCount > 0) { lineNumber1 = lineNumber2 + lineBreakCount; columnNumber1 = charRemainderCount + 1; } else { lineNumber1 = lineNumber2; columnNumber1 = columnNumber2 + charRemainderCount; } } else { lineNumber1 = lineBreakCount + 1; columnNumber1 = charRemainderCount + 1; } return(new SourceCodeNodeCoordinates(lineNumber1, columnNumber1)); }
/// <summary>Gets a source fragment</summary> /// <param name="sourceCode">Source code</param> /// <param name="nodeCoordinates">Node coordinates</param> /// <param name="tabSize">Number of spaces in the tab</param> /// <param name="maxFragmentLength">Maximum length of the source fragment</param> /// <returns>Source fragment</returns> public static string GetSourceFragment( string sourceCode, SourceCodeNodeCoordinates nodeCoordinates, byte tabSize = 4, int maxFragmentLength = 95) { string empty = string.Empty; int lineNumber1 = nodeCoordinates.LineNumber; int columnNumber = nodeCoordinates.ColumnNumber; if (!string.IsNullOrEmpty(sourceCode)) { int lineNumber2 = lineNumber1 - 1; int lineNumber3 = lineNumber1; int lineNumber4 = lineNumber1 + 1; string line1 = string.Empty; string line2 = string.Empty; string line3 = string.Empty; int num1 = 0; int lineBreakPosition = int.MinValue; int lineBreakLength = 0; do { int num2 = lineBreakPosition == int.MinValue ? 0 : lineBreakPosition + lineBreakLength; SourceCodeNavigator.FindNextLineBreak(sourceCode, num2, out lineBreakPosition, out lineBreakLength); string str = lineBreakPosition == -1 ? sourceCode.Substring(num2) : sourceCode.Substring(num2, lineBreakPosition - num2); ++num1; if (num1 == lineNumber2) { line1 = str; } else if (num1 == lineNumber3) { line2 = str; } else if (num1 == lineNumber4) { line3 = str; } }while (lineBreakPosition != -1 && num1 <= lineNumber4); int length = lineNumber4.ToString((IFormatProvider)CultureInfo.InvariantCulture).Length; if (lineNumber3 == num1) { length = lineNumber3.ToString((IFormatProvider)CultureInfo.InvariantCulture).Length; } int fragmentStartPosition; int fragmentLength; SourceCodeNavigator.CalculateCutPositions(line2, columnNumber, maxFragmentLength, out fragmentStartPosition, out fragmentLength); StringBuilderPool shared = StringBuilderPool.Shared; StringBuilder builder = shared.Rent(); if (line2.Length > 0) { if (line1.Length > 0) { builder.AppendLine(SourceCodeNavigator.FormatSourceCodeLine(line1, new SourceCodeNodeCoordinates(lineNumber2, 0), length, fragmentStartPosition, fragmentLength, tabSize)); } builder.AppendLine(SourceCodeNavigator.FormatSourceCodeLine(line2, new SourceCodeNodeCoordinates(lineNumber3, columnNumber), length, fragmentStartPosition, fragmentLength, tabSize)); if (line3.Length > 0) { builder.AppendLine(SourceCodeNavigator.FormatSourceCodeLine(line3, new SourceCodeNodeCoordinates(lineNumber4, 0), length, fragmentStartPosition, fragmentLength, tabSize)); } } empty = builder.ToString(); shared.Return(builder); } return(empty); }