From 9ed05ef421bdc19f2f931544a24298e080a5ed30 Mon Sep 17 00:00:00 2001 From: Li Jin Date: Thu, 19 Feb 2026 19:03:07 +0800 Subject: Preserve blank lines between table comments with reserve-comments --- src/yuescript/yue_ast.h | 4 ++-- src/yuescript/yue_compiler.cpp | 8 ++++++++ src/yuescript/yue_parser.cpp | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/yuescript/yue_ast.h b/src/yuescript/yue_ast.h index d44815e..3236b5a 100644 --- a/src/yuescript/yue_ast.h +++ b/src/yuescript/yue_ast.h @@ -730,13 +730,13 @@ AST_NODE(TableBlockIndent) ast_sel_list values; + YueComment_t, EmptyLine_t> values; AST_MEMBER(TableBlockIndent, &sep, &values) AST_END(TableBlockIndent) AST_NODE(TableBlock) ast_ptr sep; - ast_sel_list values; + ast_sel_list values; AST_MEMBER(TableBlock, &sep, &values) AST_END(TableBlock) diff --git a/src/yuescript/yue_compiler.cpp b/src/yuescript/yue_compiler.cpp index f67e921..38e9fbb 100644 --- a/src/yuescript/yue_compiler.cpp +++ b/src/yuescript/yue_compiler.cpp @@ -734,6 +734,7 @@ private: return isLocal(variableToString(pair->name)); } case id(): + case id(): return true; case id(): { auto pair = static_cast(item); @@ -8219,6 +8220,8 @@ private: auto metatable = x->new_ptr(); ast_sel metatableItem; ast_node* lastValueNode = values.back(); + int lastValueLine = x->m_begin.m_line; + bool prevWasEmptyLine = false; if (!_config.reserveComment) { for (auto it = values.rbegin(); it != values.rend(); ++it) { auto node = *it; @@ -8280,6 +8283,9 @@ private: switch (item->get_id()) { case id(): transformExp(static_cast(item), temp, ExpUsage::Closure); break; case id(): { + if (_config.reserveComment && !prevWasEmptyLine && value->m_begin.m_line > lastValueLine + 1) { + temp.emplace_back(Empty); + } auto comment = static_cast(item); temp.emplace_back(comment->to_string(&_config)); skipComma = true; @@ -8352,6 +8358,8 @@ private: temp.back() = indent() + (value == lastValueNode ? temp.back() : temp.back() + ',') + nl(value); } } + lastValueLine = value->m_end.m_line; + prevWasEmptyLine = ast_is(value); } if (metatable->pairs.empty() && !metatableItem) { out.push_back('{' + nl(x) + join(temp)); diff --git a/src/yuescript/yue_parser.cpp b/src/yuescript/yue_parser.cpp index 21f19a3..ffe3ad2 100644 --- a/src/yuescript/yue_parser.cpp +++ b/src/yuescript/yue_parser.cpp @@ -909,11 +909,11 @@ YueParser::YueParser() { white >> '}' ); - table_block_inner = Seperator >> key_value_line >> *((+(plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)); + table_block_inner = Seperator >> key_value_line >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)); TableBlock = ((+(plain_space >> line_break)) | (+space_break >> not_(expr("--")))) >> advance_match >> ensure(table_block_inner, pop_indent); TableBlockIndent = ('*' | '-' >> space_one) >> Seperator >> disable_arg_table_block_rule( space >> key_value_list >> -(space >> ',') >> - -((plain_space >> line_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *((+(plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)), pop_indent)) | + -((plain_space >> line_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(((plain_space >> line_break) >> key_value_line) | (+space_break >> not_(expr("--")) >> key_value_line)), pop_indent)) | (+space_break >> advance_match >> space >> ensure(key_value_list >> -(space >> ',') >> *(+space_break >> key_value_line), pop_indent)))); ClassMemberList = Seperator >> key_value >> *(space >> ',' >> space >> key_value); @@ -1019,6 +1019,7 @@ YueParser::YueParser() { MetaNormalPair; key_value_list = key_value >> *(space >> ',' >> space >> key_value); key_value_line = + (EmptyLine >> YueComment) | YueComment | check_indent_match >> space >> ( key_value_list >> -(space >> ',') | -- cgit v1.2.3-55-g6feb