|
16 | 16 | #include "CodeService/FormatElement/NoIndentElement.h" |
17 | 17 | #include "CodeService/FormatElement/SerializeContext.h" |
18 | 18 | #include "CodeService/FormatElement/IndentOnLineBreakElement.h" |
19 | | -#include "CodeService/FormatElement/PlaceholderElement.h" |
| 19 | +#include "CodeService/FormatElement/SepElement.h" |
20 | 20 | #include "CodeService/FormatElement/AlignIfLayoutElement.h" |
21 | 21 | #include "CodeService/FormatElement/MaxSpaceElement.h" |
22 | 22 | #include "CodeService/FormatElement/StringLiteralElement.h" |
@@ -246,6 +246,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNode(std::shared_ptr<LuaAstNo |
246 | 246 | { |
247 | 247 | return FormatStringLiteralExpression(node); |
248 | 248 | } |
| 249 | + case LuaAstNodeType::TableFieldSep: |
| 250 | + { |
| 251 | + return FormatTableSep(node); |
| 252 | + } |
249 | 253 | case LuaAstNodeType::LiteralExpression: |
250 | 254 | default: |
251 | 255 | { |
@@ -1810,6 +1814,11 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatStringLiteralExpression( |
1810 | 1814 | return std::make_shared<StringLiteralElement>(stringLiteralExpression); |
1811 | 1815 | } |
1812 | 1816 |
|
| 1817 | +std::shared_ptr<FormatElement> LuaFormatter::FormatTableSep(std::shared_ptr<LuaAstNode> tableSep) |
| 1818 | +{ |
| 1819 | + return std::make_shared<SepElement>(tableSep); |
| 1820 | +} |
| 1821 | + |
1813 | 1822 | void LuaFormatter::DefaultHandle(std::shared_ptr<LuaAstNode> node, std::shared_ptr<FormatElement> envElement) |
1814 | 1823 | { |
1815 | 1824 | envElement->AddChild(FormatNode(node)); |
@@ -1920,12 +1929,15 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C |
1920 | 1929 | { |
1921 | 1930 | canAlign = false; |
1922 | 1931 | } |
| 1932 | + std::shared_ptr<LuaAstNode> current = nullptr; |
| 1933 | + std::shared_ptr<LuaAstNode> nextSibling = nullptr; |
1923 | 1934 |
|
1924 | 1935 | for (; it != siblings.end(); ++it) |
1925 | 1936 | { |
1926 | | - auto current = *it; |
1927 | | - auto nextSibling = NextNode(it, siblings); |
| 1937 | + current = *it; |
| 1938 | + nextSibling = NextNode(it, siblings); |
1928 | 1939 |
|
| 1940 | + // unreachable |
1929 | 1941 | if (nextSibling == nullptr) |
1930 | 1942 | { |
1931 | 1943 | layout->AddChild(FormatNode(current)); |
@@ -1995,6 +2007,91 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignTableField(LuaAstNode::C |
1995 | 2007 | } |
1996 | 2008 | } |
1997 | 2009 |
|
| 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 | + |
1998 | 2095 | if (canAlign && _options.continuous_assign_table_field_align_to_equal_sign && layout->GetChildren().size() > 1) |
1999 | 2096 | { |
2000 | 2097 | auto alignmentLayoutElement = std::make_shared<AlignmentLayoutElement>('='); |
|
0 commit comments