Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: relative numbers #485

Merged
merged 10 commits into from
Aug 16, 2021
17 changes: 15 additions & 2 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use helix_core::{
};
use helix_view::{
document::Mode,
editor::LineNumber,
graphics::{CursorKind, Modifier, Rect, Style},
info::Info,
input::KeyEvent,
Expand Down Expand Up @@ -71,6 +72,7 @@ impl EditorView {
theme: &Theme,
is_focused: bool,
loader: &syntax::Loader,
config: &helix_view::editor::Config,
) {
let area = Rect::new(
view.area.x + GUTTER_OFFSET,
Expand All @@ -79,7 +81,7 @@ impl EditorView {
view.area.height.saturating_sub(1),
); // - 1 for statusline

self.render_buffer(doc, view, area, surface, theme, is_focused, loader);
self.render_buffer(doc, view, area, surface, theme, is_focused, loader, config);

// if we're not at the edge of the screen, draw a right border
if viewport.right() != view.area.right() {
Expand Down Expand Up @@ -115,6 +117,7 @@ impl EditorView {
theme: &Theme,
is_focused: bool,
loader: &syntax::Loader,
config: &helix_view::editor::Config,
) {
let text = doc.text().slice(..);

Expand Down Expand Up @@ -358,7 +361,16 @@ impl EditorView {
let line_number_text = if line == last_line && !draw_last {
" ~".into()
} else {
format!("{:>5}", line + 1)
match config.line_number {
LineNumber::Absolute => format!("{:>5}", line + 1),
LineNumber::Relative => {
// TODO: Put char_to_line out of the loop
let current_line = doc.text().char_to_line(selections.primary().anchor);
gbaranski marked this conversation as resolved.
Show resolved Hide resolved
let relative_line =
((current_line as isize) - (line as isize)).abs() as usize;
gbaranski marked this conversation as resolved.
Show resolved Hide resolved
format!("{:>5}", relative_line)
}
}
};
surface.set_stringn(
viewport.x + 1 - GUTTER_OFFSET,
Expand Down Expand Up @@ -925,6 +937,7 @@ impl Component for EditorView {
&cx.editor.theme,
is_focused,
loader,
&cx.editor.config,
);
}

Expand Down
13 changes: 13 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ pub struct Config {
pub scroll_lines: isize,
/// Mouse support. Defaults to true.
pub mouse: bool,
/// Line number mode.
pub line_number: LineNumber,
}

#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum LineNumber {
/// Show absolute line number
Absolute,

/// Show relative line number to the primary cursor
Relative,
gbaranski marked this conversation as resolved.
Show resolved Hide resolved
}

impl Default for Config {
Expand All @@ -37,6 +49,7 @@ impl Default for Config {
scrolloff: 5,
scroll_lines: 3,
mouse: true,
line_number: LineNumber::Absolute,
}
}
}
Expand Down