Skip to content

Commit b821e4c

Browse files
committed
t[t[1]] 不再格式化为t[ t[1] ]
1 parent 4214ac4 commit b821e4c

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

CodeFormatServer/src/LanguageService.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ std::shared_ptr<vscode::Serializable> LanguageService::OnDidChangeWatchedFiles(
400400
return nullptr;
401401
}
402402

403-
//@depreacated
403+
//@deprecated
404404
std::shared_ptr<vscode::CompletionList> LanguageService::OnCompletion(std::shared_ptr<vscode::CompletionParams> param)
405405
{
406406
auto list = std::make_shared<vscode::CompletionList>();

CodeFormatServer/src/Service/CommandService.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ void CommandService::Import(std::shared_ptr<vscode::ExecuteCommandParams> param)
124124

125125
std::string requireString = Util::format("local {} = {}(\"{}\")\n", moduleDefineName, config->import_function,
126126
moduleName);
127-
auto parser = LanguageClient::GetInstance().GetFileParser(uri);
127+
auto parser = _owner->GetFileParser(uri);
128128
auto applyParams = std::make_shared<vscode::ApplyWorkspaceEditParams>();
129129
auto it = applyParams->edit.changes.emplace(uri, std::vector<vscode::TextEdit>());
130130
auto& change = it.first->second;
@@ -133,9 +133,9 @@ void CommandService::Import(std::shared_ptr<vscode::ExecuteCommandParams> param)
133133

134134
edit.newText = requireString;
135135

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

138-
LanguageClient::GetInstance().SendRequest("workspace/applyEdit", applyParams);
138+
_owner->SendRequest("workspace/applyEdit", applyParams);
139139
}
140140

141141
void CommandService::SpellCorrect(std::shared_ptr<vscode::ExecuteCommandParams> param)
@@ -157,7 +157,7 @@ void CommandService::SpellCorrect(std::shared_ptr<vscode::ExecuteCommandParams>
157157

158158
edit.range = range;
159159

160-
LanguageClient::GetInstance().SendRequest("workspace/applyEdit", applyParams);
160+
_owner->SendRequest("workspace/applyEdit", applyParams);
161161
}
162162

163163
// void CommandService::SpellAddDict(std::shared_ptr<vscode::ExecuteCommandParams> param)

CodeService/src/AstUtil.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,24 @@ std::shared_ptr<LuaAstNode> ast_util::FindLeftIndexExpression(std::shared_ptr<Lu
263263

264264
return leftIndex;
265265
}
266+
267+
bool ast_util::WillIndexExpressionFormatError(std::shared_ptr<LuaAstNode> expression)
268+
{
269+
auto text = expression->GetText();
270+
if(text.empty())
271+
{
272+
return false;
273+
}
274+
275+
if(text.front() == '[')
276+
{
277+
return text.length() > 2 && (text[1] == '[' || text[1] == '=');
278+
}
279+
280+
if(text.back() == ']')
281+
{
282+
return text.length() > 2 && (text[text.length() - 2] == ']' || text[text.length() - 2] == '=');
283+
}
284+
285+
return false;
286+
}

CodeService/src/LuaFormatter.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,9 +1622,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatTableField(std::shared_ptr<Lu
16221622
{
16231623
auto nextNode = NextNode(it, children);
16241624
if (nextNode && nextNode->GetType() == LuaAstNodeType::Expression
1625-
&& (StringUtil::StartWith(nextNode->GetText(), "[")
1626-
|| StringUtil::EndWith(nextNode->GetText(), "]"))
1627-
)
1625+
&& ast_util::WillIndexExpressionFormatError(nextNode))
16281626
{
16291627
isIndexExprLongString = true;
16301628
env->Add<TextElement>(child);
@@ -2227,8 +2225,7 @@ std::shared_ptr<FormatElement> LuaFormatter::FormatIndexExpression(std::shared_p
22272225
expressionAfterIndexOperator = true;
22282226
auto nextNode = NextNode(it, children);
22292227
if (nextNode && nextNode->GetType() == LuaAstNodeType::Expression
2230-
&& (StringUtil::StartWith(nextNode->GetText(), "[")
2231-
|| StringUtil::EndWith(nextNode->GetText(), "]")))
2228+
&& ast_util::WillIndexExpressionFormatError(nextNode))
22322229
{
22332230
isIndexExprLongString = true;
22342231
env->Add<TextElement>(child);

include/CodeService/AstUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ namespace ast_util
1616
bool WillCallArgHaveParentheses(std::shared_ptr<LuaAstNode> callArgList, CallArgParentheses callArgParentheses);
1717

1818
std::shared_ptr<LuaAstNode> FindLeftIndexExpression(std::shared_ptr<LuaAstNode> expression);
19+
20+
bool WillIndexExpressionFormatError(std::shared_ptr<LuaAstNode> expression);
1921
}

0 commit comments

Comments
 (0)