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
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 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 @@ -93,7 +95,7 @@ impl EditorView {
};

Self::render_text_highlights(doc, offset, area, surface, theme, highlights);
Self::render_gutter(doc, view, area, surface, theme);
Self::render_gutter(doc, view, area, surface, theme, config);

if is_focused {
Self::render_focused_view_elements(view, doc, area, theme, surface);
Expand Down Expand Up @@ -459,6 +461,7 @@ impl EditorView {
viewport: Rect,
surface: &mut Surface,
theme: &Theme,
config: &helix_view::editor::Config,
) {
let text = doc.text().slice(..);
let last_line = view.last_line(doc);
Expand All @@ -473,6 +476,9 @@ impl EditorView {
// document or not. We only draw it if it's not an empty line.
let draw_last = text.line_to_byte(last_line) < text.len_bytes();

let current_line = doc
.text()
.char_to_line(doc.selection(view.id).primary().anchor);
for (i, line) in (view.first_line..(last_line + 1)).enumerate() {
use helix_core::diagnostic::Severity;
if let Some(diagnostic) = doc.diagnostics().iter().find(|d| d.line == line) {
Expand All @@ -495,7 +501,13 @@ 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 => {
let relative_line = abs_diff(current_line, line);
format!("{:>5}", relative_line)
}
}
};
surface.set_stringn(
viewport.x + 1 - GUTTER_OFFSET,
Expand Down Expand Up @@ -1042,6 +1054,7 @@ impl Component for EditorView {
&cx.editor.theme,
is_focused,
loader,
&cx.editor.config,
);
}

Expand Down Expand Up @@ -1115,3 +1128,12 @@ fn canonicalize_key(key: &mut KeyEvent) {
key.modifiers.remove(KeyModifiers::SHIFT)
}
}

#[inline]
fn abs_diff(a: usize, b: usize) -> usize {
if a > b {
a - b
} else {
b - a
}
}
13 changes: 13 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,29 @@ pub struct Config {
pub scroll_lines: isize,
/// Mouse support. Defaults to true.
pub mouse: bool,
/// Line number mode.
pub line_number: LineNumber,
/// Middle click paste support. Defaults to true
pub middle_click_paste: bool,
}

#[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 {
fn default() -> Self {
Self {
scrolloff: 5,
scroll_lines: 3,
mouse: true,
line_number: LineNumber::Absolute,
middle_click_paste: true,
}
}
Expand Down