Skip to content

Commit 538a793

Browse files
committed
更新原则
1 parent 1e774f7 commit 538a793

File tree

9 files changed

+143
-45
lines changed

9 files changed

+143
-45
lines changed

3rd/uriparser/src/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
#define PACKAGE_VERSION "0.9.5"
4545

46-
/* #undef HAVE_WPRINTF */
46+
#define HAVE_WPRINTF
4747
/* #undef HAVE_REALLOCARRAY */
4848

4949

CodeFormatServer/src/Service/Indexs/Config/ModuleConfig.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,30 @@ std::shared_ptr<ModuleConfig> ModuleConfig::LoadFromFile(std::string_view filePa
7979
}
8080
}
8181
}
82+
83+
if(json["export.rule"].is_array())
84+
{
85+
for (auto rule : json["export.rule"])
86+
{
87+
if (rule.is_string())
88+
{
89+
std::string strRule = rule;
90+
if(strRule == "init_to_dir_module")
91+
{
92+
moduleConfig->_exportRules.push_back(ExportRule::init_to_dir_module);
93+
}
94+
else if(strRule == "only_filename")
95+
{
96+
moduleConfig->_exportRules.push_back(ExportRule::only_filename);
97+
}
98+
}
99+
}
100+
}
101+
else
102+
{
103+
moduleConfig->_exportRules.push_back(ExportRule::init_to_dir_module);
104+
}
105+
82106
}
83107
catch (nlohmann::json::exception& e)
84108
{
@@ -92,3 +116,50 @@ ModuleConfig::ModuleConfig()
92116
import_function("require")
93117
{
94118
}
119+
120+
std::string ModuleConfig::GetModuleName(std::string_view path)
121+
{
122+
std::string moduleName(path);
123+
for (auto& c : moduleName)
124+
{
125+
if (c == '/' || c == '\\')
126+
{
127+
c = separator;
128+
}
129+
}
130+
131+
for (auto rule : _exportRules)
132+
{
133+
switch (rule)
134+
{
135+
case ExportRule::init_to_dir_module:
136+
{
137+
// .init 特殊规则
138+
if (moduleName.ends_with(".init"))
139+
{
140+
return moduleName.substr(0, moduleName.size() - 5);
141+
}
142+
break;
143+
}
144+
case ExportRule::only_filename:
145+
{
146+
auto dotPosition = moduleName.find_last_of(separator);
147+
if (dotPosition == std::string::npos)
148+
{
149+
return moduleName;
150+
}
151+
else
152+
{
153+
return moduleName.substr(dotPosition + 1);
154+
}
155+
break;
156+
}
157+
default:
158+
{
159+
break;
160+
}
161+
}
162+
}
163+
164+
return moduleName;
165+
}

CodeFormatServer/src/Service/Indexs/Module.cpp

Whitespace-only changes.

CodeFormatServer/src/Service/Indexs/ModuleIndex.cpp

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void ModuleIndex::SetDefaultModule(std::string workspaceUri)
4141
moduleConfig->module_root = workspacePath;
4242
moduleConfig->name = "root";
4343
moduleConfig->separator = '.';
44+
moduleConfig->_exportRules.push_back(ExportRule::init_to_dir_module);
4445
_moduleConfigMap[workspacePath] = moduleConfig;
4546
}
4647

@@ -58,19 +59,7 @@ void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
5859
auto relativePath = std::filesystem::relative(filePath, config->module_root);
5960
std::string modulePath = relativePath.replace_extension("").string();
6061

61-
for (auto& c : modulePath)
62-
{
63-
if (c == '/' || c == '\\')
64-
{
65-
c = config->separator;
66-
}
67-
}
68-
69-
// .init 特殊规则
70-
if (modulePath.ends_with(".init"))
71-
{
72-
modulePath = modulePath.substr(0, modulePath.size() - 5);
73-
}
62+
modulePath = config->GetModuleName(modulePath);
7463

7564
std::string matchName;
7665

@@ -92,7 +81,7 @@ void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
9281
// 一些代码喜欢路径带连字符
9382
for (auto& c : matchName)
9483
{
95-
if(c == '-')
84+
if (c == '-')
9685
{
9786
c = '_';
9887
}
@@ -135,7 +124,7 @@ void ModuleIndex::RebuildIndex(const std::vector<std::string>& files)
135124
BuildIndex(files);
136125
}
137126

