99#include " CodeFormatServer/LanguageClient.h"
1010#include " CodeFormatServer/Service/CodeFormatService.h"
1111#include " CodeFormatServer/Service/ModuleService.h"
12+ #include " CodeFormatServer/Service/CompletionService.h"
1213#include " Util/Url.h"
1314#include " Util/format.h"
1415
@@ -29,7 +30,8 @@ bool LanguageService::Initialize()
2930 JsonProtocol (" textDocument/didChange" , &LanguageService::OnDidChange);
3031 JsonProtocol (" textDocument/didOpen" , &LanguageService::OnDidOpen);
3132 JsonProtocol (" textDocument/didClose" , &LanguageService::OnClose);
32- JsonProtocol (" updateEditorConfig" , &LanguageService::OnEditorConfigUpdate);
33+ JsonProtocol (" config/editorconfig/update" , &LanguageService::OnEditorConfigUpdate);
34+ JsonProtocol (" config/moduleconfig/update" , &LanguageService::OnModuleConfigUpdate);
3335 JsonProtocol (" textDocument/formatting" , &LanguageService::OnFormatting);
3436 JsonProtocol (" textDocument/rangeFormatting" , &LanguageService::OnRangeFormatting);
3537 JsonProtocol (" textDocument/onTypeFormatting" , &LanguageService::OnTypeFormatting);
@@ -78,12 +80,21 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
7880 result->capabilities .completionProvider .resolveProvider = false ;
7981 result->capabilities .completionProvider .triggerCharacters = {};
8082
81- auto & configFiles = param->initializationOptions .configFiles ;
82- for (auto & configFile : configFiles )
83+ auto & editorConfigFiles = param->initializationOptions .editorConfigFiles ;
84+ for (auto & configFile : editorConfigFiles )
8385 {
84- LanguageClient::GetInstance ().UpdateOptions (configFile.workspace , configFile.path );
86+ LanguageClient::GetInstance ().UpdateCodeStyleOptions (configFile.workspace , configFile.path );
8587 }
8688
89+ auto & moduleConfigFiles = param->initializationOptions .moduleConfigFiles ;
90+ for (auto & configFile : moduleConfigFiles)
91+ {
92+ LanguageClient::GetInstance ().GetService <ModuleService>()->GetIndex ().BuildModule (
93+ configFile.workspace , configFile.path );
94+ }
95+
96+ LanguageClient::GetInstance ().GetService <ModuleService>()->GetIndex ().SetDefaultModule (param->rootPath );
97+
8798 std::filesystem::path localePath = param->initializationOptions .localeRoot ;
8899 localePath /= param->locale + " .json" ;
89100
@@ -117,7 +128,7 @@ std::shared_ptr<vscode::InitializeResult> LanguageService::OnInitialize(std::sha
117128
118129std::shared_ptr<vscode::Serializable> LanguageService::OnInitialized (std::shared_ptr<vscode::Serializable> param)
119130{
120- LanguageClient::GetInstance ().UpdateAllDiagnosis ();
131+ LanguageClient::GetInstance ().UpdateModuleInfo ();
121132 return nullptr ;
122133}
123134
@@ -189,19 +200,19 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnClose(
189200}
190201
191202std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate (
192- std::shared_ptr<vscode::EditorConfigUpdateParams > param)
203+ std::shared_ptr<vscode::ConfigUpdateParams > param)
193204{
194205 switch (param->type )
195206 {
196207 case vscode::FileChangeType::Created:
197208 case vscode::FileChangeType::Changed:
198209 {
199- LanguageClient::GetInstance ().UpdateOptions (param->source .workspace , param->source .path );
210+ LanguageClient::GetInstance ().UpdateCodeStyleOptions (param->source .workspace , param->source .path );
200211 break ;
201212 }
202213 case vscode::FileChangeType::Delete:
203214 {
204- LanguageClient::GetInstance ().RemoveOptions (param->source .workspace );
215+ LanguageClient::GetInstance ().RemoveCodeStyleOptions (param->source .workspace );
205216 break ;
206217 }
207218 }
@@ -211,6 +222,33 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnEditorConfigUpdate(
211222 return nullptr ;
212223}
213224
225+
226+ std::shared_ptr<vscode::Serializable> LanguageService::OnModuleConfigUpdate (
227+ std::shared_ptr<vscode::ConfigUpdateParams> param)
228+ {
229+ switch (param->type )
230+ {
231+ case vscode::FileChangeType::Created:
232+ case vscode::FileChangeType::Changed:
233+ {
234+ LanguageClient::GetInstance ().GetService <ModuleService>()
235+ ->GetIndex ().BuildModule (param->source .workspace , param->source .path );
236+ break ;
237+ }
238+ case vscode::FileChangeType::Delete:
239+ {
240+ LanguageClient::GetInstance ().GetService <ModuleService>()
241+ ->GetIndex ().RemoveModule (param->source .workspace );
242+ break ;
243+ }
244+ }
245+
246+ LanguageClient::GetInstance ().UpdateModuleInfo ();
247+
248+ return nullptr ;
249+ }
250+
251+
214252std::shared_ptr<vscode::Serializable> LanguageService::OnRangeFormatting (
215253 std::shared_ptr<vscode::DocumentRangeFormattingParams> param)
216254{
@@ -371,15 +409,22 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
371409 }
372410
373411 std::string uri = param->arguments [0 ];
412+ std::string filePath = url::UrlToFilePath (uri);
413+ auto config = LanguageClient::GetInstance ().GetService <ModuleService>()->GetIndex ().GetConfig (filePath);
414+ if (!config)
415+ {
416+ return nullptr ;
417+ }
374418 vscode::Range range;
375419
376420 range.Deserialize (param->arguments [1 ]);
377421
378422 std::string moduleName = param->arguments [2 ];
379- auto pos = moduleName. find_last_of ( ' . ' );
423+
380424 std::string moduleDefineName = param->arguments [3 ];
381425
382- std::string requireString = format (" local {} = require(\" {}\" )\n " , moduleDefineName, moduleName);
426+ std::string requireString = format (" local {} = {}(\" {}\" )\n " , moduleDefineName, config->import_function ,
427+ moduleName);
383428 auto parser = LanguageClient::GetInstance ().GetFileParser (uri);
384429
385430 auto applyParams = std::make_shared<vscode::ApplyWorkspaceEditParams>();
@@ -426,7 +471,18 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChangeWatchedFiles(
426471 return nullptr ;
427472}
428473
429- std::shared_ptr<vscode::Serializable > LanguageService::OnCompletion (std::shared_ptr<vscode::CompletionParams> param)
474+ std::shared_ptr<vscode::CompletionList > LanguageService::OnCompletion (std::shared_ptr<vscode::CompletionParams> param)
430475{
431- return nullptr ;
476+ auto uri = param->textDocument .uri ;
477+
478+ auto parser = LanguageClient::GetInstance ().GetFileParser (uri);
479+
480+ auto options = LanguageClient::GetInstance ().GetOptions (uri);
481+
482+ auto list = std::make_shared<vscode::CompletionList>();
483+ list->isIncomplete = true ;
484+ list->items = LanguageClient::GetInstance ().GetService <CompletionService>()->GetCompletions (
485+ param->textDocument .uri , param->position , parser, options);
486+
487+ return list;
432488}
0 commit comments