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(helix-view): dynamic line numbers #1522

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ pub enum LineNumber {

/// Show relative line number to the primary cursor
Relative,

/// Show relative line number if focused and in normal/select mode.
/// Show absolute line number if unfocused or in insert mode.
Dynamic,
}

impl std::str::FromStr for LineNumber {
Expand Down
21 changes: 11 additions & 10 deletions helix-view/src/gutter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,23 @@ pub fn line_number<'doc>(
.char_to_line(doc.selection(view.id).primary().cursor(text));

let config = config.line_number;
let mode = doc.mode;

Box::new(move |line: usize, selected: bool, out: &mut String| {
if line == last_line && !draw_last {
write!(out, "{:>1$}", '~', width).unwrap();
Some(linenr)
} else {
use crate::editor::LineNumber;
let line = match config {
LineNumber::Absolute => line + 1,
LineNumber::Relative => {
if current_line == line {
line + 1
} else {
abs_diff(current_line, line)
}
}
use crate::{document::Mode, editor::LineNumber};
let use_relative = match config {
LineNumber::Absolute => false,
LineNumber::Relative => current_line != line,
LineNumber::Dynamic => current_line != line && is_focused && mode != Mode::Insert,
};
let line = if use_relative {
abs_diff(current_line, line)
} else {
line + 1
};
let style = if selected && is_focused {
linenr_select
Expand Down