Skip to content

Commit 634d823

Browse files
committed
fix EmptyLinesWithinMethodsRule for function modules (SAP#106)
1 parent 5f5ec27 commit 634d823

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

com.sap.adt.abapcleaner/src/com/sap/adt/abapcleaner/rules/emptylines/EmptyLinesWithinMethodsRule.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class EmptyLinesWithinMethodsRule extends Rule {
2222
@Override
2323
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)."; }
2424

25+
@Override
26+
public String getHintsAndRestrictions() { return "For function modules, additional empty lines are kept at the beginning to align with ADT and SE37 behavior."; }
27+
2528
@Override
2629
public LocalDate getDateCreated() { return LocalDate.of(2020, 12, 28); }
2730

@@ -136,6 +139,15 @@ private void executeOn(Code code, Command command, boolean insertLineBreakAbove)
136139
Token token = command.getFirstToken();
137140
if (command.getPrev() != null && (command.getPrev().isMethodFunctionFormOrEventBlockStart() || command.isMethodFunctionOrFormEnd())) {
138141
int maxLineBreak = ((command.isMethodFunctionOrFormEnd()) ? configMaxEmptyLinesAtMethodEnd.getValue() : configMaxEmptyLinesAtMethodStart.getValue()) + 1;
142+
143+
// for function modules with parameters, allow 3 extra lines that are needed for additional auto-generated comment lines in SE37,
144+
// and therefore ensured when saving a function module from ADT; for function modules without parameters, 1 extra line is needed
145+
if (command.getPrev().isFunctionStart()) {
146+
Token functionName = command.getPrev().getFirstCodeToken().getNextCodeSibling();
147+
boolean hasParameters = (functionName != null && !functionName.getNextCodeSibling().isPeriod());
148+
maxLineBreak += hasParameters ? 3 : 1;
149+
}
150+
139151
if (token != null && token.lineBreaks > maxLineBreak) {
140152
token.lineBreaks = maxLineBreak;
141153
code.addRuleUse(this, command, token);

test/com.sap.adt.abapcleaner.test/src/com/sap/adt/abapcleaner/rules/emptylines/EmptyLinesWithinMethodsTest.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,4 +351,100 @@ void testIncludeType() {
351351

352352
testRule();
353353
}
354+
355+
@Test
356+
void testFunctionModuleWithParametersRemoveLine() {
357+
// ensure that 3 empty lines are kept (but the 4th is removed) in function modules with parameters,
358+
// because these 3 lines are used for auto-generated comments in SE37 and reserved by ADT upon save
359+
360+
buildSrc("FUNCTION any_function");
361+
buildSrc(" IMPORTING");
362+
buildSrc(" VALUE(im_p1) TYPE i");
363+
buildSrc(" EXPORTING");
364+
buildSrc(" ex_p1 TYPE i.");
365+
buildSrc("");
366+
buildSrc("");
367+
buildSrc("");
368+
buildSrc("");
369+
buildSrc(" ex_p1 = 1.");
370+
buildSrc("ENDFUNCTION.");
371+
372+
buildExp("FUNCTION any_function");
373+
buildExp(" IMPORTING");
374+
buildExp(" VALUE(im_p1) TYPE i");
375+
buildExp(" EXPORTING");
376+
buildExp(" ex_p1 TYPE i.");
377+
buildExp("");
378+
buildExp("");
379+
buildExp("");
380+
buildExp(" ex_p1 = 1.");
381+
buildExp("ENDFUNCTION.");
382+
383+
testRule();
384+
}
385+
386+
@Test
387+
void testFunctionModuleWithParametersKeepLines() {
388+
// with 1 empty line configured for method start, ensure that 4 empty lines are kept in function modules with parameters
389+
390+
rule.configMaxEmptyLinesAtMethodStart.setValue(1);
391+
392+
buildSrc("FUNCTION any_function");
393+
buildSrc(" IMPORTING");
394+
buildSrc(" VALUE(im_p1) TYPE i");
395+
buildSrc(" EXPORTING");
396+
buildSrc(" ex_p1 TYPE i.");
397+
buildSrc("");
398+
buildSrc("");
399+
buildSrc("");
400+
buildSrc("");
401+
buildSrc(" ex_p1 = 1.");
402+
buildSrc("ENDFUNCTION.");
403+
404+
copyExpFromSrc();
405+
406+
testRule();
407+
}
408+
409+
@Test
410+
void testFunctionModuleWithoutParametersRemoveLine() {
411+
// ensure that 1 empty line is kept (but further lines removed) in function modules without parameters,
412+
// because this line is used for auto-generated comments in SE37 and reserved by ADT upon save
413+
414+
buildSrc("FUNCTION any_function");
415+
buildSrc(" \" You can use the template 'functionModuleParameter' to add here the signature!");
416+
buildSrc(".");
417+
buildSrc("");
418+
buildSrc("");
419+
buildSrc(" \" any comment");
420+
buildSrc("ENDFUNCTION.");
421+
422+
buildExp("FUNCTION any_function");
423+
buildExp(" \" You can use the template 'functionModuleParameter' to add here the signature!");
424+
buildExp(".");
425+
buildExp("");
426+
buildExp(" \" any comment");
427+
buildExp("ENDFUNCTION.");
428+
429+
testRule();
430+
}
431+
432+
@Test
433+
void testFunctionModuleWithoutParametersKeepLines() {
434+
// with 1 empty line configured for method start, ensure that 2 empty lines are kept in function modules without parameters
435+
436+
rule.configMaxEmptyLinesAtMethodStart.setValue(1);
437+
438+
buildSrc("FUNCTION any_function");
439+
buildSrc(" \" You can use the template 'functionModuleParameter' to add here the signature!");
440+
buildSrc(".");
441+
buildSrc("");
442+
buildSrc("");
443+
buildSrc(" \" any comment");
444+
buildSrc("ENDFUNCTION.");
445+
446+
copyExpFromSrc();
447+
448+
testRule();
449+
}
354450
}

0 commit comments

Comments
 (0)