Skip to content

Commit 79644a0

Browse files
committed
resolve #13
1 parent 3d06311 commit 79644a0

File tree

15 files changed

+141
-15
lines changed

15 files changed

+141
-15
lines changed

CodeService/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ target_sources(CodeService
4444
${CodeService_SOURCE_DIR}/src/FormatElement/FormatElement.cpp
4545
${CodeService_SOURCE_DIR}/src/FormatElement/LineElement.cpp
4646
${CodeService_SOURCE_DIR}/src/FormatElement/NoIndentElement.cpp
47+
${CodeService_SOURCE_DIR}/src/FormatElement/MaxSpaceElement.cpp
4748
${CodeService_SOURCE_DIR}/src/FormatElement/AlignmentElement.cpp
4849
${CodeService_SOURCE_DIR}/src/FormatElement/ExpressionElement.cpp
4950
${CodeService_SOURCE_DIR}/src/FormatElement/FormatContext.cpp

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,7 @@ FormatContext::IndentState FormatContext::GetLastIndent() const
134134
bool FormatContext::OnlyEmptyCharBefore(int offset)
135135
{
136136
auto file = _parser->GetLuaFile();
137-
auto source = file->GetSource();
138-
auto line = file->GetLine(offset);
139-
auto start = file->GetOffsetFromPosition(line, 0);
140-
141-
for (; start < offset; start++)
142-
{
143-
auto ch = source[start];
144-
if (ch != '\t' && ch != ' ')
145-
{
146-
return false;
147-
}
148-
}
149-
150-
return true;
137+
return file->OnlyEmptyCharBefore(offset);
151138
}
152139

153140
std::shared_ptr<LuaParser> FormatContext::GetParser()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "CodeService/FormatElement/MaxSpaceElement.h"
2+
#include "Util/format.h"
3+
4+
MaxSpaceElement::MaxSpaceElement(int space)
5+
: _space(space)
6+
{
7+
}
8+
9+
FormatElementType MaxSpaceElement::GetType()
10+
{
11+
return FormatElementType::MaxSpaceElement;
12+
}
13+
14+
void MaxSpaceElement::Serialize(SerializeContext& ctx, ChildIterator selfIt, FormatElement& parent)
15+
{
16+
auto maxSpace = 0;
17+
if (_space > 0)
18+
{
19+
maxSpace = _space;
20+
}
21+
22+
const int lastOffset = GetLastValidOffset(selfIt, parent);
23+
const int nextOffset = GetNextValidOffset(selfIt, parent);
24+
25+
if (lastOffset == -1 || nextOffset == -1)
26+
{
27+
return;
28+
}
29+
30+
int space = nextOffset - lastOffset - 1;
31+
32+
if (space > maxSpace)
33+
{
34+
space = maxSpace;
35+
}
36+
37+
ctx.PrintBlank(space);
38+
}
39+
40+
void MaxSpaceElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt, FormatElement& parent)
41+
{
42+
auto maxSpace = 0;
43+
if (_space > 0)
44+
{
45+
maxSpace = _space;
46+
}
47+
48+
const int lastOffset = GetLastValidOffset(selfIt, parent);
49+
const int nextOffset = GetNextValidOffset(selfIt, parent);
50+
51+
if (lastOffset == -1 || nextOffset == -1)
52+
{
53+
return;
54+
}
55+
56+
int space = nextOffset - lastOffset - 1;
57+
58+
if (space > maxSpace)
59+
{
60+
ctx.PushDiagnosis(format(LText("the maximum blank length here is {}"), maxSpace), TextRange(lastOffset, nextOffset));
61+
}
62+
}

CodeService/src/LuaEditorConfig.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ void LuaEditorConfig::ParseFromSection(std::shared_ptr<LuaCodeStyleOptions> opti
296296
configMap.at("if_condition_align_with_each_other") == "true";
297297
}
298298

