Skip to content

Commit

Permalink
Avoid doing offset/line shift loops when there is no shifting to be done
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenburns committed Dec 28, 2023
1 parent 8be8b78 commit ca9ddd5
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/textual/document/_wrapped_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,21 +197,23 @@ def wrap_range(
line_shift = new_bottom_line_index - old_bottom_line_index

# Update the line info at all offsets below the edit region.
for y_offset in range(
top_y_offset + new_height, len(self._offset_to_line_info)
):
old_line_index, section_offset = self._offset_to_line_info[y_offset]
new_line_index = old_line_index + line_shift
new_line_info = (new_line_index, section_offset)
self._offset_to_line_info[y_offset] = new_line_info
if line_shift:
for y_offset in range(
top_y_offset + new_height, len(self._offset_to_line_info)
):
old_line_index, section_offset = self._offset_to_line_info[y_offset]
new_line_index = old_line_index + line_shift
new_line_info = (new_line_index, section_offset)
self._offset_to_line_info[y_offset] = new_line_info

# Update the offsets at all lines below the edit region
for line_index in range(
top_line_index + len(new_lines), len(self._line_index_to_offsets)
):
old_offsets = self._line_index_to_offsets[line_index]
new_offsets = [offset + offset_shift for offset in old_offsets]
self._line_index_to_offsets[line_index] = new_offsets
if offset_shift:
for line_index in range(
top_line_index + len(new_lines), len(self._line_index_to_offsets)
):
old_offsets = self._line_index_to_offsets[line_index]
new_offsets = [offset + offset_shift for offset in old_offsets]
self._line_index_to_offsets[line_index] = new_offsets

self._wrap_offsets[
top_line_index : old_bottom_line_index + 1
Expand Down

0 comments on commit ca9ddd5

Please sign in to comment.