Skip to content

Commit 2edcdad

Browse files
committed
修复bug
1 parent fe08f75 commit 2edcdad

File tree

10 files changed

+112
-64
lines changed

10 files changed

+112
-64
lines changed

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ void LanguageClient::SendNotification(std::string_view method, std::shared_ptr<v
4242
}
4343
}
4444

45-
void LanguageClient::CacheFile(std::string_view uri, std::string text)
45+
void LanguageClient::CacheFile(std::string_view uri, std::shared_ptr<LuaParser> parser)
4646
{
47-
_fileMap[std::string(uri)] = std::move(text);
47+
_fileMap[std::string(uri)] = parser;
4848
}
4949

5050
void LanguageClient::ReleaseFile(std::string_view uri)
@@ -73,17 +73,13 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
7373
return;
7474
}
7575

76-
std::string text = it->second;
77-
78-
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
76+
std::shared_ptr<LuaParser> parser = it->second;
7977

8078
if (parser->HasError())
8179
{
8280
return;
8381
}
8482

85-
parser->BuildAstWithComment();
86-
8783
LuaFormatter formatter(parser, *options);
8884
formatter.BuildFormattedElement();
8985

@@ -111,15 +107,15 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
111107
SendNotification("textDocument/publishDiagnostics", vscodeDiagnosis);
112108
}
113109

114-
std::string LanguageClient::GetFile(std::string_view uri)
110+
std::shared_ptr<LuaParser> LanguageClient::GetFileParser(std::string_view uri)
115111
{
116112
auto it = _fileMap.find(uri);
117113
if (it != _fileMap.end())
118114
{
119115
return it->second;
120116
}
121117

122-
return "";
118+
return nullptr;
123119
}
124120

125121
void LanguageClient::Run()

CodeFormatServer/src/LanguageService.cpp

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
5151

5252
vscode::DocumentOnTypeFormattingOptions typeOptions;
5353

54-
typeOptions.firstTriggerCharacter = ";";
55-
56-
typeOptions.moreTriggerCharacter = {"\"", "\'", ",", ":", "."};
54+
typeOptions.firstTriggerCharacter = "\n";
5755

5856
result->capabilities.documentOnTypeFormattingProvider = typeOptions;
5957

@@ -75,7 +73,10 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
7573
{
7674
for (auto& content : param->contentChanges)
7775
{
78-
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, content.text);
76+
auto parser = LuaParser::LoadFromBuffer(std::move(content.text));
77+
parser->BuildAstWithComment();
78+
79+
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, parser);
7980

8081
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
8182
}
@@ -85,7 +86,9 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
8586
std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
8687
std::shared_ptr<vscode::DidOpenTextDocumentParams> param)
8788
{
88-
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, param->textDocument.text);
89+
auto parser = LuaParser::LoadFromBuffer(std::move(param->textDocument.text));
90+
parser->BuildAstWithComment();
91+
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, parser);
8992
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
9093

9194
return nullptr;
@@ -94,23 +97,20 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
9497
std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
9598
std::shared_ptr<vscode::DocumentFormattingParams> param)
9699
{
97-
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
100+
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);
98101

99-
auto lastOffset = text.size();
102+
auto totalLine = parser->GetTotalLine();
100103

101104
auto result = std::make_shared<vscode::DocumentFormattingResult>();
102105

103-
if (text.empty())
106+
if (totalLine == 0)
104107
{
105108
result->hasError = true;
106109
return result;
107110
}
108111

109112
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
110113

111-
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
112-
parser->BuildAstWithComment();
113-
114114
if (parser->HasError())
115115
{
116116
result->hasError = true;
@@ -124,7 +124,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
124124
edit.newText = formatter.GetFormattedText();
125125
edit.range = vscode::Range(
126126
vscode::Position(0, 0),
127-
vscode::Position(parser->GetLine(lastOffset), parser->GetColumn(lastOffset))
127+
vscode::Position(totalLine + 1, 0)
128128
);
129129
return result;
130130
}
@@ -161,21 +161,12 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate(
161161
std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
162162
std::shared_ptr<vscode::DocumentRangeFormattingParams> param)
163163
{
164-
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
164+
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);
165165

166166
auto result = std::make_shared<vscode::DocumentFormattingResult>();
167167

168-
if (text.empty())
169-
{
170-
result->hasError = true;
171-
return result;
172-
}
173-
174168
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
175169

176-
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
177-
parser->BuildAstWithComment();
178-
179170
if (parser->HasError())
180171
{
181172
result->hasError = true;
@@ -199,33 +190,29 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
199190
std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
200191
std::shared_ptr<vscode::TextDocumentPositionParams> param)
201192
{
202-
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
193+
auto parser = LanguageClient::GetInstance().GetFileParser(param->textDocument.uri);
194+
auto position = param->position;
203195

204196
auto result = std::make_shared<vscode::DocumentFormattingResult>();
205-
206-
if (text.empty())
197+
if(parser->IsEmptyLine(position.line - 1))
207198
{
208199
result->hasError = true;
209200
return result;
210201
}
211202

212203
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
213204

214-
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
215-
parser->BuildAstWithComment();
216-
217-
// ºöÂÔ¼üÈë´íÎó
218-
// if (parser->HasError())
219-
// {
220-
// result->hasError = true;
221-
// return result;
222-
// }
205+
if (parser->HasError())
206+
{
207+
result->hasError = true;
208+
return result;
209+
}
223210

224211
LuaFormatter formatter(parser, *options);
225212
formatter.BuildFormattedElement();
226213

227214
auto& edit = result->edits.emplace_back();
228-
LuaFormatRange formattedRange(param->position.line, param->position.line);
215+
LuaFormatRange formattedRange(position.line - 1, position.line - 1);
229216

230217
edit.newText = formatter.GetRangeFormattedText(formattedRange);
231218
edit.range = vscode::Range(

CodeService/src/FormatElement/RangeFormatContext.cpp

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "CodeService/FormatElement/RangeFormatContext.h"
22
#include "CodeService/FormatElement/TextElement.h"
3+
#include <string_view>
34

45
RangeFormatContext::RangeFormatContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options,
56
LuaFormatRange validRange)
67
: FormatContext(parser, options),
78
_validRange(validRange),
8-
_inFormattedRange(false)
9+
_formattedLine(-1)
910

1011
{
1112
}
@@ -17,12 +18,10 @@ void RangeFormatContext::Print(TextElement& textElement)
1718
if (offsetLine < _validRange.StartLine || offsetLine > _validRange.EndLine)
1819
{
1920
_characterCount += textElement.GetText().size();
20-
_inFormattedRange = false;
21+
_formattedLine = -1;
2122
return;
2223
}
2324

24-
_inFormattedRange = true;
25-
2625
auto& indentState = _indentStack.top();
2726
if (static_cast<int>(_characterCount) < indentState.Indent)
2827
{
@@ -31,11 +30,7 @@ void RangeFormatContext::Print(TextElement& textElement)
3130
_os << textElement.GetText();
3231
_characterCount += textElement.GetText().size();
3332

34-
int endOffsetLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
35-
if (endOffsetLine > _validRange.EndLine)
36-
{
37-
_validRange.EndLine = endOffsetLine;
38-
}
33+
_formattedLine = _parser->GetLine(textElement.GetTextRange().EndOffset);
3934
}
4035

4136
void RangeFormatContext::PrintBlank(int blank)
@@ -44,7 +39,7 @@ void RangeFormatContext::PrintBlank(int blank)
4439
{
4540
_characterCount++;
4641
}
47-
if (_inFormattedRange)
42+
if (_formattedLine >= 0)
4843
{
4944
for (int i = 0; i < blank; i++)
5045
{
@@ -56,7 +51,7 @@ void RangeFormatContext::PrintBlank(int blank)
5651
void RangeFormatContext::PrintLine(int line)
5752
{
5853
_characterCount = 0;
59-
if (_inFormattedRange)
54+
if (_formattedLine >= 0 && _formattedLine <= _validRange.EndLine)
6055
{
6156
for (int i = 0; i < line; i++)
6257
{
@@ -65,6 +60,24 @@ void RangeFormatContext::PrintLine(int line)
6560
}
6661
}
6762

63+
std::string RangeFormatContext::GetText()
64+
{
65+
std::string formattedText = FormatContext::GetText();
66+
67+
for (int i = formattedText.size() - 1; i >= 0; i--)
68+
{
69+
char ch = formattedText[i];
70+
71+
if (ch != '\n' && ch != '\r')
72+
{
73+
formattedText = formattedText.substr(0, i + 1).append(_options.line_separator);
74+
break;
75+
}
76+
}
77+
78+
return formattedText;
79+
}
80+
6881
LuaFormatRange RangeFormatContext::GetFormattedRange()
6982
{
7083
return _validRange;

LuaParser/src/LuaParser.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ int LuaParser::GetColumn(int offset) const
7272
return _tokenParser->GetColumn(offset);
7373
}
7474

75+
int LuaParser::GetTotalLine()
76+
{
77+
return _tokenParser->GetTotalLine();
78+
}
79+
80+
bool LuaParser::IsEmptyLine(int line)
81+
{
82+
return _tokenParser->IsEmptyLine(line);
83+
}
84+
7585
void LuaParser::BuildAstWithComment()
7686
{
7787
BuildAst();

LuaParser/src/LuaTokenParser.cpp

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ LuaTokenParser::LuaTokenParser(std::string&& source)
5656

5757
bool LuaTokenParser::Parse()
5858
{
59-
6059
while (true)
6160
{
6261
auto type = llex();
@@ -130,7 +129,7 @@ int LuaTokenParser::GetLine(int offset)
130129

131130
int maxLine = static_cast<int>(_lineOffsetVec.size()) - 1;
132131
int targetLine = maxLine;
133-
int upperLine = maxLine;
132+
int upperLine = maxLine;
134133
int lowestLine = 0;
135134

136135
while (true)
@@ -174,6 +173,40 @@ int LuaTokenParser::GetColumn(int offset)
174173
return utf8Length;
175174
}
176175

176+
int LuaTokenParser::GetTotalLine()
177+
{
178+
return _linenumber;
179+
}
180+
181+
bool LuaTokenParser::IsEmptyLine(int line)
182+
{
183+
if (line < 0 || line >= _lineOffsetVec.size())
184+
{
185+
return true;
186+
}
187+
int lineStartOffset = _lineOffsetVec[line];
188+
int nextLineStartOffset = 0;
189+
if (line == _linenumber)
190+
{
191+
nextLineStartOffset = _source.size();
192+
}
193+
else
194+
{
195+
nextLineStartOffset = _lineOffsetVec[line + 1];
196+
}
197+
198+
for (int offset = lineStartOffset; offset < nextLineStartOffset; offset++)
199+
{
200+
char ch = _source[offset];
201+
if(ch != '\n' && ch != '\r' && ch != '\t' && ch != ' ')
202+
{
203+
return false;
204+
}
205+
}
206+
207+
return true;
208+
}
209+
177210
std::string& LuaTokenParser::GetSource()
178211
{
179212
return _source;

include/CodeFormatServer/LanguageClient.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include <memory>
44
#include "Session/IOSession.h"
55
#include "CodeService/LuaCodeStyleOptions.h"
6-
6+
#include "LuaParser/LuaParser.h"
77
class LanguageClient
88
{
99
public:
@@ -17,13 +17,13 @@ class LanguageClient
1717

1818
void SendNotification(std::string_view method, std::shared_ptr<vscode::Serializable> param);
1919

20-
void CacheFile(std::string_view uri, std::string text);
20+
void CacheFile(std::string_view uri, std::shared_ptr<LuaParser> parser);
2121

2222
void ReleaseFile(std::string_view uri);
2323

2424
void DiagnosticFile(const std::string_view uri);
2525

26-
std::string GetFile(const std::string_view uri);
26+
std::shared_ptr<LuaParser> GetFileParser(const std::string_view uri);
2727

2828
void Run();
2929

@@ -34,8 +34,8 @@ class LanguageClient
3434
void RemoveOptions(std::string_view workspaceUri);
3535
private:
3636
std::shared_ptr<IOSession> _session;
37-
// uri µ½file textµÄÓ³Éä
38-
std::map<std::string, std::string, std::less<>> _fileMap;
37+
// uri µ½file astµÄÓ³Éä
38+
std::map<std::string, std::shared_ptr<LuaParser>, std::less<>> _fileMap;
3939

4040
std::vector<std::pair<std::string, std::shared_ptr<LuaCodeStyleOptions>>> _optionsVector;
4141

include/CodeService/FormatElement/FormatContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class FormatContext
3939

4040
std::size_t GetCurrentIndent() const;
4141

42-
std::string GetText();
42+
virtual std::string GetText();
4343

4444
std::shared_ptr<LuaParser> GetParser();
4545

0 commit comments

Comments
 (0)