Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ i32 EditorGutterWidth(Editor *ed, const Panel *panel) {
u64 EditorLineNumberLabel(const Editor *ed, const View *view, const Buffer *buffer, u64 line) {
if (ed && ed->line_number_mode == LineNumberMode::Relative) {
u64 cursor_line = ViewCursorLine(view, buffer);
if (line == cursor_line) return line + 1;
return (line > cursor_line) ? line - cursor_line : cursor_line - line;
}
return line + 1; // buffer lines are 0-based; the display is not
Expand Down
8 changes: 4 additions & 4 deletions core/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
inline constexpr u64 kMaxRecordedChords = 128;

// What the gutter counts. Relative shows each line's distance from the cursor,
// with 0 on the cursor's own line, which is what makes a jump count something
// you read rather than work out.
// with the absolute 1-based number on the cursor's own line (vim's number +
// relativenumber), so a jump count is something you read rather than work out.
//
// There is no config file, so this is a source-level setting: change
// kLineNumberModeDefault and rebuild. :number, :relativenumber and :nonumber
Expand Down Expand Up @@ -220,8 +220,8 @@ void EditorSetScreen(Editor *ed, RectS32 screen);
// plus one blank column separating the numbers from the text.
[[nodiscard]] i32 EditorGutterWidth(Editor *ed, const Panel *panel);

// The number `line` displays: its distance from the cursor when relative (0 on
// the cursor's own line), otherwise the 1-based line number.
// The number `line` displays: its distance from the cursor when relative, or
// the 1-based line number on the cursor's own line / in absolute mode.
[[nodiscard]] u64 EditorLineNumberLabel(const Editor *ed, const View *view,
const Buffer *buffer, u64 line);

Expand Down
7 changes: 4 additions & 3 deletions docs/rendering.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ Fonts are chosen at runtime. Colours are compiled in.

## Line numbers

The gutter shows each line's distance from the cursor, with `0` on the cursor's
own line — vim's `relativenumber`. `:number` switches to absolute numbers,
`:relativenumber` back, and `:nonumber` hides the gutter entirely.
The gutter shows each line's distance from the cursor, with the absolute
1-based number on the cursor's own line — vim's `number` + `relativenumber`.
`:number` switches to absolute numbers, `:relativenumber` back, and
`:nonumber` hides the gutter entirely.

To change what it starts as, edit one constant in `core/editor/editor.h`:

Expand Down
6 changes: 3 additions & 3 deletions tests/test_vim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1598,8 +1598,8 @@ TEST(line_numbers_relative_labels_count_from_the_cursor) {
Type(&f, "2j");
CHECK_EQ(CursorLine(&f), 2);

// Zero on the cursor's own line, distances either side of it.
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 2), 0);
// Absolute number on the cursor's own line, distances either side of it.
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 2), 3);
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 1), 1);
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 0), 2);
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 3), 1);
Expand All @@ -1608,7 +1608,7 @@ TEST(line_numbers_relative_labels_count_from_the_cursor) {
// They follow the cursor rather than being computed once.
Type(&f, "j");
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 2), 1);
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 3), 0);
CHECK_EQ(EditorLineNumberLabel(&f.ed, view, buffer, 3), 4);

Destroy(&f);
}
Expand Down