rope.base.codeanalyze
Classes
Adapts source to Lines interface |
|
Convert between stdlib's ast col_offset and rope's region offsets. |
|
Functions
|
|
|
|
|
Approximate block start |
|
|
|
|
Module Contents
- class rope.base.codeanalyze.ChangeCollector(text)
- text
- changes = []
- add_change(start, end, new_text=None)
- get_changed()
- class rope.base.codeanalyze.SourceLinesAdapter(source_code)
Adapts source to Lines interface
Note: The creation of this class is expensive.
- code
- starts = None
- get_line(lineno)
- length()
- get_line_number(offset)
- get_line_start(lineno)
- get_line_end(lineno)
- class rope.base.codeanalyze.ASTLinesAdapter(source: str)
Convert between stdlib’s ast col_offset and rope’s region offsets.
ast col_offset are given in byte offset of the utf-8 source code from the CPython interpreter, this is not necessarily identical to the str offset (i.e. unicode codepoint offset) that rope uses.
Semantics are intended to match
ast.get_source_segmentexactly, including its quirks:Line numbers are 1-indexed (
node.lineno/node.end_lineno), matching theastmodule.Column offsets are UTF-8 byte offsets into the line, not character offsets – this matters for any line containing non-ASCII characters.
Line splitting uses the same boundaries the CPython parser uses (
\n,\r\n,\r) rather than the broader set recognized bystr.splitlines()(form feed, vertical tab, etc.), since those extra separators don’t correspond to a new line as far aslinenois concerned.
- exception LineNumberOutOfRange
Bases:
IndexErrorSequence index out of range.
- LINE_PATTERN_STR
- LINE_PATTERN_BYTES
- get_source_segment(node, *, padded: bool = False) str | None
Equivalent to
ast.get_source_segment(source, node, padded=padded).ast.get_source_segment(source, node)is convenient, but every call re-splits the entire source string into lines (via the privateast._splitlines_no_ff) and re-encodes whatever lines it touches to UTF-8. If you’re pulling source segments for many nodes out of the same file (e.g. walking a whole AST), that’s a lot of repeated O(n) work.
- rope.base.codeanalyze.custom_generator(lines)
- class rope.base.codeanalyze.LogicalLineFinder(lines)
- lines
- logical_line_in(line_number)
- generate_starts(start_line=1, end_line=None)
- generate_regions(start_line=1, end_line=None)
- rope.base.codeanalyze.tokenizer_generator(lines)
- class rope.base.codeanalyze.CachingLogicalLineFinder(lines, generate=custom_generator)
- lines
- property starts
- property ends
- logical_line_in(line_number)
- generate_starts(start_line=1, end_line=None)
- rope.base.codeanalyze.get_block_start(lines, lineno, maximum_indents=80)
Approximate block start
- rope.base.codeanalyze.get_block_start_patterns()
- rope.base.codeanalyze.count_line_indents(line)
- rope.base.codeanalyze.get_string_pattern_with_prefix(prefix, prefix_group_name=None)
- rope.base.codeanalyze.get_string_pattern()
- rope.base.codeanalyze.get_formatted_string_pattern()
- rope.base.codeanalyze.get_any_string_pattern()
- rope.base.codeanalyze.get_comment_pattern()