Skip to content

Commit c7ad7f3

Browse files
committed
搭建折行基础,非标准符号基础
1 parent 1bdfe5a commit c7ad7f3

File tree

11 files changed

+105
-17
lines changed

11 files changed

+105
-17
lines changed

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ FormatContext::IndentState FormatContext::CalculateIndentState(int offset)
9898
}
9999
}
100100

101-
if(state.TabIndent == 0 && state.SpaceIndent == 0)
101+
if (state.TabIndent == 0 && state.SpaceIndent == 0)
102102
{
103103
state.Style = _options.indent_style;
104104
}
@@ -125,7 +125,7 @@ FormatContext::IndentState FormatContext::GetLastIndent() const
125125
{
126126
if (_indentStack.size() < 2)
127127
{
128-
return IndentState{ 0, 0, _options.indent_style };
128+
return IndentState{0, 0, _options.indent_style};
129129
}
130130

131131
return _indentStack[_indentStack.size() - 2];
@@ -137,6 +137,29 @@ bool FormatContext::OnlyEmptyCharBefore(int offset)
137137
return file->OnlyEmptyCharBefore(offset);
138138
}
139139

140+
bool FormatContext::ShouldBreakLine(TextRange range)
141+
{
142+
auto column = GetCharacterCount();
143+
if (column == 0)
144+
{
145+
return false;
146+
}
147+
148+
auto startLine = GetLine(range.StartOffset);
149+
auto endLine = GetLine(range.EndOffset);
150+
151+
if (startLine == endLine)
152+
{
153+
column += GetColumn(range.EndOffset) - GetColumn(range.StartOffset);
154+
}
155+
else
156+
{
157+
column += _parser->GetLuaFile()->GetLineRestCharacter(range.StartOffset);
158+
}
159+
160+
return column >= _options.max_line_length;
161+
}
162+
140163
std::shared_ptr<LuaParser> FormatContext::GetParser()
141164
{
142165
return _parser;

CodeService/src/FormatElement/FormatElement.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ void FormatElement::Reset()
219219
_children.clear();
220220
}
221221

