Esempio n. 1
0
 public EditorCommandFilter(IIdeTracer tracer, IGoToStepDefinitionCommand goToStepDefinitionCommand, FormatTableCommand formatTableCommand, CommentUncommentCommand commentUncommentCommand, RenameCommand renameCommand)
 {
     this.goToStepDefinitionCommand = goToStepDefinitionCommand;
     this.formatTableCommand        = formatTableCommand;
     this.commentUncommentCommand   = commentUncommentCommand;
     this.renameCommand             = renameCommand;
     this.tracer = tracer;
 }
        private List <string> FormatText(List <string> textLines, GherkinDialect dialect)
        {
            var formattedTextLines = new List <string>();

            var stringsToInsertBefore = new List <string>();

            for (int i = 0; i < textLines.Count; i++)
            {
                string trimmedLine = textLines[i].Trim();

                if (string.IsNullOrWhiteSpace(textLines[i]))
                {
                    if (!_normalizeLineBreaks)
                    {
                        formattedTextLines.AddRange(stringsToInsertBefore);
                        stringsToInsertBefore.Clear();
                        formattedTextLines.Add(string.Empty);
                    }
                }
                else if (IsCommentLine(trimmedLine) || IsTagLine(trimmedLine))
                {
                    // Comment or tag lines should have same indent as following line,
                    // that's why we put them to temporary collection
                    stringsToInsertBefore.Add(trimmedLine);
                }
                else if (IsTableLine(trimmedLine))
                {
                    formattedTextLines.AddRange(stringsToInsertBefore.Select(str => _tableIndent + str));
                    stringsToInsertBefore.Clear();

                    //Find whole table, and format it using FormatTableCommand.FormatTableString
                    var tableLines = new List <string>();
                    tableLines.Add(_tableIndent + trimmedLine);
                    while (i + 1 < textLines.Count && IsTableLine(textLines[i + 1]))
                    {
                        i++;
                        tableLines.Add(textLines[i]);
                    }
                    var formattedTable =
                        FormatTableCommand.FormatTableString(string.Join(Environment.NewLine, tableLines));

                    if (formattedTable != null)
                    {
                        formattedTextLines.AddRange(formattedTable.Split(new[] { Environment.NewLine },
                                                                         StringSplitOptions.None));
                    }
                    else
                    {
                        //if table fails to format - leave it as it is
                        formattedTextLines.AddRange(tableLines);
                    }
                }
                else if (IsMultilineDelimeterLine(trimmedLine))
                {
                    formattedTextLines.AddRange(stringsToInsertBefore.Select(str => _multilineIndent + str));
                    stringsToInsertBefore.Clear();

                    formattedTextLines.Add(_multilineIndent + trimmedLine);

                    //Find original indent of multi-line argument
                    int whitespaces = 0;
                    while (char.IsWhiteSpace(textLines[i][whitespaces]))
                    {
                        whitespaces++;
                    }
                    string originalIndent = textLines[i].Substring(0, whitespaces);

                    while (!IsMultilineDelimeterLine(textLines[++i]))
                    {
                        string formattedLine;
                        if (textLines[i].StartsWith(originalIndent))
                        {
                            //replace original indent with MultilineIndent
                            formattedLine = _multilineIndent + textLines[i].Substring(originalIndent.Length);
                        }
                        else
                        {
                            //invalid case - leave it as it is
                            formattedLine = textLines[i];
                        }
                        formattedTextLines.Add(formattedLine);
                    }

                    formattedTextLines.Add(_multilineIndent + textLines[i].Trim());
                }
                else if (IsBlockLine(trimmedLine, dialect) || IsStepLine(trimmedLine, dialect))
                {
                    if (_normalizeLineBreaks)
                    {
                        int addLinesBefore = GetPreceedingLineBreaks(trimmedLine, dialect);
                        for (int j = 0; j < addLinesBefore; j++)
                        {
                            formattedTextLines.Add(string.Empty);
                        }
                    }
                    string indent = GetIndent(trimmedLine, dialect);

                    formattedTextLines.AddRange(stringsToInsertBefore.Select(str => indent + str));
                    stringsToInsertBefore.Clear();

                    formattedTextLines.Add(indent + trimmedLine);
                }
                else
                {
                    //Other lines - leave unchanged
                    formattedTextLines.AddRange(stringsToInsertBefore);
                    stringsToInsertBefore.Clear();

                    formattedTextLines.Add(textLines[i]);
                }
            }

            formattedTextLines.AddRange(stringsToInsertBefore);
            stringsToInsertBefore.Clear();
            return(formattedTextLines);
        }