Skip to content

Commit

Permalink
Merge pull request #177 from AlWoSp/awa/module-local-names
Browse files Browse the repository at this point in the history
Add namestyle for local variables in the outermost scope `module_local_name_style`
  • Loading branch information
CppCXY authored Jun 11, 2024
2 parents 864af38 + bc976bc commit e4028f6
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 2 deletions.
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 moduleLocalNameStyle = std::find_if(name_styles.begin(), name_styles.end(), [&](const std::pair<std::vector<NameStyleRule> &, std::string> &pair) {
return pair.second == "module_local_name_style";
});
auto localNameStyle = std::find_if(name_styles.begin(), name_styles.end(), [&](const std::pair<std::vector<NameStyleRule> &, std::string> &pair) {
return pair.second == "local_name_style";
});

// overwrite the namestyle of module_local_name_style with the namestyle of local_name_style
if (moduleLocalNameStyle != name_styles.end() && localNameStyle != name_styles.end()) {
moduleLocalNameStyle->first = localNameStyle->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

0 comments on commit e4028f6

Please sign in to comment.