Skip to content

Commit 22fa020

Browse files
committed
支持基于标准IO的语言服务
1 parent 6be1aeb commit 22fa020

File tree

12 files changed

+139
-21
lines changed

12 files changed

+139
-21
lines changed

CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ set(CMAKE_CXX_STANDARD 20)
66

77
option(BuildAsLuaLib "Build for lua dll" OFF)
88
option(BuildCodeFormat "Build CodeFormat" ON)
9-
option(BuildCodeFormatServer "Build CodeFormatServer" OFF)
9+
option(BuildCodeFormatServer "Build CodeFormatServer" ON)
1010

1111
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
1212
add_definitions(-DMSVC)
@@ -26,5 +26,6 @@ if(BuildCodeFormat)
2626
endif()
2727

2828
if(BuildCodeFormatServer)
29-
add_subdirectory(CodeFormatServer)
30-
endif()
29+
#add_subdirectory(CodeFormatServer)
30+
endif()
31+
add_subdirectory(CodeFormatServer)

CodeFormatServer/src/CodeFormatServer.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@
44
#include "CodeFormatServer/Session/SocketIOSession.h"
55
#include "CodeFormatServer/Session/StandardIOSession.h"
66

7+
// https://stackoverflow.com/questions/1598985/c-read-binary-stdin
8+
#ifdef _WIN32
9+
# include <io.h>
10+
# include <fcntl.h>
11+
# define SET_BINARY_MODE() _setmode(_fileno(stdin), _O_BINARY);\
12+
_setmode(_fileno(stdout), _O_BINARY)
13+
#else
14+
# define SET_BINARY_MODE() ((void)0)
15+
#endif
16+
717
using namespace asio::ip;
818

919
int main(int argc, char** argv)
@@ -22,8 +32,9 @@ int main(int argc, char** argv)
2232
}
2333
else
2434
{
25-
// LanguageClient::GetInstance().SetSession(std::make_shared<StandardIOSession>());
26-
// LanguageClient::GetInstance().Run();
35+
SET_BINARY_MODE();
36+
LanguageClient::GetInstance().SetSession(std::make_shared<StandardIOSession>());
37+
LanguageClient::GetInstance().Run();
2738
}
2839

2940

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,21 @@ void LanguageClient::SendNotification(std::string_view method, std::shared_ptr<v
3737
}
3838
}
3939

40-
void LanguageClient::CacheFile(const std::string& uri, std::string text)
40+
void LanguageClient::CacheFile(std::string_view uri, std::string text)
4141
{
42-
_fileMap[uri] = std::move(text);
42+
_fileMap[std::string(uri)] = std::move(text);
4343
}
4444

45-
void LanguageClient::DiagnosticFile(const std::string& uri)
45+
void LanguageClient::ReleaseFile(std::string_view uri)
46+
{
47+
auto it = _fileMap.find(uri);
48+
if (it != _fileMap.end())
49+
{
50+
_fileMap.erase(it);
51+
}
52+
}
53+
54+
void LanguageClient::DiagnosticFile(std::string_view uri)
4655
{
4756
auto it = _fileMap.find(uri);
4857
if (it == _fileMap.end())
@@ -53,6 +62,12 @@ void LanguageClient::DiagnosticFile(const std::string& uri)
5362
std::string text = it->second;
5463

5564
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
65+
66+
if(parser->HasError())
67+
{
68+
return;
69+
}
70+
5671
parser->BuildAstWithComment();
5772

5873
LuaFormatter formatter(parser, _options);
@@ -82,7 +97,7 @@ void LanguageClient::DiagnosticFile(const std::string& uri)
8297
SendNotification("textDocument/publishDiagnostics", vscodeDiagnosis);
8398
}
8499

