Skip to content

Commit c8d8c56

Browse files
committed
改进tab缩进对其他对齐策略的支持
1 parent 9d2bb7a commit c8d8c56

File tree

8 files changed

+74
-126
lines changed

8 files changed

+74
-126
lines changed

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CodeService/FormatElement/FormatElement.h"
2+
#include "Util/format.h"
23

34
FormatElement::FormatElement(TextRange range)
45
: _textRange(range)
@@ -132,6 +133,72 @@ int FormatElement::GetNextValidLine(FormatContext& ctx, ChildIterator it, Format
132133
}
133134
}
134135

136+
void FormatElement::GeneralIndentDiagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
137+
{
138+
for (auto it = _children.begin(); it != _children.end(); ++it)
139+
{
140+
const auto child = *it;
141+
142+
if (child->HasValidTextRange()
143+
&& child->GetType() != FormatElementType::IndentElement
144+
&& child->GetType() != FormatElementType::NoIndentElement)
145+
{
146+
auto writeCharacter = ctx.GetCharacterCount();
147+
if (writeCharacter != 0)
148+
{
149+
goto endIndentDiagnose;
150+
}
151+
152+
auto range = child->GetTextRange();
153+
auto line = ctx.GetLine(range.StartOffset);
154+
auto character = ctx.GetColumn(range.StartOffset);
155+
auto indentState = ctx.CalculateIndentState(range.StartOffset);
156+
auto state = ctx.GetCurrentIndent();
157+
if (ctx.GetOptions().indent_style != indentState.Style)
158+
{
159+
ctx.PushDiagnosis(
160+
format(LText("incorrect indentation style, expect {}, but here is {}"),
161+
GetIndentStyleName(state.Style),
162+
GetIndentStyleName(indentState.Style)
163+
),
164+
LuaDiagnosisPosition(line, 0),
165+
LuaDiagnosisPosition(line, character)
166+
);
167+
goto endIndentDiagnose;
168+
}
169+
170+
if (indentState.Style == IndentStyle::Space)
171+
{
172+
if (state.SpaceIndent != indentState.SpaceIndent)
173+
{
174+
ctx.PushDiagnosis(
175+
format(LText("incorrect indentation {}, here need {} space indentation"),
176+
indentState.SpaceIndent, state.SpaceIndent),
177+
LuaDiagnosisPosition(line, 0),
178+
LuaDiagnosisPosition(line, character)
179+
);
180+
}
181+
goto endIndentDiagnose;
182+
}
183+
else
184+
{
185+
if (state.SpaceIndent != indentState.SpaceIndent || state.TabIndent != indentState.TabIndent)
186+
{
187+
ctx.PushDiagnosis(
188+
format(LText("incorrect indentation, here need {} tab and {} space indentation"),
189+
state.TabIndent, state.SpaceIndent),
190+
LuaDiagnosisPosition(line, 0),
191+
LuaDiagnosisPosition(line, character)
192+
);
193+
}
194+
}
195+
}
196+
197+
endIndentDiagnose:
198+
child->Diagnosis(ctx, it, *this);
199+
}
200+
}
201+
135202
void FormatElement::CopyFrom(std::shared_ptr<FormatElement> node)
136203
{
137204
_textRange = node->_textRange;

CodeService/src/FormatElement/IndentElement.cpp

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -57,63 +57,6 @@ void IndentElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, Forma
5757
{
5858
ctx.AddIndent(FormatContext::IndentState{_specialIndent, 0, IndentStyle::Space});
5959
}
60-
61-
62-
for (auto it = _children.begin(); it != _children.end(); ++it)
63-
{
64-
const auto child = *it;
65-
66-
if (child->HasValidTextRange()
67-
&& child->GetType() != FormatElementType::IndentElement
68-
&& child->GetType() != FormatElementType::NoIndentElement)
69-
{
70-
auto range = child->GetTextRange();
71-
auto line = ctx.GetLine(range.StartOffset);
72-
auto character = ctx.GetColumn(range.StartOffset);
73-
auto indentState = ctx.CalculateIndentState(range.StartOffset);
74-
auto state = ctx.GetCurrentIndent();
75-
if (ctx.GetOptions().indent_style != indentState.Style)
76-
{
77-
ctx.PushDiagnosis(
78-
format(LText("incorrect indentation style, expect {}, but here is {}"),
79-
GetIndentStyleName(state.Style),
80-
GetIndentStyleName(indentState.Style)
81-
),
82-
LuaDiagnosisPosition(line, 0),
83-
LuaDiagnosisPosition(line, character)
84-
);
85-
goto endIndentDiagnose;
86-
}
87-
88-
if (indentState.Style == IndentStyle::Space) {
89-
if (state.SpaceIndent != indentState.SpaceIndent)
90-
{
91-
ctx.PushDiagnosis(
92-
format(LText("incorrect indentation {}, here need {} space indentation"),
93-
indentState.SpaceIndent, state.SpaceIndent),
94-
LuaDiagnosisPosition(line, 0),
95-
LuaDiagnosisPosition(line, character)
96-
);
97-
}
98-
goto endIndentDiagnose;
99-
}
100-
else
101-
{
102-
if (state.SpaceIndent != indentState.SpaceIndent || state.TabIndent != indentState.TabIndent)
103-
{
104-
ctx.PushDiagnosis(
105-
format(LText("incorrect indentation, here need {} tab and {} space indentation"),
106-
state.TabIndent, state.SpaceIndent),
107-
LuaDiagnosisPosition(line, 0),
108-
LuaDiagnosisPosition(line, character)
109-
);
110-
}
111-
}
112-
}
113-
114-
endIndentDiagnose:
115-
child->Diagnosis(ctx, it, *this);
116-
}
117-
60+
GeneralIndentDiagnosis(ctx, selfIt, parent);
11861
ctx.RecoverIndent();
11962
}

