Skip to content

Commit adbd915

Browse files
committed
语言服务支持读取.editorconfig文件
1 parent 9050804 commit adbd915

File tree

17 files changed

+221
-74
lines changed

17 files changed

+221
-74
lines changed

CodeFormat/src/CodeFormat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ int main(int argc, char** argv)
4242
parser = LuaParser::LoadFromBuffer(std::move(buffer));
4343
}
4444

45-
LuaFormatOptions options;
45+
LuaCodeStyleOptions options;
4646
if (!cmd.Get<std::string>("config").empty())
4747
{
48-
options = LuaFormatOptions::ParseFromEditorConfig(cmd.Get<std::string>("config"));
48+
options = LuaCodeStyleOptions::ParseFromEditorConfig(cmd.Get<std::string>("config"));
4949
}
5050

5151
parser->BuildAstWithComment();

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ LanguageClient& LanguageClient::GetInstance()
1010
return instance;
1111
}
1212

13+
LanguageClient::LanguageClient()
14+
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>())
15+
{
16+
}
17+
1318
void LanguageClient::SetSession(std::shared_ptr<IOSession> session)
1419
{
1520
_session = session;
@@ -63,14 +68,16 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
6368

6469
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
6570

66-
if(parser->HasError())
71+
if (parser->HasError())
6772
{
6873
return;
6974
}
7075

76+
auto options = GetOptions(uri);
77+
7178
parser->BuildAstWithComment();
7279

73-
LuaFormatter formatter(parser, _options);
80+
LuaFormatter formatter(parser, *options);
7481
formatter.BuildFormattedElement();
7582

7683
auto diagnosisInfos = formatter.GetDiagnosisInfos();
@@ -100,7 +107,7 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
100107
std::string LanguageClient::GetFile(std::string_view uri)
101108
{
102109
auto it = _fileMap.find(uri);
103-
if(it != _fileMap.end())
110+
if (it != _fileMap.end())
104111
{
105112
return it->second;
106113
}
@@ -116,7 +123,49 @@ void LanguageClient::Run()
116123
}
117124
}
118125

119-
LuaFormatOptions& LanguageClient::GetOptions()
126+
std::shared_ptr<LuaCodeStyleOptions> LanguageClient::GetOptions(std::string_view uri)
127+
{
128+
std::size_t matchLength = 0;
129+
std::shared_ptr<LuaCodeStyleOptions> options = _defaultOptions;
130+
for (auto it = _optionsVector.begin(); it != _optionsVector.end(); it++)
131+
{
132+
if (uri.starts_with(it->first) && it->first.size() > matchLength)
133+
{
134+
matchLength = it->first.size();
135+
options = it->second;
136+
}
137+
}
138+
139+
return options;
140+
}
141+
142+
void LanguageClient::UpdateOptions(std::string_view workspaceUri, std::string_view configPath)
143+
{
144+
for (auto& pair : _optionsVector)
145+
{
146+
if (pair.first == workspaceUri)
147+
{
148+
pair.second = LuaCodeStyleOptions::ParseFromEditorConfig(configPath);
149+
return;
150+
}
151+
}
152+
153+
_optionsVector.push_back({std::string(workspaceUri), LuaCodeStyleOptions::ParseFromEditorConfig(configPath)});
154+
}
155+
156+
void LanguageClient::RemoveOptions(std::string_view workspaceUri)
120157
{
121-
return _options;
158+
for (auto it = _optionsVector.begin(); it != _optionsVector.end(); it++)
159+
{
160+
if (it->first == workspaceUri)
161+
{
162+
_optionsVector.erase(it);
163+
break;
164+
}
165+
}
122166
}
167+
168+
// LuaCodeStyleOptions& LanguageClient::GetOptions()
169+
// {
170+
// return _optionsVector;
171+
// }

