@@ -66,8 +66,6 @@ struct CodePointRange
6666};
6767
6868const std::size_t hardWrapCellWidth = 32 ;
69- const std::size_t preferredWrappedColumnWidth = 18 ;
70- const std::size_t minimumWrappedColumnWidth = 10 ;
7169
7270std::size_t nextRowId (const Table &table);
7371
@@ -850,212 +848,6 @@ std::size_t wrapLongCells(Table &table, std::size_t originalTargetRow)
850848 return wrappedTargetRow;
851849}
852850
853- std::vector<std::size_t > naturalColumnWidths (const Table &table)
854- {
855- std::vector<std::size_t > widths (table.columns , 3 );
856- for (std::size_t rowIndex = 0 ; rowIndex < table.rows .size (); ++rowIndex)
857- {
858- const Row &row = table.rows [rowIndex];
859- if (row.separator )
860- continue ;
861-
862- for (std::size_t column = 0 ; column < table.columns ; ++column)
863- widths[column] = (std::max)(widths[column], displayWidth (row.cells [column]));
864- }
865- return widths;
866- }
867-
868- std::size_t formattedTableOverhead (const Table &table)
869- {
870- std::size_t overhead = table.leadingPipe ? 1 : 0 ;
871- for (std::size_t column = 0 ; column < table.columns ; ++column)
872- {
873- if (column > 0 )
874- overhead += 2 ;
875- if (table.leadingPipe || column > 0 )
876- ++overhead;
877- if (table.trailingPipe && column + 1 == table.columns )
878- overhead += 2 ;
879- }
880- return overhead;
881- }
882-
883- std::size_t widthSum (const std::vector<std::size_t > &widths)
884- {
885- std::size_t sum = 0 ;
886- for (std::size_t i = 0 ; i < widths.size (); ++i)
887- sum += widths[i];
888- return sum;
889- }
890-
891- bool containsSpace (const std::string &value)
892- {
893- for (std::size_t i = 0 ; i < value.size (); ++i)
894- {
895- if (isSpace (static_cast <unsigned char >(value[i])))
896- return true ;
897- }
898- return false ;
899- }
900-
901- std::vector<bool > textColumns (const Table &table)
902- {
903- std::vector<bool > result (table.columns , false );
904- for (std::size_t rowIndex = 0 ; rowIndex < table.rows .size (); ++rowIndex)
905- {
906- const Row &row = table.rows [rowIndex];
907- if (row.separator )
908- continue ;
909-
910- for (std::size_t column = 0 ; column < table.columns ; ++column)
911- {
912- if (displayWidth (row.cells [column]) > preferredWrappedColumnWidth && containsSpace (row.cells [column]))
913- result[column] = true ;
914- }
915- }
916- return result;
917- }
918-
919- std::size_t largestShrinkableColumn (const std::vector<std::size_t > &widths, const std::vector<std::size_t > &minimums, const std::vector<bool > &allowed)
920- {
921- std::size_t best = static_cast <std::size_t >(-1 );
922- std::size_t bestWidth = 0 ;
923- std::size_t bestSlack = 0 ;
924- for (std::size_t column = 0 ; column < widths.size (); ++column)
925- {
926- if (!allowed[column] || widths[column] <= minimums[column])
927- continue ;
928-
929- const std::size_t slack = widths[column] - minimums[column];
930- if (best == static_cast <std::size_t >(-1 ) || widths[column] > bestWidth || (widths[column] == bestWidth && slack > bestSlack))
931- {
932- best = column;
933- bestWidth = widths[column];
934- bestSlack = slack;
935- }
936- }
937- return best;
938- }
939-
940- void shrinkColumnsToBudget (std::vector<std::size_t > &widths, const std::vector<std::size_t > &minimums, const std::vector<bool > &allowed, std::size_t budget)
941- {
942- while (widthSum (widths) > budget)
943- {
944- const std::size_t column = largestShrinkableColumn (widths, minimums, allowed);
945- if (column == static_cast <std::size_t >(-1 ))
946- return ;
947- --widths[column];
948- }
949- }
950-
951- std::vector<std::size_t > targetColumnWidthsForTableWidth (const Table &table, std::size_t maxTableWidth)
952- {
953- std::vector<std::size_t > widths = naturalColumnWidths (table);
954- if (widths.empty ())
955- return widths;
956-
957- const std::size_t overhead = formattedTableOverhead (table);
958- if (maxTableWidth <= overhead + widths.size () * 3 )
959- maxTableWidth = overhead + widths.size () * 3 ;
960-
961- const std::size_t budget = maxTableWidth - overhead;
962- if (widthSum (widths) <= budget)
963- return widths;
964-
965- const std::vector<bool > textColumn = textColumns (table);
966- std::vector<std::size_t > minimums (widths.size (), 3 );
967- std::vector<bool > allowed (widths.size (), false );
968- for (std::size_t column = 0 ; column < widths.size (); ++column)
969- {
970- if (!textColumn[column])
971- continue ;
972- minimums[column] = (std::min)(widths[column], preferredWrappedColumnWidth);
973- allowed[column] = widths[column] > minimums[column];
974- }
975- shrinkColumnsToBudget (widths, minimums, allowed, budget);
976- if (widthSum (widths) <= budget)
977- return widths;
978-
979- for (std::size_t column = 0 ; column < widths.size (); ++column)
980- {
981- if (!textColumn[column])
982- continue ;
983- minimums[column] = (std::min)(widths[column], minimumWrappedColumnWidth);
984- allowed[column] = widths[column] > minimums[column];
985- }
986- shrinkColumnsToBudget (widths, minimums, allowed, budget);
987- if (widthSum (widths) <= budget)
988- return widths;
989-
990- for (std::size_t column = 0 ; column < widths.size (); ++column)
991- {
992- minimums[column] = (std::min)(widths[column], minimumWrappedColumnWidth);
993- allowed[column] = widths[column] > minimums[column];
994- }
995- shrinkColumnsToBudget (widths, minimums, allowed, budget);
996- if (widthSum (widths) <= budget)
997- return widths;
998-
999- for (std::size_t column = 0 ; column < widths.size (); ++column)
1000- {
1001- minimums[column] = 3 ;
1002- allowed[column] = widths[column] > minimums[column];
1003- }
1004- shrinkColumnsToBudget (widths, minimums, allowed, budget);
1005- return widths;
1006- }
1007-
1008- std::size_t wrapCellsToColumnWidths (Table &table, std::size_t originalTargetRow, const std::vector<std::size_t > &columnWidths)
1009- {
1010- if (table.separatorRow == static_cast <std::size_t >(-1 ) || columnWidths.size () < table.columns )
1011- return originalTargetRow;
1012-
1013- std::vector<Row> wrappedRows;
1014- wrappedRows.reserve (table.rows .size ());
1015- std::size_t wrappedTargetRow = originalTargetRow;
1016- std::size_t nextId = nextRowId (table);
1017-
1018- for (std::size_t rowIndex = 0 ; rowIndex < table.rows .size (); ++rowIndex)
1019- {
1020- const Row &row = table.rows [rowIndex];
1021- if (row.separator || rowIndex <= table.separatorRow )
1022- {
1023- if (rowIndex == originalTargetRow)
1024- wrappedTargetRow = wrappedRows.size ();
1025- wrappedRows.push_back (row);
1026- continue ;
1027- }
1028-
1029- std::vector<std::vector<std::string>> cellSegments;
1030- cellSegments.reserve (table.columns );
1031- std::size_t segmentCount = 1 ;
1032- for (std::size_t column = 0 ; column < table.columns ; ++column)
1033- {
1034- cellSegments.push_back (wrapCellSegments (row.cells [column], columnWidths[column]));
1035- segmentCount = (std::max)(segmentCount, cellSegments.back ().size ());
1036- }
1037-
1038- if (rowIndex == originalTargetRow)
1039- wrappedTargetRow = wrappedRows.size ();
1040-
1041- for (std::size_t segmentIndex = 0 ; segmentIndex < segmentCount; ++segmentIndex)
1042- {
1043- Row wrapped;
1044- wrapped.id = segmentIndex == 0 ? row.id : nextId++;
1045- wrapped.cells .reserve (table.columns );
1046- for (std::size_t column = 0 ; column < table.columns ; ++column)
1047- {
1048- const std::vector<std::string> &segments = cellSegments[column];
1049- wrapped.cells .push_back (segmentIndex < segments.size () ? segments[segmentIndex] : " " );
1050- }
1051- wrappedRows.push_back (std::move (wrapped));
1052- }
1053- }
1054-
1055- table.rows = std::move (wrappedRows);
1056- return wrappedTargetRow;
1057- }
1058-
1059851void appendSeparatorCell (std::string &line, Align align, std::size_t width)
1060852{
1061853 const std::size_t target = (std::max)(width, static_cast <std::size_t >(3 ));
@@ -2050,49 +1842,6 @@ EditResult apply(const std::vector<std::string> &lines, int row, int column, Act
20501842 return result;
20511843}
20521844
2053- EditResult applyWrappedToWidth (const std::vector<std::string> &lines, int row, int column, std::size_t maxTableWidth)
2054- {
2055- EditResult result;
2056- if (lines.empty ())
2057- {
2058- result.message = " No table found" ;
2059- return result;
2060- }
2061-
2062- std::size_t sourceRow = row < 0 ? 0 : static_cast <std::size_t >(row);
2063- if (sourceRow >= lines.size ())
2064- sourceRow = lines.size () - 1 ;
2065-
2066- const TableRange tableRange = findTableRange (lines, static_cast <int >(sourceRow));
2067- if (!tableRange.found )
2068- {
2069- result.message = " No Markdown table found" ;
2070- return result;
2071- }
2072-
2073- std::size_t tableRow = sourceRow - tableRange.firstRow ;
2074- Table table = parseTable (lines, tableRange.firstRow , tableRange.lastRow );
2075- if (!isMarkdownTable (table))
2076- {
2077- result.message = " No Markdown table found" ;
2078- return result;
2079- }
2080-
2081- if (tableRow >= table.rows .size ())
2082- tableRow = table.rows .size () - 1 ;
2083- std::size_t tableColumn = column < 0 ? 0 : static_cast <std::size_t >(column);
2084- if (tableColumn >= table.columns )
2085- tableColumn = table.columns - 1 ;
2086-
2087- const std::vector<std::size_t > columnWidths = targetColumnWidthsForTableWidth (table, maxTableWidth);
2088- const std::size_t targetRow = wrapCellsToColumnWidths (table, tableRow, columnWidths);
2089- FormatResult formatted = formatTable (table, targetRow, tableColumn);
2090- setResultFromFormat (result, formatted);
2091- result.ok = true ;
2092- result.changed = true ;
2093- return result;
2094- }
2095-
20961845EditResult convertDelimitedToTable (const std::string &text)
20971846{
20981847 EditResult result;
0 commit comments