Skip to content

Commit 5f556f0

Browse files
committed
搭建代码补全基础
1 parent cd35377 commit 5f556f0

File tree

9 files changed

+201
-36
lines changed

9 files changed

+201
-36
lines changed

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void LanguageClient::UpdateAllDiagnosis()
221221
finder.AddFindExtension(".lua");
222222
finder.AddFindExtension(".lua.txt");
223223

224-
GetService<ModuleService>()->RebuildIndexs(finder.FindFiles());
224+
GetService<ModuleService>()->GetIndex().RebuildIndex(finder.FindFiles());
225225

226226
for (auto it : _fileMap)
227227
{

CodeFormatServer/src/LanguageService.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ bool LanguageService::Initialize()
3535
JsonProtocol("textDocument/onTypeFormatting", &LanguageService::OnTypeFormatting);
3636
JsonProtocol("textDocument/codeAction", &LanguageService::OnCodeAction);
3737
JsonProtocol("workspace/executeCommand", &LanguageService::OnExecuteCommand);
38+
JsonProtocol("workspace/didChangeWatchedFiles", &LanguageService::OnDidChangeWatchedFiles);
39+
JsonProtocol("textDocument/completion", &LanguageService::OnCompletion);
3840
return true;
3941
}
4042

@@ -73,6 +75,9 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
7375
"emmylua.import.me"
7476
};
7577

78+
result->capabilities.completionProvider.resolveProvider = false;
79+
result->capabilities.completionProvider.triggerCharacters = {};
80+
7681
auto& configFiles = param->initializationOptions.configFiles;
7782
for (auto& configFile : configFiles)
7883
{
@@ -188,13 +193,13 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate(
188193
{
189194
switch (param->type)
190195
{
191-
case vscode::EditorConfigUpdateType::Created:
192-
case vscode::EditorConfigUpdateType::Changed:
196+
case vscode::FileChangeType::Created:
197+
case vscode::FileChangeType::Changed:
193198
{
194199
LanguageClient::GetInstance().UpdateOptions(param->source.workspace, param->source.path);
195200
break;
196201
}
197-
case vscode::EditorConfigUpdateType::Delete:
202+
case vscode::FileChangeType::Delete:
198203
{
199204
LanguageClient::GetInstance().RemoveOptions(param->source.workspace);
200205
break;
@@ -309,7 +314,7 @@ std::shared_ptr<vscode::CodeActionResult> LanguageService::OnCodeAction(std::sha
309314
}
310315

311316
action.kind = vscode::CodeActionKind::QuickFix;
312-
}
317+
}
313318
return codeActionResult;
314319
}
315320

@@ -391,3 +396,32 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
391396
}
392397
return result;
393398
}
399+
400+
std::shared_ptr<vscode::Serializable> LanguageService::OnDidChangeWatchedFiles(
401+
std::shared_ptr<vscode::DidChangeWatchedFilesParams> param)
402+
{
403+
for (auto& change : param->changes)
404+
{
405+
auto filePath = url::UrlToFilePath(change.uri);
406+
switch (change.type)
407+
{
408+
case vscode::FileChangeType::Created:
409+
{
410+
std::vector<std::string> files = {filePath};
411+
LanguageClient::GetInstance().GetService<ModuleService>()->GetIndex().BuildIndex(files);
412+
break;
413+
}
414+
case vscode::FileChangeType::Delete:
415+
{
416+
LanguageClient::GetInstance().GetService<ModuleService>()->GetIndex().ClearFile(filePath);
417+
break;
418+
}
419+
default:
420+
{
421+
break;
422+
}
423+
}
424+
}
425+
426+
return nullptr;
427+
}

CodeFormatServer/src/Service/Indexs/ModuleIndex.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,23 @@ void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
4040
}
4141
}
4242

43+
void ModuleIndex::ClearFile(std::string_view filePath)
44+
{
45+
auto options = LanguageClient::GetInstance().GetOptions(filePath);
46+
if (_moduleIndex.count(options->export_root) > 0)
47+
{
48+
auto& vec = _moduleIndex.at(options->export_root);
49+
for(auto it = vec.begin(); it != vec.end(); ++it)
50+
{
51+
if(it->FilePath == filePath)
52+
{
53+
vec.erase(it);
54+
return;
55+
}
56+
}
57+
}
58+
}
59+
4360
void ModuleIndex::RebuildIndex(const std::vector<std::string>& files)
4461
{
4562
_moduleIndex.clear();

CodeFormatServer/src/Service/ModuleService.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,6 @@ std::vector<std::string> ModuleService::GetCompleteItems()
210210
return {};
211211
}
212212

213-
void ModuleService::RebuildIndexs(std::vector<std::string> files)
214-
{
215-
_moduleIndex.RebuildIndex(files);
216-
}
217-
218213
bool ModuleService::IsDiagnosticRange(std::string_view filePath, vscode::Range range)
219214
{
220215
auto it = _diagnosticCaches.find(filePath);
@@ -339,3 +334,8 @@ vscode::Range ModuleService::FindRequireRange(std::shared_ptr<LuaParser> parser)
339334

340335
return range;
341336
}
337+
338+
ModuleIndex& ModuleService::GetIndex()
339+
{
340+
return _moduleIndex;
341+
}

CodeFormatServer/src/VSCode.cpp

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ nlohmann::json vscode::ServerCapabilities::Serialize()
155155
object["documentOnTypeFormattingProvider"] = documentOnTypeFormattingProvider.Serialize();
156156
object["codeActionProvider"] = codeActionProvider;
157157
object["executeCommandProvider"] = executeCommandProvider.Serialize();
158+
object["completionProvider"] = completionProvider.Serialize();
158159

