Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/renaissance/syntax_tree/ast_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,11 @@ def __get_texts(self, nodes: Sequence[Rewritable], transform: Callable[[str], st
The text between the nodes is kept. A node with rewrites queued inside it is replaced by its rewritten text,
and transform, if given, is applied to the text of each node.
"""
if not nodes:
return ""
if len(nodes) == 1:
text = self.__get_text(nodes[0])
return transform(text) if transform else text
# TODO(FrancescoPezzella): an empty `$$x` raises IndexError here, e.g. `f($$args)` -> `g($$args)` on `f()`.
# https://github.com/TNO/Renaissance.Py/issues/329
start = self.cor_offset(nodes[0].offset)
end = self.cor_offset(nodes[-1].extended_end_offset)
rewriter = Rewriter(self.content[start:end])
Expand Down
3 changes: 2 additions & 1 deletion test/syntax_tree/test_ast_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,11 @@ def test_python_placeholder_replacement(self, source: str, expected: str) -> Non
"from a import X # café\nimport b\n",
),
("f(1, 2)\n", "f($$args)", "g($$args)", "g(1, 2)\n"),
("f()\n", "f($$args)", "g($$args)", "g()\n"),
],
)
def test_python_multi_node_placeholder(self, source: str, find: str, replacement: str, expected: str) -> None:
"""Verify a `$$` placeholder bound to several nodes expands to all of them, keeping their text and layout."""
"""Verify a `$$` placeholder expands zero or more nodes, preserving text and layout."""
root = PythonRstNode.load_from_text(source, "test.py")
pattern = PythonPatternFactory(PythonFactory(PythonRstNode)).create_statements(find)
rewriter = ASTRewriter(root)
Expand Down
Loading