Skip to content

Commit faf194e

Browse files
committed
会试图让长字符串或者长注释会采用配置的行尾
1 parent 8fa05ab commit faf194e

File tree

8 files changed

+59
-23
lines changed

8 files changed

+59
-23
lines changed

CodeService/src/FormatElement/RangeFormatContext.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ void RangeFormatContext::Print(std::string_view text, TextRange range)
4848
}
4949
}
5050
}
51-
_buffer.append(text);
52-
_characterCount += text.size();
51+
InnerPrintText(text, range);
5352

5453
if (startLine < _formattedRange.StartLine)
5554
{

CodeService/src/FormatElement/SerializeContext.cpp

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ void SerializeContext::Print(std::string_view text, TextRange range)
3131
}
3232
}
3333
}
34-
_buffer.append(text);
35-
_characterCount += text.size();
34+
InnerPrintText(text, range);
3635
}
3736

3837
void SerializeContext::Print(char ch, int Offset)
@@ -125,3 +124,41 @@ void SerializeContext::PrintIndent(std::size_t indent, IndentStyle style)
125124
}
126125
}
127126
}
127+
128+
void SerializeContext::InnerPrintText(std::string_view text, TextRange range)
129+
{
130+
if (GetLine(range.StartOffset) == GetLine(range.EndOffset))
131+
{
132+
_buffer.append(text);
133+
_characterCount += text.size();
134+
return;
135+
}
136+
137+
std::size_t lastStartIndex = 0;
138+
139+
for (std::size_t i = 0; i < text.size(); i++)
140+
{
141+
char ch = text[i];
142+
143+
if (ch == '\r' || ch == '\n')
144+
{
145+
if (i - lastStartIndex != 0)
146+
{
147+
_buffer.append(text.substr(lastStartIndex, i - lastStartIndex));
148+
}
149+
_buffer.append(_options.end_of_line);
150+
lastStartIndex = i + 1;
151+
if (ch == '\r' && lastStartIndex < text.size() && text[lastStartIndex] == '\n')
152+
{
153+
lastStartIndex++;
154+
i++;
155+
}
156+
}
157+
}
158+
159+
if (lastStartIndex < text.size())
160+
{
161+
_buffer.append(text.substr(lastStartIndex, text.size() - lastStartIndex));
162+
}
163+
_characterCount = GetColumn(range.EndOffset);
164+
}

CodeService/src/FormatElement/StringLiteralElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ bool StringLiteralElement::ExistDel(char del)
5555
for (std::size_t i = 0; i < text.size(); ++i)
5656
{
5757
ch = text[i];
58-
if(ch == del)
58+
if (ch == del)
5959
{
6060
return true;
6161
}
62-
else if(ch == '\\')
62+
else if (ch == '\\')
6363
{
6464
++i;
6565
}

CodeService/src/LuaFormatter.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatLocalStatement(std::shared_pt
460460
}
461461
case LuaAstNodeType::Comment:
462462
{
463-
env->AddChild(FormatNode(node));
463+
env->AddChild(FormatComment(node));
464464
env->Add<KeepElement>(1);
465465
break;
466466
}
@@ -563,7 +563,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNameDefList(std::shared_ptr<L
563563
}
564564
case LuaAstNodeType::Comment:
565565
{
566-
env->AddChild(FormatNode(node));
566+
env->AddChild(FormatComment(node));
567567
env->Add<KeepElement>(1);
568568
break;
569569
}
@@ -662,10 +662,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAssignLeftExpressionList(std:
662662

663663
std::shared_ptr<FormatElement> LuaFormatter::FormatComment(std::shared_ptr<LuaAstNode> comment)
664664
{
665-
auto env = std::make_shared<ExpressionElement>();
666-
env->Add<TextElement>(comment);
667-
668-
return env;
665+
return std::make_shared<TextElement>(comment);
669666
}
670667

671668
std::shared_ptr<FormatElement> LuaFormatter::FormatBreakStatement(std::shared_ptr<LuaAstNode> breakNode)
@@ -712,7 +709,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatReturnStatement(std::shared_p
712709
}
713710
case LuaAstNodeType::Comment:
714711
{
715-
env->AddChild(FormatNode(child));
712+
env->AddChild(FormatComment(child));
716713
env->Add<KeepElement>(1);
717714
break;
718715
}
@@ -1330,7 +1327,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatParamList(std::shared_ptr<Lua
13301327
}
13311328
case LuaAstNodeType::Comment:
13321329
{
1333-
paramListLayoutEnv->Add<TextElement>(child);
1330+
paramListLayoutEnv->AddChild(FormatComment(child));
13341331
paramListLayoutEnv->Add<KeepElement>(1);
13351332
break;
13361333
}
@@ -1532,7 +1529,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
15321529
}
15331530
case LuaAstNodeType::Comment:
15341531
{
1535-
env->AddChild(FormatNode(child));
1532+
env->AddChild(FormatComment(child));
15361533
env->Add<KeepElement>(1);
15371534
break;
15381535
}
@@ -1630,7 +1627,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
16301627
if (lastStatementEnv)
16311628
{
16321629
lastStatementEnv->Add<KeepBlankElement>(1);
1633-
lastStatementEnv->Add<TextElement>(nextChild);
1630+
lastStatementEnv->AddChild(FormatNode(nextChild));
16341631
}
16351632
//else 应该不存在这种情况
16361633
}
@@ -1648,7 +1645,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
16481645

