Skip to content

Commit 433a473

Browse files
committed
更为正确得支持范围格式化
1 parent 2edcdad commit 433a473

File tree

7 files changed

+56
-18
lines changed

7 files changed

+56
-18
lines changed

CodeFormatServer/src/LanguageService.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
181181

182182
edit.newText = formatter.GetRangeFormattedText(formattedRange);
183183
edit.range = vscode::Range(
184-
vscode::Position(formattedRange.StartLine, 0),
185-
vscode::Position(formattedRange.EndLine + 1, 0)
184+
vscode::Position(formattedRange.StartLine, formattedRange.StartCharacter),
185+
vscode::Position(formattedRange.EndLine + 1, formattedRange.EndCharacter)
186186
);
187187
return result;
188188
}
@@ -216,8 +216,8 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
216216

217217
edit.newText = formatter.GetRangeFormattedText(formattedRange);
218218
edit.range = vscode::Range(
219-
vscode::Position(formattedRange.StartLine, 0),
220-
vscode::Position(formattedRange.EndLine + 1, 0)
219+
vscode::Position(formattedRange.StartLine, formattedRange.StartCharacter),
220+
vscode::Position(formattedRange.EndLine + 1, formattedRange.EndCharacter)
221221
);
222222
return result;
223223
}

CodeService/src/FormatElement/RangeFormatContext.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,25 @@ RangeFormatContext::RangeFormatContext(std::shared_ptr<LuaParser> parser, LuaCod
66
LuaFormatRange validRange)
77
: FormatContext(parser, options),
88
_validRange(validRange),
9-
_formattedLine(-1)
9+
_formattedRange(validRange),
10+
_inValidRange(false)
1011

1112
{
1213
}
1314

1415
void RangeFormatContext::Print(TextElement& textElement)
1516
{
16-
int offset = textElement.GetTextRange().StartOffset;
17-
int offsetLine = _parser->GetLine(offset);
18-
if (offsetLine < _validRange.StartLine || offsetLine > _validRange.EndLine)
17+
int startOffset = textElement.GetTextRange().StartOffset;
18+
int startLine = _parser->GetLine(startOffset);
19+
int endLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
20+
if (startLine > _validRange.EndLine || endLine < _validRange.StartLine)
1921
{
22+
_inValidRange = false;
2023
_characterCount += textElement.GetText().size();
21-
_formattedLine = -1;
2224
return;
2325
}
2426

27+
_inValidRange = true;
2528
auto& indentState = _indentStack.top();
2629
if (static_cast<int>(_characterCount) < indentState.Indent)
2730
{
@@ -30,7 +33,16 @@ void RangeFormatContext::Print(TextElement& textElement)
3033
_os << textElement.GetText();
3134
_characterCount += textElement.GetText().size();
3235

33-
_formattedLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
36+
if(startLine < _formattedRange.StartLine)
37+
{
38+
_formattedRange.StartLine = startLine;
39+
_formattedRange.StartCharacter = _parser->GetColumn(startOffset);
40+
}
41+
42+
if(endLine > _formattedRange.EndLine)
43+
{
44+
_formattedRange.EndLine = endLine;
45+
}
3446
}
3547

3648
void RangeFormatContext::PrintBlank(int blank)
@@ -39,7 +51,7 @@ void RangeFormatContext::PrintBlank(int blank)
3951
{
4052
_characterCount++;
4153
}
42-
if (_formattedLine >= 0)
54+
if (_inValidRange)
4355
{
4456
for (int i = 0; i < blank; i++)
4557
{
@@ -51,7 +63,7 @@ void RangeFormatContext::PrintBlank(int blank)
5163
void RangeFormatContext::PrintLine(int line)
5264
{
5365
_characterCount = 0;
54-
if (_formattedLine >= 0 && _formattedLine <= _validRange.EndLine)
66+
if (_inValidRange)
5567
{
5668
for (int i = 0; i < line; i++)
5769
{
@@ -80,5 +92,5 @@ std::string RangeFormatContext::GetText()
8092

8193
LuaFormatRange RangeFormatContext::GetFormattedRange()
8294
{
83-
return _validRange;
95+
return _formattedRange;
8496
}

CodeService/src/LuaFormatter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ std::string LuaFormatter::GetFormattedText()
5858
return ctx.GetText();
5959
}
6060

61-
std::string LuaFormatter::GetRangeFormattedText(LuaFormatRange validRange)
61+
std::string LuaFormatter::GetRangeFormattedText(LuaFormatRange& validRange)
6262
{
6363
RangeFormatContext ctx(_parser, _options, validRange);
6464
_env->Format(ctx);
6565

66+
validRange = ctx.GetFormattedRange();
67+
6668
return ctx.GetText();
6769
}
6870

LuaParser/src/LuaTokenParser.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ bool LuaTokenParser::IsEmptyLine(int line)
198198
for (int offset = lineStartOffset; offset < nextLineStartOffset; offset++)
199199
{
200200
char ch = _source[offset];
201-
if(ch != '\n' && ch != '\r' && ch != '\t' && ch != ' ')
201+
if (ch != '\n' && ch != '\r' && ch != '\t' && ch != ' ')
202202
{
203203
return false;
204204
}
@@ -679,11 +679,29 @@ void LuaTokenParser::readString(int del)
679679
case EOZ:
680680
luaError("unfinished string", TextRange(_currentParseIndex, _currentParseIndex));
681681
return;
682+
case 'z':
683+
{
684+
saveAndNext();
685+
while (lisspace(getCurrentChar()))
686+
{
687+
if (currentIsNewLine())
688+
{
689+
incLinenumber();
690+
}
691+
else
692+
{
693+
saveAndNext();
694+
}
695+
}
696+
goto no_save;
697+
}
682698
}
683699
break;
684700
}
685701
}
686702
saveAndNext();
703+
// 空语句
704+
no_save:;
687705
}
688706
saveAndNext();
689707
}

include/CodeService/FormatElement/RangeFormatContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ class RangeFormatContext : public FormatContext
1515
LuaFormatRange GetFormattedRange();
1616
private:
1717
LuaFormatRange _validRange;
18-
int _formattedLine;
18+
LuaFormatRange _formattedRange;
19+
bool _inValidRange;
1920
};

include/CodeService/LuaFormatRange.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ class LuaFormatRange
55
public:
66
LuaFormatRange(int startLine, int endLine)
77
: StartLine(startLine),
8-
EndLine(endLine)
8+
EndLine(endLine),
9+
StartCharacter(0),
10+
EndCharacter(0)
911
{
1012
}
1113

1214
int StartLine;
1315
int EndLine;
16+
17+
int StartCharacter;
18+
int EndCharacter;
1419
};

include/CodeService/LuaFormatter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class LuaFormatter
1717

1818
std::string GetFormattedText();
1919

20-
std::string GetRangeFormattedText(LuaFormatRange validRange);
20+
std::string GetRangeFormattedText(LuaFormatRange& validRange);
2121

2222
std::vector<LuaDiagnosisInfo> GetDiagnosisInfos();
2323

0 commit comments

Comments
 (0)