diff --git a/scripts/cxx-api/parser/scope/base_scope_kind.py b/scripts/cxx-api/parser/scope/base_scope_kind.py index 4c3a0943116..02aa9720687 100644 --- a/scripts/cxx-api/parser/scope/base_scope_kind.py +++ b/scripts/cxx-api/parser/scope/base_scope_kind.py @@ -8,7 +8,7 @@ from __future__ import annotations from abc import ABC, abstractmethod from typing import TYPE_CHECKING, TypeVar -from natsort import natsort_keygen +from natsort import natsort_keygen, natsorted if TYPE_CHECKING: from .scope import Scope @@ -29,6 +29,18 @@ class ScopeKind(ABC): """Called when the scope is closed. Override to perform cleanup.""" pass + def _format_scope_body(self, scope: Scope, member_suffix: str = "") -> str: + """Format the members list inside a scope's braces.""" + stringified_members = [ + member.to_string(2) + member_suffix for member in scope.get_members() + ] + stringified_members = natsorted(stringified_members) + result = "{" + if stringified_members: + result += "\n" + "\n".join(stringified_members) + result += "\n}" + return result + def print_scope(self, scope: Scope) -> None: print(self.to_string(scope)) diff --git a/scripts/cxx-api/parser/scope/category_scope_kind.py b/scripts/cxx-api/parser/scope/category_scope_kind.py index ac62b7e9d4d..f497525dc93 100644 --- a/scripts/cxx-api/parser/scope/category_scope_kind.py +++ b/scripts/cxx-api/parser/scope/category_scope_kind.py @@ -7,8 +7,6 @@ from __future__ import annotations from typing import TYPE_CHECKING -from natsort import natsorted - from .base_scope_kind import ScopeKind if TYPE_CHECKING: @@ -22,16 +20,5 @@ class CategoryScopeKind(ScopeKind): self.category_name: str = category_name def to_string(self, scope: Scope) -> str: - result = f"{self.name} {self.class_name}({self.category_name}) {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result + header = f"{self.name} {self.class_name}({self.category_name}) " + return header + self._format_scope_body(scope) diff --git a/scripts/cxx-api/parser/scope/enum_scope_kind.py b/scripts/cxx-api/parser/scope/enum_scope_kind.py index 1e1addb0214..9cab7a96d5e 100644 --- a/scripts/cxx-api/parser/scope/enum_scope_kind.py +++ b/scripts/cxx-api/parser/scope/enum_scope_kind.py @@ -7,8 +7,6 @@ from __future__ import annotations from typing import TYPE_CHECKING -from natsort import natsorted - from .base_scope_kind import ScopeKind if TYPE_CHECKING: @@ -21,22 +19,6 @@ class EnumScopeKind(ScopeKind): self.type: str | None = None def to_string(self, scope: Scope) -> str: - result = "" inheritance_string = f" : {self.type}" if self.type else "" - - result += ( - "\n" + f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - ) - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2) + ",") - - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result + header = f"\n{self.name} {scope.get_qualified_name()}{inheritance_string} " + return header + self._format_scope_body(scope, member_suffix=",") diff --git a/scripts/cxx-api/parser/scope/extendable.py b/scripts/cxx-api/parser/scope/extendable.py index 4051f79c372..523b55357e7 100644 --- a/scripts/cxx-api/parser/scope/extendable.py +++ b/scripts/cxx-api/parser/scope/extendable.py @@ -26,6 +26,13 @@ class Extendable: else: self.base_classes.append(base) + def qualify_base_classes(self, scope) -> None: + """Qualify base class names and their template arguments.""" + from ..utils import qualify_type_str + + for base in self.base_classes: + base.name = qualify_type_str(base.name, scope) + def get_inheritance_string(self) -> str: bases = [] for base in self.base_classes: diff --git a/scripts/cxx-api/parser/scope/interface_scope_kind.py b/scripts/cxx-api/parser/scope/interface_scope_kind.py index bba18019479..00da13f33b3 100644 --- a/scripts/cxx-api/parser/scope/interface_scope_kind.py +++ b/scripts/cxx-api/parser/scope/interface_scope_kind.py @@ -7,9 +7,6 @@ from __future__ import annotations from typing import TYPE_CHECKING -from natsort import natsorted - -from ..utils import qualify_type_str from .base_scope_kind import ScopeKind from .extendable import Extendable @@ -23,24 +20,9 @@ class InterfaceScopeKind(ScopeKind, Extendable): Extendable.__init__(self) def close(self, scope: Scope) -> None: - """Qualify base class names and their template arguments.""" - for base in self.base_classes: - base.name = qualify_type_str(base.name, scope) + self.qualify_base_classes(scope) def to_string(self, scope: Scope) -> str: - result = "" - - inheritance_string = self.get_inheritance_string() - result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result + inheritance = self.get_inheritance_string() + header = f"{self.name} {scope.get_qualified_name()}{inheritance} " + return header + self._format_scope_body(scope) diff --git a/scripts/cxx-api/parser/scope/protocol_scope_kind.py b/scripts/cxx-api/parser/scope/protocol_scope_kind.py index fcbd77624c6..46a24984fbc 100644 --- a/scripts/cxx-api/parser/scope/protocol_scope_kind.py +++ b/scripts/cxx-api/parser/scope/protocol_scope_kind.py @@ -7,9 +7,6 @@ from __future__ import annotations from typing import TYPE_CHECKING -from natsort import natsorted - -from ..utils import qualify_type_str from .base_scope_kind import ScopeKind from .extendable import Extendable @@ -23,24 +20,9 @@ class ProtocolScopeKind(ScopeKind, Extendable): Extendable.__init__(self) def close(self, scope: Scope) -> None: - """Qualify base class names and their template arguments.""" - for base in self.base_classes: - base.name = qualify_type_str(base.name, scope) + self.qualify_base_classes(scope) def to_string(self, scope: Scope) -> str: - result = "" - - inheritance_string = self.get_inheritance_string() - result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - - return result + inheritance = self.get_inheritance_string() + header = f"{self.name} {scope.get_qualified_name()}{inheritance} " + return header + self._format_scope_body(scope) diff --git a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py index 0a06d7e00d1..3050cf4414b 100644 --- a/scripts/cxx-api/parser/scope/struct_like_scope_kind.py +++ b/scripts/cxx-api/parser/scope/struct_like_scope_kind.py @@ -8,10 +8,7 @@ from __future__ import annotations from enum import Enum from typing import TYPE_CHECKING -from natsort import natsorted - from ..template import Template, TemplateList -from ..utils import qualify_type_str from .base_scope_kind import ScopeKind from .extendable import Extendable @@ -45,9 +42,7 @@ class StructLikeScopeKind(ScopeKind, Extendable): self.template_list.add(template) def close(self, scope: Scope) -> None: - """Qualify base class names and their template arguments.""" - for base in self.base_classes: - base.name = qualify_type_str(base.name, scope) + self.qualify_base_classes(scope) def to_string(self, scope: Scope) -> str: result = "" @@ -55,17 +50,7 @@ class StructLikeScopeKind(ScopeKind, Extendable): if self.template_list is not None: result += "\n" + self.template_list.to_string() + "\n" - inheritance_string = self.get_inheritance_string() - result += f"{self.name} {scope.get_qualified_name()}{inheritance_string} {{" - - stringified_members = [] - for member in scope.get_members(): - stringified_members.append(member.to_string(2)) - stringified_members = natsorted(stringified_members) - result += ("\n" if len(stringified_members) > 0 else "") + "\n".join( - stringified_members - ) - - result += "\n}" - + inheritance = self.get_inheritance_string() + result += f"{self.name} {scope.get_qualified_name()}{inheritance} " + result += self._format_scope_body(scope) return result