Beispiel #1
0
        /// <summary>Calculates a absolute node coordinates</summary>
        /// <param name="baseNodeCoordinates">Base node coordinates</param>
        /// <param name="lineBreakCount">Number of line breaks</param>
        /// <param name="charRemainderCount">Number of characters left</param>
        /// <returns>Absolute node coordinates</returns>
        public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates(
            SourceCodeNodeCoordinates baseNodeCoordinates,
            int lineBreakCount,
            int 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="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 = DEFAULT_TAB_SIZE, int maxFragmentLength = DEFAULT_MAX_FRAGMENT_LENGTH)
        {
            SourceCodeNodeCoordinates nodeCoordinates = CalculateNodeCoordinates(sourceCode, nodePosition);

            return(GetSourceFragment(sourceCode, nodeCoordinates, tabSize));
        }
        /// <summary>
        /// Calculates a absolute node coordinates
        /// </summary>
        /// <param name="baseNodeCoordinates">Base node coordinates</param>
        /// <param name="lineBreakCount">Number of line breaks</param>
        /// <param name="charRemainderCount">Number of characters left</param>
        /// <returns>Absolute node coordinates</returns>
        public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates(
            SourceCodeNodeCoordinates baseNodeCoordinates, int lineBreakCount, int charRemainderCount)
        {
            int absoluteLineNumber;
            int absoluteColumnNumber;

            if (!baseNodeCoordinates.IsEmpty)
            {
                int baseLineNumber   = baseNodeCoordinates.LineNumber;
                int baseColumnNumber = baseNodeCoordinates.ColumnNumber;

                if (lineBreakCount > 0)
                {
                    absoluteLineNumber   = baseLineNumber + lineBreakCount;
                    absoluteColumnNumber = charRemainderCount + 1;
                }
                else
                {
                    absoluteLineNumber   = baseLineNumber;
                    absoluteColumnNumber = baseColumnNumber + charRemainderCount;
                }
            }
            else
            {
                absoluteLineNumber   = lineBreakCount + 1;
                absoluteColumnNumber = charRemainderCount + 1;
            }

            var absoluteNodeCoordinates = new SourceCodeNodeCoordinates(absoluteLineNumber, absoluteColumnNumber);

            return(absoluteNodeCoordinates);
        }
Beispiel #4
0
        /// <summary>Formats a line of source code</summary>
        /// <param name="line">Line content</param>
        /// <param name="nodeCoordinates">Node coordinates</param>
        /// <param name="lineNumberSize">Number of symbols in the line number caption</param>
        /// <param name="fragmentStartPosition">Start position of source fragment</param>
        /// <param name="fragmentLength">Length of source fragment</param>
        /// <param name="tabSize">Number of spaces in the tab</param>
        /// <returns>Formatted line</returns>
        private static string FormatSourceCodeLine(
            string line,
            SourceCodeNodeCoordinates nodeCoordinates,
            int lineNumberSize,
            int fragmentStartPosition = 0,
            int fragmentLength        = 0,
            byte tabSize = 4)
        {
            int    lineNumber   = nodeCoordinates.LineNumber;
            int    columnNumber = nodeCoordinates.ColumnNumber;
            int    length       = line.Length;
            string source;

            if (fragmentStartPosition == 0 && fragmentLength == length)
            {
                source = line;
            }
            else if (fragmentStartPosition >= length)
            {
                source = string.Empty;
            }
            else
            {
                int  num   = fragmentStartPosition + fragmentLength - 1;
                bool flag1 = fragmentStartPosition > 0;
                bool flag2 = num <= length;
                if (num + 1 == length)
                {
                    flag2 = false;
                }
                if (num >= length)
                {
                    flag2          = false;
                    fragmentLength = length - 1 - fragmentStartPosition + 1;
                }
                source = line.Substring(fragmentStartPosition, fragmentLength);
                if (flag1)
                {
                    source = "…" + source;
                }
                if (flag2)
                {
                    source += "…";
                }
            }
            string str = string.Format("Line {0}: {1}", (object)lineNumber.ToString((IFormatProvider)CultureInfo.InvariantCulture).PadLeft(lineNumberSize), (object)source.TabsToSpaces((int)tabSize));

            if (columnNumber > 0)
            {
                int num = columnNumber - fragmentStartPosition;
                if (fragmentStartPosition > 0)
                {
                    ++num;
                }
                str = str + Environment.NewLine + string.Empty.PadRight((num < source.Length ? source.Substring(0, num - 1) : source).TabsToSpaces((int)tabSize).Length + lineNumberSize + 7).Replace(" ", "-") + "^";
            }
            return(str);
        }
Beispiel #5
0
        /// <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 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;

            CalculateLineBreakCount(sourceCode, 0, fragmentLength,
                                    out lineBreakCount, out charRemainderCount);

            var nodeCoordinates = new SourceCodeNodeCoordinates(lineBreakCount + 1, charRemainderCount + 1);

            return(nodeCoordinates);
        }
        /// <summary>
        /// Calculates a absolute node coordinates
        /// </summary>
        /// <param name="baseNodeCoordinates">Base node coordinates</param>
        /// <param name="relativeNodeCoordinates">Relative node coordinates</param>
        /// <returns>Absolute node coordinates</returns>
        public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates(
            SourceCodeNodeCoordinates baseNodeCoordinates, SourceCodeNodeCoordinates relativeNodeCoordinates)
        {
            int relativeLineNumber   = relativeNodeCoordinates.LineNumber;
            int relativeColumnNumber = relativeNodeCoordinates.ColumnNumber;

            int absoluteLineNumber;
            int absoluteColumnNumber;

            if (!baseNodeCoordinates.IsEmpty)
            {
                int baseLineNumber   = baseNodeCoordinates.LineNumber;
                int baseColumnNumber = baseNodeCoordinates.ColumnNumber;

                absoluteLineNumber   = baseLineNumber;
                absoluteColumnNumber = baseColumnNumber;

                if (relativeLineNumber > 0)
                {
                    if (relativeLineNumber == 1)
                    {
                        if (relativeColumnNumber > 0)
                        {
                            absoluteColumnNumber = baseColumnNumber + relativeColumnNumber - 1;
                        }
                    }
                    else
                    {
                        absoluteLineNumber   = baseLineNumber + relativeLineNumber - 1;
                        absoluteColumnNumber = relativeColumnNumber;
                    }
                }
            }
            else
            {
                absoluteLineNumber   = relativeLineNumber;
                absoluteColumnNumber = relativeColumnNumber;
            }

            var absoluteNodeCoordinates = new SourceCodeNodeCoordinates(absoluteLineNumber, absoluteColumnNumber);

            return(absoluteNodeCoordinates);
        }
        /// <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))
            {
                CalculateLineBreakCount(additionalContent, out lineBreakCount, out charRemainderCount);
            }

            int absoluteLineNumber;
            int absoluteColumnNumber;

            if (!baseNodeCoordinates.IsEmpty)
            {
                int baseLineNumber   = baseNodeCoordinates.LineNumber;
                int baseColumnNumber = baseNodeCoordinates.ColumnNumber;

                if (lineBreakCount > 0)
                {
                    absoluteLineNumber   = baseLineNumber + lineBreakCount;
                    absoluteColumnNumber = charRemainderCount + 1;
                }
                else
                {
                    absoluteLineNumber   = baseLineNumber;
                    absoluteColumnNumber = baseColumnNumber + charRemainderCount;
                }
            }
            else
            {
                absoluteLineNumber   = lineBreakCount + 1;
                absoluteColumnNumber = charRemainderCount + 1;
            }

            var absoluteNodeCoordinates = new SourceCodeNodeCoordinates(absoluteLineNumber, absoluteColumnNumber);

            return(absoluteNodeCoordinates);
        }
