Skip to content

Commit 7e7967c

Browse files
committed
修复formatOnType问题
1 parent 653f611 commit 7e7967c

File tree

3 files changed

+7
-11
lines changed

3 files changed

+7
-11
lines changed

CodeFormatServer/src/LanguageService.cpp

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

301301
LuaFormatRange formatRange(static_cast<int>(param->range.start.line), static_cast<int>(param->range.end.line));
302302
auto formatResult = LanguageClient::GetInstance().GetService<CodeFormatService>()->RangeFormat(
303-
formatRange, parser, options);
303+
formatRange, parser, *options);
304304

305305
auto& edit = result->edits.emplace_back();
306306
edit.newText = std::move(formatResult);
@@ -332,17 +332,14 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
332332
return result;
333333
}
334334

335+
LuaCodeStyleOptions tempOptions = *options;
336+
tempOptions.insert_final_newline = true;
335337

336338
LuaFormatRange formattedRange(static_cast<int>(position.line) - 1, static_cast<int>(position.line) - 1);
337339

338340
auto formatResult = LanguageClient::GetInstance().GetService<CodeFormatService>()->RangeFormat(
339-
formattedRange, parser, options);
341+
formattedRange, parser, tempOptions);
340342

341-
auto totalLine = parser->GetTotalLine();
342-
if(totalLine == position.line && !options->insert_final_newline)
343-
{
344-
formatResult.push_back('\n');
345-
}
346343

347344
auto& edit = result->edits.emplace_back();
348345
edit.newText = std::move(formatResult);

CodeFormatServer/src/Service/CodeFormatService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ std::string CodeFormatService::Format(std::shared_ptr<LuaParser> parser, std::sh
8888

8989
std::string CodeFormatService::RangeFormat(LuaFormatRange& range,
9090
std::shared_ptr<LuaParser> parser,
91-
std::shared_ptr<LuaCodeStyleOptions> options)
91+
LuaCodeStyleOptions& options)
9292
{
93-
LuaFormatter formatter(parser, *options);
93+
LuaFormatter formatter(parser, options);
9494
formatter.BuildFormattedElement();
9595
return formatter.GetRangeFormattedText(range);
9696
}

include/CodeFormatServer/Service/CodeFormatService.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ class CodeFormatService : public Service
2323

2424
std::string Format(std::shared_ptr<LuaParser> parser, std::shared_ptr<LuaCodeStyleOptions> options);
2525

26-
std::string RangeFormat(LuaFormatRange& range, std::shared_ptr<LuaParser> parser,
27-
std::shared_ptr<LuaCodeStyleOptions> options);
26+
std::string RangeFormat(LuaFormatRange& range, std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options);
2827

2928
void LoadDictionary(std::string_view path);
3029

0 commit comments

Comments
 (0)