138-
std::vector<std::shared_ptr<ModuleIndex::Module>> ModuleIndex::GetModules(std::string_view filePath)
127+
std::vector<std::shared_ptr<Module>> ModuleIndex::GetModules(std::string_view filePath)
139128
{
140129
std::vector<std::shared_ptr<Module>> result;
141130

CodeFormatServer/src/Service/ModuleService.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,37 +217,60 @@ std::vector<vscode::CompletionItem> ModuleService::GetModuleCompletions(std::sha
217217
auto luaModules = _moduleIndex.GetModules(path);
218218
auto insertRange = FindRequireRange(parser, config);
219219

220+
std::map<std::string, std::vector<std::shared_ptr<Module>>> completionMap;
220221

221222
for (auto& luaModule : luaModules)
222223
{
223-
auto& completion = result.emplace_back();
224+
if(completionMap.count(luaModule->MatchName) == 0)
225+
{
226+
completionMap.insert({ luaModule->MatchName, { luaModule} });
227+
}
228+
else
229+
{
230+
completionMap.at(luaModule->MatchName).push_back(luaModule);
231+
}
232+
}
224233

225-
std::string& name = luaModule->MatchName;
234+
for (auto pair : completionMap)
235+
{
236+
auto& completion = result.emplace_back();
237+
const std::string& name = pair.first;
226238
if (definedNames.count(name) > 0)
227239
{
228240
continue;
229241
}
230242

231243
completion.label = name;
232-
completion.detail = format("({})", luaModule->ModuleName);
233-
completion.documentation = format("import from {}", luaModule->FilePath);
234244
completion.insertText = name;
235245
completion.kind = vscode::CompletionItemKind::Module;
236-
// vscode::CompletionItemLabelDetails labelDetail;
237-
// labelDetail.detail = completion.detail;
238-
// labelDetail.description = completion.detail;
239-
// completion.labelDetails = labelDetail;
240-
241246
vscode::Command command;
242247
command.title = completion.detail;
243248
command.command = "emmylua.import.me";
244249
command.arguments.push_back(uri);
245250
command.arguments.push_back(insertRange.Serialize());
246-
auto object = nlohmann::json::object();
247-
object["moduleName"] = luaModule->ModuleName;
248-
object["path"] = luaModule->FilePath;
249-
object["name"] = name;
250-
command.arguments.push_back(object);
251+
252+
if (pair.second.size() == 1) {
253+
auto& luaModule = pair.second.front();
254+
completion.detail = format("({})", name);
255+
completion.documentation = format("import from {}", luaModule->FilePath);
256+
auto object = nlohmann::json::object();
257+
object["moduleName"] = luaModule->ModuleName;
258+
object["path"] = luaModule->FilePath;
259+
object["name"] = name;
260+
command.arguments.push_back(object);
261+
262+
}
263+
else
264+
{
265+
completion.detail = "import multi choice";
266+
for (auto& luaModule : pair.second) {
267+
auto object = nlohmann::json::object();
268+
object["moduleName"] = luaModule->ModuleName;
269+
object["path"] = luaModule->FilePath;
270+
object["name"] = name;
271+
command.arguments.push_back(object);
272+
}
273+
}
251274
completion.command = command;
252275
}
253276

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#pragma once
2+
3+
// 命名保持和lua.module.json一致
4+
enum class ExportRule
5+
{
6+
init_to_dir_module,
7+
only_filename,
8+
};

include/CodeFormatServer/Service/Indexs/Config/ModuleConfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <vector>
55
#include <memory>
6+
#include "ExportRule.h"
67

78
class ModuleConfig
89
{
@@ -22,6 +23,9 @@ class ModuleConfig
2223
static std::shared_ptr<ModuleConfig> LoadFromFile(std::string_view filePath);
2324

2425
ModuleConfig();
26+
27+
std::string GetModuleName(std::string_view path);
28+
2529
// 配置项采用 snake_case 命名
2630

2731
std::string name;
@@ -30,4 +34,5 @@ class ModuleConfig
3034
char separator;
3135
std::string import_function;
3236
std::map<std::string, SpecialModule> special_module;
37+
std::vector<ExportRule> _exportRules;
3338
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
class Module
4+
{
5+
public:
6+
Module(std::string_view moduleName = "", std::string_view filePath = "", std::string_view matchName = "")
7+
: ModuleName(moduleName),
8+
FilePath(filePath),
9+
MatchName(matchName)
10+
{
11+
}
12+
13+
std::string ModuleName;
14+
std::string FilePath;
15+
std::string MatchName;
16+
};

include/CodeFormatServer/Service/Indexs/ModuleIndex.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,11 @@
66
#include <set>
77
#include <memory>
88
#include "CodeFormatServer/Service/Indexs/Config/ModuleConfig.h"
9+
#include "Module.h"
910

1011
class ModuleIndex
1112
{
1213
public:
13-
class Module
14-
{
15-
public:
16-
Module(std::string_view moduleName = "", std::string_view filePath = "", std::string_view matchName = "")
17-
: ModuleName(moduleName),
18-
FilePath(filePath),
19-
MatchName(matchName)
20-
{
21-
}
22-
23-
std::string ModuleName;
24-
std::string FilePath;
25-
std::string MatchName;
26-
};
27-
2814
ModuleIndex();
2915

3016
void BuildModule(std::string workspaceUri, std::string_view moduleConfigPath);

0 commit comments

Comments
 (0)