Skip to content

Commit

Permalink
widgets: text-area: Make the starting line number a kwarg
Browse files Browse the repository at this point in the history
  • Loading branch information
royatt committed Jun 22, 2024
1 parent c9c34c7 commit 701308c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ class TextArea(ScrollView):
Changing this value will immediately re-render the `TextArea`."""

line_number_start: Reactive[int] = reactive(1, init=False)
"""The line number the first line should be."""

indent_width: Reactive[int] = reactive(4, init=False)
"""The width of tabs or the multiple of spaces to align to on pressing the `tab` key.
Expand Down Expand Up @@ -370,6 +373,7 @@ def __init__(
tab_behavior: Literal["focus", "indent"] = "focus",
read_only: bool = False,
show_line_numbers: bool = False,
line_number_start: int = 1,
max_checkpoints: int = 50,
name: str | None = None,
id: str | None = None,
Expand All @@ -387,6 +391,7 @@ def __init__(
tab_behavior: If 'focus', pressing tab will switch focus. If 'indent', pressing tab will insert a tab.
read_only: Enable read-only mode. This prevents edits using the keyboard.
show_line_numbers: Show line numbers on the left edge.
line_number_start: What line number to start on.
max_checkpoints: The maximum number of undo history checkpoints to retain.
name: The name of the `TextArea` widget.
id: The ID of the widget, used to refer to it from Textual CSS.
Expand Down Expand Up @@ -455,6 +460,7 @@ def __init__(
self.set_reactive(TextArea.soft_wrap, soft_wrap)
self.set_reactive(TextArea.read_only, read_only)
self.set_reactive(TextArea.show_line_numbers, show_line_numbers)
self.set_reactive(TextArea.line_number_start, line_number_start)

self.tab_behavior = tab_behavior

Expand All @@ -475,6 +481,7 @@ def code_editor(
tab_behavior: Literal["focus", "indent"] = "indent",
read_only: bool = False,
show_line_numbers: bool = True,
line_number_start: int = 1,
max_checkpoints: int = 50,
name: str | None = None,
id: str | None = None,
Expand All @@ -494,6 +501,7 @@ def code_editor(
soft_wrap: Enable soft wrapping.
tab_behavior: If 'focus', pressing tab will switch focus. If 'indent', pressing tab will insert a tab.
show_line_numbers: Show line numbers on the left edge.
line_number_start: What line number to start on.
name: The name of the `TextArea` widget.
id: The ID of the widget, used to refer to it from Textual CSS.
classes: One or more Textual CSS compatible class names separated by spaces.
Expand All @@ -508,6 +516,7 @@ def code_editor(
tab_behavior=tab_behavior,
read_only=read_only,
show_line_numbers=show_line_numbers,
line_number_start=line_number_start,
max_checkpoints=max_checkpoints,
name=name,
id=id,
Expand Down Expand Up @@ -691,6 +700,11 @@ def _watch_show_line_numbers(self) -> None:
self._rewrap_and_refresh_virtual_size()
self.scroll_cursor_visible()

def _watch_line_number_start(self) -> None:
"""The line number gutter max size might change and contributes to virtual size, so recalculate."""
self._rewrap_and_refresh_virtual_size()
self.scroll_cursor_visible()

def _watch_indent_width(self) -> None:
"""Changing width of tabs will change the document display width."""
self._rewrap_and_refresh_virtual_size()
Expand Down Expand Up @@ -1142,7 +1156,9 @@ def render_line(self, y: int) -> Strip:
gutter_style = theme.gutter_style

gutter_width_no_margin = gutter_width - 2
gutter_content = str(line_index + 1) if section_offset == 0 else ""
gutter_content = (
str(line_index + self.line_number_start) if section_offset == 0 else ""
)
gutter = Text(
f"{gutter_content:>{gutter_width_no_margin}} ",
style=gutter_style or "",
Expand Down Expand Up @@ -1467,7 +1483,8 @@ def gutter_width(self) -> int:
# The longest number in the gutter plus two extra characters: `│ `.
gutter_margin = 2
gutter_width = (
len(str(self.document.line_count)) + gutter_margin
len(str(self.document.line_count - 1 + self.line_number_start))
+ gutter_margin
if self.show_line_numbers
else 0
)
Expand Down

0 comments on commit 701308c

Please sign in to comment.