55#include " LuaParser/LuaParser.h"
66#include " Util/format.h"
77#include " Util/Url.h"
8+ #include " Util/FileFinder.h"
89
910LanguageClient& LanguageClient::GetInstance ()
1011{
@@ -13,7 +14,9 @@ LanguageClient& LanguageClient::GetInstance()
1314}
1415
1516LanguageClient::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+
4768void 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
173208void LanguageClient::RemoveOptions (std::string_view workspaceUri)
@@ -185,9 +220,32 @@ void LanguageClient::RemoveOptions(std::string_view workspaceUri)
185220
186221void 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+ }
0 commit comments