222+
std::shared_ptr<FormatElement> FormatElement::GetNextValidElement(ChildIterator& it, FormatElement& parent)
223+
{
224+
auto& siblings = parent.GetChildren();
225+
++it;
226+
for (; it != siblings.end(); ++it)
227+
{
228+
auto child = *it;
229+
if (child->HasValidTextRange())
230+
{
231+
return child;
232+
}
233+
}
234+
235+
return nullptr;
236+
}
237+
222238
int FormatElement::GetLastValidOffset(ChildIterator& it, FormatElement& parent)
223239
{
224240
auto& siblings = parent.GetChildren();
@@ -239,16 +255,10 @@ int FormatElement::GetLastValidOffset(ChildIterator& it, FormatElement& parent)
239255

240256
int FormatElement::GetNextValidOffset(ChildIterator& it, FormatElement& parent)
241257
{
242-
auto& siblings = parent.GetChildren();
243-
++it;
244-
for (; it != siblings.end(); ++it)
258+
auto nextElement = GetNextValidElement(it, parent);
259+
if(nextElement)
245260
{
246-
auto child = *it;
247-
if (child->HasValidTextRange())
248-
{
249-
return child->GetTextRange().StartOffset;
250-
}
261+
return nextElement->GetTextRange().StartOffset;
251262
}
252-
253263
return -1;
254264
}

CodeService/src/FormatElement/KeepElement.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,30 @@ void KeepElement::Serialize(SerializeContext& ctx, ChildIterator selfIt,
1717
FormatElement& parent)
1818
{
1919
const int lastElementLine = GetLastValidLine(ctx, selfIt, parent);
20-
const int nextElementLine = GetNextValidLine(ctx, selfIt, parent);
20+
21+
const auto nextElement = GetNextValidElement(selfIt, parent);
22+
if (!nextElement)
23+
{
24+
return;
25+
}
26+
const auto nextRange = nextElement->GetTextRange();
27+
const int nextElementLine = ctx.GetLine(nextRange.StartOffset);
2128

2229
if (nextElementLine == -1)
2330
{
2431
return;
2532
}
33+
2634
// 这个条件的意思是如果上一个元素和下一个元素没有实质的换行则保持一定的空格
2735
if (nextElementLine == lastElementLine && ctx.GetCharacterCount() != 0)
2836
{
37+
// 暂时不打断
38+
// if (nextElement->GetType() == FormatElementType::TextElement && ctx.ShouldBreakLine(nextRange))
39+
// {
40+
// ctx.PrintLine(1);
41+
// return;
42+
// }
43+
2944
ctx.PrintBlank(_keepBlank);
3045
}
3146
else
@@ -63,7 +78,8 @@ void KeepElement::Diagnosis(DiagnosisContext& ctx, ChildIterator selfIt,
6378
{
6479
if (nextOffset - lastOffset - 1 != _keepBlank)
6580
{
66-
ctx.PushDiagnosis(Util::format(LText("here need keep {} space"), _keepBlank), TextRange(lastOffset, nextOffset), DiagnosisType::Blank);
81+
ctx.PushDiagnosis(Util::format(LText("here need keep {} space"), _keepBlank),
82+
TextRange(lastOffset, nextOffset), DiagnosisType::Blank);
6783
}
6884
}
6985
else

CodeService/src/LuaFormatter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatLocalStatement(std::shared_pt
422422
}
423423
case LuaAstNodeType::NameDefList:
424424
{
425-
env->AddChild(FormatNode(node));
425+
env->AddChild(FormatNameDefList(node));
426426
break;
427427
}
428428
case LuaAstNodeType::ExpressionList:

LuaParser/src/LuaAstNode/LuaAstNode.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#include "LuaParser/LuaAstNode/LuaAstNode.h"
22
#include "LuaParser/LuaAstVisitor.h"
33

4-
LuaAstNode::LuaAstNode(LuaAstNodeType type, std::string_view text, TextRange range)
4+
LuaAstNode::LuaAstNode(LuaAstNodeType type, std::string_view text, TextRange range, LuaTokenType tokenType)
55
: _type(type),
6+
_tokenType(tokenType),
67
_text(text),
78
_textRange(range)
9+
810
{
911
}
1012

1113
LuaAstNode::LuaAstNode(LuaAstNodeType type, LuaToken& token)
12-
: LuaAstNode(type, token.Text, token.Range)
14+
: LuaAstNode(type, token.Text, token.Range, token.TokenType)
1315
{
1416
}
1517

@@ -111,6 +113,11 @@ LuaAstNodeType LuaAstNode::GetType() const
111113
return _type;
112114
}
113115

116+
LuaTokenType LuaAstNode::GetTokenType() const
117+
{
118+
return _tokenType;
119+
}
120+
114121
void LuaAstNode::AddComment(std::shared_ptr<LuaAstNode> comment)
115122
{
116123
auto childTextRange = comment->GetTextRange();

LuaParser/src/LuaFile.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,24 @@ EndOfLine LuaFile::GetEndOfLine() const
235235
{
236236
return _lineState;
237237
}
238+
239+
int LuaFile::GetLineRestCharacter(int offset)
240+
{
241+
int line = GetLine(offset);
242+
int bytesLength = 0;
243+
if(line < static_cast<int>(_lineOffsetVec.size()) - 1)
244+
{
245+
auto nextOffset = _lineOffsetVec[line + 1];
246+
bytesLength = nextOffset - offset;
247+
}
248+
else
249+
{
250+
bytesLength = static_cast<int>(_source.size()) - 1 - offset;
251+
}
252+
if (bytesLength > 0) {
253+
return static_cast<int>(utf8::Utf8nLen(_source.data() + offset, static_cast<std::size_t>(bytesLength)));
254+
}
255+
else {
256+
return 0;
257+
}
258+
}

include/CodeService/FormatElement/FormatContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class FormatContext
4040

4141
bool OnlyEmptyCharBefore(int offset);
4242

43+
bool ShouldBreakLine(TextRange range);
44+
4345
std::shared_ptr<LuaParser> GetParser();
4446

4547
LuaCodeStyleOptions& GetOptions();

include/CodeService/FormatElement/FormatElement.h

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

5252
void Reset();
5353
protected:
54+
static std::shared_ptr<FormatElement> GetNextValidElement (ChildIterator& it, FormatElement& parent);
55+
5456
static int GetLastValidOffset(ChildIterator& it, FormatElement& parent);
5557
static int GetNextValidOffset(ChildIterator& it, FormatElement& parent);
5658

include/LuaParser/LuaAstNode/LuaAstNode.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LuaAstNode: public std::enable_shared_from_this<LuaAstNode>
1717
using ChildrenContainer = std::vector<std::shared_ptr<LuaAstNode>>;
1818
using ChildIterator = ChildrenContainer::iterator;
1919

20-
LuaAstNode(LuaAstNodeType type, std::string_view text, TextRange range);
20+
LuaAstNode(LuaAstNodeType type, std::string_view text, TextRange range, LuaTokenType tokenType = 0);
2121

2222
LuaAstNode(LuaAstNodeType type, LuaToken& token);
2323

@@ -41,6 +41,8 @@ class LuaAstNode: public std::enable_shared_from_this<LuaAstNode>
4141

4242
LuaAstNodeType GetType() const;
4343

44+
LuaTokenType GetTokenType() const;
45+
4446
virtual void AddComment(std::shared_ptr<LuaAstNode> comment);
4547

4648
std::shared_ptr<LuaAstNode> GetParent();
@@ -64,6 +66,7 @@ class LuaAstNode: public std::enable_shared_from_this<LuaAstNode>
6466
void AddChildBefore(ChildIterator it, std::shared_ptr<LuaAstNode> child);
6567

6668
LuaAstNodeType _type;
69+
LuaTokenType _tokenType;
6770
std::string_view _text;
6871
TextRange _textRange;
6972

include/LuaParser/LuaFile.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class LuaFile
3939
void SetEndOfLineState(EndOfLine endOfLine);
4040

4141
EndOfLine GetEndOfLine() const;
42+
43+
int GetLineRestCharacter(int offset);
4244
protected:
4345
std::string _source;
4446
std::string _filename;

0 commit comments

Comments
 (0)