Skip to content

Commit 3f9fdeb

Browse files
committed
模块检测基础
1 parent e47147c commit 3f9fdeb

File tree

6 files changed

+131
-28
lines changed

6 files changed

+131
-28
lines changed

CodeFormatServer/src/LanguageClient.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ void LanguageClient::DiagnosticFile(std::string_view uri)
118118
}
119119

120120
auto formatDiagnostic = GetService<CodeFormatService>()->Diagnose(filename, parser, options);
121-
auto moduleDiagnosis = GetService<ModuleService>()->Diagnose(parser, options);
121+
auto moduleDiagnosis = GetService<ModuleService>()->Diagnose(filename, parser, options);
122122

123123
std::copy(formatDiagnostic.begin(), formatDiagnostic.end(), std::back_inserter(vscodeDiagnosis->diagnostics));
124124
std::copy(moduleDiagnosis.begin(), moduleDiagnosis.end(), std::back_inserter(vscodeDiagnosis->diagnostics));

CodeFormatServer/src/LanguageService.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "CodeService/LuaFormatter.h"
99
#include "CodeFormatServer/LanguageClient.h"
1010
#include "CodeFormatServer/Service/CodeFormatService.h"
11+
#include "CodeFormatServer/Service/ModuleService.h"
1112
#include "Util/Url.h"
1213

1314
using namespace std::placeholders;
@@ -23,7 +24,7 @@ LanguageService::~LanguageService()
2324
bool LanguageService::Initialize()
2425
{
2526
JsonProtocol("initialize", &LanguageService::OnInitialize);
26-
JsonProtocol("Initialized", &LanguageService::OnInitialized);
27+
JsonProtocol("initialized", &LanguageService::OnInitialized);
2728
JsonProtocol("textDocument/didChange", &LanguageService::OnDidChange);
2829
JsonProtocol("textDocument/didOpen", &LanguageService::OnDidOpen);
2930
JsonProtocol("textDocument/didClose", &LanguageService::OnClose);
@@ -276,7 +277,7 @@ std::shared_ptr<vscode::CodeActionResult> LanguageService::OnCodeAction(std::sha
276277
auto filePath = url::UrlToFilePath(uri);
277278
auto codeActionResult = std::make_shared<vscode::CodeActionResult>();
278279

279-
if(LanguageClient::GetInstance().GetService<CodeFormatService>()->IsDiagnosticRange(filePath, range))
280+
if (LanguageClient::GetInstance().GetService<CodeFormatService>()->IsDiagnosticRange(filePath, range))
280281
{
281282
auto& action = codeActionResult->actions.emplace_back();
282283
std::string title = "reformat me";
@@ -288,7 +289,26 @@ std::shared_ptr<vscode::CodeActionResult> LanguageService::OnCodeAction(std::sha
288289

289290
action.kind = vscode::CodeActionKind::QuickFix;
290291
}
292+
else if (LanguageClient::GetInstance().GetService<ModuleService>()->IsDiagnosticRange(filePath, range))
293+
{
294+
auto modules = LanguageClient::GetInstance().GetService<ModuleService>()->GetImportModules(filePath, range);
295+
auto& action = codeActionResult->actions.emplace_back();
296+
std::string title = "import me";
297+
action.title = title;
298+
action.command.title = title;
299+
action.command.command = "emmylua.import.me";
300+
action.command.arguments.push_back(param->textDocument.uri);
301+
action.command.arguments.push_back(param->range.Serialize());
302+
for (auto& module : modules)
303+
{
304+
auto object = nlohmann::json::object();
305+
object["moduleName"] = module.ModuleName;
306+
object["path"] = module.FilePath;
307+
action.command.arguments.push_back(object);
308+
}
291309

310+
action.kind = vscode::CodeActionKind::QuickFix;
311+
}
292312
return codeActionResult;
293313
}
294314

@@ -325,7 +345,8 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
325345
auto& edit = change.emplace_back();
326346
LuaFormatRange formattedRange(static_cast<int>(range.start.line), static_cast<int>(range.end.line));
327347

328-
auto formatResult = LanguageClient::GetInstance().GetService<CodeFormatService>()->RangeFormat(formattedRange, parser, options);
348+
auto formatResult = LanguageClient::GetInstance().GetService<CodeFormatService>()->RangeFormat(
349+
formattedRange, parser, options);
329350

330351
edit.newText = std::move(formatResult);
331352

@@ -336,6 +357,8 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
336357

337358
LanguageClient::GetInstance().SendRequest("workspace/applyEdit", applyParams);
338359
}
339-
360+
else if (param->command == "emmylua.import.me")
361+
{
362+
}
340363
return result;
341364
}

