Skip to content

Commit e51ecbc

Browse files
committed
兼容一些写法
1 parent 19ba80c commit e51ecbc

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

CodeFormatServer/src/LanguageService.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,6 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
426426
std::string requireString = format("local {} = {}(\"{}\")\n", moduleDefineName, config->import_function,
427427
moduleName);
428428
auto parser = LanguageClient::GetInstance().GetFileParser(uri);
429-
430429
auto applyParams = std::make_shared<vscode::ApplyWorkspaceEditParams>();
431430
auto it = applyParams->edit.changes.emplace(uri, std::vector<vscode::TextEdit>());
432431
auto& change = it.first->second;
@@ -435,7 +434,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnExecuteCommand(
435434

436435
edit.newText = requireString;
437436

438-
edit.range = LanguageClient::GetInstance().GetService<ModuleService>()->FindRequireRange(parser);
437+
edit.range = LanguageClient::GetInstance().GetService<ModuleService>()->FindRequireRange(parser, config);
439438

440439
LanguageClient::GetInstance().SendRequest("workspace/applyEdit", applyParams);
441440
}

CodeFormatServer/src/Service/CodeFormatService.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ CodeFormatService::CodeFormatService(std::shared_ptr<LanguageClient> owner)
88
}
99

1010
std::vector<vscode::Diagnostic> CodeFormatService::Diagnose(std::string_view filePath,
11-
std::shared_ptr<LuaParser> parser,
12-
std::shared_ptr<LuaCodeStyleOptions> options)
11+
std::shared_ptr<LuaParser> parser,
12+
std::shared_ptr<LuaCodeStyleOptions> options)
1313
{
1414
LuaFormatter formatter(parser, *options);
1515
formatter.BuildFormattedElement();
@@ -62,11 +62,11 @@ bool CodeFormatService::IsDiagnosticRange(std::string_view filePath, vscode::Ran
6262
}
6363

6464
auto& diagnosticSet = it->second;
65-
if(range.start == range.end)
65+
if (range.start == range.end)
6666
{
67-
for(auto& validRange: diagnosticSet)
67+
for (auto& validRange : diagnosticSet)
6868
{
69-
if(validRange.start <= range.start && validRange.end >= range.end)
69+
if (validRange.start <= range.start && validRange.end >= range.end)
7070
{
7171
return true;
7272
}

CodeFormatServer/src/Service/Indexs/ModuleIndex.cpp

Lines changed: 12 additions & 3 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+
_moduleConfigMap[workspacePath] = moduleConfig;
4445
}
4546

4647
void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
@@ -65,13 +66,12 @@ void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
6566
}
6667
}
6768

69+
// .init 特殊规则
6870
if (modulePath.ends_with(".init"))
6971
{
7072
modulePath = modulePath.substr(0, modulePath.size() - 5);
7173
}
7274

73-
74-
7575
std::string matchName;
7676

7777
auto dotPosition = modulePath.find_last_of(config->separator);
@@ -89,9 +89,18 @@ void ModuleIndex::BuildIndex(const std::vector<std::string>& files)
8989
matchName = config->special_module.at(modulePath).Name;
9090
}
9191

92+
// 一些代码喜欢路径带连字符
93+
for (auto& c : matchName)
94+
{
95+
if(c == '-')
96+
{
97+
c = '_';
98+
}
99+
}
100+
92101
if (_moduleIndex.count(config->name) == 0)
93102
{
94-
_moduleIndex.insert({ config->name, std::vector<std::shared_ptr<Module>>() });
103+
_moduleIndex.insert({config->name, std::vector<std::shared_ptr<Module>>()});
95104
}
96105

97106
_moduleIndex.at(config->name).push_back(std::make_shared<Module>(modulePath, filePath, matchName));

CodeFormatServer/src/Service/ModuleService.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ std::vector<ModuleService::LuaModule> ModuleService::GetImportModules(std::strin
117117
return result;
118118
}
119119

120-
vscode::Range ModuleService::FindRequireRange(std::shared_ptr<LuaParser> parser)
120+
vscode::Range ModuleService::FindRequireRange(std::shared_ptr<LuaParser> parser, std::shared_ptr<ModuleConfig> config)
121121
{
122122
vscode::Range range;
123123
auto chunkAst = parser->GetAst();
@@ -156,7 +156,20 @@ vscode::Range ModuleService::FindRequireRange(std::shared_ptr<LuaParser> parser)
156156
auto methodNameNode = callExpression->FindFirstOf(LuaAstNodeType::PrimaryExpression);
157157
if (methodNameNode)
158158
{
159-
if (methodNameNode->GetText() == "require")
159+
if (methodNameNode->GetText() == config->import_function)
160+
{
161+
break;
162+
}
163+
}
164+
}
165+
// 一些代码会有 local require = require的规则, 勉为其难支持一下
166+
else
167+
{
168+
auto primary = expression->FindFirstOf(LuaAstNodeType::PrimaryExpression);
169+
if (primary)
170+
{
171+
auto nameIdentify = primary->FindFirstOf(LuaAstNodeType::NameIdentify);
172+
if (nameIdentify && nameIdentify->GetText() == config->import_function)
160173
{
161174
break;
162175
}
@@ -200,17 +213,17 @@ std::vector<vscode::CompletionItem> ModuleService::GetModuleCompletions(std::sha
200213
finder.Analysis(parser);
201214

202215
auto definedNames = finder.GetDefinedName(expression);
203-
216+
auto config = _moduleIndex.GetConfig(path);
204217
auto luaModules = _moduleIndex.GetModules(path);
205-
auto insertRange = FindRequireRange(parser);
218+
auto insertRange = FindRequireRange(parser, config);
206219

207220

208221
for (auto& luaModule : luaModules)
209222
{
210223
auto& completion = result.emplace_back();
211224

212225
std::string& name = luaModule->MatchName;
213-
if(definedNames.count(name) > 0)
226+
if (definedNames.count(name) > 0)
214227
{
215228
continue;
216229
}

include/CodeFormatServer/Service/ModuleService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ModuleService : public Service
3232

3333
std::vector<LuaModule> GetImportModules(std::string_view filePath, vscode::Range range);
3434

35-
vscode::Range FindRequireRange(std::shared_ptr<LuaParser> parser);
35+
vscode::Range FindRequireRange(std::shared_ptr<LuaParser> parser, std::shared_ptr<ModuleConfig> config);
3636

3737
ModuleIndex& GetIndex();
3838

0 commit comments

Comments
 (0)