Beispiel #9
0
        /// <summary>Calculates a absolute node coordinates</summary>
        /// <param name="baseNodeCoordinates">Base node coordinates</param>
        /// <param name="relativeNodeCoordinates">Relative node coordinates</param>
        /// <returns>Absolute node coordinates</returns>
        public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates(
            SourceCodeNodeCoordinates baseNodeCoordinates,
            SourceCodeNodeCoordinates relativeNodeCoordinates)
        {
            int lineNumber1   = relativeNodeCoordinates.LineNumber;
            int columnNumber1 = relativeNodeCoordinates.ColumnNumber;
            int lineNumber2;
            int columnNumber2;

            if (!baseNodeCoordinates.IsEmpty)
            {
                int lineNumber3   = baseNodeCoordinates.LineNumber;
                int columnNumber3 = baseNodeCoordinates.ColumnNumber;
                lineNumber2   = lineNumber3;
                columnNumber2 = columnNumber3;
                if (lineNumber1 > 0)
                {
                    if (lineNumber1 == 1)
                    {
                        if (columnNumber1 > 0)
                        {
                            columnNumber2 = columnNumber3 + columnNumber1 - 1;
                        }
                    }
                    else
                    {
                        lineNumber2   = lineNumber3 + lineNumber1 - 1;
                        columnNumber2 = columnNumber1;
                    }
                }
            }
            else
            {
                lineNumber2   = lineNumber1;
                columnNumber2 = columnNumber1;
            }
            return(new SourceCodeNodeCoordinates(lineNumber2, columnNumber2));
        }
