Skip to content

Commit ce61217

Browse files
committed
搭建auto import 基础
1 parent ce3cce5 commit ce61217

File tree

22 files changed

+508
-28
lines changed

22 files changed

+508
-28
lines changed

3rd/uriparser/src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
#define PACKAGE_VERSION "0.9.5"
4545

46-
#define HAVE_WPRINTF
46+
/* #undef HAVE_WPRINTF */
4747
/* #undef HAVE_REALLOCARRAY */
4848

4949

CodeFormatServer/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ target_sources(CodeFormatServer
3333
${CodeFormatServer_SOURCE_DIR}/src/LanguageService.cpp
3434
${CodeFormatServer_SOURCE_DIR}/src/FileManager.cpp
3535
${CodeFormatServer_SOURCE_DIR}/src/VirtualFile/VirtualFile.cpp
36+
${CodeFormatServer_SOURCE_DIR}/src/Indexs/ModuleIndex.cpp
3637
${CodeFormatServer_SOURCE_DIR}/src/Session/IOSession.cpp
3738
${CodeFormatServer_SOURCE_DIR}/src/Session/SocketIOSession.cpp
3839
${CodeFormatServer_SOURCE_DIR}/src/Session/StandardIOSession.cpp
@@ -43,6 +44,9 @@ target_sources(CodeFormatServer
4344

4445
${CodeFormatServer_SOURCE_DIR}/src/VSCode.cpp
4546

47+
#service
48+
${CodeFormatServer_SOURCE_DIR}/src/Service/ModuleService.cpp
49+
4650
# mimalloc
4751
${LuaCodeStyle_SOURCE_DIR}/3rd/mimalloc-2.0.3/src/static.c
4852
)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "CodeFormatServer/Indexs/ModuleIndex.h"
2+
#include "CodeFormatServer/LanguageClient.h"
3+
4+
5+
ModuleIndex::ModuleIndex()
6+
{
7+
}
8+
9+
void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
10+
{
11+
for (auto& filename : files)
12+
{
13+
auto options = LanguageClient::GetInstance().GetOptions(filename);
14+
auto relativePath = std::filesystem::relative(filename, options->export_root);
15+
std::string modulePath = relativePath.replace_extension("").string();
16+
if (modulePath.starts_with("."))
17+
{
18+
continue;
19+
}
20+
21+
for (auto& c : modulePath)
22+
{
23+
if (c == '/' || c == '\\')
24+
{
25+
c = '.';
26+
}
27+
}
28+
29+
if (modulePath.ends_with(".init"))
30+
{
31+
modulePath = modulePath.substr(0, modulePath.size() - 5);
32+
}
33+
34+
if (_moduleIndex.count(options->export_root) == 0)
35+
{
36+
_moduleIndex.insert({options->export_root, std::set<std::string>()});
37+
}
38+
39+
_moduleIndex.at(options->export_root).insert(modulePath);
40+
}
41+
}
42+
43+
void ModuleIndex::ReBuildIndex(const std::vector<std::string>& files)
44+
{
45+
_moduleIndex.clear();
46+
BuildIndex(files);
47+
}
48+
49+
std::vector<std::string> ModuleIndex::GetModules(std::string filename)
50+
{
51+
auto options = LanguageClient::GetInstance().GetOptions(filename);
52+
53+
std::vector<std::string> modules;
54+
for (auto& from : options->import_from)
55+
{
56+
auto it = _moduleIndex.find(from);
57+
58+
if (it != _moduleIndex.end())
59+
{
60+
for (auto& modulePath : it->second)
61+
{
62+
modules.emplace_back(modulePath);
63+
}
64+
}
65+
}
66+
return modules;
67+
}

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "LuaParser/LuaParser.h"
66
#include "Util/format.h"
77
#include "Util/Url.h"
8+
#include "Util/FileFinder.h"
89

910
LanguageClient& LanguageClient::GetInstance()
1011
{
@@ -13,7 +14,9 @@ LanguageClient& LanguageClient::GetInstance()
1314
}
1415

1516
LanguageClient::LanguageClient()
16-
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>())
17+
: _defaultOptions(std::make_shared<LuaCodeStyleOptions>()),
18+
_idCounter(0),
19+
_moduleIndex(std::make_shared<ModuleIndex>())
1720
{
1821
}
1922

@@ -44,6 +47,24 @@ void LanguageClient::SendNotification(std::string_view method, std::shared_ptr<v
4447
}
4548
}
4649

