Coverage for src/renaissance/syntax_tree/ast_rewriter.py: 92%

302 statements  

« prev     ^ index     » next       coverage.py v7.17.0, created at 2026-11-16 16:04 +0000

1# Copyright (C) 2024-2026 TNO 

2# 

3# Licensed under the terms specified in the LICENSE file in the root directory 

4# of this repository, also available at: 

5# https://github.com/TNO/Renaissance.Py/blob/main/LICENSE 

6 

7"""AI: Rewriter that translates AST-level replace/remove/insert actions into byte-level edits.""" 

8 

9import re 

10import sys 

11from collections.abc import Callable, Mapping, Sequence 

12from dataclasses import dataclass, field 

13from enum import Enum 

14from typing import Protocol, Self, runtime_checkable 

15 

16from more_itertools import flatten 

17 

18from renaissance.common import Rewriter 

19from renaissance.syntax_tree.match_finder import PatternMatch 

20from renaissance.utils.ast_utils import is_ancestor_of 

21from renaissance.utils.text_utils import TextUtils 

22 

23 

24@runtime_checkable 

25class Rewritable(Protocol): 

26 """AI: Structural protocol describing the offset/text shape required for byte-level rewriting.""" 

27 

28 # Read-only members keep the protocol covariant, so implementations may expose them as properties. 

29 @property 

30 def offset(self) -> int: 

31 """Character offset at which this node starts.""" 

32 ... 

33 

34 @property 

35 def end_offset(self) -> int: 

36 """Character offset just past this node's own text.""" 

37 ... 

38 

39 @property 

40 def extended_end_offset(self) -> int: 

41 """Character offset just past this node including trailing trivia.""" 

42 ... 

43 

44 @property 

45 def filename(self) -> str: 

46 """Name of the source file this node was parsed from.""" 

47 ... 

48 

49 @property 

50 def parent(self) -> Self | None: 

51 """Enclosing node, or None at the root.""" 

52 ... 

53 

54 @property 

55 def text(self) -> str: 

56 """Source text of this node.""" 

57 ... 

58 

59 

60class _RewriteActionType(Enum): 

61 REPLACE = 1 

62 INSERT_BEFORE = 2 

63 INSERT_AFTER = 3 

64 REMOVE = 4 # TODO: Why needed? Why isn't a REMOVE Action Type just a REPLACE Action Type (with an empty string)? 

65 

66 

67class ASTRewriter: 

68 """AI: Rewriter that translates AST-level replace/remove/insert actions into byte-level edits.""" 

69 

70 def __init__( 

71 self, 

72 node, 

73 # TODO: see docs/developer/architecture/position-consistency.md - the encoding must come from the adapter, not the host. 

74 encoding: str = sys.getfilesystemencoding(), 

75 correct_indent: bool = True, 

76 ) -> None: 

77 """AI: Accumulate and apply text rewrites (replace/insert/remove) to an AST node's source.""" 

78 self.__rewrites = _RewriteActions(node, encoding, correct_indent=correct_indent) 

79 self.__filename = node.filename 

80 

81 def get_filename(self) -> str: 

82 """AI: Return the filename of the AST node this rewriter operates on.""" 

83 return self.__filename 

84 

85 def replace( 

86 self, 

87 new_content: str, 

88 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

89 include_whitespace: bool = True, 

90 include_comments: bool = True, 

91 transforms: Mapping[str, Callable[[str], str]] | None = None, 

92 ) -> None: 

93 """Queue a rewrite replacing target's source text with new_content. 

94 

95 Args: 

96 new_content: The replacement text; for a `PatternMatch` target, its placeholders are expanded. 

97 target: The nodes or match to replace. 

98 include_whitespace: Whether the whitespace around target is replaced as well. 

99 include_comments: Whether the comments around target are replaced as well. 

100 transforms: Per placeholder, a function applied to the text of each node bound to it before expansion. 

101 

102 """ 

103 self.__rewrites.add_rewrite( 

104 _RewriteAction(_RewriteActionType.REPLACE, target, new_content, include_whitespace, include_comments, transforms or {}) 

105 ) 

106 

107 def remove( 

108 self, 

109 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

110 include_whitespace: bool = True, 

111 include_comments: bool = True, 

112 ) -> None: 