Beispiel #10
0
        /// <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>
		/// 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;

			CalculateLineBreakCount(sourceCode, 0, fragmentLength,
				out lineBreakCount, out charRemainderCount);

			var nodeCoordinates = new SourceCodeNodeCoordinates(lineBreakCount + 1, charRemainderCount + 1);

			return nodeCoordinates;
		}
		/// <summary>
		/// Formats a line of source code
		/// </summary>
		/// <param name="line">Line content</param>
		/// <param name="nodeCoordinates">Node coordinates</param>
		/// <param name="lineNumberSize">Number of symbols in the line number caption</param>
		/// <param name="fragmentStartPosition">Start position of source fragment</param>
		/// <param name="fragmentLength">Length of source fragment</param>
		/// <param name="tabSize">Number of spaces in the tab</param>
		/// <returns>Formatted line</returns>
		private static string FormatSourceCodeLine(string line, SourceCodeNodeCoordinates nodeCoordinates,
			int lineNumberSize, int fragmentStartPosition = 0, int fragmentLength = 0, byte tabSize = 4)
		{
			const string ellipsisSymbol = "…";
			const byte leftPaddingSize = 7;

			int lineNumber = nodeCoordinates.LineNumber;
			int columnNumber = nodeCoordinates.ColumnNumber;
			int lineLength = line.Length;

			string processedLine;
			if ((fragmentStartPosition == 0 && fragmentLength == lineLength))
			{
				processedLine = line;
			}
			else if (fragmentStartPosition >= lineLength)
			{
				processedLine = string.Empty;
			}
			else
			{
				int fragmentEndPosition = fragmentStartPosition + fragmentLength - 1;

				bool beginningCutOff = (fragmentStartPosition > 0);
				bool endingCutOff = (fragmentEndPosition <= lineLength);
				if (fragmentEndPosition + 1 == lineLength)
				{
					endingCutOff = false;
				}

				if (fragmentEndPosition >= lineLength)
				{
					endingCutOff = false;
					fragmentEndPosition = lineLength - 1;
					fragmentLength = fragmentEndPosition - fragmentStartPosition + 1;
				}

				processedLine = line.Substring(fragmentStartPosition, fragmentLength);
				if (beginningCutOff)
				{
					processedLine = ellipsisSymbol + processedLine;
				}
				if (endingCutOff)
				{
					processedLine = processedLine + ellipsisSymbol;
				}
			}

			string result = string.Format("Line {0}: {1}",
				lineNumber.ToString(CultureInfo.InvariantCulture).PadLeft(lineNumberSize),
				processedLine.TabsToSpaces(tabSize));
			if (columnNumber > 0)
			{
				int cursorOffset = columnNumber - fragmentStartPosition;
				if (fragmentStartPosition > 0)
				{
					cursorOffset++;
				}

				result += Environment.NewLine + string.Empty
					.PadRight(processedLine.Substring(0, cursorOffset - 1)
					.TabsToSpaces(tabSize).Length + lineNumberSize + leftPaddingSize)
					.Replace(" ", "-") + "^"
					;
			}

			return result;
		}
		/// <summary>
		/// Calculates a absolute node coordinates
		/// </summary>
		/// <param name="baseNodeCoordinates">Base node coordinates</param>
		/// <param name="relativeNodeCoordinates">Relative node coordinates</param>
		/// <returns>Absolute node coordinates</returns>
		public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates(
			SourceCodeNodeCoordinates baseNodeCoordinates, SourceCodeNodeCoordinates relativeNodeCoordinates)
		{
			int relativeLineNumber = relativeNodeCoordinates.LineNumber;
			int relativeColumnNumber = relativeNodeCoordinates.ColumnNumber;

			int absoluteLineNumber;
			int absoluteColumnNumber;

			if (!baseNodeCoordinates.IsEmpty)
			{
				int baseLineNumber = baseNodeCoordinates.LineNumber;
				int baseColumnNumber = baseNodeCoordinates.ColumnNumber;

				absoluteLineNumber = baseLineNumber;
				absoluteColumnNumber = baseColumnNumber;

				if (relativeLineNumber > 0)
				{
					if (relativeLineNumber == 1)
					{
						if (relativeColumnNumber > 0)
						{
							absoluteColumnNumber = baseColumnNumber + relativeColumnNumber - 1;
						}
					}
					else
					{
						absoluteLineNumber = baseLineNumber + relativeLineNumber - 1;
						absoluteColumnNumber = relativeColumnNumber;
					}
				}
			}
			else
			{
				absoluteLineNumber = relativeLineNumber;
				absoluteColumnNumber = relativeColumnNumber;
			}

			var absoluteNodeCoordinates = new SourceCodeNodeCoordinates(absoluteLineNumber, absoluteColumnNumber);

			return absoluteNodeCoordinates;
		}
        /// <summary>
        /// Formats a line of source code
        /// </summary>
        /// <param name="line">Line content</param>
        /// <param name="nodeCoordinates">Node coordinates</param>
        /// <param name="lineNumberSize">Number of symbols in the line number caption</param>
        /// <param name="fragmentStartPosition">Start position of source fragment</param>
        /// <param name="fragmentLength">Length of source fragment</param>
        /// <param name="tabSize">Number of spaces in the tab</param>
        /// <returns>Formatted line</returns>
        private static string FormatSourceCodeLine(string line, SourceCodeNodeCoordinates nodeCoordinates,
                                                   int lineNumberSize, int fragmentStartPosition = 0, int fragmentLength = 0, byte tabSize = 4)
        {
            const string ellipsisSymbol  = "…";
            const byte   leftPaddingSize = 7;

            int lineNumber   = nodeCoordinates.LineNumber;
            int columnNumber = nodeCoordinates.ColumnNumber;
            int lineLength   = line.Length;

            string processedLine;

            if (fragmentStartPosition == 0 && fragmentLength == lineLength)
            {
                processedLine = line;
            }
            else if (fragmentStartPosition >= lineLength)
            {
                processedLine = string.Empty;
            }
            else
            {
                int fragmentEndPosition = fragmentStartPosition + fragmentLength - 1;

                bool beginningCutOff = fragmentStartPosition > 0;
                bool endingCutOff    = fragmentEndPosition <= lineLength;
                if (fragmentEndPosition + 1 == lineLength)
                {
                    endingCutOff = false;
                }

                if (fragmentEndPosition >= lineLength)
                {
                    endingCutOff        = false;
                    fragmentEndPosition = lineLength - 1;
                    fragmentLength      = fragmentEndPosition - fragmentStartPosition + 1;
                }

                processedLine = line.Substring(fragmentStartPosition, fragmentLength);
                if (beginningCutOff)
                {
                    processedLine = ellipsisSymbol + processedLine;
                }
                if (endingCutOff)
                {
                    processedLine = processedLine + ellipsisSymbol;
                }
            }

            string result = string.Format("Line {0}: {1}",
                                          lineNumber.ToString(CultureInfo.InvariantCulture).PadLeft(lineNumberSize),
                                          processedLine.TabsToSpaces(tabSize));

            if (columnNumber > 0)
            {
                int cursorOffset = columnNumber - fragmentStartPosition;
                if (fragmentStartPosition > 0)
                {
                    cursorOffset++;
                }

                result += Environment.NewLine + string.Empty
                          .PadRight(
                    (cursorOffset < processedLine.Length ?
                     processedLine.Substring(0, cursorOffset - 1) : processedLine
                    ).TabsToSpaces(tabSize).Length + lineNumberSize + leftPaddingSize
                    )
                          .Replace(" ", "-") + "^"
                ;
            }

            return(result);
        }
        /// <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 = DEFAULT_TAB_SIZE,
                                               int maxFragmentLength = DEFAULT_MAX_FRAGMENT_LENGTH)
        {
            string sourceFragment = string.Empty;
            int    lineNumber     = nodeCoordinates.LineNumber;
            int    columnNumber   = nodeCoordinates.ColumnNumber;

            if (!string.IsNullOrEmpty(sourceCode))
            {
                int previousLineNumber = lineNumber - 1;
                int currentLineNumber  = lineNumber;
                int nextLineNumber     = lineNumber + 1;

                string previousLine = string.Empty;
                string currentLine  = string.Empty;
                string nextLine     = string.Empty;

                int lineCount         = 0;
                int lineBreakPosition = int.MinValue;
                int lineBreakLength   = 0;

                do
                {
                    string line;
                    int    startLinePosition = lineBreakPosition == int.MinValue ? 0 : lineBreakPosition + lineBreakLength;

                    FindNextLineBreak(sourceCode, startLinePosition, out lineBreakPosition, out lineBreakLength);

                    if (lineBreakPosition != -1)
                    {
                        line = sourceCode.Substring(startLinePosition, lineBreakPosition - startLinePosition);
                    }
                    else
                    {
                        line = sourceCode.Substring(startLinePosition);
                    }

                    lineCount++;

                    if (lineCount == previousLineNumber)
                    {
                        previousLine = line;
                    }
                    else if (lineCount == currentLineNumber)
                    {
                        currentLine = line;
                    }
                    else if (lineCount == nextLineNumber)
                    {
                        nextLine = line;
                    }
                }while (lineBreakPosition != -1 && lineCount <= nextLineNumber);

                int lineNumberSize = nextLineNumber.ToString(CultureInfo.InvariantCulture).Length;
                if (currentLineNumber == lineCount)
                {
                    lineNumberSize = currentLineNumber.ToString(CultureInfo.InvariantCulture).Length;
                }

                int fragmentStartPosition;
                int fragmentLength;

                CalculateCutPositions(currentLine, columnNumber, maxFragmentLength,
                                      out fragmentStartPosition, out fragmentLength);

                var           stringBuilderPool     = StringBuilderPool.Shared;
                StringBuilder sourceFragmentBuilder = stringBuilderPool.Rent();

                if (currentLine.Length > 0)
                {
                    if (previousLine.Length > 0)
                    {
                        sourceFragmentBuilder.AppendLine(FormatSourceCodeLine(previousLine,
                                                                              new SourceCodeNodeCoordinates(previousLineNumber, 0),
                                                                              lineNumberSize, fragmentStartPosition, fragmentLength, tabSize));
                    }

                    sourceFragmentBuilder.AppendLine(FormatSourceCodeLine(currentLine,
                                                                          new SourceCodeNodeCoordinates(currentLineNumber, columnNumber),
                                                                          lineNumberSize, fragmentStartPosition, fragmentLength, tabSize));

                    if (nextLine.Length > 0)
                    {
                        sourceFragmentBuilder.AppendLine(FormatSourceCodeLine(nextLine,
                                                                              new SourceCodeNodeCoordinates(nextLineNumber, 0),
                                                                              lineNumberSize, fragmentStartPosition, fragmentLength, tabSize));
                    }
                }

                sourceFragment = sourceFragmentBuilder.ToString();
                stringBuilderPool.Return(sourceFragmentBuilder);
            }

            return(sourceFragment);
        }
		/// <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))
			{
				CalculateLineBreakCount(additionalContent, out lineBreakCount, out charRemainderCount);
			}

			int absoluteLineNumber;
			int absoluteColumnNumber;

			if (!baseNodeCoordinates.IsEmpty)
			{
				int baseLineNumber = baseNodeCoordinates.LineNumber;
				int baseColumnNumber = baseNodeCoordinates.ColumnNumber;

				if (lineBreakCount > 0)
				{
					absoluteLineNumber = baseLineNumber + lineBreakCount;
					absoluteColumnNumber = charRemainderCount + 1;
				}
				else
				{
					absoluteLineNumber = baseLineNumber;
					absoluteColumnNumber = baseColumnNumber + charRemainderCount;
				}
			}
			else
			{
				absoluteLineNumber = lineBreakCount + 1;
				absoluteColumnNumber = charRemainderCount + 1;
			}

			var absoluteNodeCoordinates = new SourceCodeNodeCoordinates(absoluteLineNumber, absoluteColumnNumber);

			return absoluteNodeCoordinates;
		}
		/// <summary>
		/// Calculates a absolute node coordinates
		/// </summary>
		/// <param name="baseNodeCoordinates">Base node coordinates</param>
		/// <param name="lineBreakCount">Number of line breaks</param>
		/// <param name="charRemainderCount">Number of characters left</param>
		/// <returns>Absolute node coordinates</returns>
		public static SourceCodeNodeCoordinates CalculateAbsoluteNodeCoordinates(
			SourceCodeNodeCoordinates baseNodeCoordinates, int lineBreakCount, int charRemainderCount)
		{
			int absoluteLineNumber;
			int absoluteColumnNumber;

			if (!baseNodeCoordinates.IsEmpty)
			{
				int baseLineNumber = baseNodeCoordinates.LineNumber;
				int baseColumnNumber = baseNodeCoordinates.ColumnNumber;

				if (lineBreakCount > 0)
				{
					absoluteLineNumber = baseLineNumber + lineBreakCount;
					absoluteColumnNumber = charRemainderCount + 1;
				}
				else
				{
					absoluteLineNumber = baseLineNumber;
					absoluteColumnNumber = baseColumnNumber + charRemainderCount;
				}
			}
			else
			{
				absoluteLineNumber = lineBreakCount + 1;
				absoluteColumnNumber = charRemainderCount + 1;
			}

			var absoluteNodeCoordinates = new SourceCodeNodeCoordinates(absoluteLineNumber, absoluteColumnNumber);

			return absoluteNodeCoordinates;
		}