CodeService/src/FormatElement/LongExpressionLayoutElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "CodeService/FormatElement/LongExpressionLayoutElement.h"
22
#include "CodeService/FormatElement/KeepElement.h"
33

4-
LongExpressionLayoutElement::LongExpressionLayoutElement(int continuationIndent, bool isAssignLeftExprList)
4+
LongExpressionLayoutElement::LongExpressionLayoutElement(int continuationIndent)
55
: _hasContinuation(false),
66
_continuationIndent(continuationIndent)
77
{

CodeService/src/FormatElement/NoIndentElement.cpp

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,11 @@ void NoIndentElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, For
2020
ctx.RecoverIndent();
2121
}
2222

23-
void NoIndentElement::Diagnosis(DiagnosisContext& ctx, ChildIterator shared_ptr, FormatElement& parent)
23+
void NoIndentElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
2424
{
2525
ctx.AddIndent(ctx.GetLastIndent());
2626

27-
28-
for (auto it = _children.begin(); it != _children.end(); ++it)
29-
{
30-
const auto child = *it;
31-
32-
if (child->HasValidTextRange()
33-
&& child->GetType() != FormatElementType::IndentElement
34-
&& child->GetType() != FormatElementType::NoIndentElement)
35-
{
36-
auto range = child->GetTextRange();
37-
auto line = ctx.GetLine(range.StartOffset);
38-
auto character = ctx.GetColumn(range.StartOffset);
39-
auto indentState = ctx.CalculateIndentState(range.StartOffset);
40-
auto state = ctx.GetCurrentIndent();
41-
if (ctx.GetOptions().indent_style != indentState.Style)
42-
{
43-
ctx.PushDiagnosis(
44-
format(LText("incorrect indentation style, expect {}, but here is {}"),
45-
GetIndentStyleName(state.Style),
46-
GetIndentStyleName(indentState.Style)
47-
),
48-
LuaDiagnosisPosition(line, 0),
49-
LuaDiagnosisPosition(line, character)
50-
);
51-
goto endIndentDiagnose;
52-
}
53-
54-
if (indentState.Style == IndentStyle::Space)
55-
{
56-
if (state.SpaceIndent != indentState.SpaceIndent)
57-
{
58-
ctx.PushDiagnosis(
59-
format(LText("incorrect indentation {}, here need {} space indentation"),
60-
indentState.SpaceIndent, state.SpaceIndent),
61-
LuaDiagnosisPosition(line, 0),
62-
LuaDiagnosisPosition(line, character)
63-
);
64-
}
65-
goto endIndentDiagnose;
66-
}
67-
else
68-
{
69-
if (state.SpaceIndent != indentState.SpaceIndent || state.TabIndent != indentState.TabIndent)
70-
{
71-
ctx.PushDiagnosis(
72-
format(LText("incorrect indentation, here need {} tab and {} space indentation"),
73-
state.TabIndent, state.SpaceIndent),
74-
LuaDiagnosisPosition(line, 0),
75-
LuaDiagnosisPosition(line, character)
76-
);
77-
}
78-
}
79-
}
80-
81-
endIndentDiagnose:
82-
child->Diagnosis(ctx, it, *this);
83-
}
27+
GeneralIndentDiagnosis(ctx, selfIt, parent);
8428

8529
ctx.RecoverIndent();
8630
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,4 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
328328
styleOption.second = NameStyleRuleMatcher::ParseFrom(value);
329329
}
330330
}
331-
332-
333-
if (options->indent_style == IndentStyle::Tab)
334-
{
335-
options->continuous_assign_table_field_align_to_equal_sign = false;
336-
options->align_table_field_to_first_field = false;
337-
}
338331
}

CodeService/src/LuaFormatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatExpressionList(std::shared_pt
554554

555555
std::shared_ptr<FormatElement> LuaFormatter::FormatAssignLeftExpressionList(std::shared_ptr<LuaAstNode> expressionList)
556556
{
557-
auto env = std::make_shared<LongExpressionLayoutElement>(_options.continuation_indent_size, true);
557+
auto env = std::make_shared<LongExpressionLayoutElement>(_options.continuation_indent_size);
558558

559559
for (auto& node : expressionList->GetChildren())
560560
{

include/CodeService/FormatElement/FormatElement.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class FormatElement : public std::enable_shared_from_this<FormatElement>
5454
static int GetLastValidLine(FormatContext& ctx, ChildIterator it, FormatElement& parent);
5555
static int GetNextValidLine(FormatContext& ctx, ChildIterator it, FormatElement& parent);
5656

57+
void GeneralIndentDiagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent);
5758
void AddTextRange(TextRange range);
5859

5960
TextRange _textRange;

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, bool isAssignLeftExprList = false);
8+
LongExpressionLayoutElement(int continuationIndent);
99

1010
FormatElementType GetType() override;
1111

0 commit comments

Comments
 (0)