50+
void LanguageClient::SendRequest(std::string_view method, std::shared_ptr<vscode::Serializable> param)
51+
{
52+
auto json = nlohmann::json::object();
53+
json["jsonrpc"] = "2.0";
54+
json["method"] = method;
55+
json["id"] = GetRequestId();
56+
json["params"] = param->Serialize();
57+
58+
if (_session)
59+
{
60+
auto dumpResult = json.dump();
61+
std::string message = format("Content-Length:{}\r\n\r\n", dumpResult.size());
62+
63+
message.append(dumpResult);
64+
_session->Send(std::move(message));
65+
}
66+
}
67+
4768
void LanguageClient::CacheFile(std::string_view uri, std::string&& text)
4869
{
4970
auto filename = url::UrlToFilePath(uri);
@@ -110,6 +131,9 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
110131
diagnosis.severity = vscode::DiagnosticSeverity::Warning;
111132
}
112133

134+
135+
136+
113137
SendNotification("textDocument/publishDiagnostics", vscodeDiagnosis);
114138
}
115139

@@ -133,9 +157,18 @@ void LanguageClient::Run()
133157
}
134158
}
135159

136-
std::shared_ptr<LuaCodeStyleOptions> LanguageClient::GetOptions(std::string_view uri)
160+
std::shared_ptr<LuaCodeStyleOptions> LanguageClient::GetOptions(std::string_view uriOrFilename)
137161
{
138-
auto filename = url::UrlToFilePath(uri);
162+
std::string filename;
163+
if (uriOrFilename.starts_with("file"))
164+
{
165+
filename = url::UrlToFilePath(uriOrFilename);
166+
}
167+
else
168+
{
169+
filename = std::string(uriOrFilename);
170+
}
171+
139172
std::size_t matchLength = 0;
140173
std::shared_ptr<LuaCodeStyleOptions> options = _defaultOptions;
141174
for (auto it = _editorConfigVector.begin(); it != _editorConfigVector.end(); it++)
@@ -159,6 +192,7 @@ void LanguageClient::UpdateOptions(std::string_view workspaceUri, std::string_vi
159192
{
160193
pair.second = LuaEditorConfig::LoadFromFile(std::string(configPath));
161194
pair.second->SetWorkspace(workspace);
195+
pair.second->SetRootWorkspace(_root);
162196
return;
163197
}
164198
}
@@ -168,6 +202,7 @@ void LanguageClient::UpdateOptions(std::string_view workspaceUri, std::string_vi
168202
LuaEditorConfig::LoadFromFile(std::string(configPath))
169203
});
170204
_editorConfigVector.back().second->SetWorkspace(workspace);
205+
_editorConfigVector.back().second->SetRootWorkspace(_root);
171206
}
172207

173208
void LanguageClient::RemoveOptions(std::string_view workspaceUri)
@@ -185,9 +220,32 @@ void LanguageClient::RemoveOptions(std::string_view workspaceUri)
185220

186221
void LanguageClient::UpdateAllDiagnosis()
187222
{
223+
FileFinder finder(_root);
224+
225+
finder.AddIgnoreDirectory(".git");
226+
finder.AddIgnoreDirectory(".github");
227+
finder.AddIgnoreDirectory(".svn");
228+
finder.AddIgnoreDirectory(".idea");
229+
finder.AddIgnoreDirectory(".vs");
230+
finder.AddIgnoreDirectory(".vscode");
231+
232+
finder.AddFindExtension(".lua");
233+
234+
_moduleIndex->ReBuildIndex(finder.FindFiles());
235+
188236
for (auto it : _fileMap)
189237
{
190238
auto uri = url::FilePathToUrl(it.first);
191239
DiagnosticFile(uri);
192240
}
193241
}
242+
243+
void LanguageClient::SetRoot(std::string_view root)
244+
{
245+
_root = root;
246+
}
247+
248+
uint64_t LanguageClient::GetRequestId()
249+
{
250+
return ++_idCounter;
251+
}

CodeFormatServer/src/LanguageService.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LanguageService::~LanguageService()
2222
bool LanguageService::Initialize()
2323
{
2424
JsonProtocol("initialize", &LanguageService::OnInitialize);
25+
JsonProtocol("Initialized", &LanguageService::OnInitialized);
2526
JsonProtocol("textDocument/didChange", &LanguageService::OnDidChange);
2627
JsonProtocol("textDocument/didOpen", &LanguageService::OnDidOpen);
2728
JsonProtocol("textDocument/didClose", &LanguageService::OnClose);
@@ -100,10 +101,17 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
100101
}
101102
}
102103

104+
LanguageClient::GetInstance().SetRoot(param->rootPath);
103105

104106
return result;
105107
}
106108