CodeFormatServer/src/LanguageService.cpp

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "CodeFormatServer/LanguageService.h"
22
#include "CodeFormatServer/VSCode.h"
3-
#include "CodeService/LuaFormatOptions.h"
3+
#include "CodeService/LuaCodeStyleOptions.h"
44
#include "CodeService/LuaFormatter.h"
55
#include "CodeFormatServer/LanguageClient.h"
66

@@ -24,6 +24,7 @@ bool LanguageService::Initialize()
2424
_handles["textDocument/didOpen"] = DynamicBind(OnDidOpen, vscode::DidOpenTextDocumentParams);
2525
_handles["textDocument/formatting"] = DynamicBind(OnFormatting, vscode::DocumentFormattingParams);
2626
_handles["textDocument/didClose"] = DynamicBind(OnClose, vscode::DidCloseTextDocumentParams);
27+
_handles["updateEditorConfig"] = DynamicBind(OnEditorConfigUpdate, vscode::EditorConfigUpdateParams);
2728
return true;
2829
}
2930

@@ -45,6 +46,13 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
4546
result->capabilities.documentFormattingProvider = true;
4647
result->capabilities.textDocumentSync.change = vscode::TextDocumentSyncKind::Full;
4748
result->capabilities.textDocumentSync.openClose = true;
49+
50+
auto& configFiles = param->initializationOptions.configFiles;
51+
for (auto& configFile : configFiles)
52+
{
53+
LanguageClient::GetInstance().UpdateOptions(configFile.workspace, configFile.path);
54+
}
55+
4856
return result;
4957
}
5058

@@ -84,18 +92,18 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnFormatting(
8492
return result;
8593
}
8694

87-
auto& options = LanguageClient::GetInstance().GetOptions();
95+
auto options = LanguageClient::GetInstance().GetOptions(param->textDocument.uri);
8896

8997
std::shared_ptr<LuaParser> parser = LuaParser::LoadFromBuffer(std::move(text));
9098
parser->BuildAstWithComment();
9199

92-
if(parser->HasError())
100+
if (parser->HasError())
93101
{
94102
result->hasError = true;
95103
return result;
96104
}
97105

98-
LuaFormatter formatter(parser, options);
106+
LuaFormatter formatter(parser, *options);
99107
formatter.BuildFormattedElement();
100108

101109
auto& edit = result->edits.emplace_back();
@@ -113,4 +121,25 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnClose(
113121
LanguageClient::GetInstance().ReleaseFile(param->textDocument.uri);
114122

115123
return nullptr;
116-
}
124+
}
125+
126+
std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate(
127+
std::shared_ptr<vscode::EditorConfigUpdateParams> param)
128+
{
129+
switch (param->type)
130+
{
131+
case vscode::EditorConfigUpdateType::Created:
132+
case vscode::EditorConfigUpdateType::Changed:
133+
{
134+
LanguageClient::GetInstance().UpdateOptions(param->source.workspace, param->source.path);
135+
break;
136+
}
137+
case vscode::EditorConfigUpdateType::Delete:
138+
{
139+
LanguageClient::GetInstance().RemoveOptions(param->source.workspace);
140+
break;
141+
}
142+
}
143+
144+
return nullptr;
145+
}

CodeFormatServer/src/Session/SocketIOSession.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ void SocketIOSession::Run()
4141
while (true);
4242

