diff --git a/CodeService/src/FormatElement/AlignmentLayoutElement.cpp b/CodeService/src/FormatElement/AlignmentLayoutElement.cpp index b7f58ec9..6d4b2f3f 100644 --- a/CodeService/src/FormatElement/AlignmentLayoutElement.cpp +++ b/CodeService/src/FormatElement/AlignmentLayoutElement.cpp @@ -59,16 +59,14 @@ void AlignmentLayoutElement::Diagnosis(DiagnosisContext& ctx, ChildIterator self int AlignmentLayoutElement::GetAlignPosition(FormatContext& ctx) { auto indentState = ctx.GetCurrentIndent(); - int alignOffset = 0; - - if (!ctx.GetOptions().weak_alignment_rule) - { - alignOffset = GetAlignOffset(ctx); - } - else - { - alignOffset = GetAlignOffsetWithWeakRule(ctx); - } + // if (!ctx.GetOptions().weak_alignment_rule) + // { + // alignOffset = GetAlignOffset(ctx); + // } + // else + // { + int alignOffset = GetAlignOffsetWithWeakRule(ctx); + // } if (alignOffset == -1) { @@ -78,55 +76,55 @@ int AlignmentLayoutElement::GetAlignPosition(FormatContext& ctx) return alignOffset + static_cast(indentState.SpaceIndent + indentState.TabIndent); } -int AlignmentLayoutElement::GetAlignOffset(FormatContext& ctx) -{ - int alignSignOffset = 0; - bool firstContainAlignSign = true; - // 先寻找等号对齐的位置,并且判断连续的带等号的语句是否应该对齐到等号 - // 连续的带等号的语句是否应该对齐到等号,这个行为应该由连续语句的首行决定 - // 如果被子节点内的其他语句共同决定则很难将连续对齐还原为普通排版 - for (auto statIt = _children.begin(); statIt != _children.end(); ++statIt) - { - const auto statement = *statIt; - - auto& statementChildren = statement->GetChildren(); - - for (auto it = statementChildren.begin(); it != statementChildren.end(); ++it) - { - auto textChild = *it; - if (textChild->GetType() == FormatElementType::TextElement) - { - const auto textElement = std::dynamic_pointer_cast(textChild); - if (textElement->GetText() == _alignSign) - { - const auto signPosition = ctx.GetColumn(textElement->GetTextRange().StartOffset); - if (firstContainAlignSign && it != statementChildren.begin()) - { - firstContainAlignSign = false; - auto lastStatChild = FindLastValidChild(it, statementChildren); - if (lastStatChild == nullptr) - { - return -1; - } - - const auto lastPosition = ctx.GetColumn(lastStatChild->GetTextRange().EndOffset); - - if (signPosition - lastPosition <= 2) - { - return -1; - } - } - - - alignSignOffset = std::max(alignSignOffset, - signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset) - ); - } - } - } - } - return alignSignOffset; -} +// int AlignmentLayoutElement::GetAlignOffset(FormatContext& ctx) +// { +// int alignSignOffset = 0; +// bool firstContainAlignSign = true; +// // 先寻找等号对齐的位置,并且判断连续的带等号的语句是否应该对齐到等号 +// // 连续的带等号的语句是否应该对齐到等号,这个行为应该由连续语句的首行决定 +// // 如果被子节点内的其他语句共同决定则很难将连续对齐还原为普通排版 +// for (auto statIt = _children.begin(); statIt != _children.end(); ++statIt) +// { +// const auto statement = *statIt; +// +// auto& statementChildren = statement->GetChildren(); +// +// for (auto it = statementChildren.begin(); it != statementChildren.end(); ++it) +// { +// auto textChild = *it; +// if (textChild->GetType() == FormatElementType::TextElement) +// { +// const auto textElement = std::dynamic_pointer_cast(textChild); +// if (textElement->GetText() == _alignSign) +// { +// const auto signPosition = ctx.GetColumn(textElement->GetTextRange().StartOffset); +// if (firstContainAlignSign && it != statementChildren.begin()) +// { +// firstContainAlignSign = false; +// auto lastStatChild = FindLastValidChild(it, statementChildren); +// if (lastStatChild == nullptr) +// { +// return -1; +// } +// +// const auto lastPosition = ctx.GetColumn(lastStatChild->GetTextRange().EndOffset); +// +// if (signPosition - lastPosition <= 2) +// { +// return -1; +// } +// } +// +// +// alignSignOffset = std::max(alignSignOffset, +// signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset) +// ); +// } +// } +// } +// } +// return alignSignOffset; +// } int AlignmentLayoutElement::GetAlignOffsetWithWeakRule(FormatContext& ctx) { @@ -162,9 +160,9 @@ int AlignmentLayoutElement::GetAlignOffsetWithWeakRule(FormatContext& ctx) { canAlignToSign = true; } - + // 采用最小对齐原则 alignSignOffset = std::max(alignSignOffset, - signPosition - ctx.GetColumn(statement->GetTextRange().StartOffset) + lastPosition + 2 - ctx.GetColumn(statement->GetTextRange().StartOffset) ); } } diff --git a/CodeService/src/LuaEditorConfig.cpp b/CodeService/src/LuaEditorConfig.cpp index 23995729..2bb8c2af 100644 --- a/CodeService/src/LuaEditorConfig.cpp +++ b/CodeService/src/LuaEditorConfig.cpp @@ -266,12 +266,6 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr opti std::stoi(configMap.at("max_continuous_line_distance")); } - if(configMap.count("weak_alignment_rule")) - { - options->weak_alignment_rule = - configMap.at("weak_alignment_rule") == "true"; - } - if (configMap.count("continuous_assign_statement_align_to_equal_sign")) { options->continuous_assign_statement_align_to_equal_sign = diff --git a/Test/test_script/format_text/wait_format_by_option/.editorconfig b/Test/test_script/format_text/wait_format_by_option/.editorconfig index 5b142e2f..1dda0c8c 100644 --- a/Test/test_script/format_text/wait_format_by_option/.editorconfig +++ b/Test/test_script/format_text/wait_format_by_option/.editorconfig @@ -140,8 +140,6 @@ do_statement_no_indent = true if_condition_no_continuation_indent = true [{max_continuous_line_distance-eq-2.lua}] max_continuous_line_distance = 2 -[{weak_alignment_rule-eq-true.lua}] -weak_alignment_rule = true [{table_append_expression_no_space-eq-true.lua}] table_append_expression_no_space = true [{if_condition_align_with_each_other-eq-true.lua}] diff --git a/Test/test_script/format_text/wait_format_by_option/weak_alignment_rule-eq-true.lua b/Test/test_script/format_text/wait_format_by_option/weak_alignment_rule-eq-true.lua deleted file mode 100644 index 10396b28..00000000 --- a/Test/test_script/format_text/wait_format_by_option/weak_alignment_rule-eq-true.lua +++ /dev/null @@ -1,9 +0,0 @@ -local t = 131 -local t2 = 2 -local t3 = 4 - - -local t4 = { aaa= 123 } - -p { aaa= 123, -aa = 123 } diff --git a/Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig b/Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig index 4325a48e..1dd780e4 100644 --- a/Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig +++ b/Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig @@ -135,8 +135,6 @@ do_statement_no_indent = true if_condition_no_continuation_indent = true [{max_continuous_line_distance-eq-2.lua}] max_continuous_line_distance = 2 -[{weak_alignment_rule-eq-true.lua}] -weak_alignment_rule = true [{table_append_expression_no_space-eq-true.lua}] table_append_expression_no_space = true [{if_condition_align_with_each_other-eq-true.lua}] diff --git a/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_statement_align_to_equal_sign-eq-true.lua b/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_statement_align_to_equal_sign-eq-true.lua index 5ea80b02..d9a05789 100644 --- a/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_statement_align_to_equal_sign-eq-true.lua +++ b/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_statement_align_to_equal_sign-eq-true.lua @@ -32,10 +32,10 @@ local h4 = { aaa, bbb, ccc, ddd, eee } -local h1 = {} +local h1 = {} local h222222222 = {} -local h3 = {} -local h4 = { +local h3 = {} +local h4 = { aaa, bbb, ccc, ddd, eee } diff --git a/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_table_field_align_to_equal_sign-eq-true.lua b/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_table_field_align_to_equal_sign-eq-true.lua index 282cc8ec..78a1f3bd 100644 --- a/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_table_field_align_to_equal_sign-eq-true.lua +++ b/Test/test_script/format_text/wait_format_by_option_should_be/continuous_assign_table_field_align_to_equal_sign-eq-true.lua @@ -19,8 +19,8 @@ local t4 = { } local t5 = { - aaaa = 123131, - bbbbb = 1231313131, + aaaa = 123131, + bbbbb = 1231313131, } local t6 = { @@ -29,9 +29,9 @@ local t6 = { } local t7 = { - aaaaa = 12313, - ccccc = 1231, - ddddd = 12313131, - hhhh = 12313131, + aaaaa = 12313, + ccccc = 1231, + ddddd = 12313131, + hhhh = 12313131, } diff --git a/Test/test_script/format_text/wait_format_by_option_should_be/max_continuous_line_distance-eq-2.lua b/Test/test_script/format_text/wait_format_by_option_should_be/max_continuous_line_distance-eq-2.lua index 35171ff9..819e7f74 100644 --- a/Test/test_script/format_text/wait_format_by_option_should_be/max_continuous_line_distance-eq-2.lua +++ b/Test/test_script/format_text/wait_format_by_option_should_be/max_continuous_line_distance-eq-2.lua @@ -1,7 +1,7 @@ -local t = 1 -local t2 = 123 +local t = 1 +local t2 = 123 -local t3 = 123 +local t3 = 123 local t4 = 123 diff --git a/Test/test_script/format_text/wait_format_by_option_should_be/weak_alignment_rule-eq-true.lua b/Test/test_script/format_text/wait_format_by_option_should_be/weak_alignment_rule-eq-true.lua deleted file mode 100644 index 42ed021d..00000000 --- a/Test/test_script/format_text/wait_format_by_option_should_be/weak_alignment_rule-eq-true.lua +++ /dev/null @@ -1,9 +0,0 @@ -local t = 131 -local t2 = 2 -local t3 = 4 - - -local t4 = { aaa = 123 } - -p { aaa = 123, - aa = 123 } diff --git a/Test/test_script/format_text/wait_format_should_be/table.lua b/Test/test_script/format_text/wait_format_should_be/table.lua index 2cb059d7..65a951ef 100644 --- a/Test/test_script/format_text/wait_format_should_be/table.lua +++ b/Test/test_script/format_text/wait_format_should_be/table.lua @@ -45,15 +45,15 @@ local t8 = { } local t9 = { - jfwejgwojgow--[[wkngowjgw]] = 123131, - joiwejgjwgw = 13131, - fwhoifwjofgjw = 353, - ljeogjeprjgpege = 13131, + jfwejgwojgow--[[wkngowjgw]] = 123131, + joiwejgjwgw = 13131, + fwhoifwjofgjw = 353, + ljeogjeprjgpege = 13131, } local t10 = { - aaa = 123131, - bb = 123131, + aaa = 123131, + bb = 123131, dd = 1313131, ee = 12313131, diff --git a/docs/format_action.md b/docs/format_action.md index 0dbdd1a3..339cc338 100644 --- a/docs/format_action.md +++ b/docs/format_action.md @@ -51,9 +51,9 @@ end 对于连续的赋值或者local语句,当算法识别到代码试图对齐到等号的时候,会采取对齐到等号的方式排版,识别对齐的基本原则是: -* 连续的赋值/local/注释语句,当首个赋值/local语句的等号与它左边的元素距离大于1个空格时,该连续的赋值/local语句会对齐到等号。 +* 连续的赋值/local/注释语句,当任意赋值/local语句的等号与它左边的元素距离大于1个空格时,该连续的赋值/local语句会对齐到等号。 * 这里对连续对齐的定义是: 语句之间相距不超过2行 -* 对齐会对齐到最远的等号 +* 对齐会采用最小对齐 ```lua local ttt = 123 --first @@ -63,8 +63,8 @@ cd = 345 --second 格式化后: ```lua -local ttt = 123 --first -cd = 345 --second +local ttt = 123 --first +cd = 345 --second ``` ## 表的排版 @@ -73,7 +73,8 @@ cd = 345 --second * 表内的field会和表的大括号间距一个空白或者保持相对行距 * 表内的不同field会保持相对位置不变 * 如果所有field在不同行,并且均是key = value的形式,则当不同的field试图对齐到等号的时候,算法会对齐到等号 -* 试图对齐到等号的基本原则是key = value/注释field,当首行field的等号与它左边的元素距离大于1个空格时,该表内的key = value field会对齐到等号 +* 试图对齐到等号的基本原则是连续的key = value/注释field,当其中任意一行field的等号与它左边的元素距离大于1个空格时,该连续的key = value field会对齐到等号 +* 对齐会采用最小对齐 ```lua local t = {1,2,3} diff --git a/docs/format_action_EN.md b/docs/format_action_EN.md index 2724b438..34fb0590 100644 --- a/docs/format_action_EN.md +++ b/docs/format_action_EN.md @@ -49,11 +49,12 @@ end ## Continuous assignment statement -For consecutive assignments or local statements, when the algorithm recognizes that the code tries to align to the equal sign, it will typeset in the manner of aligning to the equal sign. The basic principle of identifying alignment is: +For continuous assignment or local statements, when the algorithm recognizes that the code is trying to align to the equal sign, it will take the alignment to the equal sign layout, and the basic principles for identifying the alignment are: -* For consecutive assignment/local/comment statements, when the equal sign of the first assignment/local statement is more than 1 space away from the element to its left, the consecutive assignment/local statement will be aligned to the equal sign. -* The definition of continuous alignment here is: no more than 2 lines between statements -* Alignment will be aligned to the furthest equal sign +* Continuous assignment /local/ comment statements, when the equal sign of the arbitrary assignment /local statement is greater than 1 space away from the element to its left, the continuous assignment /local statement will align to the equal sign. +* The definition of continuous alignment here is: statements are no more than 2 lines apart +* Alignment uses minimal alignment + ```lua local ttt = 123 --first @@ -63,17 +64,18 @@ cd = 345 --second Will be formatted as: ```lua -local ttt = 123 --first -cd = 345 --second +local ttt = 123 --first +cd = 345 --second ``` ## Table layout -For common table expressions, the default formatting method is: -* The field in the table will be spaced a blank from the braces of the table or keep relative line spacing -* Different fields in the table will keep their relative positions unchanged -* If all the fields are in different rows and are in the form of key = value, when different fields try to align to the equal sign, the algorithm will align to the equal sign -* The basic principle of trying to align to the equal sign is key = value/comment field. When the distance between the equal sign of the first line of field and the element to its left is greater than 1 space, the key = value field in the table will be aligned to the equal sign +For common table expressions, the layout is as follows by default: +* The field in the table will be spaced one blank space or relative to the table's curly braces +* The different fields in the table will remain in the same relative position +* If all fields are on different lines and are all in the form of key = value, then when different fields try to align to the equal sign, the algorithm will align to the equal sign +* The basic principle for attempting to align to the equal sign is a continuous key = value/comment field, and when the equal sign of any row of field is greater than 1 space away from the element to its left, the continuous key = value field will align to the equal sign +* Alignment uses minimal alignment ```lua local t = {1,2,3} diff --git a/docs/format_config.md b/docs/format_config.md index 4605ed92..17c1c8d5 100644 --- a/docs/format_config.md +++ b/docs/format_config.md @@ -299,7 +299,7 @@ local t = 1 ## continuous_assign_statement_align_to_equal_sign -该选项表示连续赋值语句的首行语句如果等号与左边符号相距大于一个空格,则对齐到连续赋值语句的等号最大的位置,该选项默认值为true. +该选项表示连续赋值语句的任意一行如果等号与左边符号相距大于一个空格,则采用最小对齐原则对齐到等号,该选项默认值为true. 格式化前: @@ -375,10 +375,6 @@ local t = { } ``` -## weak_alignment_rule - -该选项表示是否为弱对齐规则,如果是,则连续local/赋值语句的等号中任意一个与左边的元素大于一个空格则该连续语句会对齐到等号 - ## label_no_indent 该选项表示标签无缩进。 diff --git a/docs/format_config_EN.md b/docs/format_config_EN.md index 53419169..76d68e9a 100644 --- a/docs/format_config_EN.md +++ b/docs/format_config_EN.md @@ -301,7 +301,7 @@ This option indicates the definition of continuous lines, and its value determin ## continuous_assign_statement_align_to_equal_sign -This option means that if the first line of the continuous assignment statement is more than one space between the equal sign and the left symbol, it will be aligned to the largest equal sign of the continuous assignment statement. The default value of this option is true. +This option means that any line of a continuous assignment statement that is aligned to the equal sign using the principle of least alignment if the equal sign is greater than one space apart from the left symbol, and the default value for this option is true. Before formatting: @@ -365,9 +365,9 @@ after formatting: ```lua -- true local t = { - aadddd = 123, - bbb = 456, - ddd = 789 + aadddd = 123, + bbb = 456, + ddd = 789 } -- false local t = { @@ -377,10 +377,6 @@ local t = { } ``` -## weak_alignment_rule - -This option indicates whether it is a weak alignment rule. If so, if any of the equal signs of the continuous local/assignment statement and the element to the left are greater than one space, the continuous statement will be aligned to the equal sign - ## label_no_indent This option means that the labels are not indented. diff --git a/include/CodeService/FormatElement/AlignmentLayoutElement.h b/include/CodeService/FormatElement/AlignmentLayoutElement.h index d936fb0d..b528963b 100644 --- a/include/CodeService/FormatElement/AlignmentLayoutElement.h +++ b/include/CodeService/FormatElement/AlignmentLayoutElement.h @@ -12,7 +12,7 @@ class AlignmentLayoutElement : public FormatElement void Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent) override; private: int GetAlignPosition(FormatContext& ctx); - int GetAlignOffset(FormatContext& ctx); + // int GetAlignOffset(FormatContext& ctx); int GetAlignOffsetWithWeakRule(FormatContext& ctx); void AlignmentSerialize(SerializeContext& ctx, ChildIterator selfIt, int alignSignPosition, FormatElement& parent); diff --git a/include/CodeService/LuaCodeStyleOptions.h b/include/CodeService/LuaCodeStyleOptions.h index abafdd24..f65a092e 100644 --- a/include/CodeService/LuaCodeStyleOptions.h +++ b/include/CodeService/LuaCodeStyleOptions.h @@ -96,11 +96,6 @@ class LuaCodeStyleOptions */ int max_continuous_line_distance = 1; - /* - * 如果采用弱对齐规则,则连续行内的等号只要有一个等号与左边的元素间距超过一个空格则连续行内对齐到等号 - */ - bool weak_alignment_rule = false; - bool continuous_assign_statement_align_to_equal_sign = true; bool continuous_assign_table_field_align_to_equal_sign = true; diff --git a/lua.template.editorconfig b/lua.template.editorconfig index c088fc2f..e22e9994 100644 --- a/lua.template.editorconfig +++ b/lua.template.editorconfig @@ -28,9 +28,6 @@ align_table_field_to_first_field = false keep_one_space_between_namedef_and_attribute = false # continous line distance max_continuous_line_distance = 1 -# if true, iff any one of the consecutive rows meets the condition of aligning to the equal sign, -# the consecutive rows will be aligned to the equal sign -weak_alignment_rule = false # see document for detail continuous_assign_statement_align_to_equal_sign = true # see document for detail