rope.base.codeanalyze

Classes

ChangeCollector

SourceLinesAdapter

Adapts source to Lines interface

ASTLinesAdapter

Convert between stdlib's ast col_offset and rope's region offsets.

ArrayLinesAdapter

LinesToReadline

LogicalLineFinder

CachingLogicalLineFinder

Functions

custom_generator(lines)

tokenizer_generator(lines)

get_block_start(lines, lineno[, maximum_indents])

Approximate block start

get_block_start_patterns()

count_line_indents(line)

get_string_pattern_with_prefix(prefix[, prefix_group_name])

get_string_pattern()

get_formatted_string_pattern()

get_any_string_pattern()

get_comment_pattern()

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_segment exactly, including its quirks:

  • Line numbers are 1-indexed (node.lineno / node.end_lineno), matching the ast module.

  • 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 by str.splitlines() (form feed, vertical tab, etc.), since those extra separators don’t correspond to a new line as far as lineno is concerned.

exception LineNumberOutOfRange

Bases: IndexError

Sequence index out of range.

LINE_PATTERN_STR
LINE_PATTERN_BYTES
get_line(lineno: int) → str
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 private ast._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.

class rope.base.codeanalyze.ArrayLinesAdapter(lines)
lines
get_line(line_number)
length()
class rope.base.codeanalyze.LinesToReadline(lines, start)
lines
current
readline()
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()