| 726 | return ''.join(row) |
| 727 | |
| 728 | def _should_show_carets(self, start_offset, end_offset, all_lines, anchors): |
| 729 | with suppress(SyntaxError, ImportError): |
| 730 | import ast |
| 731 | tree = ast.parse('\n'.join(all_lines)) |
| 732 | if not tree.body: |
| 733 | return False |
| 734 | statement = tree.body[0] |
| 735 | value = None |
| 736 | def _spawns_full_line(value): |
| 737 | return ( |
| 738 | value.lineno == 1 |
| 739 | and value.end_lineno == len(all_lines) |
| 740 | and value.col_offset == start_offset |
| 741 | and value.end_col_offset == end_offset |
| 742 | ) |
| 743 | match statement: |
| 744 | case ast.Return(value=ast.Call()): |
| 745 | if isinstance(statement.value.func, ast.Name): |
| 746 | value = statement.value |
| 747 | case ast.Assign(value=ast.Call()): |
| 748 | if ( |
| 749 | len(statement.targets) == 1 and |
| 750 | isinstance(statement.targets[0], ast.Name) |
| 751 | ): |
| 752 | value = statement.value |
| 753 | if value is not None and _spawns_full_line(value): |
| 754 | return False |
| 755 | if anchors: |
| 756 | return True |
| 757 | if all_lines[0][:start_offset].lstrip() or all_lines[-1][end_offset:].rstrip(): |
| 758 | return True |
| 759 | return False |
| 760 | |
| 761 | def format(self, **kwargs): |
| 762 | """Format the stack ready for printing. |