85-
std::string LanguageClient::GetFile(const std::string& uri)
100+
std::string LanguageClient::GetFile(std::string_view uri)
86101
{
87102
auto it = _fileMap.find(uri);
88103
if(it != _fileMap.end())

CodeFormatServer/src/LanguageService.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ bool LanguageService::Initialize()
2121

2222
_handles["initialize"] = DynamicBind(OnInitialize, vscode::InitializeParams);
2323
_handles["textDocument/didChange"] = DynamicBind(OnDidChange, vscode::DidChangeTextDocumentParams);
24-
_handles["textDocument/didOpen"] = DynamicBind(OnDidOpen, vscode::DidOpenTextDocumentParam);
24+
_handles["textDocument/didOpen"] = DynamicBind(OnDidOpen, vscode::DidOpenTextDocumentParams);
2525
_handles["textDocument/formatting"] = DynamicBind(OnFormatting, vscode::DocumentFormattingParams);
26+
_handles["textDocument/didClose"] = DynamicBind(OnClose, vscode::DidCloseTextDocumentParams);
2627
return true;
2728
}
2829

@@ -60,7 +61,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
6061
}
6162

6263
std::shared_ptr<vscode::Serializable> LanguageService::OnDidOpen(
63-
std::shared_ptr<vscode::DidOpenTextDocumentParam> param)
64+
std::shared_ptr<vscode::DidOpenTextDocumentParams> param)
6465
{
6566
LanguageClient::GetInstance().CacheFile(param->textDocument.uri, param->textDocument.text);
6667
LanguageClient::GetInstance().DiagnosticFile(param->textDocument.uri);
@@ -88,6 +89,12 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
8889
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
8990
parser->BuildAstWithComment();
9091

92+
if(parser->HasError())
93+
{
94+
result->hasError = true;
95+
return result;
96+
}
97+
9198
LuaFormatter formatter(parser, options);
9299
formatter.BuildFormattedElement();
93100

@@ -99,3 +106,11 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
99106
);
100107
return result;
101108
}
109+
110+
std::shared_ptr<vscode::Serializable> LanguageService::OnClose(
111+
std::shared_ptr<vscode::DidCloseTextDocumentParams> param)
112+
{
113+
LanguageClient::GetInstance().ReleaseFile(param->textDocument.uri);
114+
115+
return nullptr;
116+
}

CodeFormatServer/src/Protocol/ProtocolParser.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ ProtocolParser::ProtocolParser()
1212