159160
return object;
160161
}
@@ -331,6 +332,15 @@ nlohmann::json vscode::ExecuteCommandOptions::Serialize()
331332
return object;
332333
}
333334

335+
nlohmann::json vscode::CompletionOptions::Serialize()
336+
{
337+
auto object = nlohmann::json::object();
338+
object["triggerCharacters"] = SerializeArray(triggerCharacters);
339+
object["resolveProvider"] = resolveProvider;
340+
341+
return object;
342+
}
343+
334344
void vscode::TextDocumentPositionParams::Deserialize(nlohmann::json json)
335345
{
336346
textDocument.Deserialize(json["textDocument"]);
@@ -365,7 +375,7 @@ nlohmann::json vscode::Command::Serialize()
365375

366376
auto array = nlohmann::json::array();
367377

368-
for(auto& arg: arguments)
378+
for (auto& arg : arguments)
369379
{
370380
array.push_back(arg);
371381
}
@@ -401,10 +411,12 @@ void vscode::ExecuteCommandParams::Deserialize(nlohmann::json json)
401411
{
402412
command = json["command"];
403413
auto args = json["arguments"];
404-
if(args.is_array())
414+
if (args.is_array())
405415
{
406-
for (auto arg : args) {
407-
if (!arg.is_null()) {
416+
for (auto arg : args)
417+
{
418+
if (!arg.is_null())
419+
{
408420
arguments.push_back(arg);
409421
}
410422
}
@@ -414,7 +426,7 @@ void vscode::ExecuteCommandParams::Deserialize(nlohmann::json json)
414426
nlohmann::json vscode::OptionalVersionedTextDocumentIdentifier::Serialize()
415427
{
416428
auto object = TextDocument::Serialize();
417-
if(version.has_value())
429+
if (version.has_value())
418430
{
419431
object["version"] = version.value();
420432
}
@@ -443,7 +455,8 @@ nlohmann::json vscode::WorkspaceEdit::Serialize()
443455

444456
auto changesObject = nlohmann::json::object();
445457

446-
for (auto it : changes) {
458+
for (auto it : changes)
459+
{
447460
changesObject[it.first] = SerializeArray(it.second);
448461
}
449462
object["changes"] = changesObject;
@@ -455,7 +468,7 @@ nlohmann::json vscode::ApplyWorkspaceEditParams::Serialize()
455468
{
456469
auto object = nlohmann::json::object();
457470

458-
if(label.has_value())
471+
if (label.has_value())
459472
{
460473
object["label"] = label.value();
461474
}
@@ -468,3 +481,26 @@ nlohmann::json vscode::ApplyWorkspaceEditParams::Serialize()
468481

469482
return object;
470483
}
484+
485+
void vscode::FileEvent::Deserialize(nlohmann::json json)
486+
{
487+
uri = json["uri"];
488+
type = static_cast<FileChangeType>(static_cast<uint32_t>(json["type"]));
489+
}
490+
491+
void vscode::DidChangeWatchedFilesParams::Deserialize(nlohmann::json json)
492+
{
493+
if (json["changes"].is_array())
494+
{
495+
for (auto& v : json["changes"])
496+
{
497+
changes.emplace_back().Deserialize(v);
498+
}
499+
}
500+
}
501+
502+
void vscode::CompletionParams::Deserialize(nlohmann::json json)
503+
{
504+
textDocument.Deserialize(json["textDocument"]);
505+
position.Deserialize(json["position"]);
506+
}

include/CodeFormatServer/LanguageService.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,13 @@ class LanguageService
5959

6060
std::shared_ptr<vscode::Serializable> OnExecuteCommand(std::shared_ptr<vscode::ExecuteCommandParams> param);
6161

62+
std::shared_ptr<vscode::Serializable> OnDidChangeWatchedFiles(std::shared_ptr<vscode::DidChangeWatchedFilesParams> param);
63+
64+
std::shared_ptr<vscode::Serializable> OnCompletion(std::shared_ptr<vscode::CompletionParams> param);
65+
6266
std::map<std::string, MessageHandle, std::less<>> _handles;
6367
};
6468

6569

6670

71+

include/CodeFormatServer/Service/Indexs/ModuleIndex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ModuleIndex
2121

2222
void BuildIndex(const std::vector<std::string>& files);
2323

24+
void ClearFile(std::string_view filePath);
25+
2426
void RebuildIndex(const std::vector<std::string>& files);
2527

2628
std::vector<Module>& GetModules(std::shared_ptr<LuaCodeStyleOptions> options);

include/CodeFormatServer/Service/ModuleService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ class ModuleService : public Service
3030
std::shared_ptr<LuaCodeStyleOptions> options);
3131
std::vector<std::string> GetCompleteItems();
3232

33-
void RebuildIndexs(std::vector<std::string> files);
3433
bool IsDiagnosticRange(std::string_view filePath, vscode::Range range);
3534
std::vector<LuaModule> GetImportModules(std::string_view filePath, vscode::Range range);
3635

3736
vscode::Range FindRequireRange(std::shared_ptr<LuaParser> parser);
3837

38+
ModuleIndex& GetIndex();
3939
private:
4040
ModuleIndex _moduleIndex;
4141
std::map<std::string, std::multimap<vscode::Range, LuaModule>, std::less<>> _diagnosticCaches;

0 commit comments

Comments
 (0)