Skip to content

Commit 128830e

Browse files
committed
add new rule "Align PERFORM parameters" (#83)
1 parent 3962fb4 commit 128830e

File tree

10 files changed

+706
-36
lines changed

10 files changed

+706
-36
lines changed

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/parser/Term.java

+1-20
Original file line numberDiff line numberDiff line change
@@ -414,25 +414,6 @@ public boolean contains(Token searchToken) {
414414
}
415415

416416
public boolean condense() {
417-
if (firstToken == lastToken)
418-
return false;
419-
420-
boolean changed = false;
421-
422-
Token token = firstToken.getNext();
423-
while (token != null) {
424-
if (token.getPrev() != null && token.getPrev().isComment()) {
425-
// skip this case
426-
} else if (token.isAttached()) {
427-
// skip attached Tokens, too
428-
} else if (token.setWhitespace()) {
429-
changed = true;
430-
}
431-
432-
if (token == lastToken)
433-
break;
434-
token = token.getNext();
435-
}
436-
return changed;
417+
return firstToken.condenseUpTo(lastToken, ABAP.MAX_LINE_LENGTH, firstToken.getStartIndexInLine());
437418
}
438419
}

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/parser/Token.java

+33
Original file line numberDiff line numberDiff line change
@@ -2586,4 +2586,37 @@ public boolean isSqlLiteralType() {
25862586
public boolean isSqlTypeInCast() {
25872587
return textEqualsAny(ABAP.abapSqlLiteralTypes) && parent != null && parent.textEquals("CAST(") && getPrevCodeSibling() != null && getPrevCodeSibling().isKeyword("AS");
25882588
}
2589+
2590+
public boolean condenseUpTo(Token last, int maxLineLength, int indent) {
2591+
Token token = this;
2592+
if (token == last)
2593+
return false;
2594+
2595+
boolean changed = false;
2596+
int indexInLine = token.getEndIndexInLine();
2597+
token = token.getNext();
2598+
while (token != null) {
2599+
// if needed, move Token to the next line, otherwise directly behind the previous Token
2600+
int spacesLeft = (token.isAttached() || token.isCommaOrPeriod() || token.isChainColon()) ? 0 : 1;
2601+
if (token.isAsteriskCommentLine()) {
2602+
// do nothing
2603+
} else if (token.getPrev().isComment() || indexInLine + spacesLeft + token.getTextLength() > maxLineLength) {
2604+
if (token.setWhitespace(1, indent)) {
2605+
changed = true;
2606+
}
2607+
indexInLine = indent + token.getTextLength();
2608+
} else {
2609+
if (token.setWhitespace(0, spacesLeft)) {
2610+
changed = true;
2611+
}
2612+
indexInLine += spacesLeft + token.getTextLength();
2613+
}
2614+
2615+
if (token == last) {
2616+
break;
2617+
}
2618+
token = token.getNext();
2619+
}
2620+
return changed;
2621+
}
25892622
}

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/rulebase/Rule.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.*;
2121

