diff --git a/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h b/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h index 1e4f4848..d6c8088e 100644 --- a/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h +++ b/CodeFormatCore/include/CodeFormatCore/Config/LuaStyle.h @@ -117,8 +117,8 @@ class LuaStyle { // [Indent] /* - * if语句的条件可以无延续行缩进 - */ + * if语句的条件可以无延续行缩进 + */ bool never_indent_before_if_condition = false; bool never_indent_comment_on_if_branch = false; @@ -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; }; diff --git a/CodeFormatCore/src/Config/LuaStyle.cpp b/CodeFormatCore/src/Config/LuaStyle.cpp index f37ea87f..f6a71267 100644 --- a/CodeFormatCore/src/Config/LuaStyle.cpp +++ b/CodeFormatCore/src/Config/LuaStyle.cpp @@ -409,4 +409,7 @@ void LuaStyle::Parse(std::map> &configMap) end_statement_with_semicolon = EndStmtWithSemicolon::Never; } } + + // Custom: remove_leading_zero_in_float + BOOL_OPTION(remove_leading_zero_in_float) } diff --git a/CodeFormatCore/src/Format/FormatBuilder.cpp b/CodeFormatCore/src/Format/FormatBuilder.cpp index 92f83312..38f4a5c1 100644 --- a/CodeFormatCore/src/Format/FormatBuilder.cpp +++ b/CodeFormatCore/src/Format/FormatBuilder.cpp @@ -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);