forked from chipsalliance/verible
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalign.cc
1373 lines (1220 loc) · 53 KB
/
align.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017-2020 The Verible Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/formatting/align.h"
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <vector>
#include "absl/strings/str_join.h"
#include "common/formatting/format_token.h"
#include "common/formatting/token_partition_tree.h"
#include "common/formatting/unwrapped_line.h"
#include "common/strings/display_utils.h"
#include "common/strings/range.h"
#include "common/text/concrete_syntax_leaf.h"
#include "common/text/concrete_syntax_tree.h"
#include "common/text/token_info.h"
#include "common/text/token_stream_view.h"
#include "common/text/tree_utils.h"
#include "common/util/algorithm.h"
#include "common/util/container_iterator_range.h"
#include "common/util/enum_flags.h"
#include "common/util/iterator_range.h"
#include "common/util/logging.h"
#include "common/util/tree_operations.h"
#include "common/util/vector_tree.h"
#include "common/util/vector_tree_iterators.h"
namespace verible {
static const verible::EnumNameMap<AlignmentPolicy>& AlignmentPolicyNameMap() {
static const verible::EnumNameMap<AlignmentPolicy> kAlignmentPolicyNameMap({
{"align", AlignmentPolicy::kAlign},
{"flush-left", AlignmentPolicy::kFlushLeft},
{"preserve", AlignmentPolicy::kPreserve},
{"infer", AlignmentPolicy::kInferUserIntent},
// etc.
});
return kAlignmentPolicyNameMap;
}
std::ostream& operator<<(std::ostream& stream, AlignmentPolicy policy) {
return AlignmentPolicyNameMap().Unparse(policy, stream);
}
bool AbslParseFlag(absl::string_view text, AlignmentPolicy* policy,
std::string* error) {
return AlignmentPolicyNameMap().Parse(text, policy, error, "AlignmentPolicy");
}
std::string AbslUnparseFlag(const AlignmentPolicy& policy) {
std::ostringstream stream;
stream << policy;
return stream.str();
}
static int EffectiveCellWidth(const FormatTokenRange& tokens) {
if (tokens.empty()) return 0;
VLOG(2) << __FUNCTION__;
// Sum token text lengths plus required pre-spacings (except first token).
// Note: LeadingSpacesLength() honors where original spacing when preserved.
return std::accumulate(
tokens.begin(), tokens.end(), -tokens.front().LeadingSpacesLength(),
[](int total_width, const PreFormatToken& ftoken) {
const int pre_width = ftoken.LeadingSpacesLength();
const int text_length = ftoken.token->text().length();
VLOG(2) << " +" << pre_width << " +" << text_length;
// TODO(fangism): account for multi-line tokens like
// block comments.
return total_width + ftoken.LeadingSpacesLength() + text_length;
});
}
static int EffectiveLeftBorderWidth(const FormatTokenRange& tokens) {
if (tokens.empty()) return 0;
return tokens.front().before.spaces_required;
}
using ColumnsTreePath = SyntaxTreePath;
struct AlignmentCell {
// Slice of format tokens in this cell (may be empty range).
FormatTokenRange tokens;
// The width of this token excerpt that complies with minimum spacing.
int compact_width = 0;
// Width of the left-side spacing before this cell, which can be considered
// as a space-only column, usually no more than 1 space wide.
int left_border_width = 0;
// Returns true when neither the cell nor its subcells contain any tokens.
bool IsUnused() const { return (tokens.empty() && compact_width == 0); }
// Returns true when the cell contains subcells with tokens.
bool IsComposite() const { return (tokens.empty() && compact_width > 0); }
int TotalWidth() const { return left_border_width + compact_width; }
FormatTokenRange ConstTokensRange() const {
return FormatTokenRange(tokens.begin(), tokens.end());
}
void UpdateWidths() {
compact_width = EffectiveCellWidth(ConstTokensRange());
left_border_width = EffectiveLeftBorderWidth(tokens);
}
};
using AlignmentRow = VectorTree<AlignmentCell>;
using AlignmentMatrix = std::vector<AlignmentRow>;
std::ostream& operator<<(std::ostream& stream, const AlignmentCell& cell) {
if (!cell.tokens.empty()) {
// See UnwrappedLine::AsCode for similar printing.
stream << absl::StrJoin(cell.tokens, " ",
[](std::string* out, const PreFormatToken& token) {
absl::StrAppend(out, token.Text());
});
}
return stream;
}
// Type of functions used to generate textual node representations that are
// suitable for use in rectangular cell.
// The function is called with a tree node as its only argument. It should
// return a string containing the cell's text and a single character used as
// a filler for cell's empty space.
template <typename ValueType>
using CellLabelGetterFunc =
std::function<std::pair<std::string, char>(const VectorTree<ValueType>&)>;
// Recursively creates a tree with cells textual data. Its main purpose is to
// split multi-line cell labels and calculate how many lines have to be printed.
// This is a helper function used in ColumsTreeFormatter.
template <typename ValueType, typename Cell>
static std::size_t CreateTextNodes(
const VectorTree<ValueType>& src_node, VectorTree<Cell>* dst_node,
const CellLabelGetterFunc<ValueType>& get_cell_label) {
static constexpr std::size_t kMinCellWidth = 2;
std::size_t depth = 0;
std::size_t subtree_depth = 0;
for (const auto& src_child : src_node.Children()) {
const auto [text, filler] = get_cell_label(src_child);
const std::vector<std::string> lines = absl::StrSplit(text, '\n');
auto* dst_child = dst_node;
for (const auto& line : lines) {
dst_child->Children().emplace_back(
Cell{line, filler, std::max(line.size(), kMinCellWidth)});
dst_child = &dst_child->Children().back();
}
depth = std::max(depth, lines.size());
subtree_depth = std::max(
subtree_depth, CreateTextNodes(src_child, dst_child, get_cell_label));
}
return depth + subtree_depth;
}
// Prints visualization of columns tree 'root' to a 'stream'. The 'root' node
// itself is not visualized. The 'get_cell_label' callback is used to get the
// cell label printed for each node.
//
// The label's text can contain multiple lines. Each line can contain up to 3
// fields separated by tab character ('\t'). The first field is aligned to the
// left. The second field is either aligned to the right (when there are
// 2 fields) or centered (when there are 3 fields). The third field is aligned
// to the right. Empty space is filled with label's filler character.
template <typename ValueType>
static void ColumnsTreeFormatter(
std::ostream& stream, const VectorTree<ValueType>& root,
const CellLabelGetterFunc<ValueType>& get_cell_label) {
if (root.Children().empty()) return;
static constexpr absl::string_view kCellSeparator = "|";
struct Cell {
std::string text;
char filler = ' ';
std::size_t width = 0;
};
VectorTree<Cell> text_tree;
const std::size_t depth =
CreateTextNodes<ValueType, Cell>(root, &text_tree, get_cell_label);
// Adjust cells width to fit all their children
for (auto& node : VectorTreePostOrderTraversal(text_tree)) {
// Include separator width in cell width
node.Value().width += kCellSeparator.size();
if (is_leaf(node)) continue;
const std::size_t children_width =
std::accumulate(node.Children().begin(), node.Children().end(), 0,
[](std::size_t width, const VectorTree<Cell>& child) {
return width + child.Value().width;
});
if (node.Value().width < children_width) {
node.Value().width = children_width;
}
}
// Adjust cells width to fill their parents
for (auto& node : VectorTreePreOrderTraversal(text_tree)) {
if (is_leaf(node)) continue;
std::size_t children_width =
std::accumulate(node.Children().begin(), node.Children().end(), 0,
[](std::size_t width, const VectorTree<Cell>& child) {
return width + child.Value().width;
});
// There is at least one child; each cell minimum width is equal to:
// CreateTextNodes::kMinCellWidth + kCellSeparator.size()
CHECK_GT(children_width, 0);
if (node.Value().width > children_width) {
auto extra_width = node.Value().width - children_width;
for (auto& child : node.Children()) {
CHECK_GT(children_width, 0);
const auto added_child_width =
extra_width * child.Value().width / children_width; // NOLINT
extra_width -= added_child_width;
children_width -= child.Value().width;
child.Value().width += added_child_width;
}
}
}
std::vector<std::string> lines(depth);
auto range = VectorTreePreOrderTraversal(text_tree);
auto level_offset = NumAncestors(text_tree) + 1;
for (auto& node : make_range(range.begin() + 1, range.end())) {
auto& cell = node.Value();
const std::size_t level = NumAncestors(node) - level_offset;
if (level > 0 && verible::IsFirstChild(node)) {
const int padding_len = lines[level - 1].size() - lines[level].size() -
node.Parent()->Value().width;
if (padding_len > 0) {
if (lines[level].empty())
lines[level].append(std::string(padding_len, ' '));
else if (padding_len > int(kCellSeparator.size()))
lines[level].append(absl::StrCat(
kCellSeparator,
std::string(padding_len - kCellSeparator.size(), ' ')));
}
}
const std::vector<absl::string_view> parts =
absl::StrSplit(cell.text, '\t');
CHECK_LE(parts.size(), 3);
const auto width = cell.width - kCellSeparator.size();
switch (parts.size()) {
case 1: {
const std::string pad(width - parts[0].size(), cell.filler);
absl::StrAppend(&lines[level], kCellSeparator, parts[0], pad);
break;
}
case 2: {
const std::string pad(width - parts[0].size() - parts[1].size(),
cell.filler);
absl::StrAppend(&lines[level], kCellSeparator, parts[0], pad,
parts.back());
break;
}
case 3: {
std::size_t pos =
std::clamp((width - parts[1].size()) / 2, parts[0].size() + 1,
width - parts[2].size() - parts[1].size() - 1);
const std::string left_pad(pos - parts[0].size(), cell.filler);
const std::string right_pad(
width - parts[2].size() - (pos + parts[1].size()), cell.filler);
absl::StrAppend(&lines[level], kCellSeparator, parts[0], left_pad,
parts[1], right_pad, parts[2]);
break;
}
}
}
for (const auto& line : lines) {
if (!line.empty()) stream << line << kCellSeparator << "\n";
}
}
// These properties are calculated/aggregated from alignment cells.
struct AlignedColumnConfiguration {
int width = 0;
int left_border = 0;
int TotalWidth() const { return left_border + width; }
void UpdateFromCell(const AlignmentCell& cell) {
width = std::max(width, cell.compact_width);
left_border = std::max(left_border, cell.left_border_width);
}
};
/* static */ ColumnPositionTree* ColumnSchemaScanner::ReserveNewColumn(
ColumnPositionTree* parent_column, const Symbol& symbol,
const AlignmentColumnProperties& properties, const SyntaxTreePath& path) {
CHECK_NOTNULL(parent_column);
// The path helps establish a total ordering among all desired alignment
// points, given that they may come from optional or repeated language
// constructs.
const SyntaxTreeLeaf* leaf = GetLeftmostLeaf(symbol);
// It is possible for a node to be empty, in which case, ignore.
if (leaf == nullptr) return nullptr;
if (parent_column->Parent() != nullptr && parent_column->Children().empty()) {
// Starting token of a column and its first subcolumn must be the same.
// (subcolumns overlap their parent column).
CHECK_EQ(parent_column->Value().starting_token, leaf->get());
}
// It's possible the previous cell's path was intentionally altered
// to effectively fuse it with the cell that is about to be added.
// When this occurs, take the (previous) leftmost token, and suppress
// adding a new column.
if (parent_column->Children().empty() ||
parent_column->Children().back().Value().path != path) {
parent_column->Children().emplace_back(
ColumnPositionEntry{path, leaf->get(), properties});
const auto& column = parent_column->Children().back();
ColumnsTreePath column_path;
verible::Path(column, column_path);
VLOG(2) << "reserving new column for " << TreePathFormatter(path) << " at "
<< TreePathFormatter(column_path);
}
return &parent_column->Children().back();
}
struct AggregateColumnData {
AggregateColumnData() = default;
// This is taken as the first seen set of properties in any given column.
AlignmentColumnProperties properties;
// These tokens's positions will be used to identify alignment cell
// boundaries.
std::vector<TokenInfo> starting_tokens;
SyntaxTreePath path;
void Import(const ColumnPositionEntry& cell) {
if (starting_tokens.empty()) {
path = cell.path;
// Take the first set of properties, and ignore the rest.
// They should be consistent, coming from alignment cell scanners,
// but this is not verified.
properties = cell.properties;
}
starting_tokens.push_back(cell.starting_token);
}
};
class ColumnSchemaAggregator {
public:
void Collect(const ColumnPositionTree& columns) {
CollectColumnsTree(columns, &columns_);
}
// Sort columns by syntax tree path assigned to them and create an index that
// maps syntax tree path to a column. Call this after collecting all columns.
void Finalize() {
syntax_to_columns_map_.clear();
for (auto& node : VectorTreePreOrderTraversal(columns_)) {
if (node.Parent()) {
// Index the column
auto it = syntax_to_columns_map_.emplace_hint(
syntax_to_columns_map_.end(), node.Value().path, ColumnsTreePath{});
verible::Path(node, it->second);
}
if (!is_leaf(node)) {
// Sort subcolumns. This puts negative paths (leading non-tree token
// columns) before empty, zero, and positive ones.
std::sort(node.Children().begin(), node.Children().end(),
[](const auto& a, const auto& b) {
return a.Value().path < b.Value().path;
});
// Propagate left_border_override property to the left subcolumn
auto& left_child_data = node.Children().front().Value();
left_child_data.properties.left_border_override =
std::max(left_child_data.properties.left_border_override,
node.Value().properties.left_border_override);
}
}
}
const std::map<SyntaxTreePath, ColumnsTreePath>& SyntaxToColumnsMap() const {
return syntax_to_columns_map_;
}
const VectorTree<AggregateColumnData>& Columns() const { return columns_; }
VectorTree<AlignmentColumnProperties> ColumnProperties() const {
return Transform<VectorTree<AlignmentColumnProperties>>(
columns_, [](const VectorTree<AggregateColumnData>& data_node) {
return data_node.Value().properties;
});
}
private:
void CollectColumnsTree(const ColumnPositionTree& column,
VectorTree<AggregateColumnData>* aggregate_column) {
CHECK_NOTNULL(aggregate_column);
for (const auto& subcolumn : column.Children()) {
const auto [index_entry, insert] =
syntax_to_columns_map_.try_emplace(subcolumn.Value().path);
VectorTree<verible::AggregateColumnData>* aggregate_subcolumn;
if (insert) {
aggregate_column->Children().emplace_back();
aggregate_subcolumn = &aggregate_column->Children().back();
// Put aggregate column node's path in created index entry
verible::Path(*aggregate_subcolumn, index_entry->second);
} else {
// Fact: existing aggregate_subcolumn is a direct child of
// aggregate_column
CHECK_GT(static_cast<int>(aggregate_column->Children().size()),
index_entry->second.back());
aggregate_subcolumn =
&aggregate_column->Children()[index_entry->second.back()];
}
aggregate_subcolumn->Value().Import(subcolumn.Value());
CollectColumnsTree(subcolumn, aggregate_subcolumn);
}
}
// Keeps track of unique positions where new columns are desired.
// The nodes are sets of starting tokens, from which token ranges will be
// computed per cell.
VectorTree<AggregateColumnData> columns_;
// 1:1 map between syntax tree's path and columns tree's path
std::map<SyntaxTreePath, ColumnsTreePath> syntax_to_columns_map_;
};
// CellLabelGetterFunc which creates a label with column's path relative to
// its parent column and either '<' or '>' filler characters indicating whether
// the column flushes to the left or the right.
// `T` should be either AggregateColumnData or ColumnPositionEntry.
template <typename T>
static std::pair<std::string, char> GetColumnDataCellLabel(
const VectorTree<T>& node) {
std::ostringstream label;
const SyntaxTreePath& path = node.Value().path;
auto begin = path.begin();
if (node.Parent()) {
// Find and skip common prefix
const auto& parent_path = node.Parent()->Value().path;
auto parent_begin = parent_path.begin();
while (begin != path.end() && parent_begin != parent_path.end() &&
*begin == *parent_begin) {
++begin;
++parent_begin;
}
}
label << " \t ";
if (begin != path.begin() && begin != path.end()) label << ".";
label << SequenceFormatter(
iterator_range<SyntaxTreePath::const_iterator>(begin, path.end()), ".");
label << " \t ";
return {label.str(), node.Value().properties.flush_left ? '<' : '>'};
}
std::ostream& operator<<(std::ostream& stream,
const VectorTree<AggregateColumnData>& tree) {
ColumnsTreeFormatter<AggregateColumnData>(
stream, tree, GetColumnDataCellLabel<AggregateColumnData>);
return stream;
}
std::ostream& operator<<(std::ostream& stream, const ColumnPositionTree& tree) {
ColumnsTreeFormatter<ColumnPositionEntry>(
stream, tree, GetColumnDataCellLabel<ColumnPositionEntry>);
return stream;
}
std::ostream& operator<<(std::ostream& stream,
const VectorTree<AlignmentCell>& tree) {
ColumnsTreeFormatter<AlignmentCell>(
stream, tree,
[](const VectorTree<AlignmentCell>& node)
-> std::pair<std::string, char> {
const auto& cell = node.Value();
if (cell.IsUnused()) {
return {"", '.'};
} else {
const auto width_info = absl::StrCat("\t(", cell.left_border_width,
"+", cell.compact_width, ")\t");
if (cell.IsComposite())
return {absl::StrCat("/", width_info, "\\"), '`'};
std::ostringstream label;
label << "\t" << cell << "\t\n" << width_info;
return {label.str(), ' '};
}
});
return stream;
}
std::ostream& operator<<(std::ostream& stream,
const VectorTree<AlignedColumnConfiguration>& tree) {
ColumnsTreeFormatter<AlignedColumnConfiguration>(
stream, tree, [](const VectorTree<AlignedColumnConfiguration>& node) {
const auto& cell = node.Value();
const auto label =
absl::StrCat("\t", cell.left_border, "+", cell.width, "\t");
return std::pair<std::string, char>{label, ' '};
});
return stream;
}
struct AlignmentRowData {
// Range of format tokens whose space is to be adjusted for alignment.
FormatTokenRange ftoken_range;
// Set of cells found that correspond to an ordered, sparse set of columns
// to be aligned with other rows.
ColumnPositionTree sparse_columns;
};
static void FillAlignmentRow(
const AlignmentRowData& row_data,
const std::map<SyntaxTreePath, ColumnsTreePath>& columns_map,
AlignmentRow* row) {
const auto& sparse_columns(row_data.sparse_columns);
FormatTokenRange remaining_tokens_range(row_data.ftoken_range);
FormatTokenRange* prev_cell_tokens = nullptr;
if (!is_leaf(sparse_columns)) {
for (const auto& col : VectorTreeLeavesTraversal(sparse_columns)) {
const auto column_loc_iter = columns_map.find(col.Value().path);
CHECK(column_loc_iter != columns_map.end());
const auto token_iter = std::find_if(
remaining_tokens_range.begin(), remaining_tokens_range.end(),
[=](const PreFormatToken& ftoken) {
return BoundsEqual(ftoken.Text(),
col.Value().starting_token.text());
});
CHECK(token_iter != remaining_tokens_range.end());
remaining_tokens_range.set_begin(token_iter);
if (prev_cell_tokens != nullptr) prev_cell_tokens->set_end(token_iter);
AlignmentRow& row_cell = verible::DescendPath(
*row, column_loc_iter->second.begin(), column_loc_iter->second.end());
row_cell.Value().tokens = remaining_tokens_range;
prev_cell_tokens = &row_cell.Value().tokens;
}
}
}
// Recursively calculates widths of each cell's subcells and, if needed, updates
// cell's width to fit all subcells.
static void UpdateAndPropagateRowCellWidths(AlignmentRow* node) {
node->Value().UpdateWidths();
if (is_leaf(*node)) return;
int total_width = 0;
for (auto& child : node->Children()) {
UpdateAndPropagateRowCellWidths(&child);
total_width += child.Value().TotalWidth();
}
if (node->Value().tokens.empty()) {
node->Value().left_border_width =
node->Children().front().Value().left_border_width;
node->Value().compact_width = total_width - node->Value().left_border_width;
}
}
static void ComputeRowCellWidths(AlignmentRow* row) {
VLOG(2) << __FUNCTION__;
UpdateAndPropagateRowCellWidths(row);
// Force leftmost table border to be 0 because these cells start new lines
// and thus should not factor into alignment calculation.
// Note: this is different from how StateNode calculates column positions.
auto* front = row;
while (!front->Children().empty()) {
front = &front->Children().front();
front->Value().left_border_width = 0;
}
VLOG(2) << "end of " << __FUNCTION__;
}
using AlignedFormattingColumnSchema = VectorTree<AlignedColumnConfiguration>;
static AlignedFormattingColumnSchema ComputeColumnWidths(
const AlignmentMatrix& matrix,
const VectorTree<AlignmentColumnProperties>& column_properties) {
VLOG(2) << __FUNCTION__;
AlignedFormattingColumnSchema column_configs =
Transform<AlignedFormattingColumnSchema>(
matrix.front(),
[](const AlignmentRow&) { return AlignedColumnConfiguration{}; });
// Check which cell before delimiter is the longest
// If this cell is in the last row, the sizes of column with delimiter
// must be set to 0
int longest_cell_before_delimiter = 0;
bool align_to_last_row = false;
for (const AlignmentRow& row : matrix) {
auto column_prop_iter =
VectorTreePreOrderTraversal(column_properties).begin();
const auto column_prop_end =
VectorTreePreOrderTraversal(column_properties).end();
for (const auto& node : VectorTreePreOrderTraversal(row)) {
const auto next_prop = std::next(column_prop_iter, 1);
if (next_prop != column_prop_end &&
next_prop->Value().contains_delimiter) {
if (longest_cell_before_delimiter < node.Value().TotalWidth()) {
longest_cell_before_delimiter = node.Value().TotalWidth();
if (&row == &matrix.back()) align_to_last_row = true;
}
break;
}
++column_prop_iter;
}
}
for (const AlignmentRow& row : matrix) {
auto column_iter = VectorTreePreOrderTraversal(column_configs).begin();
auto column_prop_iter =
VectorTreePreOrderTraversal(column_properties).begin();
for (const auto& node : VectorTreePreOrderTraversal(row)) {
if (column_prop_iter->Value().contains_delimiter && align_to_last_row) {
column_iter->Value().width = 0;
column_iter->Value().left_border = 0;
} else {
column_iter->Value().UpdateFromCell(node.Value());
if (column_prop_iter->Value().left_border_override !=
verible::AlignmentColumnProperties::kNoBorderOverride) {
column_iter->Value().left_border =
column_prop_iter->Value().left_border_override;
}
}
++column_iter;
++column_prop_iter;
}
}
// Make sure columns are wide enough to fit all their subcolumns
for (auto& column_iter : VectorTreePostOrderTraversal(column_configs)) {
if (!is_leaf(column_iter)) {
int children_width = std::accumulate(
column_iter.Children().begin(), column_iter.Children().end(), 0,
[](int width, const AlignedFormattingColumnSchema& node) {
return width + node.Value().TotalWidth();
});
column_iter.Value().left_border =
std::max(column_iter.Value().left_border,
column_iter.Children().front().Value().left_border);
column_iter.Value().width =
std::max(column_iter.Value().width,
children_width - column_iter.Value().left_border);
}
}
VLOG(2) << "end of " << __FUNCTION__;
return column_configs;
}
// Saved spacing mutation so that it can be examined before applying.
// There is one of these for every format token that immediately follows an
// alignment column boundary.
struct DeferredTokenAlignment {
// Points to the token to be modified.
FormatTokenRange::iterator ftoken;
// This is the spacing that would produce aligned formatting.
int new_before_spacing;
DeferredTokenAlignment(FormatTokenRange::iterator t, int spaces)
: ftoken(t), new_before_spacing(spaces) {}
// This value reflects an edit-distance (number of spaces) between aligned and
// flushed-left formatting.
int AlignVsFlushLeftSpacingDifference() const {
return new_before_spacing - ftoken->before.spaces_required;
}
};
static void ComputeAlignedRowCellSpacings(
const VectorTree<verible::AlignedColumnConfiguration>& column_configs,
const VectorTree<verible::AlignmentColumnProperties>& properties,
const AlignmentRow& row, std::vector<DeferredTokenAlignment>* align_actions,
int* accrued_spaces) {
ColumnsTreePath node_path;
verible::Path(row, node_path);
VLOG(2) << TreePathFormatter(node_path) << " " << __FUNCTION__ << std::endl;
if (row.Children().empty()) return;
auto column_config_it = column_configs.Children().begin();
auto column_properties_it = properties.Children().begin();
for (const auto& cell : row.Children()) {
node_path.clear();
verible::Path(cell, node_path);
if (cell.Value().IsUnused()) {
const int total_width = column_config_it->Value().left_border +
column_config_it->Value().width;
VLOG(2) << TreePathFormatter(node_path)
<< " unused cell; width: " << total_width;
*accrued_spaces += total_width;
} else if (cell.Value().IsComposite()) {
// Cummulative subcolumns width might be smaller than their parent
// column's width.
const int subcolumns_width = std::accumulate(
column_config_it->Children().begin(),
column_config_it->Children().end(), 0,
[](int width, const VectorTree<AlignedColumnConfiguration>& node) {
return width + node.Value().TotalWidth();
});
const int padding =
column_config_it->Value().TotalWidth() - subcolumns_width;
VLOG(2) << TreePathFormatter(node_path) << " composite cell"
<< "; padding: " << padding << "; flush: "
<< (column_properties_it->Value().flush_left ? "left" : "right");
if (!column_properties_it->Value().flush_left) *accrued_spaces += padding;
ComputeAlignedRowCellSpacings(*column_config_it, *column_properties_it,
cell, align_actions, accrued_spaces);
if (column_properties_it->Value().flush_left) *accrued_spaces += padding;
} else {
*accrued_spaces += column_config_it->Value().left_border;
VLOG(2) << TreePathFormatter(node_path) << " token cell"
<< "; starting token: " << cell.Value().tokens.front().Text();
// Align by setting the left-spacing based on sum of cell widths
// before this one.
const int padding =
column_config_it->Value().width - cell.Value().compact_width;
FormatTokenRange::iterator ftoken = cell.Value().tokens.begin();
int left_spacing;
if (column_properties_it->Value().flush_left) {
if (column_properties_it->Value().contains_delimiter) {
left_spacing = 0;
*accrued_spaces += padding;
} else {
left_spacing = *accrued_spaces;
*accrued_spaces = padding;
}
} else { // flush right
left_spacing = *accrued_spaces + padding;
*accrued_spaces = 0;
}
align_actions->emplace_back(ftoken, left_spacing);
VLOG(2) << TreePathFormatter(node_path)
<< " ... left_spacing: " << left_spacing;
}
++column_config_it;
++column_properties_it;
}
}
// Align cells by adjusting pre-token spacing for a single row.
static std::vector<DeferredTokenAlignment> ComputeAlignedRowSpacings(
const VectorTree<verible::AlignedColumnConfiguration>& column_configs,
const VectorTree<verible::AlignmentColumnProperties>& properties,
const AlignmentRow& row) {
VLOG(2) << __FUNCTION__ << "; row:\n" << row;
std::vector<DeferredTokenAlignment> align_actions;
int accrued_spaces = 0;
ComputeAlignedRowCellSpacings(column_configs, properties, row, &align_actions,
&accrued_spaces);
VLOG(2) << "end of " << __FUNCTION__;
return align_actions;
}
static const AlignmentRow* RightmostSubcolumnWithTokens(
const AlignmentRow& node) {
if (!node.Value().tokens.empty()) return &node;
for (const auto& child : reversed_view(node.Children())) {
if (child.Value().TotalWidth() > 0) {
return RightmostSubcolumnWithTokens(child);
}
}
return nullptr;
}
static FormatTokenRange EpilogRange(const TokenPartitionTree& partition,
const AlignmentRow& last_subcol) {
// Identify the unaligned epilog tokens of this 'partition', i.e. those not
// spanned by 'row'.
auto partition_end = partition.Value().TokensRange().end();
auto row_end = last_subcol.Value().tokens.end();
return FormatTokenRange(row_end, partition_end);
}
// This width calculation accounts for the unaligned tokens in the tail position
// of each aligned row (e.g. unaligned trailing comments).
static bool AlignedRowsFitUnderColumnLimit(
const std::vector<TokenPartitionIterator>& rows,
const AlignmentMatrix& matrix, int total_column_width, int column_limit) {
auto partition_iter = rows.begin();
for (const auto& row : matrix) {
const auto* rightmost_subcolumn = RightmostSubcolumnWithTokens(row);
if (rightmost_subcolumn) {
// Identify the unaligned epilog text on each partition.
const FormatTokenRange epilog_range(
EpilogRange(**partition_iter, *rightmost_subcolumn));
const int aligned_partition_width =
total_column_width + EffectiveCellWidth(epilog_range);
if (aligned_partition_width > column_limit) {
VLOG(1) << "Total aligned partition width " << aligned_partition_width
<< " exceeds limit " << column_limit
<< ", so not aligning this group.";
return false;
}
}
++partition_iter;
}
return true;
}
// Holds alignment calculations for an alignable group of token partitions.
struct AlignablePartitionGroup::GroupAlignmentData {
// Contains alignment calculations.
AlignmentMatrix matrix;
// If this is empty, don't do any alignment.
std::vector<std::vector<DeferredTokenAlignment>> align_actions_2D;
int MaxAbsoluteAlignVsFlushLeftSpacingDifference() const {
int result = std::numeric_limits<int>::min();
for (const auto& align_actions : align_actions_2D) {
for (const auto& action : align_actions) {
int abs_diff = std::abs(action.AlignVsFlushLeftSpacingDifference());
result = std::max(abs_diff, result);
}
}
return result;
}
AlignmentPolicy InferUserIntendedAlignmentPolicy(
const TokenPartitionRange& partitions) const;
};
AlignablePartitionGroup::GroupAlignmentData
AlignablePartitionGroup::CalculateAlignmentSpacings(
const std::vector<TokenPartitionIterator>& rows,
const AlignmentCellScannerFunction& cell_scanner_gen, int column_limit) {
VLOG(1) << __FUNCTION__;
GroupAlignmentData result;
// Alignment requires 2+ rows.
if (rows.size() <= 1) return result;
// Rows validation:
// In many (but not all) cases, all rows' nodes have the same type.
// TODO(fangism): plumb through an optional verification function.
VLOG(2) << "Walking syntax subtrees for each row";
ColumnSchemaAggregator column_schema;
std::vector<AlignmentRowData> alignment_row_data;
alignment_row_data.reserve(rows.size());
// Simultaneously step through each node's tree, adding a column to the
// schema if *any* row wants it. This captures optional and repeated
// constructs.
for (const auto& row : rows) {
// Each row should correspond to an individual list element
const UnwrappedLine& unwrapped_line = row->Value();
// Scan each token-range for cell boundaries based on syntax,
// and establish partial ordering based on syntax tree paths.
auto sparse_columns = cell_scanner_gen(*row);
// Make sure columns are properly ordered.
std::sort(
sparse_columns.Children().begin(), sparse_columns.Children().end(),
[](const auto& a, const auto& b) {
return CompareSyntaxTreePath(a.Value().path, b.Value().path) < 0;
});
const AlignmentRowData row_data{
// Extract the range of format tokens whose spacings should be adjusted.
unwrapped_line.TokensRange(), std::move(sparse_columns)};
alignment_row_data.emplace_back(row_data);
VLOG(2) << "Row sparse columns:\n" << row_data.sparse_columns;
// Aggregate union of all column keys (syntax tree paths).
column_schema.Collect(row_data.sparse_columns);
}
VLOG(2) << "Generating column schema from collected row data";
column_schema.Finalize();
VLOG(2) << "Column schema:\n" << column_schema.Columns();
// Populate a matrix of cells, where cells span token ranges.
// Null cells (due to optional constructs) are represented by empty ranges,
// effectively width 0.
VLOG(2) << "Filling dense matrix from sparse representation";
result.matrix.resize(rows.size());
{
auto row_data_iter = alignment_row_data.cbegin();
for (auto& row : result.matrix) {
VLOG(3) << "Row tokens: "
<< StringSpanOfTokenRange(
FormatTokenRange(row_data_iter->ftoken_range.begin(),
row_data_iter->ftoken_range.end()));
row = Transform<AlignmentRow>(column_schema.Columns(),
[](const VectorTree<AggregateColumnData>&) {
return AlignmentCell{};
});
FillAlignmentRow(*row_data_iter, column_schema.SyntaxToColumnsMap(),
&row);
ComputeRowCellWidths(&row);
VLOG(2) << "Filled row:\n" << row;
++row_data_iter;
}
}
// Extract other non-computed column properties.
const auto column_properties = column_schema.ColumnProperties();
// Compute max widths per column.
VectorTree<AlignedColumnConfiguration> column_configs(
ComputeColumnWidths(result.matrix, column_properties));
VLOG(2) << "Column widths:\n" << column_configs;
{
// Total width does not include initial left-indentation.
// Assume indentation is the same for all partitions in each group.
const int indentation = rows.front()->Value().IndentationSpaces();
const int total_column_width =
indentation + column_configs.Value().TotalWidth();
VLOG(2) << "Total (aligned) column width = " << total_column_width;
// if the aligned columns would exceed the column limit, then refuse to
// align for now. However, this check alone does not include text that
// follows the last aligned column, like trailing comments and EOL comments.
if (total_column_width > column_limit) {
VLOG(1) << "Total aligned column width " << total_column_width
<< " exceeds limit " << column_limit
<< ", so not aligning this group.";
return result;
}
// Also check for length of unaligned trailing tokens.
if (!AlignedRowsFitUnderColumnLimit(rows, result.matrix, total_column_width,
column_limit))
return result;
}
// TODO(fangism): implement overflow mitigation fallback strategies.
// At this point, the proposed alignment/padding 'fits'.
// Compute pre-token spacings of each row to align to the column configs.
// Store the mutation set in a 2D structure that reflects the original token
// partitions and alignment matrix representation.
result.align_actions_2D.reserve(result.matrix.size());
for (const auto& row : result.matrix) {
result.align_actions_2D.push_back(
ComputeAlignedRowSpacings(column_configs, column_properties, row));
}
return result;
}
// This applies pre-calculated alignment spacings to aligned groups of format
// tokens.
void AlignablePartitionGroup::ApplyAlignment(
const GroupAlignmentData& align_data) const {
auto row = alignable_rows_.begin();
for (const auto& align_actions : align_data.align_actions_2D) {
(*row)->Children().clear();
VLOG(3) << __FUNCTION__ << " processing row: " << **row;
if (!align_actions.empty()) {
auto& node = **row;
auto& line = node.Value();
auto ftokens = line.TokensRange();
line.SetPartitionPolicy(PartitionPolicyEnum::kAlreadyFormatted);
verible::TokenPartitionTree* current_cell = nullptr;
if (align_actions.front().ftoken != ftokens.begin()) {
node.Children().emplace_back(
UnwrappedLine(0, ftokens.begin(), PartitionPolicyEnum::kInline));
current_cell = &node.Children().back();
}