From 7961355ba1c0cd521372496c507a31a51b41ddf2 Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Wed, 24 Nov 2021 12:17:41 +0530 Subject: Change cursor shape on mode change Fixes #323. Due to terminal limitations we can only change the shape of the primary cursor. --- helix-view/src/editor.rs | 42 ++++++++++++++++++++++++++++++++++++------ helix-view/src/graphics.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 6 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 1ce33760..9c77f270 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,6 +1,6 @@ use crate::{ clipboard::{get_clipboard_provider, ClipboardProvider}, - document::SCRATCH_BUFFER_NAME, + document::{Mode, SCRATCH_BUFFER_NAME}, graphics::{CursorKind, Rect}, theme::{self, Theme}, tree::{self, Tree}, @@ -9,7 +9,7 @@ use crate::{ use futures_util::future; use std::{ - collections::BTreeMap, + collections::{BTreeMap, HashMap}, io::stdin, path::{Path, PathBuf}, pin::Pin, @@ -22,7 +22,7 @@ use anyhow::Error; pub use helix_core::diagnostic::Severity; pub use helix_core::register::Registers; -use helix_core::syntax; +use helix_core::{hashmap, syntax}; use helix_core::{Position, Selection}; use serde::Deserialize; @@ -103,6 +103,30 @@ pub struct Config { /// Whether to display infoboxes. Defaults to true. pub auto_info: bool, pub file_picker: FilePickerConfig, + /// Shape for cursor in each mode + pub cursor_shape: CursorShapeConfig, +} + +#[derive(Debug, Clone, PartialEq, Deserialize)] +#[serde(transparent)] +pub struct CursorShapeConfig(HashMap); + +impl std::ops::Deref for CursorShapeConfig { + type Target = HashMap; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl Default for CursorShapeConfig { + fn default() -> Self { + Self(hashmap!( + Mode::Insert => CursorKind::Bar, + Mode::Normal => CursorKind::Block, + Mode::Select => CursorKind::Underline, + )) + } } #[derive(Debug, Clone, PartialEq, Eq, Deserialize)] @@ -110,7 +134,6 @@ pub struct Config { pub enum LineNumber { /// Show absolute line number Absolute, - /// Show relative line number to the primary cursor Relative, } @@ -135,6 +158,7 @@ impl Default for Config { completion_trigger_len: 2, auto_info: true, file_picker: FilePickerConfig::default(), + cursor_shape: CursorShapeConfig::default(), } } } @@ -594,9 +618,15 @@ impl Editor { let inner = view.inner_area(); pos.col += inner.x as usize; pos.row += inner.y as usize; - (Some(pos), CursorKind::Hidden) + let cursorkind = self + .config + .cursor_shape + .get(&doc.mode()) + .copied() + .unwrap_or_default(); + (Some(pos), cursorkind) } else { - (None, CursorKind::Hidden) + (None, CursorKind::default()) } } diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs index 0bfca04a..c9dd21e3 100644 --- a/helix-view/src/graphics.rs +++ b/helix-view/src/graphics.rs @@ -1,4 +1,6 @@ +use anyhow::{anyhow, Error}; use bitflags::bitflags; +use serde::de::{self, Deserialize, Deserializer}; use std::{ cmp::{max, min}, str::FromStr, @@ -17,6 +19,36 @@ pub enum CursorKind { Hidden, } +impl Default for CursorKind { + fn default() -> Self { + Self::Block + } +} + +impl FromStr for CursorKind { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "bar" => Ok(Self::Bar), + "block" => Ok(Self::Block), + "underline" => Ok(Self::Underline), + _ => Err(anyhow!("Invalid cursor '{}'", s)), + } + } +} + +// toml deserializer doesn't seem to recognize string as enum +impl<'de> Deserialize<'de> for CursorKind { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + s.parse().map_err(de::Error::custom) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Margin { pub vertical: u16, -- cgit v1.2.3-70-g09d2 From 17473b51d37001d4966b82d83d61d3e695e8403d Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Thu, 25 Nov 2021 22:35:07 +0530 Subject: Use serde attribute to rename to lowercase --- helix-view/src/graphics.rs | 30 +++--------------------------- 1 file changed, 3 insertions(+), 27 deletions(-) (limited to 'helix-view/src') diff --git a/helix-view/src/graphics.rs b/helix-view/src/graphics.rs index c9dd21e3..acdaa696 100644 --- a/helix-view/src/graphics.rs +++ b/helix-view/src/graphics.rs @@ -1,12 +1,12 @@ -use anyhow::{anyhow, Error}; use bitflags::bitflags; -use serde::de::{self, Deserialize, Deserializer}; +use serde::Deserialize; use std::{ cmp::{max, min}, str::FromStr, }; -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Deserialize)] +#[serde(rename_all = "lowercase")] /// UNSTABLE pub enum CursorKind { /// █ @@ -25,30 +25,6 @@ impl Default for CursorKind { } } -impl FromStr for CursorKind { - type Err = Error; - - fn from_str(s: &str) -> Result { - match s { - "bar" => Ok(Self::Bar), - "block" => Ok(Self::Block), - "underline" => Ok(Self::Underline), - _ => Err(anyhow!("Invalid cursor '{}'", s)), - } - } -} - -// toml deserializer doesn't seem to recognize string as enum -impl<'de> Deserialize<'de> for CursorKind { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let s = String::deserialize(deserializer)?; - s.parse().map_err(de::Error::custom) - } -} - #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Margin { pub vertical: u16, -- cgit v1.2.3-70-g09d2 From 058796c18e786309322731ff68b15a0f3901b60b Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Mon, 29 Nov 2021 11:09:04 +0530 Subject: Change default cursors to block for all modes --- book/src/configuration.md | 15 ++++++++------- helix-view/src/editor.rs | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'helix-view/src') diff --git a/book/src/configuration.md b/book/src/configuration.md index a40a8959..2998bcdc 100644 --- a/book/src/configuration.md +++ b/book/src/configuration.md @@ -15,8 +15,9 @@ line-number = "relative" mouse = false [editor.cursor-shape] -normal = "underline" -insert = "block" +insert = "bar" +normal = "block" +select = "underline" [editor.file-picker] hidden = false @@ -46,11 +47,11 @@ hidden = false Defines the shape of cursor in each mode. Note that due to limitations of the terminal environment, only the primary cursor can change shape. -| Key | Description | Default | -| --- | ----------- | -------- | -| `normal` | Cursor shape in [normal mode][normal mode] | `block` | -| `insert` | Cursor shape in [insert mode][insert mode] | `bar` | -| `select` | Cursor shape in [select mode][select mode] | `underline` | +| Key | Description | Default | +| --- | ----------- | ------- | +| `normal` | Cursor shape in [normal mode][normal mode] | `block` | +| `insert` | Cursor shape in [insert mode][insert mode] | `block` | +| `select` | Cursor shape in [select mode][select mode] | `block` | [normal mode]: ./keymap.md#normal-mode [insert mode]: ./keymap.md#insert-mode diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 9c77f270..b558c183 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -122,9 +122,9 @@ impl std::ops::Deref for CursorShapeConfig { impl Default for CursorShapeConfig { fn default() -> Self { Self(hashmap!( - Mode::Insert => CursorKind::Bar, + Mode::Insert => CursorKind::Block, Mode::Normal => CursorKind::Block, - Mode::Select => CursorKind::Underline, + Mode::Select => CursorKind::Block, )) } } -- cgit v1.2.3-70-g09d2 From 259678585c3410044683bf4d2619b2d024a04514 Mon Sep 17 00:00:00 2001 From: Blaž Hrastnik Date: Wed, 1 Dec 2021 13:08:20 +0900 Subject: ui: Optimize tree-sitter style lookups Tree sitter returns an index referring to the position of the scope in the scopes array. We can use that same index to avoid a hashmap lookup and instead store the styles in an array. This currently stores the styles in both a map and an array because the UI still uses hashmap lookups, but it's a reasonable tradeoff. --- helix-term/src/ui/editor.rs | 6 ++---- helix-view/src/theme.rs | 27 +++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 8 deletions(-) (limited to 'helix-view/src') diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 6299014c..a4fbca99 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -311,8 +311,7 @@ impl EditorView { if LineEnding::from_rope_slice(&grapheme).is_some() { if !out_of_bounds { let style = spans.iter().fold(text_style, |acc, span| { - let style = theme.get(theme.scopes()[span.0].as_str()); - acc.patch(style) + acc.patch(theme.highlight(span.0)) }); // we still want to render an empty cell with the style @@ -346,8 +345,7 @@ impl EditorView { if !out_of_bounds { let style = spans.iter().fold(text_style, |acc, span| { - let style = theme.get(theme.scopes()[span.0].as_str()); - acc.patch(style) + acc.patch(theme.highlight(span.0)) }); // if we're offscreen just keep going until we hit a new line diff --git a/helix-view/src/theme.rs b/helix-view/src/theme.rs index 757316bd..2366ff7d 100644 --- a/helix-view/src/theme.rs +++ b/helix-view/src/theme.rs @@ -78,8 +78,11 @@ impl Loader { #[derive(Clone, Debug)] pub struct Theme { - scopes: Vec, + // UI styles are stored in a HashMap styles: HashMap, + // tree-sitter highlight styles are stored in a Vec to optimize lookups + scopes: Vec, + highlights: Vec