Skip to content

Commit f863ca5

Browse files
committed
语言服务支持代码风格诊断
1 parent d88bbfb commit f863ca5

File tree

8 files changed

+220
-141
lines changed

8 files changed

+220
-141
lines changed

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "CodeFormatServer/LanguageClient.h"
2+
#include "Util/format.h"
23

34
LanguageClient& LanguageClient::GetInstance()
45
{
@@ -16,6 +17,23 @@ std::shared_ptr<IOSession> LanguageClient::GetSession()
1617
return _session;
1718
}
1819

20+
void LanguageClient::SendNotification(std::string_view method, std::shared_ptr<vscode::Serializable> param)
21+
{
22+
auto json = nlohmann::json::object();
23+
json["jsonrpc"] = "2.0";
24+
json["method"] = method;
25+
json["params"] = param->Serialize();
26+
27+
if(_session)
28+
{
29+
auto dumpResult = json.dump();
30+
std::string message = format("Content-Length:{}\r\n\r\n", dumpResult.size());
31+
32+
message.append(dumpResult);
33+
_session->Send(std::move(message));
34+
}
35+
}
36+
1937
void LanguageClient::Run()
2038
{
2139
if (_session)

CodeFormatServer/src/LanguageService.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "CodeFormatServer/VSCode.h"
33
#include "CodeService/LuaFormatOptions.h"
44
#include "CodeService/LuaFormatter.h"
5+
#include "CodeFormatServer/LanguageClient.h"
56

67
using namespace std::placeholders;
78

@@ -25,7 +26,7 @@ bool LanguageService::Initialize()
2526
}
2627

2728
std::shared_ptr<vscode::Serializable> LanguageService::Dispatch(std::string_view method,
28-
std::shared_ptr<vscode::Serializable> param)
29+
std::shared_ptr<vscode::Serializable> param)
2930
{
3031
auto it = _handles.find(method);
3132
if (it != _handles.end())
@@ -49,15 +50,35 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
4950
std::shared_ptr<vscode::DidChangeTextDocumentParams> param)
5051
{
5152
LuaFormatOptions options;
52-
53+
5354
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(param->contentChanges[0].text));
5455
parser->BuildAstWithComment();
5556

5657
LuaFormatter formatter(parser, options);
5758
formatter.BuildFormattedElement();
5859

59-
auto diagnosis = formatter.GetDiagnosisInfos();
60+
auto diagnosisInfos = formatter.GetDiagnosisInfos();
6061

62+
auto vscodeDiagnosis = std::make_shared<vscode::PublishDiagnosticsParams>();
63+
vscodeDiagnosis->uri = param->textDocument.uri;
6164

65+
for (auto diagnosisInfo : diagnosisInfos)
66+
{
67+
auto& diagnosis = vscodeDiagnosis->diagnostics.emplace_back();
68+
diagnosis.message = diagnosisInfo.Message;
69+
diagnosis.range = vscode::Range(
70+
vscode::Position(
71+
diagnosisInfo.Range.Start.Line,
72+
diagnosisInfo.Range.Start.Character
73+
),
74+
vscode::Position(
75+
diagnosisInfo.Range.End.Line,
76+
diagnosisInfo.Range.End.Character
77+
));
78+
diagnosis.severity = vscode::DiagnosticSeverity::Warning;
79+
}
6280

81+
LanguageClient::GetInstance().SendNotification("textDocument/publishDiagnostics", vscodeDiagnosis);
82+
83+
return nullptr;
6384
}

CodeFormatServer/src/Session/SocketIOSession.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ void SocketIOSession::Run()
3838
}
3939
while (true);
4040

41-
std::string result = Handle(protocolBuffer.ReadOneProtocol());
41+
do {
42+
std::string result = Handle(protocolBuffer.ReadOneProtocol());
4243

43-
protocolBuffer.Reset();
44-
if (!result.empty()) {
45-
asio::write(_socket, asio::buffer(result));
46-
}
44+
protocolBuffer.Reset();
45+
if (!result.empty()) {
46+
Send(result);
47+
}
48+
} while (protocolBuffer.CanReadOneProtocol());
4749
}
4850
endLoop:
4951
return;
5052
}
53+
54+
void SocketIOSession::Send(std::string_view content)
55+
{
56+
asio::write(_socket, asio::buffer(content));
57+
}

CodeFormatServer/src/VSCode.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,20 @@ std::shared_ptr<vscode::Serializable> vscode::MakeFromRequest(std::string_view m
1919
{
2020
return MakeRequestObject<InitializeParams>(json);
2121
}
22-
else if(method == "textDocument/didChange")
22+
else if (method == "textDocument/didChange")
2323
{
2424
return MakeRequestObject<DidChangeTextDocumentParams>(json);
2525
}
2626

2727
return nullptr;
2828
}
2929

30+
vscode::Position::Position(uint64_t _line, uint64_t _character)
31+
: line(_line),
32+
character(_character)
33+
{
34+
}
35+
3036
nlohmann::json vscode::Position::Serialize()
3137
{
3238
auto object = nlohmann::json::object();
@@ -42,6 +48,12 @@ void vscode::Position::Deserialize(nlohmann::json json)
4248
character = json["character"];
4349
}
4450

51+
vscode::Range::Range(Position _start, Position _end)
52+
: start(_start),
53+
end(_end)
54+
{
55+
}
56+
4557
nlohmann::json vscode::Range::Serialize()
4658
{
4759
auto object = nlohmann::json::object();
@@ -72,6 +84,10 @@ void vscode::Location::Deserialize(nlohmann::json json)
7284
uri = json["uri"];
7385
}
7486

87+
vscode::Diagnostic::Diagnostic()
88+
{
89+
}
90+
7591
nlohmann::json vscode::Diagnostic::Serialize()
7692
{
7793
auto object = nlohmann::json::object();
@@ -102,6 +118,23 @@ void vscode::TextDocument::Deserialize(nlohmann::json json)
102118
uri = json["uri"];
103119
}
104120

121+
nlohmann::json vscode::PublishDiagnosticsParams::Serialize()
122+
{
123+
auto object = nlohmann::json::object();
124+
object["uri"] = uri;
125+
126+
auto array = nlohmann::json::array();
127+
128+
for(auto& diagnositc: diagnostics)
129+
{
130+
array.push_back(diagnositc.Serialize());
131+
}
132+
133+
object["diagnostics"] = array;
134+
135+
return object;
136+
}
137+
105138
void vscode::PublishDiagnosticsParams::Deserialize(nlohmann::json json)
106139
{
107140
uri = json["uri"];

include/CodeFormatServer/LanguageClient.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class LanguageClient
1212

1313
std::shared_ptr<IOSession> GetSession();
1414

15+
void SendNotification(std::string_view method, std::shared_ptr<vscode::Serializable> param);
16+
1517
void Run();
1618
private:
1719
std::shared_ptr<IOSession> _session;

include/CodeFormatServer/Session/IOSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class IOSession
99
virtual ~IOSession();
1010

1111
virtual void Run() = 0;
12-
// virtual void Write();
12+
virtual void Send(std::string_view content) = 0;
1313
protected:
1414
std::string Handle(std::string_view msg);
1515
private:

include/CodeFormatServer/Session/SocketIOSession.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class SocketIOSession : public IOSession
99
explicit SocketIOSession(asio::ip::tcp::socket&& socket);
1010

1111
void Run() override;
12-
12+
void Send(std::string_view content) override;
1313
private:
1414
asio::ip::tcp::socket _socket;
1515
};

0 commit comments

Comments
 (0)