109+
std::shared_ptr<vscode::Serializable> LanguageService::OnInitialized(std::shared_ptr<vscode::Serializable> param)
110+
{
111+
LanguageClient::GetInstance().UpdateAllDiagnosis();
112+
return nullptr;
113+
}
114+
107115
std::shared_ptr<vscode::Serializable> LanguageService::OnDidChange(
108116
std::shared_ptr<vscode::DidChangeTextDocumentParams> param)
109117
{
@@ -278,11 +286,12 @@ std::shared_ptr<vscode::CodeActionResult> LanguageService::OnCodeAction(std::sha
278286
std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
279287
std::shared_ptr<vscode::ExecuteCommandParams> param)
280288
{
281-
if(param->command == "emmylua.reformat.me")
289+
auto result = std::make_shared<vscode::Serializable>();
290+
if (param->command == "emmylua.reformat.me")
282291
{
283-
if(param->arguments.size() < 2)
292+
if (param->arguments.size() < 2)
284293
{
285-
return nullptr;
294+
return result;
286295
}
287296

288297
std::string uri = param->arguments[0];
@@ -292,29 +301,32 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
292301

293302
auto parser = LanguageClient::GetInstance().GetFileParser(uri);
294303

295-
auto result = std::make_shared<vscode::DocumentFormattingResult>();
304+
auto applyParams = std::make_shared<vscode::ApplyWorkspaceEditParams>();
296305

297306
auto options = LanguageClient::GetInstance().GetOptions(uri);
298307

299308
if (parser->HasError())
300309
{
301-
result->hasError = true;
302310
return result;
303311
}
304312

305313
LuaFormatter formatter(parser, *options);
306314
formatter.BuildFormattedElement();
315+
auto it = applyParams->edit.changes.emplace(uri, std::vector<vscode::TextEdit>());
316+
auto& change = it.first->second;
307317

308-
auto& edit = result->edits.emplace_back();
318+
auto& edit = change.emplace_back();
309319
LuaFormatRange formattedRange(static_cast<int>(range.start.line), static_cast<int>(range.end.line));
310320

311321
edit.newText = formatter.GetRangeFormattedText(formattedRange);
312322
edit.range = vscode::Range(
313323
vscode::Position(formattedRange.StartLine, formattedRange.StartCharacter),
314324
vscode::Position(formattedRange.EndLine + 1, formattedRange.EndCharacter)
315325
);
316-
return result;
326+
327+
328+
LanguageClient::GetInstance().SendRequest("workspace/applyEdit", applyParams);
317329
}
318330

319-
return nullptr;
331+
return result;
320332
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "CodeFormatServer/Service/ModuleService.h"
2+
#include "LuaParser/LuaAstVisitor.h"
3+
4+
class UnResolveModuleFinder : public LuaAstVisitor
5+
{
6+
protected:
7+
void VisitParamList(const std::shared_ptr<LuaAstNode>& node) override;
8+
9+
void VisitLocalFunctionStatement(const std::shared_ptr<LuaAstNode>& node) override;
10+
11+
void VisitLocalStatement(const std::shared_ptr<LuaAstNode>& node) override;
12+
13+
void VisitIdentify(const std::shared_ptr<LuaAstNode>& node) override;
14+
15+
};
16+
17+
18+
19+
20+
std::vector<vscode::Diagnostic> ModuleService::GetModuleDiagnostics()
21+
{
22+
}

CodeFormatServer/src/VSCode.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CodeFormatServer/VSCode.h"
22

3+
34
vscode::Serializable::~Serializable()
45
{
56
}
@@ -409,3 +410,61 @@ void vscode::ExecuteCommandParams::Deserialize(nlohmann::json json)
409410
}
410411
}
411412
}
413+
414+
nlohmann::json vscode::OptionalVersionedTextDocumentIdentifier::Serialize()
415+
{
416+
auto object = TextDocument::Serialize();
417+
if(version.has_value())
418+
{
419+
object["version"] = version.value();
420+
}
421+
else
422+
{
423+
object["version"] = nullptr;
424+
}
425+
426+
return object;
427+
}
428+
429+
nlohmann::json vscode::TextDocumentEdit::Serialize()
430+
{
431+
auto object = nlohmann::json::object();
432+
433+
object["textDocument"] = textDocument.Serialize();
434+
435+
object["edits"] = SerializeArray(edits);
436+
437+
return object;
438+
}
439+
440+
nlohmann::json vscode::WorkspaceEdit::Serialize()
441+
{
442+
auto object = nlohmann::json::object();
443+
444+
auto changesObject = nlohmann::json::object();
445+
446+
for (auto it : changes) {
447+
changesObject[it.first] = SerializeArray(it.second);
448+
}
449+
object["changes"] = changesObject;
450+
451+
return object;
452+
}
453+
454+
nlohmann::json vscode::ApplyWorkspaceEditParams::Serialize()
455+
{
456+
auto object = nlohmann::json::object();
457+
458+
if(label.has_value())
459+
{
460+
object["label"] = label.value();
461+
}
462+
else
463+
{
464+
object["label"] = nullptr;
465+
}
466+
467+
object["edit"] = edit.Serialize();
468+
469+
return object;
470+
}

0 commit comments

Comments
 (0)