113 """AI: Queue a rewrite removing target's source text.""" 

114 self.__rewrites.add(_RewriteActionType.REMOVE, target, "", include_whitespace, include_comments) 

115 

116 def insert_before( 

117 self, 

118 new_content: str, 

119 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

120 include_whitespace: bool = True, 

121 include_comments: bool = True, 

122 ) -> None: 

123 """AI: Queue a rewrite inserting new_content immediately before target's source text.""" 

124 self.__rewrites.add( 

125 _RewriteActionType.INSERT_BEFORE, 

126 target, 

127 new_content, 

128 include_whitespace, 

129 include_comments, 

130 ) 

131 

132 def insert_after( 

133 self, 

134 new_content: str, 

135 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

136 include_whitespace: bool = True, 

137 include_comments: bool = True, 

138 ) -> None: 

139 """AI: Queue a rewrite inserting new_content immediately after target's source text.""" 

140 self.__rewrites.add( 

141 _RewriteActionType.INSERT_AFTER, 

142 target, 

143 new_content, 

144 include_whitespace, 

145 include_comments, 

146 ) 

147 

148 def apply_to_string(self) -> str: 

149 """AI: Return the rewritten source as a string, applying all queued rewrites.""" 

150 return self.__rewrites.apply_to_string() 

151 

152 def apply(self) -> bytes: 

153 """AI: Return the rewritten source as bytes, applying all queued rewrites (or the original content if none are queued).""" 

154 if len(self.__rewrites.rewrites) == 0: 

155 return self.__rewrites.content 

156 return self.__rewrites.apply() 

157 

158 def has_changed(self) -> bool: 

159 """AI: Return whether any rewrites have been queued.""" 

160 return len(self.__rewrites.rewrites) > 0 

161 

162 

163@dataclass 

164class _RewriteAction: 

165 """Data container for a rewrite action to be applied later on to the AST.""" 

166 

167 action: _RewriteActionType 

168 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch] 

169 replacement: str 

170 include_whitespace: bool 

171 include_comments: bool 

172 transforms: Mapping[str, Callable[[str], str]] = field(default_factory=dict[str, Callable[[str], str]]) 

173 nodes: Sequence[Rewritable] = field(init=False) 

174 

175 def __post_init__(self) -> None: 

176 """Derive the nodes the action applies to from its target.""" 

177 self.nodes = self._get_nodes(self.target) 

178 

179 @staticmethod 

180 def _get_nodes( 

181 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

182 ) -> Sequence[Rewritable]: 

183 if isinstance(target, Rewritable): 

184 return [target] 

185 if isinstance(target, PatternMatch): 

186 return target.nodes 

187 assert isinstance(target, Sequence), "type of target violates its type requirements " + type(target).__name__ 

188 if len(target) > 0: 

189 if isinstance(target[0], Rewritable): 

190 return [n for n in target if isinstance(n, Rewritable)] 

191 last = target[-1] 

192 assert isinstance(last, PatternMatch), "type within Sequence violates its requirements " + type(last).__name__ 

193 return last.nodes 

194 # TODO: is this correct? Can the other matches indeed be ignored? 

195 return [] 

196 

197 

198class _RewriteActions: 

199 """Data container for a list of rewrite actions to be applied later on to the AST.""" 

200 

201 def __init__( 

202 self, 

203 node: Rewritable, 

204 encoding: str, 

205 correct_indent: bool, 

206 rewrites: list[_RewriteAction] | None = None, 

207 ) -> None: 

208 self.rewrites: list[_RewriteAction] = rewrites or [] 

209 self.node = node 

210 self.encoding = encoding 

211 # self.content = self.node.root.binary_file_content()[self.node.offset : self.node.extended_end_offset] 

212 # TODO: see docs/developer/architecture/position-consistency.md - re-encodes node text instead of using the root buffer. 

213 self.content = node.text.encode(sys.getfilesystemencoding()) 

214 self.correct_indent = correct_indent 

215 

216 def add( 

217 self, 

218 action: _RewriteActionType, 

219 target: Rewritable | Sequence[Rewritable] | PatternMatch | Sequence[PatternMatch], 

220 replacement: str, 

221 include_whitespace: bool, 

222 include_comments: bool, 

223 ) -> None: 

