Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 5 additions & 2 deletions CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ class LuaStyle {
// [Indent]

/*
* if语句的条件可以无延续行缩进
*/
* if语句的条件可以无延续行缩进
*/
bool never_indent_before_if_condition = false;

bool never_indent_comment_on_if_branch = false;
Expand Down Expand Up @@ -168,4 +168,7 @@ class LuaStyle {
bool leading_comma_style = false;

EndStmtWithSemicolon end_statement_with_semicolon = EndStmtWithSemicolon::Keep;

// [preference] Remove leading zero in floats between 0 and 1 (e.g., 0.5 -> .5)
bool remove_leading_zero_in_float = false;
};
3 changes: 3 additions & 0 deletions CodeFormatCore/src/Config/LuaStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,7 @@ void LuaStyle::Parse(std::map<std::string, std::string, std::less<>> &configMap)
end_statement_with_semicolon = EndStmtWithSemicolon::Never;
}
}

// Custom: remove_leading_zero_in_float
BOOL_OPTION(remove_leading_zero_in_float)
}
24 changes: 24 additions & 0 deletions CodeFormatCore/src/Format/FormatBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ void FormatBuilder::WriteSyntaxNode(LuaSyntaxNode &syntaxNode, const LuaSyntaxTr
WriteText(text);
break;
}
case TK_NUMBER: {
// Remove leading zero in floats between 0 and 1 if enabled
const auto &style = _state.GetStyle();
if (style.remove_leading_zero_in_float) {
// Handle positive and negative numbers
if (text.size() > 1 && text[0] == '0' && text[1] == '.') {
// 0.xxx -> .xxx
_state.CurrentWidth() += utf8::Utf8nLen(text.data() + 1, text.size() - 1);
_formattedText.append(text.substr(1));
break;
} else if (text.size() > 2 && text[0] == '-' && text[1] == '0' && text[2] == '.') {
// -0.xxx -> -.xxx
_state.CurrentWidth() += utf8::Utf8nLen(text.data(), 1); // '-'
_state.CurrentWidth() += utf8::Utf8nLen(text.data() + 2, text.size() - 2);
_formattedText.push_back('-');
_formattedText.append(text.substr(2));
break;
}
}
// Default behavior
_state.CurrentWidth() += syntaxNode.GetUtf8Length(t);
_formattedText.append(text);
break;
}
default: {
_state.CurrentWidth() += syntaxNode.GetUtf8Length(t);
_formattedText.append(text);
Expand Down
Loading