Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add namestyle for local variables in the outermost scope module_local_name_style #177

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ class LuaDiagnosticStyle {
std::vector<NameStyleRule> const_variable_name_style = {
NameStyleRule(NameStyleType::SnakeCase),
NameStyleRule(NameStyleType::UpperSnakeCase)};

std::vector<NameStyleRule> module_local_name_style = {NameStyleRule(NameStyleType::SnakeCase)};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ enum class NameDefineType {
ImportModuleName,
ModuleDefineName,
TableFieldDefineName,
ConstVariableName
ConstVariableName,
ModuleLocalVariableName
};

struct NameStyleInfo {
Expand Down
17 changes: 16 additions & 1 deletion CodeFormatCore/src/Config/LuaDiagnosticStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
{module_name_style, "module_name_style" },
{require_module_name_style, "require_module_name_style" },
{class_name_style, "class_name_style" },
{const_variable_name_style, "const_variable_name_style" }
{const_variable_name_style, "const_variable_name_style" },
{module_local_name_style, "module_local_name_style" }
};
for (auto &pair: name_styles) {
if (auto n = root.GetValue(pair.second); !n.IsNull()) {
Expand All @@ -119,4 +120,18 @@ void LuaDiagnosticStyle::ParseTree(InfoTree &tree) {
}
}
}
// module_local_name_style should fallback on local_name_style if not defined
if (root.GetValue("module_local_name_style").IsNull() && !root.GetValue("local_name_style").IsNull()) {
auto module_local_name_style_pair = std::find_if(name_styles.begin(), name_styles.end(), [&](const auto &pair) {
AndreaWalchshoferSCCH marked this conversation as resolved.
Show resolved Hide resolved
return pair.second == "module_local_name_style";
});
auto local_name_style_pair = std::find_if(name_styles.begin(), name_styles.end(), [&](const auto &pair) {
AndreaWalchshoferSCCH marked this conversation as resolved.
Show resolved Hide resolved
return pair.second == "local_name_style";
});

// overwrite the namestyle of module_local_name_style with the namestyle of local_name_style
if (module_local_name_style_pair != name_styles.end() && local_name_style_pair != name_styles.end()) {
module_local_name_style_pair->first = local_name_style_pair->first;
}
}
}
15 changes: 15 additions & 0 deletions CodeFormatCore/src/Diagnostic/NameStyle/NameStyleChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ void NameStyleChecker::CheckInBody(LuaSyntaxNode &n, const LuaSyntaxTree &t) {
}

if (!matchConstRule) {
// check for non-special, non-const variables that are in the outermost scope
if (_scopeStack.size() == 1) {
PushStyleCheck(NameDefineType::ModuleLocalVariableName, name);
break;
}

PushStyleCheck(NameDefineType::LocalVariableName, name);
}
}
Expand Down Expand Up @@ -459,6 +465,15 @@ void NameStyleChecker::Diagnostic(DiagnosticBuilder &d, const LuaSyntaxTree &t)
}
break;
}
case NameDefineType::ModuleLocalVariableName: {
if (!matcher.Match(n, t, state.GetDiagnosticStyle().module_local_name_style)) {
d.PushDiagnostic(DiagnosticType::NameStyle,
n.GetTextRange(t),
MakeDiagnosticInfo("ModuleLocalVariableName", n, t,
state.GetDiagnosticStyle().module_local_name_style));
}
break;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions docs/name_style.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* require_module_name_style
* class_name_style
* const_variable_name_style
* module_local_name_style (没有额外作用域的变量,如果没有设置默认值,则将其默认值设置为 `local_name_style`)

每一个可配置项的格式都是相同的, 每个可配置项可配置的值支持如下格式:
* 单字符串 例如:
Expand Down
1 change: 1 addition & 0 deletions docs/name_style_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The configurable items are:
* require_module_name_style
* class_name_style
* const_variable_name_style
* module_local_name_style (variables without additional scope, setting defaults to the one of `local_name_style` if not set)

The format of each configurable item is the same, and the configurable value of each configurable item supports the following formats:
* Single string Example:
Expand Down
Loading