MCPcopy
hub / github.com/Textualize/rich / _get_code_index_for_syntax_position

Function _get_code_index_for_syntax_position

rich/syntax.py:844–868  ·  view source on GitHub ↗

Returns the index of the code string for the given positions. Args: newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet. position (SyntaxPosition): The position to search for. Returns: Optional[int]: The index of the

(
    newlines_offsets: Sequence[int], position: SyntaxPosition
)

Source from the content-addressed store, hash-verified

842
843
844def _get_code_index_for_syntax_position(
845 newlines_offsets: Sequence[int], position: SyntaxPosition
846) -> Optional[int]:
847 """
848 Returns the index of the code string for the given positions.
849
850 Args:
851 newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet.
852 position (SyntaxPosition): The position to search for.
853
854 Returns:
855 Optional[int]: The index of the code string for this position, or `None`
856 if the given position's line number is out of range (if it's the column that is out of range
857 we silently clamp its value so that it reaches the end of the line)
858 """
859 lines_count = len(newlines_offsets)
860
861 line_number, column_index = position
862 if line_number > lines_count or len(newlines_offsets) < (line_number + 1):
863 return None # `line_number` is out of range
864 line_index = line_number - 1
865 line_length = newlines_offsets[line_index + 1] - newlines_offsets[line_index] - 1
866 # If `column_index` is out of range: let's silently clamp it:
867 column_index = min(line_length, column_index)
868 return newlines_offsets[line_index] + column_index
869
870
871if __name__ == "__main__": # pragma: no cover

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected