Skip to content

Commit db5f6f3

Browse files
committed
完整的支持 insert_final_newline
1 parent 76cdc84 commit db5f6f3

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

CodeFormatServer/src/LanguageService.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,6 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
206206

207207
auto result = std::make_shared<vscode::DocumentFormattingResult>();
208208

209-
if (totalLine == 0)
210-
{
211-
result->hasError = true;
212-
return result;
213-
}
214-
215209
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
216210

217211
if (parser->HasError())
@@ -344,6 +338,12 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
344338
auto formatResult = LanguageClient::GetInstance().GetService<CodeFormatService>()->RangeFormat(
345339
formattedRange, parser, options);
346340

341+
auto totalLine = parser->GetTotalLine();
342+
if(totalLine == position.line)
343+
{
344+
formatResult.push_back('\n');
345+
}
346+
347347
auto& edit = result->edits.emplace_back();
348348
edit.newText = std::move(formatResult);
349349
edit.range = vscode::Range(

CodeService/src/FormatElement/SerializeContext.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,23 @@ void SerializeContext::PrintBlank(int blank)
7070

7171
std::string SerializeContext::GetText()
7272
{
73+
if (!_options.insert_final_newline && !_buffer.empty())
74+
{
75+
std::size_t validSize = _buffer.size();
76+
for (; validSize >= 1; validSize--)
77+
{
78+
char ch = _buffer[validSize - 1];
79+
if (ch != '\r' && ch != '\n')
80+
{
81+
break;
82+
}
83+
}
84+
if (validSize > 0 && validSize <= _buffer.size())
85+
{
86+
_buffer.resize(validSize);
87+
}
88+
}
89+
7390
return std::move(_buffer);
7491
}
7592

0 commit comments

Comments
 (0)