Beispiel #18
0
        /// <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);
        }
		/// <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 = DEFAULT_TAB_SIZE,
			int maxFragmentLength = DEFAULT_MAX_FRAGMENT_LENGTH)
		{
			string sourceFragment = string.Empty;
			int lineNumber = nodeCoordinates.LineNumber;
			int columnNumber = nodeCoordinates.ColumnNumber;

			if (!string.IsNullOrEmpty(sourceCode))
			{
				int previousLineNumber = lineNumber - 1;
				int currentLineNumber = lineNumber;
				int nextLineNumber = lineNumber + 1;

				string previousLine = string.Empty;
				string currentLine = string.Empty;
				string nextLine = string.Empty;

				int lineCount = 0;
				int lineBreakPosition = int.MinValue;
				int lineBreakLength = 0;

				do
				{
					string line;
					int startLinePosition = (lineBreakPosition == int.MinValue) ? 0 : lineBreakPosition + lineBreakLength;

					FindNextLineBreak(sourceCode, startLinePosition, out lineBreakPosition, out lineBreakLength);

					if (lineBreakPosition != -1)
					{
						line = sourceCode.Substring(startLinePosition, lineBreakPosition - startLinePosition);
					}
					else
					{
						line = sourceCode.Substring(startLinePosition);
					}

					lineCount++;

					if (lineCount == previousLineNumber)
					{
						previousLine = line;
					}
					else if (lineCount == currentLineNumber)
					{
						currentLine = line;
					}
					else if (lineCount == nextLineNumber)
					{
						nextLine = line;
					}
				}
				while (lineBreakPosition != -1 && lineCount <= nextLineNumber);

				int lineNumberSize = (nextLineNumber).ToString(CultureInfo.InvariantCulture).Length;
				if (currentLineNumber == lineCount)
				{
					lineNumberSize = currentLineNumber.ToString(CultureInfo.InvariantCulture).Length;
				}

				int fragmentStartPosition;
				int fragmentLength;

				CalculateCutPositions(currentLine, columnNumber, maxFragmentLength,
					out fragmentStartPosition, out fragmentLength);

				var sourceFragmentBuilder = new StringBuilder();

				if (currentLine.Length > 0)
				{
					if (previousLine.Length > 0)
					{
						sourceFragmentBuilder.AppendLine(FormatSourceCodeLine(previousLine,
							new SourceCodeNodeCoordinates(previousLineNumber, 0),
							lineNumberSize, fragmentStartPosition, fragmentLength, tabSize));
					}

					sourceFragmentBuilder.AppendLine(FormatSourceCodeLine(currentLine,
						new SourceCodeNodeCoordinates(currentLineNumber, columnNumber),
						lineNumberSize, fragmentStartPosition, fragmentLength, tabSize));

					if (nextLine.Length > 0)
					{
						sourceFragmentBuilder.AppendLine(FormatSourceCodeLine(nextLine,
							new SourceCodeNodeCoordinates(nextLineNumber, 0),
							lineNumberSize, fragmentStartPosition, fragmentLength, tabSize));
					}
				}

				sourceFragment = sourceFragmentBuilder.ToString();
			}

			return sourceFragment;
		}