Skip to content

Commit c6dc71f

Browse files
committed
恢复缩进检测,修复格式化bug
1 parent 65d8917 commit c6dc71f

File tree

11 files changed

+66
-4
lines changed

11 files changed

+66
-4
lines changed

CodeService/src/FormatElement/KeepElement.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ void KeepElement::Serialize(FormatContext& ctx, int position, FormatElement& par
3939

4040
void KeepElement::Diagnosis(DiagnosisContext& ctx, int position, FormatElement& parent)
4141
{
42+
if(position == 0)
43+
{
44+
return;
45+
}
46+
4247
int lastOffset = getLastValidOffset(position, parent);
4348
int nextOffset = getNextValidOffset(position, parent);
4449

@@ -57,4 +62,8 @@ void KeepElement::Diagnosis(DiagnosisContext& ctx, int position, FormatElement&
5762
ctx.PushDiagnosis(format(LText("here need keep {} space"), _keepBlank), TextRange(lastOffset, nextOffset));
5863
}
5964
}
65+
else
66+
{
67+
ctx.SetCharacterCount(0);
68+
}
6069
}

CodeService/src/FormatElement/KeepLineElement.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,6 @@ void KeepLineElement::Diagnosis(DiagnosisContext& ctx, int position, FormatEleme
5656
TextRange(lastElementOffset, nextElementOffset));
5757
}
5858
}
59+
ctx.SetCharacterCount(0);
60+
5961
}

CodeService/src/FormatElement/LineElement.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ void LineElement::Serialize(FormatContext& ctx, int position, FormatElement& par
1515
{
1616
ctx.PrintLine(1);
1717
}
18+
19+
void LineElement::Diagnosis(DiagnosisContext& ctx, int position, FormatElement& parent)
20+
{
21+
ctx.SetCharacterCount(0);
22+
}

CodeService/src/FormatElement/LongExpressionLayoutElement.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "CodeService/FormatElement/LongExpressionLayoutElement.h"
2+
#include "CodeService/FormatElement/KeepElement.h"
23

3-
LongExpressionLayoutElement::LongExpressionLayoutElement(int continuationIndent)
4+
LongExpressionLayoutElement::LongExpressionLayoutElement(int continuationIndent, bool isAssignLeftExprList)
45
: _hasContinuation(false),
56
_continuationIndent(continuationIndent)
67
{

CodeService/src/FormatElement/MinLineElement.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ void MinLineElement::Diagnosis(DiagnosisContext& ctx, int position, FormatElemen
5959
{
6060
ctx.PushDiagnosis(format(LText("here need at least {} line"), _line), TextRange(lastOffset, nextOffset));
6161
}
62+
ctx.SetCharacterCount(0);
6263
}

CodeService/src/FormatElement/StatementElement.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,9 @@ void StatementElement::Serialize(FormatContext& ctx, int position, FormatElement
1111

1212
ctx.PrintLine(1);
1313
}
14+
15+
void StatementElement::Diagnosis(DiagnosisContext& ctx, int position, FormatElement& parent)
16+
{
17+
FormatElement::Diagnosis(ctx, position, parent);
18+
ctx.SetCharacterCount(0);
19+
}

CodeService/src/LuaFormatter.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAssignment(std::shared_ptr<Lu
382382
env->Add<KeepBlankElement>(1);
383383
isLeftExprList = false;
384384
}
385+
385386
break;
386387
}
387388
default:
@@ -461,6 +462,36 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionList(std::shared_pt
461462
return env;
462463
}
463464

465+
std::shared_ptr<FormatElement> LuaFormatter::FormatAssignLeftExpressionList(std::shared_ptr<LuaAstNode> expressionList)
466+
{
467+
auto env = std::make_shared<LongExpressionLayoutElement>(_options.continuation_indent, true);
468+
469+
for (auto node : expressionList->GetChildren())
470+
{
471+
switch (node->GetType())
472+
{
473+
case LuaAstNodeType::Expression:
474+
{
475+
auto subEnv = std::make_shared<SubExpressionElement>();
476+
env->AddChild(FormatExpression(node, subEnv));
477+
env->Add<KeepElement>(0);
478+
break;
479+
}
480+
case LuaAstNodeType::GeneralOperator:
481+
{
482+
env->Add<TextElement>(node);
483+
env->Add<KeepElement>(1);
484+
break;
485+
}
486+
default:
487+
DefaultHandle(node, env);
488+
env->Add<KeepElement>(1);
489+
}
490+
}
491+
492+
return env;
493+
}
494+
464495
std::shared_ptr<FormatElement> LuaFormatter::FormatComment(std::shared_ptr<LuaAstNode> comment)
465496
{
466497
auto env = std::make_shared<ExpressionElement>();
@@ -910,6 +941,10 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatCallArgList(std::shared_ptr<L
910941
}
911942
else
912943
{
944+
auto& exprListChildren = exprListEnv->GetChildren();
945+
auto keepElement = std::make_shared<KeepElement>(0);
946+
exprListChildren.insert(exprListChildren.begin(), keepElement);
947+
913948
env->AddChild(exprListEnv);
914949
}
915950

@@ -1444,8 +1479,8 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatNodeAndBlockOrEnd(int& curren
14441479
{
14451480
env->Add<KeepBlankElement>(1);
14461481
env->Add<TextElement>(comment);
1482+
currentIndex++;
14471483
}
1448-
currentIndex++;
14491484
}
14501485

14511486
bool blockExist = false;

include/CodeService/FormatElement/LineElement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ class LineElement : public FormatElement
1010
FormatElementType GetType() override;
1111

1212
void Serialize(FormatContext& ctx, int position, FormatElement& parent) override;
13+
void Diagnosis(DiagnosisContext& ctx, int position, FormatElement& parent) override;
1314
};

include/CodeService/FormatElement/LongExpressionLayoutElement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class LongExpressionLayoutElement : public FormatElement
66
{
77
public:
8-
LongExpressionLayoutElement(int continuationIndent);
8+
LongExpressionLayoutElement(int continuationIndent, bool isAssignLeftExprList = false);
99

1010
FormatElementType GetType() override;
1111

include/CodeService/FormatElement/StatementElement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class StatementElement : public FormatElement
99
FormatElementType GetType() override;
1010

1111
void Serialize(FormatContext& ctx, int position, FormatElement& parent) override;
12-
12+
void Diagnosis(DiagnosisContext& ctx, int position, FormatElement& parent) override;
1313
};

0 commit comments

Comments
 (0)