private int CalculateNewCaretLinePosition(TableCaretPosition caretPosition, int[] widths, string indent) { if (caretPosition.IsUnknown) { return(caretPosition.Column); } if (caretPosition.IsBeforeFirstCell) { return(Math.Min(caretPosition.Column, indent.Length)); } Debug.Assert(caretPosition.Cell != null); var positionAfterCellOpenPipe = indent.Length + widths.Take(Math.Min(widths.Length, caretPosition.Cell.Value)) .Sum(w => w + PADDING_LENGHT * 2 + PIPE_LENGHT) + PIPE_LENGHT; if (caretPosition.IsAfterLastCell) { return(positionAfterCellOpenPipe); // position after the last cell pipe (position of the open pipe of the one after last cell) } Debug.Assert(caretPosition.IsInCell); // position within the cell + 2*padding, if cell has been shrinked, we move position to end of cell (Math.Min) return(positionAfterCellOpenPipe + PADDING_LENGHT + Math.Min(caretPosition.Column, widths[caretPosition.Cell.Value] + PADDING_LENGHT)); }
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 void RestoreCaretPosition(IWpfTextView textView, TableCaretPosition caretPosition, int newCaretLineColumn) { var line = textView.TextSnapshot.GetLineFromLineNumber(caretPosition.Line); var lineStartPosition = line.Start.Position; textView.Caret.MoveTo(new SnapshotPoint(textView.TextSnapshot, Math.Min(lineStartPosition + newCaretLineColumn, line.End.Position))); }
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('|'); } var lineText = textSnapshot.GetLineFromLineNumber(nextLine - 1).GetText(); var unfinishedCell = GetUnfinishedCell(lineText); if (unfinishedCell != null) { result.Append(' '); result.Append(unfinishedCell); } result.Append(newLine); nextLine++; } result.Remove(result.Length - newLine.Length, newLine.Length); newCaretLinePosition = CalculateNewCaretLinePosition(caretPosition, widths, indent); return(result.ToString()); }