forked from SAP/abap-cleaner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add new rule "Break before DEFINE etc."
- Loading branch information
Showing
7 changed files
with
548 additions
and
289 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...adt.abapcleaner/src/com/sap/adt/abapcleaner/rules/ddl/position/DdlPositionDefineRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.sap.adt.abapcleaner.rules.ddl.position; | ||
|
||
import java.time.LocalDate; | ||
|
||
import com.sap.adt.abapcleaner.parser.Code; | ||
import com.sap.adt.abapcleaner.parser.Command; | ||
import com.sap.adt.abapcleaner.parser.Token; | ||
import com.sap.adt.abapcleaner.parser.TokenSearch; | ||
import com.sap.adt.abapcleaner.programbase.IntegrityBrokenException; | ||
import com.sap.adt.abapcleaner.programbase.UnexpectedSyntaxAfterChanges; | ||
import com.sap.adt.abapcleaner.programbase.UnexpectedSyntaxBeforeChanges; | ||
import com.sap.adt.abapcleaner.rulebase.ConfigEnumValue; | ||
import com.sap.adt.abapcleaner.rulebase.ConfigIntValue; | ||
import com.sap.adt.abapcleaner.rulebase.ConfigValue; | ||
import com.sap.adt.abapcleaner.rulebase.Profile; | ||
import com.sap.adt.abapcleaner.rulebase.RuleID; | ||
import com.sap.adt.abapcleaner.rulebase.RuleReference; | ||
import com.sap.adt.abapcleaner.rulebase.RuleSource; | ||
import com.sap.adt.abapcleaner.rulehelpers.RuleForDdlPosition; | ||
|
||
public class DdlPositionDefineRule extends RuleForDdlPosition { | ||
private final static RuleReference[] references = new RuleReference[] { new RuleReference(RuleSource.ABAP_CLEANER) }; | ||
|
||
@Override | ||
public RuleID getID() { return RuleID.DDL_POSITION_DEFINE; } | ||
|
||
@Override | ||
public String getDisplayName() { return "Break before DEFINE etc."; } | ||
|
||
@Override | ||
public String getDescription() { return "Standardizes line breaks and indentation before DEFINE / EXTEND etc. keywords, entity name and WITH PARAMETERS."; } | ||
|
||
@Override | ||
public LocalDate getDateCreated() { return LocalDate.of(2024, 9, 9); } | ||
|
||
@Override | ||
public RuleReference[] getReferences() { return references; } | ||
|
||
@Override | ||
public String getExample() { | ||
return "" | ||
+ LINE_SEP + "@EndUserText.label: 'Any Description' define" | ||
+ LINE_SEP + " view" | ||
+ LINE_SEP + " entity" | ||
+ LINE_SEP + "C_AnyEntity with" | ||
+ LINE_SEP + "parameters" | ||
+ LINE_SEP + "// comment" | ||
+ LINE_SEP + "P_AnyParam : AnyType," | ||
+ LINE_SEP + " P_OtherParam : OtherType," | ||
+ LINE_SEP + " P_ThirdParam : ThirdType" | ||
+ LINE_SEP + "" | ||
+ LINE_SEP + " as select from I_AnyEntity as AnyAlias" | ||
+ LINE_SEP + "" | ||
+ LINE_SEP + " left outer to one join I_OtherEntity as OtherAlias" | ||
+ LINE_SEP + " on AnyAlias.IdField = OtherAlias.IdField" | ||
+ LINE_SEP + "" | ||
+ LINE_SEP + "{" | ||
+ LINE_SEP + " key AnyAlias.AnyKeyField," | ||
+ LINE_SEP + " OtherAlias.AnyNonKeyField" | ||
+ LINE_SEP + "}"; | ||
} | ||
|
||
final ConfigEnumValue<DdlLineBreakWithoutNever> configBreakBeforeDefine = new ConfigEnumValue<DdlLineBreakWithoutNever>(this, "BreakBeforeDefine", "Break before DEFINE etc. keywords:", lineBreakSelectionWithoutNever, DdlLineBreakWithoutNever.values(), DdlLineBreakWithoutNever.ALWAYS); | ||
final ConfigIntValue configDefineIndent = new ConfigIntValue(this, "DefineIndent", "Indent if breaking:", "", 0, 0, MAX_INDENT); | ||
|
||
final ConfigEnumValue<DdlLineBreak> configBreakBeforeEntityName = new ConfigEnumValue<DdlLineBreak>(this, "BreakBeforeEntityName", "Break before entity name:", lineBreakSelection, DdlLineBreak.values(), DdlLineBreak.NEVER); | ||
final ConfigIntValue configEntityNameIndent = new ConfigIntValue(this, "EntityNameIndent", "Indent if breaking:", "", 0, 2, MAX_INDENT); | ||
|
||
final ConfigEnumValue<DdlLineBreak> configBreakBeforeWithParams = new ConfigEnumValue<DdlLineBreak>(this, "BreakBeforeWithParams", "Break before WITH PARAMETERS:", lineBreakSelection, DdlLineBreak.values(), DdlLineBreak.ALWAYS); | ||
final ConfigIntValue configWithParamsIndent = new ConfigIntValue(this, "WithParamsIndent", "Indent if breaking:", "", 0, 2, MAX_INDENT); | ||
final ConfigIntValue configParamsIndent = new ConfigIntValue(this, "ParamsIndent", "Indent of parameters:", "", 0, 4, MAX_INDENT); | ||
|
||
private final ConfigValue[] configValues = new ConfigValue[] { configBreakBeforeDefine, configDefineIndent, configBreakBeforeEntityName, configEntityNameIndent, configBreakBeforeWithParams, configWithParamsIndent, configParamsIndent }; | ||
|
||
@Override | ||
public ConfigValue[] getConfigValues() { return configValues; } | ||
|
||
public DdlPositionDefineRule(Profile profile) { | ||
super(profile); | ||
initializeConfiguration(); | ||
} | ||
|
||
// ------------------------------------------------------------------------- | ||
|
||
@Override | ||
protected boolean executeOn(Code code, Command command) throws UnexpectedSyntaxBeforeChanges, UnexpectedSyntaxAfterChanges, IntegrityBrokenException { | ||
boolean changed = false; | ||
|
||
if (command.isDdlAnnotation()) | ||
return false; | ||
|
||
Token firstCode = command.getFirstCodeToken(); | ||
if (firstCode == null) // pro forma | ||
return false; | ||
|
||
DdlLineBreak breakBeforeDefine = getLineBreak(DdlLineBreakWithoutNever.forValue(configBreakBeforeDefine.getValue())); | ||
DdlLineBreak breakBeforeEntityName = DdlLineBreak.forValue(configBreakBeforeEntityName.getValue()); | ||
DdlLineBreak breakBeforeWithParams = DdlLineBreak.forValue(configBreakBeforeWithParams.getValue()); | ||
|
||
// break before DEFINE etc., condense definition keywords and (un)break before entity name | ||
Token entityName = command.getDdlOrDclEntityNameToken(); | ||
if (entityName != null) { | ||
boolean emptyLine = (command.getPrev() != null); | ||
changed |= breakBefore(firstCode, breakBeforeDefine, emptyLine, configDefineIndent.getValue()); | ||
changed |= condense(firstCode, entityName.getPrevCodeToken()); | ||
|
||
changed |= breakBefore(entityName, breakBeforeEntityName, false, configEntityNameIndent.getValue()); | ||
} | ||
|
||
// (un)break before "WITH PARAMETERS" | ||
Token parametersToken = firstCode.getLastTokenOnSiblings(true, TokenSearch.ASTERISK, "WITH", "PARAMETERS"); | ||
if (parametersToken != null) { | ||
Token withToken = parametersToken.getPrevCodeSibling(); | ||
changed |= breakBefore(withToken, breakBeforeWithParams, false, configWithParamsIndent.getValue()); | ||
changed |= condense(withToken, parametersToken); | ||
|
||
// set indent of parameters, their annotations and comments | ||
Command paramCommand = command.getFirstChild(); | ||
int paramIndent = configParamsIndent.getValue(); | ||
while (paramCommand != null) { | ||
// do not move comments at the end of the parameter list, as they probably belong to the following Command | ||
if (paramCommand.isCommentLine() && paramCommand.getNextNonCommentSibling() == null) | ||
break; | ||
paramCommand = setNewIndent(paramCommand, paramIndent); | ||
if (paramCommand == null) // pro forma | ||
break; | ||
paramCommand = paramCommand.getNextSibling(); | ||
} | ||
} | ||
|
||
return changed; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.