Ejemplo n.º 1
0
        /// <summary>
        /// Adds context to the result item, coming from code block starting at given position
        /// </summary>
        protected void AddContextToItem(AbstractResultItem item, EditPoint2 editPoint)
        {
            StringBuilder context = new StringBuilder();
            // indices +1 !!

            int topLines = 0;

            int currentLine         = item.ReplaceSpan.iStartLine;
            int contextRelativeLine = 0;

            // add NumericConstants.ContextLineRadius lines above the result item with at least 2 non-whitespace characters
            while (currentLine >= 1 && topLines < NumericConstants.ContextLineRadius)
            {
                editPoint.MoveToLineAndOffset(currentLine, 1);
                string lineText = editPoint.GetText(editPoint.LineLength);
                if (lineText.Trim().Length > 0)
                {
                    context.Insert(0, lineText + Environment.NewLine);
                    contextRelativeLine++;
                    if (lineText.Trim().Length > 1)
                    {
                        topLines++;
                    }
                }
                currentLine--;
            }

            editPoint.MoveToLineAndOffset(item.ReplaceSpan.iStartLine + 1, 1);
            context.Append(editPoint.GetText(item.ReplaceSpan.iStartIndex));

            context.Append(StringConstants.ContextSubstituteText); // add text that will be displayed instead of actual result item

            editPoint.MoveToLineAndOffset(item.ReplaceSpan.iEndLine + 1, item.ReplaceSpan.iEndIndex + 1);
            context.Append(editPoint.GetText(editPoint.LineLength - item.ReplaceSpan.iEndIndex + 1));

            int botLines = 0;

            currentLine = item.ReplaceSpan.iEndLine + 2;
            // add NumericConstants.ContextLineRadius lines below the result item with at least 2 non-whitespace characters
            while (botLines < NumericConstants.ContextLineRadius)
            {
                editPoint.MoveToLineAndOffset(currentLine, 1);
                string lineText = editPoint.GetText(editPoint.LineLength);
                if (lineText.Trim().Length > 0)
                {
                    context.Append(Environment.NewLine + lineText);
                    if (lineText.Trim().Length > 1)
                    {
                        botLines++;
                    }
                }
                editPoint.EndOfLine();
                if (editPoint.AtEndOfDocument)
                {
                    break;
                }
                currentLine++;
            }

            item.Context             = context.ToString();
            item.ContextRelativeLine = contextRelativeLine; // index of "middle" line
        }