Skip to content

Commit c97d479

Browse files
committed
fix AlignDeclarationsRule for complex table types (#129)
1 parent 0e4de67 commit c97d479

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/rules/alignment/AlignDeclarationsRule.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,22 @@ private Token readDeclarationLine(AlignLine line, Token token, int additionalInd
563563
Token typeEnd = token;
564564
while (typeEnd != null && !typeEnd.isAnyKeyword("LENGTH", "DECIMALS", "VALUE", "READ-ONLY") && !typeEnd.textEqualsAny(".", ",")) {
565565
// do not align table declarations with "WITH ... KEY ..." sections, because they usually should not be put on a single line;
566-
// however, do accept the short cases of "WITH EMPTY KEY" and "WITH [UNIQUE | NON-UNIQUE] DEFAULT KEY" and "WITH [UNIQUE | NON-UNIQUE] KEY comp1"
567-
if (typeEnd.isKeyword("WITH")
566+
// however, do accept the short cases of "WITH EMPTY KEY" and "WITH [UNIQUE | NON-UNIQUE] DEFAULT KEY" and "WITH [UNIQUE | NON-UNIQUE] KEY comp1 [comp2 [comp3]]"
567+
if (typeEnd.isKeyword("ASSOCIATION")
568+
|| typeEnd.isKeyword("WITH")
568569
&& !typeEnd.matchesOnSiblings(true, "WITH", "EMPTY", "KEY")
569570
&& !typeEnd.matchesOnSiblings(true, "WITH", TokenSearch.makeOptional("UNIQUE|NON-UNIQUE"), "DEFAULT", "KEY")
570571
&& !typeEnd.matchesOnSiblings(true, "WITH", TokenSearch.makeOptional("UNIQUE|NON-UNIQUE"), "KEY", TokenSearch.ANY_IDENTIFIER, ",|.")) {
571-
return null;
572+
573+
// for more complex cases (with multiple components, multiple WITH key definitions, or ASSOCIATIONs)
574+
// only align up to (but excluding) "WITH" or "ASSOCIATION", thus leaving the WITH or ASSOCIATION section(s) unchanged
575+
// and keeping possible line breaks as well as manual alignment
576+
Term typeInfo = Term.createForTokenRange(typeStart, typeEnd.getPrev());
577+
line.setCell(Columns.TYPE.getValue(), new AlignCellTerm(typeInfo));
578+
return typeEnd.getLastTokenOnSiblings(true, TokenSearch.ASTERISK, ".|,");
572579
}
573580
typeEnd = typeEnd.getNextSibling();
574581
}
575-
if (typeEnd == null)
576-
return null;
577582
Term typeInfo = Term.createForTokenRange(typeStart, typeEnd.getPrev());
578583
line.setCell(Columns.TYPE.getValue(), new AlignCellTerm(typeInfo));
579584
token = typeEnd;

test/com.sap.adt.abapcleaner.test/src/com/sap/adt/abapcleaner/rules/alignment/AlignDeclarationsTest.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -681,17 +681,47 @@ void testDataBeginOfNested() {
681681
}
682682

683683
@Test
684-
void testTableDeclarationWithKey() {
685-
// ensure that table declarations with "WITH ... KEY ..." sections are not aligned (except for very simply cases
686-
// with just one component), because they usually should not be put on a single line
684+
void testTableDeclarationWithKeyOf3Components() {
685+
// ensure that in table declarations with more than one component in the "WITH" section,
686+
// the "WITH ..." part is kept unchanged, but everything else is aligned
687687

688688
buildSrc(" DATA:");
689689
buildSrc(" lt_item TYPE ty_tt_item,");
690690
buildSrc(" lts_buffer TYPE SORTED TABLE OF ty_s_buffer");
691691
buildSrc(" WITH NON-UNIQUE KEY comp1 comp2 comp3,");
692692
buildSrc(" lv_index TYPE i.");
693693

694-
copyExpFromSrc();
694+
buildExp(" DATA:");
695+
buildExp(" lt_item TYPE ty_tt_item,");
696+
buildExp(" lts_buffer TYPE SORTED TABLE OF ty_s_buffer");
697+
buildExp(" WITH NON-UNIQUE KEY comp1 comp2 comp3,");
698+
buildExp(" lv_index TYPE i.");
699+
700+
putAnyMethodAroundSrcAndExp();
701+
702+
testRule();
703+
}
704+
705+
@Test
706+
void testTableDeclarationWithMultiKey() {
707+
// ensure that in table declarations with multiple "WITH ... KEY ..." sections, the "WITH ..." part is kept unchanged,
708+
// but everything else is aligned
709+
710+
buildSrc(" DATA:");
711+
buildSrc(" lt_item TYPE ty_tt_item,");
712+
buildSrc(" lts_buffer TYPE SORTED TABLE OF ty_s_buffer");
713+
buildSrc(" WITH NON-UNIQUE KEY comp1 comp2 comp3");
714+
buildSrc(" WITH UNIQUE KEY key_name COMPONENTS comp1 comp2");
715+
buildSrc(" comp3 comp4,");
716+
buildSrc(" lv_index TYPE i.");
717+
718+
buildExp(" DATA:");
719+
buildExp(" lt_item TYPE ty_tt_item,");
720+
buildExp(" lts_buffer TYPE SORTED TABLE OF ty_s_buffer");
721+
buildExp(" WITH NON-UNIQUE KEY comp1 comp2 comp3");
722+
buildExp(" WITH UNIQUE KEY key_name COMPONENTS comp1 comp2");
723+
buildExp(" comp3 comp4,");
724+
buildExp(" lv_index TYPE i.");
695725

696726
putAnyMethodAroundSrcAndExp();
697727

@@ -1556,4 +1586,5 @@ void testMoveOverlengthValuesBelowNameOrTypeAlignNameOnly() {
15561586

15571587
testRule();
15581588
}
1589+
15591590
}

0 commit comments

Comments
 (0)