[DEV] better geneation of parentheses

This commit is contained in:
Edouard DUPIN 2018-01-04 23:46:08 +01:00
parent f8f7128c78
commit bec2be2cd1
4 changed files with 46 additions and 24 deletions

View File

@ -189,7 +189,7 @@ estyle::ParentheseProperty::ParentheseProperty() :
inSpaceAfter(false),
outSpaceBefore(false),
outSpaceAfter(false),
oneLineMaxSize(-1) {
oneLineMaxSize(20000) {
}
estyle::ParentheseProperty::ParentheseProperty(class estyle::Generator* _generator, const etk::String& _typeName):
@ -197,7 +197,7 @@ estyle::ParentheseProperty::ParentheseProperty(class estyle::Generator* _generat
inSpaceAfter(_generator, "parenthese-" + _typeName + "-in-space-after", false, "Set a space after the input parenthese (if not already present)"),
outSpaceBefore(_generator, "parenthese-" + _typeName + "-out-space-before", false, "Set a space before the input parenthese (if not already present)"),
outSpaceAfter(_generator, "parenthese-" + _typeName + "-out-space-after", false, "Set a space after the input parenthese (if not already present)"),
oneLineMaxSize(_generator, "parenthese-" + _typeName + "-single-one-line-size-max", -1, "Set in a single line if the size if < XXX (-1 to disable)") {
oneLineMaxSize(_generator, "parenthese-" + _typeName + "-single-one-line-size-max", 20000, "Set in a single line if the size if < XXX (-1 to disable)") {
}
@ -238,15 +238,10 @@ estyle::Generator::Generator():
propertyIndentSize(this, "indent-size", 4, "default 4 sapce in one tabulation indentation"),
propertyDoxygenOneLine(this, "doxygen-1-line-type", true, "true: single line doxygen comment is done with '//!', false '///'"),
propertyDoxygenMultipleLine(this, "doxygen-N-line-type", DOXYGEN_MULTI_LINE__NORMAL, "0: /** */ ..."),
propertySemiColonReturnBetweenAction(this, "semi-colon-return-between-action", false, "true: 2 action separate with a ';' element have a newLine added"),
propertyForConditionMultiLineBigSize(this, "condition-for-multi-line-big-size", 80, "If the condition inside for (...;...;...) if bigger than XX char"),
propertyIfConditionMultiLineBigSize(this, "condition-if-multi-line-big-size", 80, "If the condition inside if (...&&...) if bigger than XX char"),
propertyWhileConditionMultiLineBigSize(this, "condition-while-multi-line-big-size", 80, "If the condition inside while (...&&...) if bigger than XX char"),
propertyDoWhileConditionMultiLineBigSize(this, "condition-do-while-multi-line-big-size", 80, "If the condition inside do ... while (...&&...) if bigger than XX char")
propertySemiColonReturnBetweenAction(this, "semi-colon-return-between-action", false, "true: 2 action separate with a ';' element have a newLine added")
{
setPropertyDoxygenMultiLine(propertyDoxygenMultipleLine);
propertyBrace.set("if", etk::move(estyle::BraceProperty(this, "if")));
propertyBrace.set("if", etk::move(estyle::BraceProperty(this, "if")));
propertyBrace.set("else", etk::move(estyle::BraceProperty(this, "else")));
propertyBrace.set("function", etk::move(estyle::BraceProperty(this, "function", false)));
propertyBrace.set("for", etk::move(estyle::BraceProperty(this, "for")));
@ -259,6 +254,13 @@ estyle::Generator::Generator():
propertyBrace.set("class", etk::move(estyle::BraceProperty(this, "class", false)));
propertyBrace.set("struct", etk::move(estyle::BraceProperty(this, "struct", false)));
propertyParenthese.set("if", etk::move(estyle::ParentheseProperty(this, "if")));
propertyParenthese.set("for", etk::move(estyle::ParentheseProperty(this, "for")));
propertyParenthese.set("while", etk::move(estyle::ParentheseProperty(this, "while")));
propertyParenthese.set("do-while", etk::move(estyle::ParentheseProperty(this, "do-while")));
propertyParenthese.set("switch", etk::move(estyle::ParentheseProperty(this, "switch")));
propertyParenthese.set("block", etk::move(estyle::ParentheseProperty(this, "block")));
//ParentheseProperty
}
@ -700,8 +702,8 @@ int32_t estyle::Generator::process(int32_t _startId,
// write section A
process(jjj+1, endSectionA);
m_output += ";";
ESTYLE_WARNING("big size: " << propertyForConditionMultiLineBigSize.get() << " <= " << rawSize);
if (propertyForConditionMultiLineBigSize.get() <= rawSize) {
ESTYLE_WARNING("big size: " << propertyParenthese[m_type].oneLineMaxSize.get() << " <= " << rawSize);
if (propertyParenthese[m_type].oneLineMaxSize.get() <= rawSize) {
addNewLine();
addIndent();
} else {
@ -710,7 +712,7 @@ int32_t estyle::Generator::process(int32_t _startId,
// write section B
process(endSectionA+1, endSectionB);
m_output += ";";
if (propertyForConditionMultiLineBigSize.get() <= rawSize) {
if (propertyParenthese[m_type].oneLineMaxSize.get() <= rawSize) {
addNewLine();
addIndent();
} else {
@ -1136,12 +1138,31 @@ bool estyle::Generator::nextIs(int64_t _pos, enum estyle::lexer::tocken _nextTyp
}
return false;
}
/*
eproperty::Value<bool> inSpaceBefore;
eproperty::Value<bool> inSpaceAfter;
eproperty::Value<bool> outSpaceBefore;
eproperty::Value<bool> outSpaceAfter;
eproperty::Value<int32_t> oneLineMaxSize;
*/
int64_t estyle::Generator::generateCondition(int64_t _pos) {
int32_t sectionEnd = endOfSection(_pos);
int32_t nbCondition = countCurrentLevelCondition(_pos);
int32_t rawSize = countRawSize(_pos, sectionEnd);
bool onOneLine = propertyParenthese[m_type].oneLineMaxSize.get() >= rawSize;
if (propertyParenthese[m_type].inSpaceBefore.get() == true) {
addSpace();
}
m_output += "(";
if (nbCondition == 0) {
if (propertyParenthese[m_type].inSpaceAfter.get() == true) {
addSpace();
}
int32_t nbCondition = countCurrentLevelCondition(_pos);
if ( nbCondition == 0
|| onOneLine == true) {
ESTYLE_WARNING("==> only one element");
process(_pos+1, sectionEnd);
} else {
@ -1165,8 +1186,13 @@ int64_t estyle::Generator::generateCondition(int64_t _pos) {
}
offsetPop();
}
//addSpaceIfNeeded();
if (propertyParenthese[m_type].outSpaceBefore.get() == true) {
addSpace();
}
m_output += ")";
if (propertyParenthese[m_type].outSpaceAfter.get() == true) {
addSpace();
}
return sectionEnd;
}

View File

@ -64,12 +64,8 @@ namespace estyle {
eproperty::Value<bool> propertyDoxygenOneLine;
eproperty::List<int32_t> propertyDoxygenMultipleLine;
eproperty::Value<bool> propertySemiColonReturnBetweenAction;
eproperty::Value<bool> propertySemiColonReturnBetweenAction; // add a \n if we have a ";" elemeent
eproperty::Value<int32_t> propertyForConditionMultiLineBigSize;
eproperty::Value<int32_t> propertyIfConditionMultiLineBigSize;
eproperty::Value<int32_t> propertyWhileConditionMultiLineBigSize;
eproperty::Value<int32_t> propertyDoWhileConditionMultiLineBigSize;
// Brace section
// brace for "if"
etk::Map<etk::String, estyle::BraceProperty> propertyBrace;

View File

@ -435,7 +435,7 @@ TEST(testFor, condition_1) {
etk::String base = "action_A;for(instanciation=4; condition<6; ++inclemantation){action_B;}action_D;";
etk::String output = interface.process(base);
EXPECT_EQ(output, outRef);
interface.properties.set("condition-for-multi-line-big-size", "20");
interface.properties.set("parenthese-for-single-one-line-size-max", "20");
output = interface.process(base);
outRef = "action_A;for(instanciation =4;\n condition<6;\n ++inclemantation){action_B;}action_D;";
EXPECT_EQ(output, outRef);

View File

@ -430,11 +430,11 @@ TEST(testWhile, condition_1) {
//interface.properties.set("brace-while-in-new-line-before", "true");
//interface.properties.set("brace-while-in-new-line-after", "true");
//interface.properties.set("brace-while-in-space-before", "true");
etk::String outRef = "action_A;while (condition<6 && my_stupid_variable == 77){action_B;}action_D;";
etk::String outRef = "action_A;while(condition<6&&my_stupid_variable==77){action_B;}action_D;";
etk::String base = "action_A;while(condition<6 && my_stupid_variable == 77){action_B;}action_D;";
etk::String output = interface.process(base);
EXPECT_EQ(output, outRef);
interface.properties.set("condition-while-multi-line-big-size", "20");
interface.properties.set("parenthese-while-single-one-line-size-max", "20");
output = interface.process(base);
outRef = "action_A;while( condition<6\n && my_stupid_variable==77){action_B;}action_D;";
EXPECT_EQ(output, outRef);