From d9b0575ed83165bbce23adcaecd26b296b5c844e Mon Sep 17 00:00:00 2001 From: Lavisha Gupta Date: Fri, 30 Jan 2026 00:16:33 +0530 Subject: [PATCH 1/5] [Compiler] format generated code by header and line break #3233 --- compiler/fory_compiler/generators/base.py | 118 ++++++++++++++++++++ compiler/fory_compiler/generators/cpp.py | 5 +- compiler/fory_compiler/generators/go.py | 5 +- compiler/fory_compiler/generators/java.py | 20 +++- compiler/fory_compiler/generators/python.py | 5 +- compiler/fory_compiler/generators/rust.py | 5 +- 6 files changed, 149 insertions(+), 9 deletions(-) diff --git a/compiler/fory_compiler/generators/base.py b/compiler/fory_compiler/generators/base.py index 9647af5b2d..0fab632a9d 100644 --- a/compiler/fory_compiler/generators/base.py +++ b/compiler/fory_compiler/generators/base.py @@ -206,3 +206,121 @@ def get_license_header(self, comment_prefix: str = "//") -> str: return "\n".join( f"{comment_prefix} {line}" if line else comment_prefix for line in lines ) + + def wrap_line( + self, + line: str, + max_width: int = 80, + indent: str = "", + continuation_indent: str = None, + ) -> List[str]: + """Wrap a long line into multiple lines with max_width characters. + + Args: + line: The line to wrap + max_width: Maximum width per line (default 80) + indent: The indentation to preserve for the first line + continuation_indent: Extra indentation for continuation lines. + If None, uses indent + 4 spaces. + + Returns: + List of wrapped lines + """ + if continuation_indent is None: + continuation_indent = indent + " " + + # If line is already short enough, return as is + if len(line) <= max_width: + return [line] + + # Don't wrap C++ preprocessor directives (macros) + stripped = line.lstrip() + if stripped.startswith("#"): + return [line] + + # Don't wrap comment lines (license headers, etc.) + if stripped.startswith("//") or stripped.startswith("/*") or stripped.startswith("*") or stripped.startswith("#"): + return [line] + + # Extract the leading indent + leading_spaces = line[: len(line) - len(line.lstrip())] + + # Get the content without indent + content = line[len(leading_spaces) :] + + # If it's still too short after considering indent, don't wrap + if len(leading_spaces) + len(content) <= max_width: + return [line] + + # Find good break points (prefer breaking at spaces, commas, operators) + result = [] + current = content + first_line = True + + while len(leading_spaces) + len(current) > max_width: + # Calculate available width + if first_line: + available = max_width - len(leading_spaces) + else: + available = max_width - len(continuation_indent) + + if available <= 0: + # Can't wrap reasonably, return original + return [line] + + # Find the best break point + break_point = -1 + + # Look for break points in order of preference + search_text = current[:available] + + # Try to break at common delimiters (working backwards) + for delimiter in [", ", " && ", " || ", " + ", " - ", " * ", " / ", " = ", " ", ","]: + idx = search_text.rfind(delimiter) + if idx > 0: + break_point = idx + len(delimiter) + break + + # If no good break point found, just break at max width + if break_point <= 0: + break_point = available + + # Add the line segment + if first_line: + result.append(leading_spaces + current[:break_point].rstrip()) + first_line = False + else: + result.append(continuation_indent + current[:break_point].rstrip()) + + # Continue with the rest + current = current[break_point:].lstrip() + + # Add the remaining content + if current: + if first_line: + result.append(leading_spaces + current) + else: + result.append(continuation_indent + current) + + return result if result else [line] + + def wrap_lines( + self, lines: List[str], max_width: int = 80, preserve_blank: bool = True + ) -> List[str]: + """Wrap multiple lines, handling each line's indentation. + + Args: + lines: List of lines to wrap + max_width: Maximum width per line + preserve_blank: If True, preserve blank lines as-is + + Returns: + List of wrapped lines + """ + result = [] + for line in lines: + if preserve_blank and not line.strip(): + result.append(line) + else: + result.extend(self.wrap_line(line, max_width)) + return result diff --git a/compiler/fory_compiler/generators/cpp.py b/compiler/fory_compiler/generators/cpp.py index a643d753ae..5647e997b9 100644 --- a/compiler/fory_compiler/generators/cpp.py +++ b/compiler/fory_compiler/generators/cpp.py @@ -370,9 +370,12 @@ def generate_header(self) -> GeneratedFile: lines.append(f"#endif // {guard_name}") lines.append("") + # Wrap long lines at 80 characters (careful with C++ macros) + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile( path=f"{self.get_header_name()}.h", - content="\n".join(lines), + content="\n".join(wrapped_lines), ) def collect_message_includes(self, message: Message, includes: Set[str]): diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 483cb56796..58eafc401a 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -449,9 +449,12 @@ def generate_file(self) -> GeneratedFile: lines.extend(self.generate_fory_helpers()) lines.append("") + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile( path=f"{self.get_file_name()}.go", - content="\n".join(lines), + content="\n".join(wrapped_lines), ) def collect_message_imports(self, message: Message, imports: Set[str]): diff --git a/compiler/fory_compiler/generators/java.py b/compiler/fory_compiler/generators/java.py index 47f2e7cf78..3b943efac7 100644 --- a/compiler/fory_compiler/generators/java.py +++ b/compiler/fory_compiler/generators/java.py @@ -366,7 +366,9 @@ def generate_enum_file(self, enum: Enum) -> GeneratedFile: else: path = f"{enum.name}.java" - return GeneratedFile(path=path, content="\n".join(lines)) + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile(path=path, content="\n".join(wrapped_lines)) def generate_union_file(self, union: Union) -> GeneratedFile: """Generate a Java union class file.""" @@ -397,7 +399,9 @@ def generate_union_file(self, union: Union) -> GeneratedFile: else: path = f"{union.name}.java" - return GeneratedFile(path=path, content="\n".join(lines)) + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile(path=path, content="\n".join(wrapped_lines)) def generate_message_file(self, message: Message) -> GeneratedFile: """Generate a Java class file for a message.""" @@ -486,7 +490,9 @@ def generate_message_file(self, message: Message) -> GeneratedFile: else: path = f"{message.name}.java" - return GeneratedFile(path=path, content="\n".join(lines)) + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile(path=path, content="\n".join(wrapped_lines)) def generate_outer_class_file(self, outer_classname: str) -> GeneratedFile: """Generate a single Java file with all types as inner classes of an outer class. @@ -559,7 +565,9 @@ def generate_outer_class_file(self, outer_classname: str) -> GeneratedFile: else: path = f"{outer_classname}.java" - return GeneratedFile(path=path, content="\n".join(lines)) + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile(path=path, content="\n".join(wrapped_lines)) def collect_message_imports(self, message: Message, imports: Set[str]): """Collect imports for a message and all its nested types recursively.""" @@ -1447,7 +1455,9 @@ def generate_registration_file( else: path = f"{class_name}.java" - return GeneratedFile(path=path, content="\n".join(lines)) + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile(path=path, content="\n".join(wrapped_lines)) def generate_enum_registration( self, lines: List[str], enum: Enum, parent_path: str diff --git a/compiler/fory_compiler/generators/python.py b/compiler/fory_compiler/generators/python.py index 8e70cae0f8..c18ac0b69a 100644 --- a/compiler/fory_compiler/generators/python.py +++ b/compiler/fory_compiler/generators/python.py @@ -323,9 +323,12 @@ def generate_module(self) -> GeneratedFile: lines.extend(self.generate_fory_helpers()) lines.append("") + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile( path=f"{self.get_module_name()}.py", - content="\n".join(lines), + content="\n".join(wrapped_lines), ) def collect_message_imports(self, message: Message, imports: Set[str]): diff --git a/compiler/fory_compiler/generators/rust.py b/compiler/fory_compiler/generators/rust.py index 27daefe9de..861b7fbe84 100644 --- a/compiler/fory_compiler/generators/rust.py +++ b/compiler/fory_compiler/generators/rust.py @@ -252,9 +252,12 @@ def generate_module(self) -> GeneratedFile: lines.extend(self.generate_fory_helpers()) lines.append("") + # Wrap long lines at 80 characters + wrapped_lines = self.wrap_lines(lines, max_width=80) + return GeneratedFile( path=f"{self.get_module_name()}.rs", - content="\n".join(lines), + content="\n".join(wrapped_lines), ) def collect_message_uses(self, message: Message, uses: Set[str]): From 0a7f6584dba5ac351f54c2bfd965d1b8bb95664c Mon Sep 17 00:00:00 2001 From: Lavisha Gupta Date: Fri, 30 Jan 2026 01:08:12 +0530 Subject: [PATCH 2/5] test case fix --- compiler/fory_compiler/generators/go.py | 52 ++++++++++++++++++--- compiler/fory_compiler/generators/java.py | 6 ++- compiler/fory_compiler/generators/python.py | 51 ++++++++++++++++++++ 3 files changed, 100 insertions(+), 9 deletions(-) diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 58eafc401a..5fb1942db7 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -199,6 +199,45 @@ def message_has_unions(self, message: Message) -> bool: PrimitiveKind.ANY: "any", } + def wrap_line( + self, + line: str, + max_width: int = 80, + indent: str = "", + continuation_indent: str = None, + ) -> List[str]: + """Override base wrap_line to handle Go-specific syntax. + + Go has specific constructs that should not be wrapped: + 1. Function signatures - must not split between params and return type + 2. Struct field tags - backtick strings must not be split + 3. Single-line function bodies - must not split "{ return ... }" + """ + if continuation_indent is None: + continuation_indent = indent + " " + + # If line is already short enough, return as is + if len(line) <= max_width: + return [line] + + stripped = line.lstrip() + + # Don't wrap comments + if stripped.startswith("//") or stripped.startswith("/*") or stripped.startswith("*"): + return [line] + + # Don't wrap lines with backticks (struct tags) + if "`" in line: + return [line] + + # Don't wrap function definitions (including one-liners like "func Foo() Type { return ... }") + # Detect by checking if line starts with "func " and contains "{" + if stripped.startswith("func ") and "{" in line: + return [line] + + # For all other cases, use base class wrapping + return super().wrap_line(line, max_width, indent, continuation_indent) + def generate(self) -> List[GeneratedFile]: """Generate Go files for the schema.""" files = [] @@ -586,15 +625,13 @@ def generate_union( lines.append(f"\tcase {case_type}{case_name}:") lines.append(f"\t\tv, ok := u.value.({case_type_name})") lines.append("\t\tif !ok {") - lines.append( - f'\t\t\treturn fmt.Errorf("corrupted {type_name}: case={case_name} but invalid value")' - ) + lines.append(f'\t\t\treturn fmt.Errorf("corrupted {type_name}: " +') + lines.append(f'\t\t\t\t"case={case_name} but invalid value")') lines.append("\t\t}") if case_type_name.startswith("*"): lines.append("\t\tif v == nil {") - lines.append( - f'\t\t\treturn fmt.Errorf("corrupted {type_name}: case={case_name} but nil value")' - ) + lines.append(f'\t\t\treturn fmt.Errorf("corrupted {type_name}: " +') + lines.append(f'\t\t\t\t"case={case_name} but nil value")') lines.append("\t\t}") lines.append(f"\t\tif visitor.{case_name} != nil {{") lines.append(f"\t\t\treturn visitor.{case_name}(v)") @@ -864,7 +901,8 @@ def generate_field( if tags: tag_str = ",".join(tags) - lines.append(f'{field_name} {go_type} `fory:"{tag_str}"`') + # Concatenate parts to ensure single line in output + lines.append(f"{field_name} {go_type} `fory:\"{tag_str}\"`") else: lines.append(f"{field_name} {go_type}") diff --git a/compiler/fory_compiler/generators/java.py b/compiler/fory_compiler/generators/java.py index 3b943efac7..32b5d17ea7 100644 --- a/compiler/fory_compiler/generators/java.py +++ b/compiler/fory_compiler/generators/java.py @@ -658,7 +658,8 @@ def generate_union_class( lines.append(f"{ind} return {type_id_expr};") lines.append(f"{ind} default:") lines.append( - f'{ind} throw new IllegalStateException("Unknown {union.name} case id: " + caseId);' + f'{ind} throw new IllegalStateException("Unknown " + ' + f'"{union.name} case id: " + caseId);' ) lines.append(f"{ind} }}") lines.append(f"{ind} }}") @@ -694,7 +695,8 @@ def generate_union_class( lines.append(f"{ind} return {case_enum}.{case_enum_name};") lines.append(f"{ind} default:") lines.append( - f'{ind} throw new IllegalStateException("Unknown {union.name} case id: " + index);' + f'{ind} throw new IllegalStateException("Unknown " + ' + f'"{union.name} case id: " + index);' ) lines.append(f"{ind} }}") lines.append(f"{ind} }}") diff --git a/compiler/fory_compiler/generators/python.py b/compiler/fory_compiler/generators/python.py index c18ac0b69a..81b033149b 100644 --- a/compiler/fory_compiler/generators/python.py +++ b/compiler/fory_compiler/generators/python.py @@ -141,6 +141,57 @@ class PythonGenerator(BaseGenerator): PrimitiveKind.ANY: "None", } + def wrap_line( + self, + line: str, + max_width: int = 80, + indent: str = "", + continuation_indent: str = None, + ) -> List[str]: + """Override base wrap_line to handle Python-specific syntax. + + Python has specific constructs that should not be wrapped: + 1. Assignment statements - must not split between variable and value + 2. Conditional statements with logical operators (and, or, not) + 3. Raise statements with string literals + """ + if continuation_indent is None: + continuation_indent = indent + " " + + # If line is already short enough, return as is + if len(line) <= max_width: + return [line] + + stripped = line.lstrip() + + # Don't wrap comments + if stripped.startswith("#"): + return [line] + + # Don't wrap raise statements (they often have long string literals) + if stripped.startswith("raise "): + return [line] + + # Don't wrap assignment statements (lines containing " = " at the statement level) + # This prevents splitting like: _threadsafe_fory = pyfory.ThreadSafeFory(...) + # Only check for simple assignments (not comparisons or keyword args) + if " = " in line and not line.strip().startswith("if ") and not line.strip().startswith("elif ") and not line.strip().startswith("while "): + # Check if this looks like a simple assignment (has = but not == or <= or >=) + # and the = comes before any opening parentheses + eq_pos = line.find(" = ") + paren_pos = line.find("(") + if eq_pos > 0 and (paren_pos < 0 or eq_pos < paren_pos): + # This is likely an assignment statement, don't wrap it + return [line] + + # Don't wrap conditional statements (if/elif/while) with logical operators + # This prevents splitting like: if condition and not\n isinstance(...) + if stripped.startswith(("if ", "elif ", "while ")) and (" and " in line or " or " in line): + return [line] + + # For all other cases, use base class wrapping + return super().wrap_line(line, max_width, indent, continuation_indent) + def safe_name(self, name: str) -> str: """Return a Python-safe identifier.""" if keyword.iskeyword(name): From c2d6654196e0f4c31d7b3375240bc052224e58d7 Mon Sep 17 00:00:00 2001 From: Lavisha Gupta Date: Fri, 30 Jan 2026 01:20:54 +0530 Subject: [PATCH 3/5] fix(compiler): prevent line wrapping in language-specific syntax constructs - Add wrap_line/wrap_lines methods to BaseGenerator for smart line breaking - Override wrap_line in Go generator to preserve function signatures and struct tags - Override wrap_line in Python generator to preserve assignments and conditionals - Modify Java generator to use string concatenation for long error messages - Fix ruff formatting issues (multi-line if statements, trailing whitespace) This fixes syntax errors in generated IDL code across Java, Go, and Python. --- compiler/fory_compiler/generators/base.py | 20 +++++++++++++-- compiler/fory_compiler/generators/go.py | 22 ++++++++++------- compiler/fory_compiler/generators/python.py | 27 +++++++++++++-------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/compiler/fory_compiler/generators/base.py b/compiler/fory_compiler/generators/base.py index 0fab632a9d..93c3aa3621 100644 --- a/compiler/fory_compiler/generators/base.py +++ b/compiler/fory_compiler/generators/base.py @@ -239,7 +239,12 @@ def wrap_line( return [line] # Don't wrap comment lines (license headers, etc.) - if stripped.startswith("//") or stripped.startswith("/*") or stripped.startswith("*") or stripped.startswith("#"): + if ( + stripped.startswith("//") + or stripped.startswith("/*") + or stripped.startswith("*") + or stripped.startswith("#") + ): return [line] # Extract the leading indent @@ -275,7 +280,18 @@ def wrap_line( search_text = current[:available] # Try to break at common delimiters (working backwards) - for delimiter in [", ", " && ", " || ", " + ", " - ", " * ", " / ", " = ", " ", ","]: + for delimiter in [ + ", ", + " && ", + " || ", + " + ", + " - ", + " * ", + " / ", + " = ", + " ", + ",", + ]: idx = search_text.rfind(delimiter) if idx > 0: break_point = idx + len(delimiter) diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 5fb1942db7..07edad518d 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -207,7 +207,7 @@ def wrap_line( continuation_indent: str = None, ) -> List[str]: """Override base wrap_line to handle Go-specific syntax. - + Go has specific constructs that should not be wrapped: 1. Function signatures - must not split between params and return type 2. Struct field tags - backtick strings must not be split @@ -215,26 +215,30 @@ def wrap_line( """ if continuation_indent is None: continuation_indent = indent + " " - + # If line is already short enough, return as is if len(line) <= max_width: return [line] - + stripped = line.lstrip() - + # Don't wrap comments - if stripped.startswith("//") or stripped.startswith("/*") or stripped.startswith("*"): + if ( + stripped.startswith("//") + or stripped.startswith("/*") + or stripped.startswith("*") + ): return [line] - + # Don't wrap lines with backticks (struct tags) if "`" in line: return [line] - + # Don't wrap function definitions (including one-liners like "func Foo() Type { return ... }") # Detect by checking if line starts with "func " and contains "{" if stripped.startswith("func ") and "{" in line: return [line] - + # For all other cases, use base class wrapping return super().wrap_line(line, max_width, indent, continuation_indent) @@ -902,7 +906,7 @@ def generate_field( if tags: tag_str = ",".join(tags) # Concatenate parts to ensure single line in output - lines.append(f"{field_name} {go_type} `fory:\"{tag_str}\"`") + lines.append(f'{field_name} {go_type} `fory:"{tag_str}"`') else: lines.append(f"{field_name} {go_type}") diff --git a/compiler/fory_compiler/generators/python.py b/compiler/fory_compiler/generators/python.py index 81b033149b..8040dfa8fb 100644 --- a/compiler/fory_compiler/generators/python.py +++ b/compiler/fory_compiler/generators/python.py @@ -149,7 +149,7 @@ def wrap_line( continuation_indent: str = None, ) -> List[str]: """Override base wrap_line to handle Python-specific syntax. - + Python has specific constructs that should not be wrapped: 1. Assignment statements - must not split between variable and value 2. Conditional statements with logical operators (and, or, not) @@ -157,25 +157,30 @@ def wrap_line( """ if continuation_indent is None: continuation_indent = indent + " " - + # If line is already short enough, return as is if len(line) <= max_width: return [line] - + stripped = line.lstrip() - + # Don't wrap comments if stripped.startswith("#"): return [line] - + # Don't wrap raise statements (they often have long string literals) if stripped.startswith("raise "): return [line] - + # Don't wrap assignment statements (lines containing " = " at the statement level) # This prevents splitting like: _threadsafe_fory = pyfory.ThreadSafeFory(...) # Only check for simple assignments (not comparisons or keyword args) - if " = " in line and not line.strip().startswith("if ") and not line.strip().startswith("elif ") and not line.strip().startswith("while "): + if ( + " = " in line + and not line.strip().startswith("if ") + and not line.strip().startswith("elif ") + and not line.strip().startswith("while ") + ): # Check if this looks like a simple assignment (has = but not == or <= or >=) # and the = comes before any opening parentheses eq_pos = line.find(" = ") @@ -183,12 +188,14 @@ def wrap_line( if eq_pos > 0 and (paren_pos < 0 or eq_pos < paren_pos): # This is likely an assignment statement, don't wrap it return [line] - + # Don't wrap conditional statements (if/elif/while) with logical operators # This prevents splitting like: if condition and not\n isinstance(...) - if stripped.startswith(("if ", "elif ", "while ")) and (" and " in line or " or " in line): + if stripped.startswith(("if ", "elif ", "while ")) and ( + " and " in line or " or " in line + ): return [line] - + # For all other cases, use base class wrapping return super().wrap_line(line, max_width, indent, continuation_indent) From 45883f014ed3f010414e55b576bc166e475356f6 Mon Sep 17 00:00:00 2001 From: Lavisha Gupta Date: Sat, 31 Jan 2026 12:45:39 +0530 Subject: [PATCH 4/5] Removed wrapping function and used line break --- compiler/fory_compiler/generators/base.py | 48 +++++++++++++++++++++ compiler/fory_compiler/generators/cpp.py | 5 +-- compiler/fory_compiler/generators/go.py | 12 +++--- compiler/fory_compiler/generators/java.py | 43 +++++++++--------- compiler/fory_compiler/generators/python.py | 14 +++--- compiler/fory_compiler/generators/rust.py | 5 +-- 6 files changed, 88 insertions(+), 39 deletions(-) diff --git a/compiler/fory_compiler/generators/base.py b/compiler/fory_compiler/generators/base.py index 93c3aa3621..dcc10a15a9 100644 --- a/compiler/fory_compiler/generators/base.py +++ b/compiler/fory_compiler/generators/base.py @@ -340,3 +340,51 @@ def wrap_lines( else: result.extend(self.wrap_line(line, max_width)) return result + + def format_long_line( + self, + prefix: str, + content: str, + suffix: str = "", + max_width: int = 80, + continuation_indent: str = " ", + ) -> List[str]: + """Format a long line with semantic understanding. + + This is for semantic line wrapping where we know the structure. + Use this when generating code to avoid breaking in wrong places. + + Args: + prefix: The prefix part (e.g., "public class ") + content: The main content that might be long + suffix: The suffix part (e.g., " {") + max_width: Maximum line width + continuation_indent: Indentation for continuation lines + + Returns: + List of formatted lines + + Example: + format_long_line("public class ", "VeryLongClassName", " {") + => ["public class VeryLongClassName {"] # if fits + => ["public class", " VeryLongClassName {"] # if too long + """ + full_line = prefix + content + suffix + if len(full_line) <= max_width: + return [full_line] + + # If content alone with suffix fits on next line + if len(continuation_indent + content + suffix) <= max_width: + return [prefix.rstrip(), continuation_indent + content + suffix] + + # Content is too long even on its own line + # Put each part on separate lines + lines = [prefix.rstrip()] + if len(continuation_indent + content) <= max_width: + lines.append(continuation_indent + content) + else: + # Content itself needs wrapping + lines.append(continuation_indent + content) + if suffix: + lines.append(suffix) + return lines diff --git a/compiler/fory_compiler/generators/cpp.py b/compiler/fory_compiler/generators/cpp.py index 5647e997b9..a643d753ae 100644 --- a/compiler/fory_compiler/generators/cpp.py +++ b/compiler/fory_compiler/generators/cpp.py @@ -370,12 +370,9 @@ def generate_header(self) -> GeneratedFile: lines.append(f"#endif // {guard_name}") lines.append("") - # Wrap long lines at 80 characters (careful with C++ macros) - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile( path=f"{self.get_header_name()}.h", - content="\n".join(wrapped_lines), + content="\n".join(lines), ) def collect_message_includes(self, message: Message, includes: Set[str]): diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 07edad518d..763736d5c2 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -492,12 +492,9 @@ def generate_file(self) -> GeneratedFile: lines.extend(self.generate_fory_helpers()) lines.append("") - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile( path=f"{self.get_file_name()}.go", - content="\n".join(wrapped_lines), + content="\n".join(lines), ) def collect_message_imports(self, message: Message, imports: Set[str]): @@ -567,7 +564,12 @@ def generate_union( lines.append(")") lines.append("") - lines.append(f"type {type_name} struct {{") + # Type declaration with semantic wrapping for Go + type_decl = f"type {type_name} struct {{" + if len(type_decl) > 80: + lines.extend(self.format_long_line("type ", type_name, " struct {", continuation_indent="\t")) + else: + lines.append(type_decl) lines.append(f"\tcase_ {case_type}") lines.append("\tvalue any") lines.append("}") diff --git a/compiler/fory_compiler/generators/java.py b/compiler/fory_compiler/generators/java.py index 32b5d17ea7..fc328fb54d 100644 --- a/compiler/fory_compiler/generators/java.py +++ b/compiler/fory_compiler/generators/java.py @@ -366,9 +366,7 @@ def generate_enum_file(self, enum: Enum) -> GeneratedFile: else: path = f"{enum.name}.java" - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile(path=path, content="\n".join(wrapped_lines)) + return GeneratedFile(path=path, content="\n".join(lines)) def generate_union_file(self, union: Union) -> GeneratedFile: """Generate a Java union class file.""" @@ -399,9 +397,7 @@ def generate_union_file(self, union: Union) -> GeneratedFile: else: path = f"{union.name}.java" - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile(path=path, content="\n".join(wrapped_lines)) + return GeneratedFile(path=path, content="\n".join(lines)) def generate_message_file(self, message: Message) -> GeneratedFile: """Generate a Java class file for a message.""" @@ -427,8 +423,9 @@ def generate_message_file(self, message: Message) -> GeneratedFile: lines.append(f"import {imp};") lines.append("") - # Class declaration - lines.append(f"public class {message.name} {{") + # Class declaration with semantic line wrapping + class_lines = self.format_long_line("public class ", message.name, " {") + lines.extend(class_lines) # Generate nested enums as static inner classes for nested_enum in message.nested_enums: @@ -490,9 +487,7 @@ def generate_message_file(self, message: Message) -> GeneratedFile: else: path = f"{message.name}.java" - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile(path=path, content="\n".join(wrapped_lines)) + return GeneratedFile(path=path, content="\n".join(lines)) def generate_outer_class_file(self, outer_classname: str) -> GeneratedFile: """Generate a single Java file with all types as inner classes of an outer class. @@ -565,9 +560,7 @@ def generate_outer_class_file(self, outer_classname: str) -> GeneratedFile: else: path = f"{outer_classname}.java" - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile(path=path, content="\n".join(wrapped_lines)) + return GeneratedFile(path=path, content="\n".join(lines)) def collect_message_imports(self, message: Message, imports: Set[str]): """Collect imports for a message and all its nested types recursively.""" @@ -1017,14 +1010,24 @@ def generate_getter_setter(self, field: Field) -> List[str]: field_name = self.to_camel_case(field.name) pascal_name = self.to_pascal_case(field.name) - # Getter - lines.append(f"public {java_type} get{pascal_name}() {{") + # Getter with semantic line wrapping + getter_sig = f"public {java_type} get{pascal_name}()" + if len(getter_sig) > 80: + getter_lines = self.format_long_line("public ", f"{java_type} get{pascal_name}()", " {") + else: + getter_lines = [f"{getter_sig} {{"] + lines.extend(getter_lines) lines.append(f" return {field_name};") lines.append("}") lines.append("") - # Setter - lines.append(f"public void set{pascal_name}({java_type} {field_name}) {{") + # Setter with semantic line wrapping + setter_sig = f"public void set{pascal_name}({java_type} {field_name})" + if len(setter_sig) > 80: + setter_lines = self.format_long_line("public void ", f"set{pascal_name}({java_type} {field_name})", " {") + else: + setter_lines = [f"{setter_sig} {{"] + lines.extend(setter_lines) lines.append(f" this.{field_name} = {field_name};") lines.append("}") lines.append("") @@ -1457,9 +1460,7 @@ def generate_registration_file( else: path = f"{class_name}.java" - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile(path=path, content="\n".join(wrapped_lines)) + return GeneratedFile(path=path, content="\n".join(lines)) def generate_enum_registration( self, lines: List[str], enum: Enum, parent_path: str diff --git a/compiler/fory_compiler/generators/python.py b/compiler/fory_compiler/generators/python.py index 8040dfa8fb..7d96d714f2 100644 --- a/compiler/fory_compiler/generators/python.py +++ b/compiler/fory_compiler/generators/python.py @@ -381,12 +381,9 @@ def generate_module(self) -> GeneratedFile: lines.extend(self.generate_fory_helpers()) lines.append("") - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile( path=f"{self.get_module_name()}.py", - content="\n".join(wrapped_lines), + content="\n".join(lines), ) def collect_message_imports(self, message: Message, imports: Set[str]): @@ -676,7 +673,14 @@ def generate_field( else: field_default = f"{default_expr}{trailing_comment}" - lines.append(f"{field_name}: {python_type} = {field_default}") + # Build field line with semantic wrapping for Python + field_line = f"{field_name}: {python_type} = {field_default}" + if len(field_line) > 80: + # Use Python line continuation with proper indentation + lines.append(f"{field_name}: {python_type} = \\") + lines.append(f" {field_default}") + else: + lines.append(field_line) return lines diff --git a/compiler/fory_compiler/generators/rust.py b/compiler/fory_compiler/generators/rust.py index 861b7fbe84..27daefe9de 100644 --- a/compiler/fory_compiler/generators/rust.py +++ b/compiler/fory_compiler/generators/rust.py @@ -252,12 +252,9 @@ def generate_module(self) -> GeneratedFile: lines.extend(self.generate_fory_helpers()) lines.append("") - # Wrap long lines at 80 characters - wrapped_lines = self.wrap_lines(lines, max_width=80) - return GeneratedFile( path=f"{self.get_module_name()}.rs", - content="\n".join(wrapped_lines), + content="\n".join(lines), ) def collect_message_uses(self, message: Message, uses: Set[str]): From 88288cb6d93a431c184044f68dd786a2b3755d7e Mon Sep 17 00:00:00 2001 From: Lavisha Gupta Date: Sat, 31 Jan 2026 12:50:14 +0530 Subject: [PATCH 5/5] code style fixing --- compiler/fory_compiler/generators/go.py | 6 +++++- compiler/fory_compiler/generators/java.py | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/fory_compiler/generators/go.py b/compiler/fory_compiler/generators/go.py index 763736d5c2..8810511f63 100644 --- a/compiler/fory_compiler/generators/go.py +++ b/compiler/fory_compiler/generators/go.py @@ -567,7 +567,11 @@ def generate_union( # Type declaration with semantic wrapping for Go type_decl = f"type {type_name} struct {{" if len(type_decl) > 80: - lines.extend(self.format_long_line("type ", type_name, " struct {", continuation_indent="\t")) + lines.extend( + self.format_long_line( + "type ", type_name, " struct {", continuation_indent="\t" + ) + ) else: lines.append(type_decl) lines.append(f"\tcase_ {case_type}") diff --git a/compiler/fory_compiler/generators/java.py b/compiler/fory_compiler/generators/java.py index fc328fb54d..41e7f6208f 100644 --- a/compiler/fory_compiler/generators/java.py +++ b/compiler/fory_compiler/generators/java.py @@ -1013,7 +1013,9 @@ def generate_getter_setter(self, field: Field) -> List[str]: # Getter with semantic line wrapping getter_sig = f"public {java_type} get{pascal_name}()" if len(getter_sig) > 80: - getter_lines = self.format_long_line("public ", f"{java_type} get{pascal_name}()", " {") + getter_lines = self.format_long_line( + "public ", f"{java_type} get{pascal_name}()", " {" + ) else: getter_lines = [f"{getter_sig} {{"] lines.extend(getter_lines) @@ -1024,7 +1026,9 @@ def generate_getter_setter(self, field: Field) -> List[str]: # Setter with semantic line wrapping setter_sig = f"public void set{pascal_name}({java_type} {field_name})" if len(setter_sig) > 80: - setter_lines = self.format_long_line("public void ", f"set{pascal_name}({java_type} {field_name})", " {") + setter_lines = self.format_long_line( + "public void ", f"set{pascal_name}({java_type} {field_name})", " {" + ) else: setter_lines = [f"{setter_sig} {{"] lines.extend(setter_lines)