@@ -77,7 +77,7 @@ class UnResolveModuleFinder : public LuaAstVisitor
7777 }
7878 }
7979
80- void VisitBlock (const std::shared_ptr<LuaAstNode>& block)
80+ void VisitBlock (const std::shared_ptr<LuaAstNode>& block) override
8181 {
8282 _scopeMap.insert ({block, Scope ()});
8383 }
@@ -144,16 +144,56 @@ ModuleService::ModuleService(std::shared_ptr<LanguageClient> owner)
144144{
145145}
146146
147- std::vector<vscode::Diagnostic> ModuleService::Diagnose (std::shared_ptr<LuaParser> parser,
147+ std::vector<vscode::Diagnostic> ModuleService::Diagnose (std::string_view filePath,
148+ std::shared_ptr<LuaParser> parser,
148149 std::shared_ptr<LuaCodeStyleOptions> options)
149150{
150- // std::vector<vscode::Diagnostic> result;
151- // UnResolveModuleFinder finder;
152- // auto& undefinedModules = finder.GetUndefinedModule(parser);
151+ std::vector<vscode::Diagnostic> result;
152+ UnResolveModuleFinder finder;
153+ auto & undefinedModules = finder.GetUndefinedModule (parser);
154+ auto & luaModules = _moduleIndex.GetModules (options);
153155
156+ std::multimap<vscode::Range, ModuleIndex::Module> rangeModule;
157+ for (auto & undefinedModule : undefinedModules)
158+ {
159+ auto undefinedModuleName = undefinedModule->GetText ();
160+ for (auto & luaModule : luaModules)
161+ {
162+ std::string name;
154163
155- // return result;
156- return {};
164+ auto dotPosition = luaModule.ModuleName .find_last_of (' .' );
165+ if (dotPosition == std::string_view::npos)
166+ {
167+ name = luaModule.ModuleName ;
168+ }
169+ else
170+ {
171+ name = luaModule.ModuleName .substr (dotPosition + 1 );
172+ }
173+
174+ if (undefinedModuleName == name)
175+ {
176+ auto & diagnostic = result.emplace_back ();
177+ diagnostic.message = " not import module" ;
178+ auto textRange = undefinedModule->GetTextRange ();
179+ auto range = vscode::Range (
180+ vscode::Position (
181+ parser->GetLine (textRange.StartOffset ),
182+ parser->GetColumn (textRange.StartOffset )
183+ ),
184+ vscode::Position (
185+ parser->GetLine (textRange.EndOffset ),
186+ parser->GetColumn (textRange.EndOffset )
187+ )
188+ );
189+ rangeModule.insert ({range, luaModule});
190+ diagnostic.range = range;
191+ diagnostic.severity = vscode::DiagnosticSeverity::Information;
192+ }
193+ }
194+ }
195+ _diagnosticCaches[std::string (filePath)] = std::move (rangeModule);
196+ return result;
157197}
158198
159199std::vector<std::string> ModuleService::GetCompleteItems ()
@@ -165,3 +205,33 @@ void ModuleService::RebuildIndexs(std::vector<std::string> files)
165205{
166206 _moduleIndex.RebuildIndex (files);
167207}
208+
209+ bool ModuleService::IsDiagnosticRange (std::string_view filePath, vscode::Range range)
210+ {
211+ auto it = _diagnosticCaches.find (filePath);
212+ if (it == _diagnosticCaches.end ())
213+ {
214+ return false ;
215+ }
216+
217+ return it->second .count (range) > 0 ;
218+ }
219+
220+ std::vector<ModuleIndex::Module> ModuleService::GetImportModules (std::string_view filePath, vscode::Range range)
221+ {
222+ auto fIt = _diagnosticCaches.find (filePath);
223+ if (fIt == _diagnosticCaches.end ())
224+ {
225+ return {};
226+ }
227+
228+ auto & rangeModules = fIt ->second ;
229+ auto equalRange = rangeModules.equal_range (range);
230+ std::vector<ModuleIndex::Module> result;
231+ for (auto it = equalRange.first ; it != equalRange.second ; ++it)
232+ {
233+ result.emplace_back (it->second );
234+ }
235+
236+ return result;
237+ }
0 commit comments