rope.base.codeanalyze ===================== .. py:module:: rope.base.codeanalyze Classes ------- .. autoapisummary:: rope.base.codeanalyze.ChangeCollector rope.base.codeanalyze.SourceLinesAdapter rope.base.codeanalyze.ASTLinesAdapter rope.base.codeanalyze.ArrayLinesAdapter rope.base.codeanalyze.LinesToReadline rope.base.codeanalyze.LogicalLineFinder rope.base.codeanalyze.CachingLogicalLineFinder Functions --------- .. autoapisummary:: rope.base.codeanalyze.custom_generator rope.base.codeanalyze.tokenizer_generator rope.base.codeanalyze.get_block_start rope.base.codeanalyze.get_block_start_patterns rope.base.codeanalyze.count_line_indents rope.base.codeanalyze.get_string_pattern_with_prefix 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 Module Contents --------------- .. py:class:: ChangeCollector(text) .. py:attribute:: text .. py:attribute:: changes :value: [] .. py:method:: add_change(start, end, new_text=None) .. py:method:: get_changed() .. py:class:: SourceLinesAdapter(source_code) Adapts source to Lines interface Note: The creation of this class is expensive. .. py:attribute:: code .. py:attribute:: starts :value: None .. py:method:: get_line(lineno) .. py:method:: length() .. py:method:: get_line_number(offset) .. py:method:: get_line_start(lineno) .. py:method:: get_line_end(lineno) .. py:class:: 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. .. py:exception:: LineNumberOutOfRange Bases: :py:obj:`IndexError` Sequence index out of range. .. py:attribute:: LINE_PATTERN_STR .. py:attribute:: LINE_PATTERN_BYTES .. py:method:: get_line(lineno: int) -> str .. py:method:: 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. .. py:class:: ArrayLinesAdapter(lines) .. py:attribute:: lines .. py:method:: get_line(line_number) .. py:method:: length() .. py:class:: LinesToReadline(lines, start) .. py:attribute:: lines .. py:attribute:: current .. py:method:: readline() .. py:function:: custom_generator(lines) .. py:class:: LogicalLineFinder(lines) .. py:attribute:: lines .. py:method:: logical_line_in(line_number) .. py:method:: generate_starts(start_line=1, end_line=None) .. py:method:: generate_regions(start_line=1, end_line=None) .. py:function:: tokenizer_generator(lines) .. py:class:: CachingLogicalLineFinder(lines, generate=custom_generator) .. py:attribute:: lines .. py:property:: starts .. py:property:: ends .. py:method:: logical_line_in(line_number) .. py:method:: generate_starts(start_line=1, end_line=None) .. py:function:: get_block_start(lines, lineno, maximum_indents=80) Approximate block start .. py:function:: get_block_start_patterns() .. py:function:: count_line_indents(line) .. py:function:: get_string_pattern_with_prefix(prefix, prefix_group_name=None) .. py:function:: get_string_pattern() .. py:function:: get_formatted_string_pattern() .. py:function:: get_any_string_pattern() .. py:function:: get_comment_pattern()