Skip to content

Commit fe08f75

Browse files
committed
简单支持typeformatting
1 parent d7ab045 commit fe08f75

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

CodeFormatServer/src/LanguageService.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ bool LanguageService::Initialize()
2626
_handles["textDocument/didClose"] = DynamicBind(OnClose, vscode::DidCloseTextDocumentParams);
2727
_handles["updateEditorConfig"] = DynamicBind(OnEditorConfigUpdate, vscode::EditorConfigUpdateParams);
2828
_handles["textDocument/rangeFormatting"] = DynamicBind(OnRangeFormatting, vscode::DocumentRangeFormattingParams);
29+
_handles["textDocument/onTypeFormatting"] = DynamicBind(OnTypeFormatting, vscode::TextDocumentPositionParams);
30+
2931
return true;
3032
}
3133

@@ -46,6 +48,15 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
4648

4749
result->capabilities.documentFormattingProvider = true;
4850
result->capabilities.documentRangeFormattingProvider = true;
51+
52+
vscode::DocumentOnTypeFormattingOptions typeOptions;
53+
54+
typeOptions.firstTriggerCharacter = ";";
55+
56+
typeOptions.moreTriggerCharacter = {"\"", "\'", ",", ":", "."};
57+
58+
result->capabilities.documentOnTypeFormattingProvider = typeOptions;
59+
4960
result->capabilities.textDocumentSync.change = vscode::TextDocumentSyncKind::Full;
5061
result->capabilities.textDocumentSync.openClose = true;
5162

@@ -184,3 +195,42 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting(
184195
);
185196
return result;
186197
}
198+
199+
std::shared_ptr<vscode::Serializable> LanguageService::OnTypeFormatting(
200+
std::shared_ptr<vscode::TextDocumentPositionParams> param)
201+
{
202+
auto text = LanguageClient::GetInstance().GetFile(param->textDocument.uri);
203+
204+
auto result = std::make_shared<vscode::DocumentFormattingResult>();
205+
206+
if (text.empty())
207+
{
208+
result->hasError = true;
209+
return result;
210+
}
211+
212+
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
213+
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+
// }
223+
224+
LuaFormatter formatter(parser, *options);
225+
formatter.BuildFormattedElement();
226+
227+
auto& edit = result->edits.emplace_back();
228+
LuaFormatRange formattedRange(param->position.line, param->position.line);
229+
230+
edit.newText = formatter.GetRangeFormattedText(formattedRange);
231+
edit.range = vscode::Range(
232+
vscode::Position(formattedRange.StartLine, 0),
233+
vscode::Position(formattedRange.EndLine + 1, 0)
234+
);
235+
return result;
236+
}

CodeFormatServer/src/VSCode.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ std::shared_ptr<vscode::Serializable> vscode::MakeFromRequest(std::string_view m
4343
{
4444
return MakeRequestObject<DocumentRangeFormattingParams>(json);
4545
}
46+
else if (method == "textDocument/onTypeFormatting")
47+
{
48+
return MakeRequestObject<TextDocumentPositionParams>(json);
49+
}
4650

4751
return nullptr;
4852
}
@@ -194,6 +198,8 @@ nlohmann::json vscode::ServerCapabilities::Serialize()
194198
object["textDocumentSync"] = textDocumentSync.Serialize();
195199
object["documentFormattingProvider"] = documentFormattingProvider;
196200
object["documentRangeFormattingProvider"] = documentRangeFormattingProvider;
201+
object["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.Serialize();
202+
197203
return object;
198204
}
199205

@@ -337,3 +343,25 @@ void vscode::DocumentRangeFormattingParams::Deserialize(nlohmann::json json)
337343
textDocument.Deserialize(json["textDocument"]);
338344
range.Deserialize(json["range"]);
339345
}
346+
347+
nlohmann::json vscode::DocumentOnTypeFormattingOptions::Serialize()
348+
{
349+
auto object = nlohmann::json::object();
350+
351+
object["firstTriggerCharacter"] = firstTriggerCharacter;
352+
auto array = nlohmann::json::array();
353+
354+
for (auto& c : moreTriggerCharacter)
355+
{
356+
array.push_back(c);
357+
}
358+
359+
object["moreTriggerCharacter"] = array;
360+
return object;
361+
}
362+
363+
void vscode::TextDocumentPositionParams::Deserialize(nlohmann::json json)
364+
{
365+
textDocument.Deserialize(json["textDocument"]);
366+
position.Deserialize(json["position"]);
367+
}

include/CodeFormatServer/LanguageService.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ class LanguageService
3434

3535
std::shared_ptr<vscode::Serializable> OnRangeFormatting(std::shared_ptr<vscode::DocumentRangeFormattingParams> param);
3636

37+
std::shared_ptr<vscode::Serializable> OnTypeFormatting(std::shared_ptr<vscode::TextDocumentPositionParams> param);
38+
3739
std::map<std::string, MessageHandle, std::less<>> _handles;
3840
};
3941

include/CodeFormatServer/VSCode.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace vscode
1010
{
11+
1112
class Serializable
1213
{
1314
public:
@@ -127,12 +128,22 @@ class TextDocumentSyncOptions : public Serializable
127128
nlohmann::json Serialize() override;
128129
};
129130

131+
class DocumentOnTypeFormattingOptions : public Serializable
132+
{
133+
public:
134+
std::string firstTriggerCharacter;
135+
std::vector<std::string> moreTriggerCharacter;
136+
137+
nlohmann::json Serialize() override;
138+
};
139+
130140
class ServerCapabilities : public Serializable
131141
{
132142
public:
133143
TextDocumentSyncOptions textDocumentSync;
134144
bool documentFormattingProvider = false;
135145
bool documentRangeFormattingProvider = false;
146+
DocumentOnTypeFormattingOptions documentOnTypeFormattingProvider;
136147

137148
nlohmann::json Serialize() override;
138149
};
@@ -268,4 +279,13 @@ class DocumentRangeFormattingParams: public Serializable
268279
void Deserialize(nlohmann::json json) override;
269280
};
270281

282+
class TextDocumentPositionParams: public Serializable
283+
{
284+
public:
285+
TextDocument textDocument;
286+
Position position;
287+
288+
void Deserialize(nlohmann::json json) override;
289+
};
290+
271291
}

0 commit comments

Comments
 (0)