224 rewrite = _RewriteAction(action, target, replacement, include_whitespace, include_comments) 

225 self.add_rewrite(rewrite) 

226 

227 def add_rewrite(self, rewrite: _RewriteAction) -> None: 

228 self.rewrites.append(rewrite) 

229 

230 def apply(self) -> bytes: 

231 self.__check_for_conflicting_rewrites() 

232 rewriter = Rewriter(self.content[:]) 

233 

234 for rewrite in self.rewrites: 

235 # skip nested rewrites as they are handled recursively by the parent rewrite 

236 # except for if the rewrite node is the root node 

237 if any(self.__is_ancestor_in_nodes(n) for n in rewrite.nodes if n != self.node): 

238 continue 

239 new_content, nodelist = self.__prepare_replacement_content(rewrite.replacement, rewrite.target, rewrite.transforms) 

240 if rewrite.action == _RewriteActionType.REPLACE: 

241 self.__replace( 

242 rewriter, 

243 new_content, 

244 nodelist, 

245 rewrite.include_whitespace, 

246 rewrite.include_comments, 

247 ) 

248 elif rewrite.action == _RewriteActionType.INSERT_BEFORE: 

249 self.__insert( 

250 rewriter, 

251 new_content, 

252 True, 

253 nodelist, 

254 rewrite.include_whitespace, 

255 rewrite.include_comments, 

256 ) 

257 elif rewrite.action == _RewriteActionType.INSERT_AFTER: 

258 self.__insert( 

259 rewriter, 

260 new_content, 

261 False, 

262 nodelist, 

263 rewrite.include_whitespace, 

264 rewrite.include_comments, 

265 ) 

266 elif rewrite.action == _RewriteActionType.REMOVE: 

267 self.__remove( 

268 rewriter, 

269 nodelist, 

270 rewrite.include_whitespace, 

271 rewrite.include_comments, 

272 ) 

273 return rewriter.apply() 

274 

275 def apply_to_string(self) -> str: 

276 return self.apply().decode(self.encoding) 

277 

278 def __check_for_conflicting_rewrites(self) -> None: 

279 """Raise if two queued replace/remove rewrites target overlapping source ranges. 

280 

281 Insert rewrites are ignored: they only add text before or after a node without 

282 overwriting it, so they can be combined with any other rewrite. A node nested inside 

283 another rewritten node is not treated as overlapping. 

284 

285 TODO: this only turns silent corruption into a clear error - it doesn't merge 

286 conflicting rewrites into a correct result. Recipes must still avoid queuing more than 

287 one rewrite per node/range before a commit. 

288 """ 

289 # INSERT_BEFORE/AFTER can work with each other and with REPLACE/REMOVE 

290 replacing = [r for r in self.rewrites if r.action in (_RewriteActionType.REPLACE, _RewriteActionType.REMOVE)] 

291 all_nodes = [(rewrite, node) for rewrite in replacing for node in rewrite.nodes if node != self.node] 

292 for i, (rewrite_a, node_a) in enumerate(all_nodes): 

293 for rewrite_b, node_b in all_nodes[i + 1 :]: 

294 if rewrite_a is rewrite_b or is_ancestor_of(node_b, node_a) or is_ancestor_of(node_a, node_b): 

295 continue 

296 if not (node_a.end_offset < node_b.offset or node_a.offset > node_b.end_offset): 

297 raise ValueError( 

298 f"Conflicting rewrites queued for overlapping source ranges " 

299 f"({node_a.offset}-{node_a.end_offset} and {node_b.offset}-{node_b.end_offset}) " 

300 "in the same file - applying both would corrupt the output." 

301 ) 

302 

303 @staticmethod 

304 def __is_nested(node: Rewritable, maybe_ancestor: Rewritable) -> bool: 

305 """Return True if maybe_ancestor is an ancestor of node, walking .parent (works for every Rewritable).""" 

306 parent = node.parent 

307 while parent: 

308 if parent is maybe_ancestor: 

309 return True 

310 parent = parent.parent 

311 return False 

312 

313 def __is_ancestor_in_nodes(self, node: Rewritable) -> bool: 