1313
void ProtocolParser::Parse(std::string_view msg)
1414
{
15-
std::cout << msg << std::endl;
1615
auto jsonMessage = nlohmann::json::parse(msg);
1716
if (jsonMessage["id"].is_number())
1817
{

CodeFormatServer/src/Session/SocketIOSession.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ void SocketIOSession::Run()
1818
{
1919
asio::error_code code;
2020
std::size_t readSize = 0;
21+
22+
2123
do
2224
{
2325
char* writableCursor = protocolBuffer.GetWritableCursor();
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,51 @@
1-
#include "CodeFormatServer/Session/StandardIOSession.h"
1+
#include "CodeFormatServer/Session/StandardIOSession.h"
2+
3+
#include <iostream>
4+
#include <thread>
5+
#include "CodeFormatServer/Protocol/ProtocolParser.h"
6+
#include "CodeFormatServer/Protocol/ProtocolBuffer.h"
7+
8+
void StandardIOSession::Run()
9+
{
10+
ProtocolBuffer protocolBuffer(1024);
11+
while (true)
12+
{
13+
std::size_t readSize = 0;
14+
do
15+
{
16+
char* writableCursor = protocolBuffer.GetWritableCursor();
17+
std::size_t capacity = protocolBuffer.GetRestCapacity();
18+
19+
std::cin.peek();
20+
readSize += std::cin.readsome(writableCursor, capacity);
21+
if (!std::cin)
22+
{
23+
goto endLoop;
24+
}
25+
26+
protocolBuffer.SetReadableSize(readSize);
27+
28+
if (protocolBuffer.CanReadOneProtocol())
29+
{
30+
break;
31+
}
32+
33+
} while (true);
34+
35+
do {
36+
std::string result = Handle(protocolBuffer.ReadOneProtocol());
37+
38+
protocolBuffer.Reset();
39+
if (!result.empty()) {
40+
Send(result);
41+
}
42+
} while (protocolBuffer.CanReadOneProtocol());
43+
}
44+
endLoop:
45+
return;
46+
}
47+
48+
void StandardIOSession::Send(std::string_view content)
49+
{
50+
std::cout.write(content.data(), content.size());
51+
}

CodeFormatServer/src/VSCode.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ std::shared_ptr<vscode::Serializable> vscode::MakeFromRequest(std::string_view m
2525
}
2626
else if (method == "textDocument/didOpen")
2727
{
28-
return MakeRequestObject<DidOpenTextDocumentParam>(json);
28+
return MakeRequestObject<DidOpenTextDocumentParams>(json);
2929
}
3030
else if (method == "textDocument/formatting")
3131
{
3232
return MakeRequestObject<DocumentFormattingParams>(json);
3333
}
34+
else if (method == "textDocument/didClose")
35+
{
36+
return MakeRequestObject<DidCloseTextDocumentParams>(json);
37+
}
3438

3539
return nullptr;
3640
}
@@ -263,7 +267,7 @@ void vscode::TextDocumentItem::Deserialize(nlohmann::json json)
263267
text = json["text"];
264268
}
265269

266-
void vscode::DidOpenTextDocumentParam::Deserialize(nlohmann::json json)
270+
void vscode::DidOpenTextDocumentParams::Deserialize(nlohmann::json json)
267271
{
268272
textDocument.Deserialize(json["textDocument"]);
269273
}
@@ -290,3 +294,8 @@ nlohmann::json vscode::DocumentFormattingResult::Serialize()
290294
return array;
291295
}
292296
}
297+
298+
void vscode::DidCloseTextDocumentParams::Deserialize(nlohmann::json json)
299+
{
300+
textDocument.Deserialize(json["textDocument"]);
301+
}

include/CodeFormatServer/LanguageClient.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class LanguageClient
1515

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

18-
void CacheFile(const std::string& uri, std::string text);
18+
void CacheFile(std::string_view uri, std::string text);
1919

20-
void DiagnosticFile(const std::string& uri);
20+
void ReleaseFile(std::string_view uri);
2121

22-
std::string GetFile(const std::string& uri);
22+
void DiagnosticFile(const std::string_view uri);
23+
24+
std::string GetFile(const std::string_view uri);
2325

2426
void Run();
2527

include/CodeFormatServer/LanguageService.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class LanguageService
1111
using MessageHandle = std::function<std::shared_ptr<vscode::Serializable>(std::shared_ptr<vscode::Serializable>)>;
1212

1313
LanguageService();
14+
1415
~LanguageService();
1516

1617
bool Initialize();
@@ -23,9 +24,12 @@ class LanguageService
2324

2425
std::shared_ptr<vscode::Serializable> OnDidChange(std::shared_ptr<vscode::DidChangeTextDocumentParams> param);
2526

26-
std::shared_ptr<vscode::Serializable> OnDidOpen(std::shared_ptr<vscode::DidOpenTextDocumentParam> param);
27+
std::shared_ptr<vscode::Serializable> OnDidOpen(std::shared_ptr<vscode::DidOpenTextDocumentParams> param);
2728

2829
std::shared_ptr<vscode::Serializable> OnFormatting(std::shared_ptr<vscode::DocumentFormattingParams> param);
2930

31+
std::shared_ptr<vscode::Serializable> OnClose(std::shared_ptr<vscode::DidCloseTextDocumentParams> param);
32+
3033
std::map<std::string, MessageHandle, std::less<>> _handles;
3134
};
35+

0 commit comments

Comments
 (0)