Skip to content

Commit 8f2ed58

Browse files
committed
修复,当存在行尾分号时,内联注释会多间距一个空白的bug
1 parent eda85bb commit 8f2ed58

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ void FormatElement::AddChild(std::shared_ptr<FormatElement> child)
3131
_children.push_back(child);
3232
}
3333

34+
35+
3436
void FormatElement::AddChildren(ChildContainer& children)
3537
{
3638
for (const auto& child : children)
@@ -213,6 +215,19 @@ void FormatElement::CopyFrom(std::shared_ptr<FormatElement> node)
213215
_children = node->_children;
214216
}
215217

218+
void FormatElement::TrimEnd()
219+
{
220+
if(_children.empty())
221+
{
222+
return;
223+
}
224+
225+
while(!_children.empty() && !_children.back()->HasValidTextRange())
226+
{
227+
_children.pop_back();
228+
}
229+
}
230+
216231
void FormatElement::Reset()
217232
{
218233
_textRange = TextRange();

CodeService/src/LuaFormatter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,9 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatBlock(std::shared_ptr<LuaAstN
319319
if (last && _parser->GetLine(last->GetTextRange().EndOffset)
320320
== _parser->GetLine(statement->GetTextRange().StartOffset))
321321
{
322-
if (!last->GetChildren().empty() && last->GetChildren().back()->HasValidTextRange())
322+
if (!last->GetChildren().empty())
323323
{
324+
last->TrimEnd();
324325
last->Add<KeepBlankElement>(_options.statement_inline_comment_space);
325326
}
326327
last->AddChild(FormatComment(statement));
@@ -1822,6 +1823,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatAlignStatement(LuaAstNode::Ch
18221823
auto lastStatementEnv = env->LastValidElement();
18231824
if (lastStatementEnv)
18241825
{
1826+
lastStatementEnv->TrimEnd();
18251827
lastStatementEnv->Add<KeepBlankElement>(_options.statement_inline_comment_space);
18261828
lastStatementEnv->AddChild(FormatNode(nextChild));
18271829
}

include/CodeService/FormatElement/FormatElement.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class FormatElement : public std::enable_shared_from_this<FormatElement>
4949

5050
void CopyFrom(std::shared_ptr<FormatElement> node);
5151

52+
void TrimEnd();
53+
5254
void Reset();
5355
protected:
5456
static std::shared_ptr<FormatElement> GetNextValidElement (ChildIterator& it, FormatElement& parent);

lua.template.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ if_condition_align_with_each_other = false
6969
# however, if the expression list has cross row expression, it will not be aligned to the first expression
7070
local_assign_continuation_align_to_first_expression = false
7171

72+
statement_inline_comment_space = 1
73+
7274
# [indentation]
7375

7476
# if true, the label loses its current indentation

0 commit comments

Comments
 (0)