private void FormatHasRows(IHasRows hasRows, StringBuilder result, string indent = INDENT) { foreach (var tableRow in hasRows.Rows) { FormatRow(tableRow, result, indent); } }
private TableCaretPosition GetCaretPosition(CaretPosition caretPosition, IHasRows hasRows) { var line = caretPosition.BufferPosition.GetContainingLine(); var caretColumn = caretPosition.BufferPosition.Position - line.Start.Position; var row = hasRows.Rows.FirstOrDefault(r => r.Location.Line == line.LineNumber + 1); if (row == null || row.Cells.Any(c => c.Location.Column <= 0)) { return(TableCaretPosition.CreateUnknown(line.LineNumber, caretColumn)); } var cellIndex = FindCellIndex(caretColumn, row, line.GetText(), out var cell); if (cellIndex < 0) // before first cell { return(TableCaretPosition.CreateBeforeFirstCell(line.LineNumber, caretColumn)); } if (cellIndex == int.MaxValue) // after last cell { return(TableCaretPosition.CreateAfterLastCell(line.LineNumber)); } return(TableCaretPosition.CreateInCell(line.LineNumber, cellIndex, Math.Max(caretColumn - (cell.Location.Column - 1), 0))); }
private string GetFormattedTableText(IHasRows hasRows, string indent, string newLine, TableCaretPosition caretPosition, ITextSnapshot textSnapshot, out int newCaretLinePosition) { var widths = GetWidths(hasRows); int nextLine = ((IHasLocation)hasRows).Location.Line; var result = new StringBuilder(); foreach (var row in hasRows.Rows) { while (row.Location.Line > nextLine) { var nonRowLine = textSnapshot.GetLineFromLineNumber(nextLine - 1); result.Append(nonRowLine.GetText()); result.Append(newLine); nextLine++; } result.Append(indent); result.Append("|"); foreach (var item in row.Cells.Select((c, i) => new { c, i })) { result.Append(new string(' ', PADDING_LENGHT)); result.Append(Escape(item.c.Value).PadRight(widths[item.i])); result.Append(new string(' ', PADDING_LENGHT)); result.Append('|'); } result.Append(newLine); nextLine++; } result.Remove(result.Length - newLine.Length, newLine.Length); newCaretLinePosition = CalculateNewCaretLinePosition(caretPosition, widths, indent); return(result.ToString()); }
private int[] GetWidths(IHasRows hasRows) { var widths = new int[hasRows.Rows.Max(r => r.Cells.Count())]; foreach (var row in hasRows.Rows) { foreach (var item in row.Cells.Select((c, i) => new { c, i })) { widths[item.i] = Math.Max(widths[item.i], Escape(item.c.Value).Length); } } return(widths); }
private string GetIndent(ITextSnapshot textSnapshot, IHasRows hasRows) { var firstLine = textSnapshot.GetLineFromLineNumber(hasRows.Rows.First().Location.Line - 1); return(Regex.Match(firstLine.GetText(), @"^\s*").Value); }