16491646
if (nextChild->GetType() == LuaAstNodeType::Comment)
16501647
{
1651-
auto comment = FormatNode(nextChild);
1648+
auto comment = FormatComment(nextChild);
16521649
auto commentStatement = std::make_shared<StatementElement>();
16531650
commentStatement->AddChild(comment);
16541651
env->AddChild(commentStatement);
@@ -1789,7 +1786,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(LuaAstNode:
17891786
if (nextLine == currentLine)
17901787
{
17911788
env->Add<KeepBlankElement>(1);
1792-
env->Add<TextElement>(comment);
1789+
env->AddChild(FormatComment(comment));
17931790
++it;
17941791
}
17951792
}
@@ -1889,7 +1886,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlockFromParent(LuaAstNode::C
18891886
for (auto comment : comments)
18901887
{
18911888
auto commentStatement = std::make_shared<StatementElement>();
1892-
commentStatement->Add<TextElement>(comment);
1889+
commentStatement->AddChild(FormatComment(comment));
18931890
indentElement->AddChild(commentStatement);
18941891
indentElement->Add<KeepLineElement>();
18951892
}
@@ -2084,7 +2081,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
20842081
}
20852082
case LuaAstNodeType::Comment:
20862083
{
2087-
env->Add<TextElement>(child);
2084+
env->AddChild(FormatComment(child));
20882085
env->Add<KeepElement>(1);
20892086
break;
20902087
}
@@ -2357,7 +2354,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<Lu
23572354
}
23582355
case LuaAstNodeType::Comment:
23592356
{
2360-
auto comment = FormatNode(statement);
2357+
auto comment = FormatComment(statement);
23612358
auto commentStatement = std::make_shared<StatementElement>();
23622359
commentStatement->AddChild(comment);
23632360
indentEnv->AddChild(commentStatement);
@@ -2380,7 +2377,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatRangeBlock(std::shared_ptr<Lu
23802377
if (currentLine == nextLine)
23812378
{
23822379
statEnv->Add<KeepBlankElement>(1);
2383-
statEnv->Add<TextElement>(next);
2380+
statEnv->AddChild(FormatComment(next));
23842381
++it;
23852382
}
23862383
}

include/CodeService/FormatElement/SerializeContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class SerializeContext : public FormatContext
2020
void SetReadySize(std::size_t size);
2121
protected:
2222
void PrintIndent(std::size_t indent, IndentStyle style);
23+
void InnerPrintText(std::string_view text, TextRange range);
2324
std::string _buffer;
2425
};
2526

include/CodeService/LuaCodeStyleOptions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class LuaCodeStyleOptions
3939
*/
4040
int continuation_indent_size = 4;
4141

42+
4243
bool local_assign_continuation_align_to_first_expression = false;
4344

4445
/*

include/CodeService/LuaFormatter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class LuaFormatter
133133

134134
// special handle range format
135135
std::shared_ptr<FormatElement> FormatRangeBlock(std::shared_ptr<LuaAstNode> blockNode, LuaFormatRange& validRange);
136+
136137
private:
137138
std::shared_ptr<LuaParser> _parser;
138139
LuaCodeStyleOptions& _options;

include/LuaParser/TextRange.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class TextRange
77
public:
88
explicit TextRange(int startOffset = 0, int endOffset = -1);
99

10-
explicit TextRange(std::size_t startOffset, std::size_t endOffset);
10+
TextRange(std::size_t startOffset, std::size_t endOffset);
1111

1212
bool IsEmpty() const;
1313

0 commit comments

Comments
 (0)