314 """Check if the given node is a descendant of any nodes in the rewrite list. 

315 

316 Args: 

317 node (Rewritable): The node to check. 

318 

319 Returns: 

320 bool: True if the node is a descendant of any nodes in the rewrite list, False otherwise. 

321 

322 """ 

323 rewrite_nodes = list(flatten(rewrite.nodes for rewrite in self.rewrites)) 

324 

325 # need to test 

326 # 1 

327 # | node | 

328 # |rew| 

329 # 2 

330 # | rew | 

331 # |node| 

332 def no_conflict(node1, rew) -> bool: 

333 return not (node1.end_offset < rew.offset or node1.offset > rew.end_offset) 

334 

335 result = any(no_conflict(node, rew) for rew in rewrite_nodes) 

336 

337 # TODO(FrancescoPezzella): an outer rewrite should suppress the rewrites nested inside it, but both are applied. 

338 # Dropping `and False` is not enough: no_conflict also matches each node with its own rewrite, so result is almost 

339 # always True and apply() would skip nearly every rewrite. A fix has to leave out the node's own rewrite and use a 

340 # true ancestor check such as __is_nested. 

341 # https://github.com/TNO/Renaissance.Py/issues/286 

342 return result and False # TODO: Why `and False` 

343 

344 def __replace( 

345 self, 

346 rewriter: Rewriter, 

347 new_content: str, 

348 nodes: Sequence[Rewritable], 

349 include_whitespace: bool, 

350 include_comments: bool, 

351 ) -> None: 

352 """Replace the content of the given node(s) with new content. 

353 

354 Args: 

355 rewriter (Rewriter): The rewriter used to apply the content replacement. 

356 new_content (str): The new content to insert in the specified range. 

357 nodes (Sequence[Rewritable]): The nodes whose content is to be replaced. 

358 include_whitespace (bool): Whether to include surrounding whitespace when determining the replacement range. 

359 include_comments (bool): Whether to include surrounding comments when determining the replacement range. 

360 

361 """ 

362 if not nodes: 

363 return 

364 start_offset, end_offset = _RewriteActions.__correct_for_comments_and_whitespace( 

365 self.node.offset, 

366 self.content, 

367 include_whitespace, 

368 include_comments, 

369 nodes, 

370 ) 

371 # start_offset =nodes[0].get_start_offset() 

372 # end_offset =nodes[-1].get_start_offset()+nodes[-1].get_length()+1 

373 indent = self.derive_indent(start_offset) 

374 if self.correct_indent: 

375 if new_content.startswith("\n"): 

376 # a blank first line would otherwise strand the original indent as trailing whitespace 

377 start_offset -= indent 

378 new_content = TextUtils.shift_right(new_content, indent, start_line=1) 

379 self.__replace_bytes(rewriter, start_offset, end_offset, new_content) 

380 

381 def __remove( 

382 self, 

383 rewriter: Rewriter, 

384 nodes: Sequence[Rewritable], 

385 include_whitespace: bool = False, 

386 include_comments: bool = False, 

387 ) -> None: 

388 """Remove a list of AST nodes from the content, optionally including surrounding whitespace and comments. 

389 

390 Args: 

391 rewriter (Rewriter): The rewriter used to apply the removal. 

392 nodes (Sequence[Rewritable]): The list of AST nodes to remove. 

393 include_whitespace (bool, optional): Whether to include surrounding whitespace in the removal. Defaults to False. 

394 include_comments (bool, optional): Whether to include surrounding comments in the removal. Defaults to False. 

395 

396 """ 

397 if not nodes: 

398 return 

399 

400 start_offset, end_offset = _RewriteActions.__correct_for_comments_and_whitespace( 

401 self.node.offset, 

402 self.content, 

403 include_whitespace, 

404 include_comments, 

405 nodes, 

406 ) 

407 indent = self.derive_indent(start_offset) 

408 # remove the indent in front of it 

409 start_offset -= indent 

410 # remove the line if it is empty 

411 if ( 

412 start_offset > 0 

413 and self.content[start_offset - 1] == ord("\n") 

414 and (end_offset >= len(self.content) or self.content[end_offset] == ord("\n")) 

415 ): 

416 start_offset -= 1 

