Skip to content

Commit 72c43e7

Browse files
committed
支持设置table sep风格和支持配置表内尾部sep的保留
1 parent 3ab6c9f commit 72c43e7

24 files changed

+376
-9
lines changed

CodeService/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ target_sources(CodeService
4343
${CodeService_SOURCE_DIR}/src/FormatElement/StatementElement.cpp
4444
${CodeService_SOURCE_DIR}/src/FormatElement/TextElement.cpp
4545
${CodeService_SOURCE_DIR}/src/FormatElement/OperatorElement.cpp
46-
${CodeService_SOURCE_DIR}/src/FormatElement/KeyWordElement.cpp
46+
${CodeService_SOURCE_DIR}/src/FormatElement/KeyWordElement.cpp
47+
${CodeService_SOURCE_DIR}/src/FormatElement/SepElement.cpp
4748
${CodeService_SOURCE_DIR}/src/FormatElement/SpaceElement.cpp
4849
${CodeService_SOURCE_DIR}/src/FormatElement/FormatElement.cpp
4950
${CodeService_SOURCE_DIR}/src/FormatElement/LineElement.cpp
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "CodeService/FormatElement/SepElement.h"
2+
3+
SepElement::SepElement()
4+
: TextElement(""),
5+
_empty(false)
6+
{
7+
}
8+
9+
SepElement::SepElement(std::shared_ptr<LuaAstNode> node)
10+
: TextElement(node),
11+
_empty(false)
12+
{
13+
}
14+
15+
FormatElementType SepElement::GetType()
16+
{
17+
return FormatElementType::SepElement;
18+
}
19+
20+
void SepElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
21+
{
22+
if (_empty)
23+
{
24+
_text = "";
25+
}
26+
else
27+
{
28+
switch (ctx.GetOptions().table_separator_style)
29+
{
30+
case TableSeparatorStyle::Comma:
31+
{
32+
_text = ",";
33+
break;
34+
}
35+
case TableSeparatorStyle::Semicolon:
36+
{
37+
_text = ";";
38+
break;
39+
}
40+
default:
41+
{
42+
if(_text.empty())
43+
{
44+
_text = ",";
45+
}
46+
47+
break;
48+
}
49+
}
50+
}
51+
52+
TextElement::Serialize(ctx, selfIt, parent);
53+
}
54+
55+
void SepElement::SetEmpty()
56+
{
57+
_empty = true;
58+
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,45 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
387387
configMap.at("space_before_open_square_bracket") == "true";
388388
}
389389

390+
if(configMap.count("table_separator_style"))
391+
{
392+
auto style = configMap.at("table_separator_style");
393+
if(style == "none")
394+
{
395+
options->table_separator_style = TableSeparatorStyle::None;
396+
}
397+
else if(style == "comma")
398+
{
399+
options->table_separator_style = TableSeparatorStyle::Comma;
400+
}
401+
else if(style == "semicolon")
402+
{
403+
options->table_separator_style = TableSeparatorStyle::Semicolon;
404+
}
405+
}
406+
407+
if(configMap.count("trailing_table_separator"))
408+
{
409+
auto action = configMap.at("trailing_table_separator");
410+
if(action == "keep")
411+
{
412+
options->trailing_table_separator = TrailingTableSeparator::Keep;
413+
}
414+
else if(action == "never")
415+
{
416+
options->trailing_table_separator = TrailingTableSeparator::Never;
417+
}
418+
else if(action == "always")
419+
{
420+
options->trailing_table_separator = TrailingTableSeparator::Always;
421+
}
422+
else if(action == "smart")
423+
{
424+
options->trailing_table_separator = TrailingTableSeparator::Smart;
425+
}
426+
}
427+
428+
390429
if (configMap.count("end_of_line"))
391430
{
392431
auto lineSeparatorSymbol = configMap.at("end_of_line");

CodeService/src/LuaFormatter.cpp

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "CodeService/FormatElement/NoIndentElement.h"
1717
#include "CodeService/FormatElement/SerializeContext.h"
1818
#include "CodeService/FormatElement/IndentOnLineBreakElement.h"
19-
#include "CodeService/FormatElement/PlaceholderElement.h"
19+
#include "CodeService/FormatElement/SepElement.h"
2020
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
2121
#include "CodeService/FormatElement/MaxSpaceElement.h"
2222
#include "CodeService/FormatElement/StringLiteralElement.h"
@@ -246,6 +246,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNode(std::shared_ptr<LuaAstNo
246246
{
247247
return FormatStringLiteralExpression(node);
248248
}
249+
case LuaAstNodeType::TableFieldSep:
250+
{
251+
return FormatTableSep(node);
252+
}
249253
case LuaAstNodeType::LiteralExpression:
250254
default:
251255
{
@@ -1810,6 +1814,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatStringLiteralExpression(
18101814
return std::make_shared<StringLiteralElement>(stringLiteralExpression);
18111815
}
18121816

1817+
std::shared_ptr<FormatElement> LuaFormatter::FormatTableSep(std::shared_ptr<LuaAstNode> tableSep)
1818+
{
1819+
return std::make_shared<SepElement>(tableSep);
1820+
}
1821+
18131822
void LuaFormatter::DefaultHandle(std::shared_ptr<LuaAstNode> node, std::shared_ptr<FormatElement> envElement)
18141823
{
18151824
envElement->AddChild(FormatNode(node));
@@ -1920,12 +1929,15 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
19201929
{
19211930
canAlign = false;
19221931
}
1932+
std::shared_ptr<LuaAstNode> current = nullptr;
1933+
std::shared_ptr<LuaAstNode> nextSibling = nullptr;
19231934

19241935
for (; it != siblings.end(); ++it)
19251936
{
1926-
auto current = *it;
1927-
auto nextSibling = NextNode(it, siblings);
1937+
current = *it;
1938+
nextSibling = NextNode(it, siblings);
19281939

1940+
// unreachable
19291941
if (nextSibling == nullptr)
19301942
{
19311943
layout->AddChild(FormatNode(current));
@@ -1995,6 +2007,91 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C
19952007
}
19962008
}
19972009

2010+
// table field finish
2011+
if (nextSibling != nullptr
2012+
&& nextSibling->GetTokenType() == '}'
2013+
&& _options.trailing_table_separator != TrailingTableSeparator::Keep)
2014+
{
2015+
// find table field
2016+
auto& layoutChildren = layout->GetChildren();
2017+
auto rIt = layoutChildren.rbegin();
2018+
for (; rIt != layoutChildren.rend(); rIt++)
2019+
{
2020+
auto child = *rIt;
2021+
// table field
2022+
if (child->Is(FormatElementType::ExpressionElement))
2023+
{
2024+
break;
2025+
}
2026+
}
2027+
2028+
// if founded
2029+
if (rIt != layoutChildren.rend())
2030+
{
2031+
std::shared_ptr<SepElement> sep = nullptr;
2032+
auto sepIt = rIt.base();
2033+
2034+
for (; sepIt != layoutChildren.end(); sepIt++)
2035+
{
2036+
auto child = *sepIt;
2037+
if (child->HasValidTextRange())
2038+
{
2039+
if (child->Is(FormatElementType::SepElement))
2040+
{
2041+
sep = std::dynamic_pointer_cast<SepElement>(child);
2042+
}
2043+
break;
2044+
}
2045+
}
2046+
2047+
switch (_options.trailing_table_separator)
2048+
{
2049+
case TrailingTableSeparator::Never:
2050+
{
2051+
if (sep)
2052+
{
2053+
sep->SetEmpty();
2054+
}
2055+
break;
2056+
}
2057+
case TrailingTableSeparator::Always:
2058+
{
2059+
if (!sep)
2060+
{
2061+
sep = std::make_shared<SepElement>();
2062+
layoutChildren.insert(rIt.base(), sep);
2063+
}
2064+
2065+
break;
2066+
}
2067+
case TrailingTableSeparator::Smart:
2068+
{
2069+
auto line = _parser->GetLine((*rIt)->GetTextRange().EndOffset);
2070+
if (line == leftBraceLine)
2071+
{
2072+
// 不要合并上去
2073+
if (sep)
2074+
{
2075+
sep->SetEmpty();
2076+
}
2077+
}
2078+
else if (!sep)
2079+
{
2080+
sep = std::make_shared<SepElement>();
2081+
layoutChildren.insert(rIt.base(), sep);
2082+
}
2083+
2084+
break;
2085+
}
2086+
default:
2087+
{
2088+
break;
2089+
}
2090+
}
2091+
}
2092+
}
2093+
2094+
19982095
if (canAlign && _options.continuous_assign_table_field_align_to_equal_sign && layout->GetChildren().size() > 1)
19992096
{
20002097
auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>('=');

CodeService/src/TypeFormat/LuaTypeFormat.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ void LuaTypeFormat::FormatLine(int line)
205205
LuaCodeStyleOptions temp = _options;
206206
temp.insert_final_newline = true;
207207
temp.remove_expression_list_finish_comma = false;
208+
temp.trailing_table_separator = TrailingTableSeparator::Keep;
208209
LuaFormatter formatter(_parser, temp);
209210
formatter.BuildFormattedElement();
210211

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,13 @@ keep_line_after_repeat_statement = minLine:1
176176
keep_line_after_for_statement = minLine:1
177177
keep_line_after_local_or_assign_statement = minLine:1
178178
keep_line_after_function_define_statement = minLine:1
179+
[{table_separator_style-eq-comma.lua}]
180+
table_separator_style = comma
181+
[{table_separator_style-eq-semicolon.lua}]
182+
table_separator_style = semicolon
183+
[{trailing_table_separator-eq-never.lua}]
184+
trailing_table_separator = never
185+
[{trailing_table_separator-eq-always.lua}]
186+
trailing_table_separator = always
187+
[{trailing_table_separator-eq-smart.lua}]
188+
trailing_table_separator = smart
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local t = { aa = b, }
2+
local d = { hh = eee, hiiiii, }
3+
local e = { hiii; ddddd; iiiiiii = 123; }
4+
local okokok = {
5+
aaaa --comment
6+
}
7+
local ododod = {
8+
aaaaa, --okjfoipwifw
9+
--njfjowjofjwp
10+
}
11+
local opopop = {
12+
ffffffff;
13+
aaaaa; --okjfoipwifw
14+
--njfjowjofjwp
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local t = { aa = b, }
2+
local d = { hh = eee, hiiiii, }
3+
local e = { hiii; ddddd; iiiiiii = 123; }
4+
local okokok = {
5+
aaaa --comment
6+
}
7+
local ododod = {
8+
aaaaa, --okjfoipwifw
9+
--njfjowjofjwp
10+
}
11+
local opopop = {
12+
ffffffff;
13+
aaaaa; --okjfoipwifw
14+
--njfjowjofjwp
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local d = { aaaaaaaa }
2+
local e = { hhhhhh }
3+
local ssssss = {
4+
aaaaa -- okokok
5+
}
6+
local ddddddd = {
7+
hello ----
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
local d = { aaaaaaaa, }
2+
local e = { hhhhhh; }
3+
local ssssss = {
4+
aaaaaa, -- okokok
5+
}
6+
local ddddddd = {
7+
hello; ----
8+
}

0 commit comments

Comments
 (0)