2222
public abstract class Rule {
23-
public static final int RULE_COUNT = 63;
23+
public static final int RULE_COUNT = 64;
2424
public static final int RULE_GROUP_COUNT = 7;
2525

2626
protected static final String LINE_SEP = ABAP.LINE_SEPARATOR;
@@ -132,7 +132,8 @@ static Rule[] getAllRules(Profile profile) {
132132
new AlignParametersRule(profile),
133133
new AlignLogicalExpressionsRule(profile),
134134
new AlignCondExpressionsRule(profile),
135-
new AlignFormDeclarationRule(profile)
135+
new AlignFormDeclarationRule(profile),
136+
new AlignPerformRule(profile)
136137
};
137138

138139
StringBuilder errors = new StringBuilder();

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/rulebase/RuleID.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ public enum RuleID {
9292
ALIGN_PARAMETERS,
9393
ALIGN_LOGICAL_EXPRESSIONS,
9494
ALIGN_COND_EXPRESSIONS,
95-
ALIGN_FORM_DECLARATION;
95+
ALIGN_FORM_DECLARATION,
96+
ALIGN_PERFORM;
9697

9798
public static final int SIZE = java.lang.Integer.SIZE;
9899

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/rulehelpers/AlignTable.java

+9
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,15 @@ public void removeAllLinesOfParent(Token parentToken) {
323323
}
324324
}
325325

326+
public void removeAllLinesWithOutCellIn(int columnIndex) {
327+
for (int i = lines.size() - 1; i >= 0; --i) {
328+
AlignLine line = lines.get(i);
329+
if (line.getCell(columnIndex) == null) {
330+
removeLineAt(i);
331+
}
332+
}
333+
}
334+
326335
public final void setMaxLineLength(int maxLineLength) {
327336
this.maxLineLength = maxLineLength;
328337
}

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

+13-13
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private enum Columns {
5050
public String getDisplayName() { return "Align FORM declarations"; }
5151

5252
@Override
53-
public String getDescription() { return "Aligns (obsolete) FORM declarations."; }
53+
public String getDescription() { return "Aligns obsolete subroutine declarations with FORM."; }
5454

5555
@Override
5656
public LocalDate getDateCreated() { return LocalDate.of(2023, 10, 31); }
@@ -60,32 +60,32 @@ private enum Columns {
6060

6161
@Override
6262
public String getExample() {
63-
return LINE_SEP + "FORM any_form USING iv_any_value TYPE string."
63+
return LINE_SEP + "FORM any_subroutine USING iv_any_value TYPE string."
6464
+ LINE_SEP
65-
+ LINE_SEP + " \" any FORM implementation"
65+
+ LINE_SEP + " \" any subroutine implementation"
6666
+ LINE_SEP + "ENDFORM."
6767
+ LINE_SEP
6868
+ LINE_SEP
69-
+ LINE_SEP + "FORM other_form USING iv_any_value TYPE i iv_other_value TYPE string CHANGING cv_third_value TYPE i."
70-
+ LINE_SEP + " \" other FORM implementation"
69+
+ LINE_SEP + "FORM other_subroutine USING iv_any_value TYPE i iv_other_value TYPE string CHANGING cv_third_value TYPE i."
70+
+ LINE_SEP + " \" other subroutine implementation"
7171
+ LINE_SEP + "ENDFORM."
7272
+ LINE_SEP
7373
+ LINE_SEP
74-
+ LINE_SEP + "FORM third_form_with_a_long_name TABLES it_any_table STRUCTURE ty_s_any_struc"
74+
+ LINE_SEP + "FORM third_subr_with_a_long_name TABLES it_any_table STRUCTURE ty_s_any_struc"
7575
+ LINE_SEP + " it_other_table TYPE STANDARD TABLE it_third_table it_fourth_table TYPE ty_tt_any"
7676
+ LINE_SEP + " CHANGING ct_table TYPE ty_tt_table cs_struc TYPE LINE OF ty_tt_any cs_other_struc LIKE cs_any"
7777
+ LINE_SEP + " cs_third_struc LIKE LINE OF ct_table."
78-
+ LINE_SEP + " \" third FORM implementation"
78+
+ LINE_SEP + " \" third subroutine implementation"
7979
+ LINE_SEP + "ENDFORM."
8080
+ LINE_SEP
8181
+ LINE_SEP
82-
+ LINE_SEP + "FORM fourth_form"
82+
+ LINE_SEP + "FORM fourth_subroutine"
8383
+ LINE_SEP + " USING"
8484
+ LINE_SEP + " VALUE(iv_any) TYPE string"
8585
+ LINE_SEP + " iv_other TYPE REF TO object"
8686
+ LINE_SEP + " RAISING"
8787
+ LINE_SEP + " cx_any_exception RESUMABLE(cx_other_exception) cx_third_exception."
88-
+ LINE_SEP + " \" fourth FORM implementation"
88+
+ LINE_SEP + " \" fourth subroutine implementation"
8989
+ LINE_SEP + "ENDFORM.";
9090
}
9191

@@ -120,7 +120,7 @@ protected boolean executeOn(Code code, Command command, int releaseRestriction)
120120

121121
// add FORM name
122122
Token formName = formKeyword.getNextCodeSibling();
123-
if (!formName.isIdentifier())
123+
if (!formName.isIdentifier()) // pro forma
124124
return false;
125125
line.setCell(Columns.FORM_NAME.getValue(), new AlignCellToken(formName));
126126

@@ -189,15 +189,14 @@ protected boolean executeOn(Code code, Command command, int releaseRestriction)
189189
if (table.getLineCount() > 1 && line.getCell(Columns.PARAMETER_NAME.getValue()) == null)
190190
table.removeLastLine();
191191

192-
int basicIndent = command.getFirstToken().spacesLeft;
193192
boolean breakAfterFormName = (table.getLineCount() > configParamCountBehindFormName.getValue());
194193
if (breakAfterFormName) {
195194
table.getColumn(Columns.FORM_NAME.getValue()).setForceLineBreakAfter(false);
196-
table.getColumn(Columns.PARAMETER_GROUP.getValue()).setForceIndent(basicIndent + ABAP.INDENT_STEP);
195+
table.getColumn(Columns.PARAMETER_GROUP.getValue()).setForceIndent(ABAP.INDENT_STEP);
197196
}
198197
if (!configContinueAfterParamGroupKeyword.getValue()) {
199198
table.getColumn(Columns.PARAMETER_GROUP.getValue()).setForceLineBreakAfter(false);
200-
int paramGroupIndent = basicIndent + (breakAfterFormName ? ABAP.INDENT_STEP : formKeyword.getTextLength() + 1 + formName.getTextLength() + 1);
199+
int paramGroupIndent = (breakAfterFormName ? ABAP.INDENT_STEP : formKeyword.getTextLength() + 1 + formName.getTextLength() + 1);
201200
table.getColumn(Columns.PARAMETER_NAME.getValue()).setForceIndent(paramGroupIndent + ABAP.INDENT_STEP);
202201
}
203202
if (!configAlignTypes.getValue()) {
@@ -208,6 +207,7 @@ protected boolean executeOn(Code code, Command command, int releaseRestriction)
208207
}
209208
}
210209

210+
int basicIndent = command.getFirstToken().spacesLeft;
211211
int firstLineBreaks = command.getFirstToken().lineBreaks;
212212
Command[] changedCommands = table.align(basicIndent, firstLineBreaks, false, true);
213213
for (Command changedCommand : changedCommands) {

0 commit comments

Comments
 (0)