417 self.__replace_bytes(rewriter, start_offset, end_offset, "") 

418 

419 def derive_indent(self, start_offset: int) -> int: 

420 indent = 0 # len(nodes[0].indent) 

421 if start_offset > 0: 

422 while len(self.content) > (start_offset - indent - 1) and self.content[start_offset - indent - 1] == 32: 

423 indent += 1 

424 return indent 

425 

426 def __insert( 

427 self, 

428 rewriter: Rewriter, 

429 new_content: str, 

430 before: bool, 

431 nodes: Sequence[Rewritable], 

432 include_whitespace: bool, 

433 include_comments: bool, 

434 ) -> None: 

435 if not nodes: 

436 return 

437 content = self.content 

438 indent = TextUtils.get_spaces_before(content, nodes[0].offset) 

439 spaces = " " * indent 

440 # if flattened_nodes[-1] has a new line after white space then we need to add a new line: 

441 ext_start_offset, ext_end_offset = _RewriteActions.__correct_for_comments_and_whitespace( 

442 self.node.offset, 

443 self.content, 

444 include_whitespace, 

445 include_comments, 

446 nodes, 

447 ) 

448 white_space = ( 

449 "" 

450 if not include_whitespace 

451 else "\n" + spaces 

452 if ext_end_offset < len(content) and content[ext_end_offset] in b"\n" 

453 else spaces 

454 ) 

455 # indent the new content except the first line 

456 # TODO(FrancescoPezzella): unlike __replace, this ignores correct_indent, so the re-indentation can't be turned off. 

457 # https://github.com/TNO/Renaissance.Py/issues/283 

458 new_content = TextUtils.shift_right(new_content, indent, start_line=1) 

459 

460 if before: 

461 # restore the node's original indent, consumed as new_content's first-line indent 

462 if not white_space and new_content.endswith("\n"): 

463 new_content += spaces 

464 self.__replace_bytes(rewriter, ext_start_offset, ext_start_offset, new_content + white_space) 

465 else: 

466 self.__replace_bytes(rewriter, ext_end_offset, ext_end_offset, white_space + new_content) 

467 

468 def __replace_bytes(self, rewriter: Rewriter, start: int, end: int, new_content: str) -> None: 

469 """Replace the content in the specified range with new content. 

470 

471 Args: 

472 rewriter (Rewriter): The rewriter used to apply the byte replacement. 

473 start (int): The starting index of the range to be replaced. 

474 end (int): The ending index of the range to be replaced. 

475 new_content (str): The new content to insert in the specified range. 

476 

477 """ 

478 rewriter.replace(start, end, new_content.encode(self.encoding)) 

479 

480 def __compose_replacement( 

481 self, replacement: str, matches: Sequence[PatternMatch], transforms: Mapping[str, Callable[[str], str]] 

482 ) -> str: 

483 all_placeholders = {p: n for m in matches for p, n in m.expansions.items()} 

484 for placeholder, nodes in all_placeholders.items(): 

485 quoted_placeholder = re.escape(placeholder) 

486 raw_signature = self.__get_texts(nodes, transforms.get(placeholder)) 

487 # replacement = replacement.replace(placeholder, raw_signature) 

488 while placeholder in replacement: 

489 pattern = re.compile(r"( *)" + quoted_placeholder) 

490 matcher = pattern.search(replacement) 

491 

492 if matcher: 

493 spaces = matcher[1] 

494 place_holder_length = len(placeholder) 

495 index = replacement.index(placeholder) 

496 # TODO a regex may be provided between backticks and the groups are used. This needs a better design 

497 # The transforms argument of replace() can do the same with a function per placeholder 

498 if index + place_holder_length < len(replacement) and replacement[index + place_holder_length] == "`": 

499 # ` ` means get regex 

500 end_index = replacement.index("`", index + place_holder_length + 1) 

501 if not end_index: 

502 raise ValueError("No closing ` found") 

503 regex = replacement[index + place_holder_length + 1 : end_index] 

504 regex_match = re.match(regex, raw_signature) 

505 if regex_match: 

506 raw_signature = "".join(regex_match.groups()) 

507 place_holder_length = end_index - index + 1 

508 # TODO(FrancescoPezzella): this also indents the lines inside a multi-line string in the captured text. 

