Reduce Alignment handler struct to a single function. TabluarAlignTokens() interface is now thinner and more flexible in allowing the callers to return AlignablePartitionGroups that can each have their own column extraction function. ExtractAlignmentGroupsAdapter() still works for cases where one wishes to only handle one type of subrange aligment per syntax-tree node type. PiperOrigin-RevId: 331872744
diff --git a/common/formatting/align.cc b/common/formatting/align.cc index 8ad8f2a..816bcb5 100644 --- a/common/formatting/align.cc +++ b/common/formatting/align.cc
@@ -815,12 +815,12 @@ return AlignmentPolicy::kPreserve; } -void TabularAlignTokens(TokenPartitionTree* partition_ptr, - const AlignedFormattingHandler& alignment_handler, - std::vector<PreFormatToken>* ftokens, - absl::string_view full_text, - const ByteOffsetSet& disabled_byte_ranges, - AlignmentPolicy policy, int column_limit) { +void TabularAlignTokens( + TokenPartitionTree* partition_ptr, + const ExtractAlignmentGroupsFunction& extract_alignment_groups, + std::vector<PreFormatToken>* ftokens, absl::string_view full_text, + const ByteOffsetSet& disabled_byte_ranges, AlignmentPolicy policy, + int column_limit) { VLOG(1) << __FUNCTION__; // Each subpartition is presumed to correspond to a list element or // possibly some other ignored element like comments. @@ -833,7 +833,7 @@ if (subpartitions_range.empty()) return; VLOG(1) << "extracting alignment partition groups..."; const std::vector<AlignablePartitionGroup> alignment_groups( - alignment_handler.extract_alignment_groups(subpartitions_range)); + extract_alignment_groups(subpartitions_range)); for (const auto& alignment_group : alignment_groups) { const TokenPartitionRange partition_range( alignment_group.alignable_rows.front(),
diff --git a/common/formatting/align.h b/common/formatting/align.h index 4558cfa..6521aee 100644 --- a/common/formatting/align.h +++ b/common/formatting/align.h
@@ -232,38 +232,27 @@ std::string AbslUnparseFlag(const AlignmentPolicy& policy); -// This struct bundles together the various functions needed for aligned -// formatting. -// TODO(fangism): Support heterogeneous sub-range alignment. -// The current structure limits each node-type's handler to have only -// one aligner for all of its sub-ranges. -struct AlignedFormattingHandler { - // This function subdivides a range of token partitions (e.g. all of the - // children of a parent partition of interest) into groups of lines that will - // align with each other. - ExtractAlignmentGroupsFunction extract_alignment_groups; -}; - // This aligns sections of text by modifying the spacing between tokens. // 'partition_ptr' is a partition that can span one or more sections of // code to align. The partitions themselves are not reshaped, however, // the inter-token spacing of tokens spanned by these partitions can be // modified. -// Currently, alignment groups are separated at partition boundaries -// that span one or more blank lines (hard-coded for now). +// 'extract_alignment_groups' is a function that returns groups of token +// partitions to align along with their column extraction functions. +// (See AlignablePartitionGroup.) // +// How it works: // Let a 'line' be a unit of text to be aligned. // Groups of lines are aligned together, as if their contents were table cells. // Vertical alignment is achieved by sizing each column in the table to // the max cell width in each column, and padding spaces as necessary. // -// See description of AlignedFormattingHandler for a description of each -// function needed for aligned formatting. -// // Other parameters: // 'full_text' is the string_view buffer of whole text being formatted, not just // the text spanned by 'partition_ptr'. // 'ftokens' points to the array of PreFormatTokens that spans 'full_text'. +// 'disabled_byte_ranges' contains information about which ranges of text +// are to preserve their original spacing (no-formatting). // 'policy' allows selective enabling/disabling of alignment. // 'column_limit' is the column width beyond which the aligner should fallback // to a safer action, e.g. refusing to align and leaving spacing untouched. @@ -285,12 +274,12 @@ // aaa bb [11] [22] // ccc[33] dd [444] // -void TabularAlignTokens(TokenPartitionTree* partition_ptr, - const AlignedFormattingHandler& alignment_handler, - std::vector<PreFormatToken>* ftokens, - absl::string_view full_text, - const ByteOffsetSet& disabled_byte_ranges, - AlignmentPolicy policy, int column_limit); +void TabularAlignTokens( + TokenPartitionTree* partition_ptr, + const ExtractAlignmentGroupsFunction& extract_alignment_groups, + std::vector<PreFormatToken>* ftokens, absl::string_view full_text, + const ByteOffsetSet& disabled_byte_ranges, AlignmentPolicy policy, + int column_limit); } // namespace verible
diff --git a/common/formatting/align_test.cc b/common/formatting/align_test.cc index dc86054..501ebea 100644 --- a/common/formatting/align_test.cc +++ b/common/formatting/align_test.cc
@@ -108,11 +108,10 @@ static bool IgnoreNone(const TokenPartitionTree&) { return false; } -static const AlignedFormattingHandler kDefaultAlignmentHandler{ - .extract_alignment_groups = ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kDefaultAlignmentHandler = + ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreNone, - AlignmentCellScannerGenerator<TokenColumnizer>()), -}; + AlignmentCellScannerGenerator<TokenColumnizer>()); TEST_F(TabularAlignTokenTest, EmptyPartitionRange) { const auto begin = pre_format_tokens_.begin(); @@ -280,11 +279,10 @@ "five six\n"); } -static const AlignedFormattingHandler kFlushRightAlignmentHandler{ - .extract_alignment_groups = ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kFlushRightAlignmentHandler = + ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreNone, - AlignmentCellScannerGenerator<TokenColumnizerRightFlushed>()), -}; + AlignmentCellScannerGenerator<TokenColumnizerRightFlushed>()); TEST_F(Sparse3x3MatrixAlignmentTest, RightFlushed) { // Require 1 space between tokens. @@ -337,11 +335,9 @@ return partition.Value().TokensRange().front().Text() == "three"; }; - const AlignedFormattingHandler handler{ - .extract_alignment_groups = ExtractAlignmentGroupsAdapter( - &verible::GetSubpartitionsBetweenBlankLines, ignore_threes, - AlignmentCellScannerGenerator<TokenColumnizer>()), - }; + const ExtractAlignmentGroupsFunction handler = ExtractAlignmentGroupsAdapter( + &verible::GetSubpartitionsBetweenBlankLines, ignore_threes, + AlignmentCellScannerGenerator<TokenColumnizer>()); TabularAlignTokens(&partition_, handler, &pre_format_tokens_, sample_, ByteOffsetSet(), AlignmentPolicy::kAlign, 40);
diff --git a/verilog/formatting/align.cc b/verilog/formatting/align.cc index 84325ef..08d7781 100644 --- a/verilog/formatting/align.cc +++ b/verilog/formatting/align.cc
@@ -41,7 +41,6 @@ namespace verilog { namespace formatter { -using verible::AlignablePartitionGroup; using verible::AlignmentCellScannerGenerator; using verible::AlignmentColumnProperties; using verible::AlignmentGroupAction; @@ -49,6 +48,7 @@ using verible::ByteOffsetSet; using verible::ColumnSchemaScanner; using verible::down_cast; +using verible::ExtractAlignmentGroupsFunction; using verible::FormatTokenRange; using verible::MutableFormatTokenRange; using verible::PreFormatToken; @@ -778,60 +778,53 @@ bool previous_token_was_case_colon_ = false; }; -static const verible::AlignedFormattingHandler kPortDeclarationAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kPortDeclarationAligner = + verible::ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreWithinPortDeclarationPartitionGroup, - AlignmentCellScannerGenerator<PortDeclarationColumnSchemaScanner>()), -}; + AlignmentCellScannerGenerator<PortDeclarationColumnSchemaScanner>()); -static const verible::AlignedFormattingHandler kActualNamedParameterAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kActualNamedParameterAligner = + verible::ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreWithinActualNamedParameterPartitionGroup, AlignmentCellScannerGenerator< - ActualNamedParameterColumnSchemaScanner>()), -}; + ActualNamedParameterColumnSchemaScanner>()); -static const verible::AlignedFormattingHandler kActualNamedPortAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kActualNamedPortAligner = + verible::ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreWithinActualNamedPortPartitionGroup, - AlignmentCellScannerGenerator<ActualNamedPortColumnSchemaScanner>()), -}; + AlignmentCellScannerGenerator<ActualNamedPortColumnSchemaScanner>()); -static const verible::AlignedFormattingHandler kDataDeclarationAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kDataDeclarationAligner = + verible::ExtractAlignmentGroupsAdapter( &GetConsecutiveDataDeclarationGroups, &IgnoreCommentsAndPreprocessingDirectives, - AlignmentCellScannerGenerator<DataDeclarationColumnSchemaScanner>()), -}; + AlignmentCellScannerGenerator<DataDeclarationColumnSchemaScanner>()); -static const verible::AlignedFormattingHandler kClassPropertyAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kClassPropertyAligner = + verible::ExtractAlignmentGroupsAdapter( &GetConsecutiveDataDeclarationGroups, &IgnoreCommentsAndPreprocessingDirectives, - AlignmentCellScannerGenerator<ClassPropertyColumnSchemaScanner>()), -}; + AlignmentCellScannerGenerator<ClassPropertyColumnSchemaScanner>()); -static const verible::AlignedFormattingHandler kCaseItemAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kCaseItemAligner = + verible::ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreMultilineCaseStatements, - AlignmentCellScannerGenerator<CaseItemColumnSchemaScanner>()), -}; + AlignmentCellScannerGenerator<CaseItemColumnSchemaScanner>()); -static const verible::AlignedFormattingHandler kParameterDeclarationAligner{ - .extract_alignment_groups = verible::ExtractAlignmentGroupsAdapter( +static const ExtractAlignmentGroupsFunction kParameterDeclarationAligner = + verible::ExtractAlignmentGroupsAdapter( &verible::GetSubpartitionsBetweenBlankLines, &IgnoreWithinPortDeclarationPartitionGroup, AlignmentCellScannerGenerator< - ParameterDeclarationColumnSchemaScanner>()), -}; + ParameterDeclarationColumnSchemaScanner>()); struct AlignedFormattingConfiguration { // Set of functions for driving specific code aligners. - verible::AlignedFormattingHandler handler; + ExtractAlignmentGroupsFunction handler; // This function extracts a specific alignment policy from the // Verilog-specific style structure.