66#include " Util/format.h"
77#include " Util/Url.h"
88#include " Util/FileFinder.h"
9-
9+ # include " asio.hpp "
1010#include " CodeFormatServer/Service/ModuleService.h"
1111#include " CodeFormatServer/Service/CodeFormatService.h"
1212#include " CodeFormatServer/Service/CompletionService.h"
@@ -20,7 +20,8 @@ LanguageClient& LanguageClient::GetInstance()
2020
2121LanguageClient::LanguageClient ()
2222 : _defaultOptions(std::make_shared<LuaCodeStyleOptions>()),
23- _idCounter(0 )
23+ _idCounter(0 ),
24+ _ioc(nullptr )
2425{
2526}
2627
@@ -30,12 +31,12 @@ void LanguageClient::InitializeService()
3031 AddService<ModuleService>();
3132 AddService<CompletionService>();
3233
33- for (auto service : _services)
34+ for (auto service : _services)
3435 {
3536 service->Initialize ();
3637 }
3738
38- for (auto service : _services)
39+ for (auto service : _services)
3940 {
4041 service->Start ();
4142 }
@@ -90,13 +91,13 @@ void LanguageClient::UpdateFile(std::string_view uri, vscode::Range range, std::
9091{
9192 auto filename = url::UrlToFilePath (uri);
9293 auto it = _fileMap.find (filename);
93- if (it == _fileMap.end ())
94+ if (it == _fileMap.end ())
9495 {
9596 auto virtualFile = std::make_shared<VirtualFile>(filename);
9697 virtualFile->UpdateFile (std::move (text));
9798 _fileMap[filename] = virtualFile;
9899 }
99- else if (range.start .line == -1 )
100+ else if (range.start .line == -1 )
100101 {
101102 it->second ->UpdateFile (std::move (text));
102103 }
@@ -116,13 +117,13 @@ void LanguageClient::UpdateFile(std::string_view uri, std::vector<vscode::TextDo
116117 }
117118}
118119
119- void LanguageClient::ParseFile (std::string_view uri)
120+ void LanguageClient::ClearFile (std::string_view uri)
120121{
121122 auto filename = url::UrlToFilePath (uri);
122123 auto it = _fileMap.find (filename);
123124 if (it != _fileMap.end ())
124125 {
125- it-> second -> MakeParser ( );
126+ _fileMap. erase (it );
126127 }
127128}
128129
@@ -161,6 +162,35 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
161162 SendNotification (" textDocument/publishDiagnostics" , vscodeDiagnosis);
162163}
163164
165+ void LanguageClient::DelayDiagnosticFile (std::string_view uri)
166+ {
167+ auto stringUri = std::string (uri);
168+ auto task = std::make_shared<asio::steady_timer>(GetIOContext (), std::chrono::milliseconds (500 ));
169+ auto it = _fileDiagnosticTask.find (uri);
170+ if (it != _fileDiagnosticTask.end ())
171+ {
172+ it->second ->cancel ();
173+ it->second = task;
174+ }
175+ else {
176+ _fileDiagnosticTask.insert ({ stringUri, task });
177+ }
178+
179+ task->async_wait ([this , stringUri](const asio::error_code& code)
180+ {
181+ if (code == asio::error::operation_aborted)
182+ {
183+ return ;
184+ }
185+ DiagnosticFile (stringUri);
186+ auto it = _fileDiagnosticTask.find (stringUri);
187+ if (it != _fileDiagnosticTask.end ())
188+ {
189+ _fileDiagnosticTask.erase (it);
190+ }
191+ });
192+ }
193+
164194std::shared_ptr<LuaParser> LanguageClient::GetFileParser (std::string_view uri)
165195{
166196 auto filename = url::UrlToFilePath (uri);
@@ -173,12 +203,14 @@ std::shared_ptr<LuaParser> LanguageClient::GetFileParser(std::string_view uri)
173203 return nullptr ;
174204}
175205
176- void LanguageClient::Run ()
206+ int LanguageClient::Run (std::shared_ptr<asio::io_context> ioc )
177207{
208+ _ioc = ioc;
178209 if (_session)
179210 {
180- _session->Run ();
211+ return _session->Run (*ioc );
181212 }
213+ return 1 ;
182214}
183215
184216std::shared_ptr<LuaCodeStyleOptions> LanguageClient::GetOptions (std::string_view uriOrFilename)
@@ -273,6 +305,11 @@ void LanguageClient::SetRoot(std::string_view root)
273305 _root = root;
274306}
275307
308+ asio::io_context& LanguageClient::GetIOContext ()
309+ {
310+ return *_ioc;
311+ }
312+
276313uint64_t LanguageClient::GetRequestId ()
277314{
278315 return ++_idCounter;
0 commit comments