From 67bf4250caf14d3a632abdbfa367c04b318d782c Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 25 Nov 2021 10:07:23 +0800 Subject: Optimize space for DocumentId with NonZeroUsize (#1097) Now Option uses one byte rather than two--- helix-view/src/lib.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'helix-view/src/lib.rs') diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index 3e779356..4d19ee2e 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -12,8 +12,18 @@ pub mod theme; pub mod tree; pub mod view; -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug)] -pub struct DocumentId(usize); +use std::num::NonZeroUsize; + +// uses NonZeroUsize so Option use a byte rather than two +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] +pub struct DocumentId(NonZeroUsize); + +impl Default for DocumentId { + fn default() -> DocumentId { + // Safety: 1 is non-zero + DocumentId(unsafe { NonZeroUsize::new_unchecked(1) }) + } +} slotmap::new_key_type! { pub struct ViewId; -- cgit v1.2.3-70-g09d2 From 225e8ccf31ac554ae0977b4359c50edb2670594e Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Tue, 23 Nov 2021 12:56:46 +0900 Subject: Extract gutters into helix-view --- helix-term/src/ui/editor.rs | 97 +-------------------------------------------- helix-view/src/gutter.rs | 95 ++++++++++++++++++++++++++++++++++++++++++++ helix-view/src/lib.rs | 1 + helix-view/src/view.rs | 24 +++++++++-- 4 files changed, 117 insertions(+), 100 deletions(-) create mode 100644 helix-view/src/gutter.rs (limited to 'helix-view/src/lib.rs') diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 82fb8fbf..83be816f 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -17,7 +17,6 @@ use helix_core::{ }; use helix_view::{ document::{Mode, SCRATCH_BUFFER_NAME}, - editor::{Config, LineNumber}, graphics::{CursorKind, Modifier, Rect, Style}, info::Info, input::KeyEvent, @@ -421,97 +420,12 @@ impl EditorView { .map(|range| range.cursor_line(text)) .collect(); - use std::fmt::Write; - - fn diagnostic<'doc>( - doc: &'doc Document, - _view: &View, - theme: &Theme, - _config: &Config, - _is_focused: bool, - _width: usize, - ) -> GutterFn<'doc> { - let warning = theme.get("warning"); - let error = theme.get("error"); - let info = theme.get("info"); - let hint = theme.get("hint"); - let diagnostics = doc.diagnostics(); - - Box::new(move |line: usize, _selected: bool, out: &mut String| { - use helix_core::diagnostic::Severity; - if let Some(diagnostic) = diagnostics.iter().find(|d| d.line == line) { - write!(out, "●").unwrap(); - return Some(match diagnostic.severity { - Some(Severity::Error) => error, - Some(Severity::Warning) | None => warning, - Some(Severity::Info) => info, - Some(Severity::Hint) => hint, - }); - } - None - }) - } - - fn line_number<'doc>( - doc: &'doc Document, - view: &View, - theme: &Theme, - config: &Config, - is_focused: bool, - width: usize, - ) -> GutterFn<'doc> { - let text = doc.text().slice(..); - let last_line = view.last_line(doc); - // Whether to draw the line number for the last line of the - // 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 linenr = theme.get("ui.linenr"); - let linenr_select: Style = theme.try_get("ui.linenr.selected").unwrap_or(linenr); - - let current_line = doc - .text() - .char_to_line(doc.selection(view.id).primary().cursor(text)); - - let config = config.line_number; - - Box::new(move |line: usize, selected: bool, out: &mut String| { - if line == last_line && !draw_last { - write!(out, "{:>1$}", '~', width).unwrap(); - Some(linenr) - } else { - let line = match config { - LineNumber::Absolute => line + 1, - LineNumber::Relative => { - if current_line == line { - line + 1 - } else { - abs_diff(current_line, line) - } - } - }; - let style = if selected && is_focused { - linenr_select - } else { - linenr - }; - write!(out, "{:>1$}", line, width).unwrap(); - Some(style) - } - }) - } - - type GutterFn<'doc> = Box Option