509 # https://github.com/TNO/Renaissance.Py/issues/283 

510 indent_replacement = raw_signature.replace("\n", "\n" + spaces) 

511 if ( 

512 placeholder.startswith("$$") 

513 and index + place_holder_length < len(replacement) 

514 and replacement[index + place_holder_length] == ";" 

515 ): 

516 place_holder_length += 1 

517 # replace the placeholder with the indent replacement 

518 replacement = replacement[:index] + indent_replacement + replacement[index + place_holder_length :] 

519 else: 

520 print("Match doesn't match unexpectedly") 

521 return replacement 

522 

523 def __get_texts(self, nodes: Sequence[Rewritable], transform: Callable[[str], str] | None = None) -> str: 

524 """Return the source from the first of the sibling nodes to the end of the last, dedented to the first. 

525 

526 The text between the nodes is kept. A node with rewrites queued inside it is replaced by its rewritten text, 

527 and transform, if given, is applied to the text of each node. 

528 """ 

529 if len(nodes) == 1: 

530 text = self.__get_text(nodes[0]) 

531 return transform(text) if transform else text 

532 # TODO(FrancescoPezzella): an empty `$$x` raises IndexError here, e.g. `f($$args)` -> `g($$args)` on `f()`. 

533 # https://github.com/TNO/Renaissance.Py/issues/329 

534 start = self.cor_offset(nodes[0].offset) 

535 end = self.cor_offset(nodes[-1].extended_end_offset) 

536 rewriter = Rewriter(self.content[start:end]) 

537 for node in nodes: 

538 text = self.__get_text(node) 

539 if transform: 

540 text = transform(text) 

541 if text != node.text: 

542 rewriter.replace( 

543 self.cor_offset(node.offset) - start, self.cor_offset(node.extended_end_offset) - start, text.encode(self.encoding) 

544 ) 

545 result = rewriter.apply().decode(self.encoding) 

546 return TextUtils.shift_left(result, self.derive_indent(start), start_line=1) 

547 

548 def __get_text(self, node: Rewritable) -> str: 

549 if self._should_skip(node): 

550 return "" 

551 

552 if node == self.node: 

553 return node.text 

554 # the descendants may need to be rewritten as well 

555 rewrites = [rewrite for rewrite in self.rewrites if any(is_ancestor_of(node, rewrite_node) for rewrite_node in rewrite.nodes)] 

556 if rewrites: 

557 rewriter = _RewriteActions(node, self.encoding, self.correct_indent, rewrites) 

558 return rewriter.apply_to_string() 

559 return node.text 

560 

561 def __prepare_replacement_content( 

562 self, 

563 new_content: str, 

564 target: PatternMatch | Rewritable | Sequence[Rewritable], 

565 transforms: Mapping[str, Callable[[str], str]], 

566 ) -> tuple[str, Sequence[Rewritable]]: 

567 if isinstance(target, PatternMatch): 

568 new_content = self.__compose_replacement(new_content, [target], transforms) 

569 node_list = target.nodes 

570 else: 

571 node_list = ( 

572 [target] if isinstance(target, Rewritable) else target 

573 ) # TODO How to make a Sequence[Rewritable] as type hints also show list[Rewritable]? 

574 return new_content, node_list 

575 

576 def _should_skip(self, node: Rewritable): 

577 """If the node is not the first node of a pattern match it should be skipped.""" 

578 return any(node in rewrite.nodes[1:] for rewrite in self.rewrites if isinstance(rewrite.target, PatternMatch)) 

579 

580 @staticmethod 

581 def __correct_for_comments_and_whitespace( 

582 offset: int, 

583 content: bytes, 

584 include_whitespace: bool, 

585 include_comments: bool, 

586 nodes: Sequence[Rewritable], 

587 ) -> tuple[int, int]: 

588 start_offset = nodes[0].offset - offset 

589 end_offset = nodes[-1].extended_end_offset - offset 

590 if include_comments: 

591 preceding_node = nodes[0].preceding_sibling 

592 parent = nodes[0].parent 

593 start_comment_location = 0 

594 if preceding_node: 

595 # start after the comment of the preceding node 

596 start_comment_location = preceding_node.extended_end_offset - offset 

