File size: 3,376 Bytes
04f192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.
#
# SPDX-License-Identifier: Apache-2.0

from abc import ABC
from typing import Literal, override

from copilotj.plugin._base import Verbosity
from copilotj.plugin.awt._base import ComponentBase
from copilotj.plugin.awt.component import (
    ButtonNode,
    CanvasNode,
    CheckboxNode,
    ChoiceNode,
    LabelNode,
    ListNode,
    ScrollbarNode,
    TextAreaNode,
    TextFieldNode,
    UnknownNode,
)

__all__ = ["TypedComponentNode", "ContainerNodeBase", "ContainerNode"]

# NOTE: put typed node here to avoid cycle imports
type TypedComponentNode = "ButtonNode| CanvasNode | CheckboxNode| ChoiceNode| ContainerNode | LabelNode| ListNode | ScrollbarNode | TextAreaNode | TextFieldNode | UnknownNode"


class ContainerNodeBase[T: str](ComponentBase[T], ABC):
    is_container: Literal[True]  # Mark this class as a container, used for pydantic validation
    children: list[TypedComponentNode] | None

    def _describe_one_line(self) -> str:
        """Provides a single-line description for the container itself."""
        return self.type

    @override
    def _describe(self, *, level: int, verbosity: Verbosity) -> list[str]:
        """Describe the container and its children in a tree-like structure.

        Generates a multi-line string list representing the container and its children in a tree-like structure.
        """
        # Start with the current container's own one-line description.
        # The 'name' attribute is inherited from ComponentBase and should be set
        # when the node is created if it's available from the Java side.
        # Example: description = f"{self._describe_one_line()} (name='{getattr(self, 'name', 'N/A')}')"
        description = self._describe_one_line()
        lines = [description]
        if verbosity < Verbosity.NORMAL:
            return lines

        lines.extend(self._describe_children(self.children, level=level, verbosity=verbosity))
        return lines

    @classmethod
    def _describe_children(
        cls, children: list[TypedComponentNode] | None, *, level: int, verbosity: Verbosity
    ) -> list[str]:
        lines = []
        num_children = len(children) if children else 0
        for i, child in enumerate(children or []):
            is_last_child = i == num_children - 1

            # Determine the prefix for connecting to the child
            connector_prefix = "└── " if is_last_child else "├── "

            # Recursively get the description lines for the child
            # Pass down the incremented level and the same verbosity
            child_description_lines = child._describe(level=level + 1, verbosity=verbosity)

            if child_description_lines:
                # Add the first line of the child's description with the connector
                lines.append(connector_prefix + child_description_lines[0])

                # Determine the prefix for subsequent lines of this child's description
                # This ensures multi-line descriptions from children are indented correctly
                indentation_prefix = "    " if is_last_child else "│   "
                for child_line in child_description_lines[1:]:
                    lines.append(indentation_prefix + child_line)

        return lines


class ContainerNode(ContainerNodeBase[str]):
    pass