CodeFormatServer/src/Service/Indexs/ModuleIndex.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
3333

3434
if (_moduleIndex.count(options->export_root) == 0)
3535
{
36-
_moduleIndex.insert({options->export_root, std::set<std::string>()});
36+
_moduleIndex.insert({options->export_root, std::vector<Module>()});
3737
}
3838

39-
_moduleIndex.at(options->export_root).insert(modulePath);
39+
_moduleIndex.at(options->export_root).emplace_back(modulePath, filename);
4040
}
4141
}
4242

@@ -46,22 +46,17 @@ void ModuleIndex::RebuildIndex(const std::vector<std::string>& files)
4646
BuildIndex(files);
4747
}
4848

49-
std::vector<std::string> ModuleIndex::GetModules(std::string filename)
49+
std::vector<ModuleIndex::Module>& ModuleIndex::GetModules(std::shared_ptr<LuaCodeStyleOptions> options)
5050
{
51-
auto options = LanguageClient::GetInstance().GetOptions(filename);
5251

53-
std::vector<std::string> modules;
5452
for (auto& from : options->import_from)
5553
{
5654
auto it = _moduleIndex.find(from);
5755

5856
if (it != _moduleIndex.end())
5957
{
60-
for (auto& modulePath : it->second)
61-
{
62-
modules.emplace_back(modulePath);
63-
}
58+
return it->second;
6459
}
6560
}
66-
return modules;
61+
return _emptyModules;
6762
}

CodeFormatServer/src/Service/ModuleService.cpp

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

159199
std::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+
}

include/CodeFormatServer/Service/Indexs/ModuleIndex.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@
44
#include <vector>
55
#include <string>
66
#include <set>
7+
#include <memory>
8+
#include "CodeService/LuaCodeStyleOptions.h"
79

810
class ModuleIndex
911
{
1012
public:
13+
class Module
14+
{
15+
public:
16+
std::string ModuleName;
17+
std::string FilePath;
18+
};
19+
1120
ModuleIndex();
1221

1322
void BuildIndex(const std::vector<std::string>& files);
1423

1524
void RebuildIndex(const std::vector<std::string>& files);
1625

17-
std::vector<std::string> GetModules(std::string filename);
26+
std::vector<Module>& GetModules(std::shared_ptr<LuaCodeStyleOptions> options);
1827

1928
private:
20-
std::map<std::string, std::set<std::string>> _moduleIndex;
29+
std::map<std::string, std::vector<Module>> _moduleIndex;
30+
std::vector<Module> _emptyModules;
2131
};

include/CodeFormatServer/Service/ModuleService.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,29 @@
33
#include <vector>
44
#include <memory>
55
#include <string>
6+
#include <map>
67
#include "LuaParser/LuaParser.h"
78
#include "CodeFormatServer/VSCode.h"
89
#include "CodeService/LuaCodeStyleOptions.h"
910
#include "Service.h"
1011
#include "Indexs/ModuleIndex.h"
1112

12-
class ModuleService: public Service
13+
14+
class ModuleService : public Service
1315
{
1416
public:
1517
LANGUAGE_SERVICE(ModuleService);
1618

1719
explicit ModuleService(std::shared_ptr<LanguageClient> owner);
1820

19-
std::vector<vscode::Diagnostic> Diagnose(std::shared_ptr<LuaParser> parser, std::shared_ptr<LuaCodeStyleOptions> options);
21+
std::vector<vscode::Diagnostic> Diagnose(std::string_view filePath, std::shared_ptr<LuaParser> parser,
22+
std::shared_ptr<LuaCodeStyleOptions> options);
2023
std::vector<std::string> GetCompleteItems();
2124

2225
void RebuildIndexs(std::vector<std::string> files);
23-
26+
bool IsDiagnosticRange(std::string_view filePath, vscode::Range range);
27+
std::vector<ModuleIndex::Module> GetImportModules(std::string_view filePath, vscode::Range range);
2428
private:
2529
ModuleIndex _moduleIndex;
26-
};
30+
std::map<std::string, std::multimap<vscode::Range, ModuleIndex::Module>, std::less<>> _diagnosticCaches;
31+
};

0 commit comments

Comments
 (0)