299+
if(configMap.count("long_chain_expression_allow_one_space_after_colon"))
300+
{
301+
options->long_chain_expression_allow_one_space_after_colon =
302+
configMap.at("long_chain_expression_allow_one_space_after_colon") == "true";
303+
}
304+
299305
if (configMap.count("end_of_line"))
300306
{
301307
auto lineSeparatorSymbol = configMap.at("end_of_line");

CodeService/src/LuaFormatter.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "CodeService/FormatElement/IndentOnLineBreakElement.h"
2020
#include "CodeService/FormatElement/PlaceholderElement.h"
2121
#include "CodeService/FormatElement/AlignIfLayoutElement.h"
22+
#include "CodeService/FormatElement/MaxSpaceElement.h"
2223
#include "Util/StringUtil.h"
2324

2425
bool nextMatch(LuaAstNode::ChildIterator it, LuaAstNodeType type, const LuaAstNode::ChildrenContainer& container)
@@ -1080,7 +1081,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIfStatement(std::shared_ptr<L
10801081
}
10811082
}
10821083

1083-
if(_options.if_condition_align_with_each_other)
1084+
if (_options.if_condition_align_with_each_other)
10841085
{
10851086
auto ifAlignLayout = std::make_shared<AlignIfElement>();
10861087
ifAlignLayout->CopyFrom(env);
@@ -2040,8 +2041,20 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
20402041
expressionAfterIndexOperator = true;
20412042
}
20422043
}
2044+
20432045
env->Add<TextElement>(child);
2046+
if (_options.long_chain_expression_allow_one_space_after_colon)
2047+
{
2048+
if (child->GetText() == ":" && _parser->GetLuaFile()->OnlyEmptyCharBefore(
2049+
child->GetTextRange().StartOffset))
2050+
{
2051+
env->Add<MaxSpaceElement>(1);
2052+
continue;
2053+
}
2054+
}
2055+
20442056
env->Add<KeepElement>(0);
2057+
20452058
break;
20462059
}
20472060
case LuaAstNodeType::Comment:

LuaParser/src/LuaFile.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,24 @@ void LuaFile::PushLine(int offset)
126126
_lineOffsetVec.push_back(offset);
127127
}
128128

129+
bool LuaFile::OnlyEmptyCharBefore(int offset)
130+
{
131+
auto source = GetSource();
132+
auto line = GetLine(offset);
133+
auto start = GetOffsetFromPosition(line, 0);
134+
135+
for (; start < offset; start++)
136+
{
137+
auto ch = source[start];
138+
if (ch != '\t' && ch != ' ')
139+
{
140+
return false;
141+
}
142+
}
143+
144+
return true;
145+
}
146+
129147
std::string& LuaFile::GetSource()
130148
{
131149
return _source;

Test/test_script/format_text/wait_format_by_option/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ weak_alignment_rule = true
146146
table_append_expression_no_space = true
147147
[{if_condition_align_with_each_other-eq-true.lua}]
148148
if_condition_align_with_each_other = true
149+
[{long_chain_expression_allow_one_space_after_colon-eq-true.lua}]
150+
long_chain_expression_allow_one_space_after_colon = true
149151
[{minLine-eq-1.lua}]
150152
keep_line_after_if_statement = minLine:1
151153
keep_line_after_do_statement = minLine:1
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
local getKeyMap = util.switch()
2+
: case 'local'
3+
: call(function(source)
4+
local t = 13
5+
end)

Test/test_script/format_text/wait_format_by_option_should_be/.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ weak_alignment_rule = true
141141
table_append_expression_no_space = true
142142
[{if_condition_align_with_each_other-eq-true.lua}]
143143
if_condition_align_with_each_other = true
144+
[{long_chain_expression_allow_one_space_after_colon-eq-true.lua}]
145+
long_chain_expression_allow_one_space_after_colon = true
144146
[{minLine-eq-1.lua}]
145147
keep_line_after_if_statement = minLine:1
146148
keep_line_after_do_statement = minLine:1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local getKeyMap = util.switch()
2+
: case 'local'
3+
: call(function(source)
4+
local t = 13
5+
end)
6+
7+

0 commit comments

Comments
 (0)