597 preceding_end_offset = _RewriteActions.__get_comment_after_location(start_comment_location, start_offset, content) 

598 if preceding_end_offset != (-1, -1): 

599 start_comment_location = preceding_end_offset[1] 

600 elif parent: 

601 start_comment_location = parent.offset - offset 

602 # get the comment belonging to the preceding node 

603 extended_location = _RewriteActions.get_comment_location(start_comment_location, start_offset, content) 

604 if extended_location != (-1, -1): 

605 start_offset = extended_location[0] 

606 next_sibling = nodes[-1].next_sibling 

607 end_comment_location = next_sibling.offset - offset if next_sibling else parent.end_offset - offset if parent else len(content) 

608 location_after_comment = _RewriteActions.__get_comment_after_location(end_offset, end_comment_location, content) 

609 if location_after_comment != (-1, -1): 

610 end_offset = location_after_comment[1] 

611 if include_whitespace: 

612 end_offset = _RewriteActions.__extend_with_whitespace(end_offset, content) 

613 return start_offset, end_offset 

614 

615 def cor_offset(self, offset: int): 

616 return offset - self.node.offset 

617 

618 @staticmethod 

619 def get_comment_location(start_offset: int, stop_offset: int, content: bytes) -> tuple[int, int]: 

620 """Get the location of the comment before the location, but after the stop_location. 

621 

622 A comment is a line that starts with // or a block that starts with /* and ends with */ 

623 or a line that starts with #. 

624 """ 

625 # TODO: the // and # branches below only find the single closest comment line 

626 # search last occurrence of //, /*, # in a byte array 

627 comment_start = content.rfind(b"//", start_offset, stop_offset) 

628 if comment_start != -1: 

629 comment_end = _RewriteActions.__get_end_of_line(content, comment_start) 

630 return comment_start, comment_end 

631 comment_start = content.rfind(b"/*", start_offset, stop_offset) 

632 if comment_start != -1: 

633 comment_end = content.find(b"*/", comment_start, stop_offset) 

634 if comment_end != -1: 

635 comment_end += len("*/") 

636 return comment_start, comment_end 

637 comment_start = content.rfind(b"#", start_offset, stop_offset) 

638 if comment_start != -1: 

639 comment_end = _RewriteActions.__get_end_of_line(content, comment_start) 

640 return comment_start, comment_end 

641 return -1, -1 

642 

643 @staticmethod 

644 def __extend_with_whitespace(start_offset: int, content: bytes) -> int: 

645 end_location = _RewriteActions.__get_end_of_line(content, start_offset) 

646 text = content[start_offset:end_location] 

647 for byt in text: 

648 if byt not in b" \t": 

649 return start_offset 

650 return end_location 

651 

652 @staticmethod 

653 def __get_comment_after_location(start_offset: int, end_offset: int, content: bytes) -> tuple[int, int]: 

654 """Get the location of the comment before the location, but after the stop_location. 

655 

656 A comment is a line that starts with // or a block that starts with /* and ends with */ 

657 or a line that starts with #. 

658 """ 

659 # TODO: same single-line limitation as get_comment_location - a multi-line trailing 

660 # comment block is only captured up to its first line here. 

661 line_end_offset = _RewriteActions.__get_end_of_line(content, start_offset) 

662 if line_end_offset == -1: 

663 line_end_offset = len(content) 

664 comment_start = content.find(b"//", start_offset, line_end_offset) 

665 if comment_start == -1: 

666 comment_start = content.rfind(b"#", start_offset, line_end_offset) 

667 if comment_start != -1: 

668 return comment_start, line_end_offset 

669 comment_start = content.rfind(b"/*", start_offset, line_end_offset) 

670 if comment_start != -1: 

671 # a block comment must start on the same line but doesn't have to finish on the same line 

672 comment_end = content.find(b"*/", comment_start, end_offset) 

673 if comment_end != -1: 

674 comment_end += len("*/") 

675 return comment_start, comment_end 

676 return -1, -1 

677 

678 @staticmethod 

679 def __get_end_of_line(content: bytes, start: int) -> int: 

680 location = content.find(b"\n", start) 

681 if location == -1: 

682 return len(content) 

683 return location