4343
do {
44-
std::string result = Handle(protocolBuffer.ReadOneProtocol());
44+
auto content = protocolBuffer.ReadOneProtocol();
45+
// std::cout << content << std::endl;
46+
std::string result = Handle(content);
4547

4648
protocolBuffer.Reset();
4749
if (!result.empty()) {

CodeFormatServer/src/VSCode.cpp

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ std::shared_ptr<vscode::Serializable> vscode::MakeFromRequest(std::string_view m
3535
{
3636
return MakeRequestObject<DidCloseTextDocumentParams>(json);
3737
}
38+
else if (method == "updateEditorConfig")
39+
{
40+
return MakeRequestObject<EditorConfigUpdateParams>(json);
41+
}
3842

3943
return nullptr;
4044
}
@@ -199,15 +203,22 @@ void vscode::ServerCapabilities::Deserialize(nlohmann::json json)
199203
textDocumentSync.Deserialize(json["textDocumentSync"]);
200204
}
201205

202-
nlohmann::json vscode::InitializeParams::Serialize()
206+
void vscode::InitializationOptions::Deserialize(nlohmann::json json)
203207
{
204-
auto object = nlohmann::json::object();
205-
object["rootPath"] = rootPath;
206-
object["rootUri"] = rootUri;
207-
object["locale"] = locale;
208-
object["initializationOptions"] = initializationOptions;
208+
auto configFileArray = json["configFiles"];
209209

210-
return object;
210+
for(auto& configSource: configFileArray)
211+
{
212+
auto& source = configFiles.emplace_back();
213+
source.Deserialize(configSource);
214+
}
215+
216+
auto workspaceFolderArray = json["workspaceFolders"];
217+
218+
for(auto& workspaceUri: workspaceFolderArray)
219+
{
220+
workspaceFolders.emplace_back(workspaceUri);
221+
}
211222
}
212223

213224
void vscode::InitializeParams::Deserialize(nlohmann::json json)
@@ -216,7 +227,7 @@ void vscode::InitializeParams::Deserialize(nlohmann::json json)
216227
rootUri = json["rootUri"];
217228
locale = json["locale"];
218229

219-
initializationOptions = json["initializationOptions"];
230+
initializationOptions.Deserialize(json["initializationOptions"]);
220231
}
221232

222233
nlohmann::json vscode::InitializeResult::Serialize()
@@ -299,3 +310,16 @@ void vscode::DidCloseTextDocumentParams::Deserialize(nlohmann::json json)
299310
{
300311
textDocument.Deserialize(json["textDocument"]);
301312
}
313+
314+
void vscode::EditorConfigSource::Deserialize(nlohmann::json json)
315+
{
316+
uri = json["uri"];
317+
path = json["path"];
318+
workspace = json["workspace"];
319+
}
320+
321+
void vscode::EditorConfigUpdateParams::Deserialize(nlohmann::json json)
322+
{
323+
type = json["type"];
324+
source.Deserialize(json["source"]);
325+
}

CodeService/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ target_sources(CodeService
5252
${CodeService_SOURCE_DIR}/src/FormatElement/KeepElement.cpp
5353
${CodeService_SOURCE_DIR}/src/FormatElement/SubExpressionElement.cpp
5454
${CodeService_SOURCE_DIR}/src/FormatElement/DiagnosisContext.cpp
55-
${CodeService_SOURCE_DIR}/src/LuaFormatOptions.cpp
55+
${CodeService_SOURCE_DIR}/src/LuaCodeStyleOptions.cpp
5656
)
5757

5858
target_link_libraries(CodeService LuaParser)

CodeService/src/FormatElement/DiagnosisContext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "Util/format.h"
44

5-
DiagnosisContext::DiagnosisContext(std::shared_ptr<LuaParser> parser, LuaFormatOptions& options)
5+
DiagnosisContext::DiagnosisContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options)
66
: _parser(parser),
77
_options(options)
88
{
@@ -118,7 +118,7 @@ std::vector<LuaDiagnosisInfo> DiagnosisContext::GetDiagnosisInfos()
118118
return _diagnosisInfos;
119119
}
120120

121-
const LuaFormatOptions& DiagnosisContext::GetOptions()
121+
const LuaCodeStyleOptions& DiagnosisContext::GetOptions()
122122
{
123123
return _options;
124124
}

CodeService/src/FormatElement/FormatContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "CodeService/FormatElement/FormatContext.h"
22

3-
FormatContext::FormatContext(std::shared_ptr<LuaParser> parser, LuaFormatOptions& options)
3+
FormatContext::FormatContext(std::shared_ptr<LuaParser> parser, LuaCodeStyleOptions& options)
44
: _options(options),
55
_characterCount(0),
66
_parser(parser)

0 commit comments

Comments
 (0)