Skip to content

Commit

Permalink
fix EmptyLinesWithinMethodsRule for function modules (SAP#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgrassau committed Sep 22, 2023
1 parent 5f5ec27 commit 634d823
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class EmptyLinesWithinMethodsRule extends Rule {
@Override
public String getDescription() { return "Restricts the number of consecutive empty lines within methods, and adds an empty line between declarations and the first executable statement (or the comments preceding it)."; }

@Override
public String getHintsAndRestrictions() { return "For function modules, additional empty lines are kept at the beginning to align with ADT and SE37 behavior."; }

@Override
public LocalDate getDateCreated() { return LocalDate.of(2020, 12, 28); }

Expand Down Expand Up @@ -136,6 +139,15 @@ private void executeOn(Code code, Command command, boolean insertLineBreakAbove)
Token token = command.getFirstToken();
if (command.getPrev() != null && (command.getPrev().isMethodFunctionFormOrEventBlockStart() || command.isMethodFunctionOrFormEnd())) {
int maxLineBreak = ((command.isMethodFunctionOrFormEnd()) ? configMaxEmptyLinesAtMethodEnd.getValue() : configMaxEmptyLinesAtMethodStart.getValue()) + 1;

// for function modules with parameters, allow 3 extra lines that are needed for additional auto-generated comment lines in SE37,
// and therefore ensured when saving a function module from ADT; for function modules without parameters, 1 extra line is needed
if (command.getPrev().isFunctionStart()) {
Token functionName = command.getPrev().getFirstCodeToken().getNextCodeSibling();
boolean hasParameters = (functionName != null && !functionName.getNextCodeSibling().isPeriod());
maxLineBreak += hasParameters ? 3 : 1;
}

if (token != null && token.lineBreaks > maxLineBreak) {
token.lineBreaks = maxLineBreak;
code.addRuleUse(this, command, token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,100 @@ void testIncludeType() {

testRule();
}

@Test
void testFunctionModuleWithParametersRemoveLine() {
// ensure that 3 empty lines are kept (but the 4th is removed) in function modules with parameters,
// because these 3 lines are used for auto-generated comments in SE37 and reserved by ADT upon save

buildSrc("FUNCTION any_function");
buildSrc(" IMPORTING");
buildSrc(" VALUE(im_p1) TYPE i");
buildSrc(" EXPORTING");
buildSrc(" ex_p1 TYPE i.");
buildSrc("");
buildSrc("");
buildSrc("");
buildSrc("");
buildSrc(" ex_p1 = 1.");
buildSrc("ENDFUNCTION.");

buildExp("FUNCTION any_function");
buildExp(" IMPORTING");
buildExp(" VALUE(im_p1) TYPE i");
buildExp(" EXPORTING");
buildExp(" ex_p1 TYPE i.");
buildExp("");
buildExp("");
buildExp("");
buildExp(" ex_p1 = 1.");
buildExp("ENDFUNCTION.");

testRule();
}

@Test
void testFunctionModuleWithParametersKeepLines() {
// with 1 empty line configured for method start, ensure that 4 empty lines are kept in function modules with parameters

rule.configMaxEmptyLinesAtMethodStart.setValue(1);

buildSrc("FUNCTION any_function");
buildSrc(" IMPORTING");
buildSrc(" VALUE(im_p1) TYPE i");
buildSrc(" EXPORTING");
buildSrc(" ex_p1 TYPE i.");
buildSrc("");
buildSrc("");
buildSrc("");
buildSrc("");
buildSrc(" ex_p1 = 1.");
buildSrc("ENDFUNCTION.");

copyExpFromSrc();

testRule();
}

@Test
void testFunctionModuleWithoutParametersRemoveLine() {
// ensure that 1 empty line is kept (but further lines removed) in function modules without parameters,
// because this line is used for auto-generated comments in SE37 and reserved by ADT upon save

buildSrc("FUNCTION any_function");
buildSrc(" \" You can use the template 'functionModuleParameter' to add here the signature!");
buildSrc(".");
buildSrc("");
buildSrc("");
buildSrc(" \" any comment");
buildSrc("ENDFUNCTION.");

buildExp("FUNCTION any_function");
buildExp(" \" You can use the template 'functionModuleParameter' to add here the signature!");
buildExp(".");
buildExp("");
buildExp(" \" any comment");
buildExp("ENDFUNCTION.");

testRule();
}

@Test
void testFunctionModuleWithoutParametersKeepLines() {
// with 1 empty line configured for method start, ensure that 2 empty lines are kept in function modules without parameters

rule.configMaxEmptyLinesAtMethodStart.setValue(1);

buildSrc("FUNCTION any_function");
buildSrc(" \" You can use the template 'functionModuleParameter' to add here the signature!");
buildSrc(".");
buildSrc("");
buildSrc("");
buildSrc(" \" any comment");
buildSrc("ENDFUNCTION.");

copyExpFromSrc();

testRule();
}
}

0 comments on commit 634d823

Please sign in to comment.