Skip to content

Commit

Permalink
resolve #15
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Feb 17, 2022
1 parent 79644a0 commit 0e21e3c
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 145 deletions.
120 changes: 59 additions & 61 deletions CodeService/src/FormatElement/AlignmentLayoutElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -78,55 +76,55 @@ int AlignmentLayoutElement::GetAlignPosition(FormatContext& ctx)
return alignOffset + static_cast<int>(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<TextElement>(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<TextElement>(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)
{
Expand Down Expand Up @@ -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)
);
}
}
Expand Down
6 changes: 0 additions & 6 deletions CodeService/src/LuaEditorConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,6 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ local t4 = {
}

local t5 = {
aaaa = 123131,
bbbbb = 1231313131,
aaaa = 123131,
bbbbb = 1231313131,
}

local t6 = {
Expand All @@ -29,9 +29,9 @@ local t6 = {
}

local t7 = {
aaaaa = 12313,
ccccc = 1231,
ddddd = 12313131,
hhhh = 12313131,
aaaaa = 12313,
ccccc = 1231,
ddddd = 12313131,
hhhh = 12313131,

}
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

12 changes: 6 additions & 6 deletions Test/test_script/format_text/wait_format_should_be/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions docs/format_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ end

对于连续的赋值或者local语句,当算法识别到代码试图对齐到等号的时候,会采取对齐到等号的方式排版,识别对齐的基本原则是:

* 连续的赋值/local/注释语句,当首个赋值/local语句的等号与它左边的元素距离大于1个空格时,该连续的赋值/local语句会对齐到等号。
* 连续的赋值/local/注释语句,当任意赋值/local语句的等号与它左边的元素距离大于1个空格时,该连续的赋值/local语句会对齐到等号。
* 这里对连续对齐的定义是: 语句之间相距不超过2行
* 对齐会对齐到最远的等号
* 对齐会采用最小对齐

```lua
local ttt = 123 --first
Expand All @@ -63,8 +63,8 @@ cd = 345 --second
格式化后:

```lua
local ttt = 123 --first
cd = 345 --second
local ttt = 123 --first
cd = 345 --second
```

## 表的排版
Expand All @@ -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}
Expand Down
24 changes: 13 additions & 11 deletions docs/format_action_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
Expand Down
6 changes: 1 addition & 5 deletions docs/format_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ local t <const> = 1

## continuous_assign_statement_align_to_equal_sign

该选项表示连续赋值语句的首行语句如果等号与左边符号相距大于一个空格,则对齐到连续赋值语句的等号最大的位置,该选项默认值为true.
该选项表示连续赋值语句的任意一行如果等号与左边符号相距大于一个空格,则采用最小对齐原则对齐到等号,该选项默认值为true.

格式化前:

Expand Down Expand Up @@ -375,10 +375,6 @@ local t = {
}
```

## weak_alignment_rule

该选项表示是否为弱对齐规则,如果是,则连续local/赋值语句的等号中任意一个与左边的元素大于一个空格则该连续语句会对齐到等号

## label_no_indent

该选项表示标签无缩进。
Expand Down
12 changes: 4 additions & 8 deletions docs/format_config_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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 = {
Expand All @@ -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.
Expand Down
Loading

0 comments on commit 0e21e3c

Please sign in to comment.