on master help sign in
Remove ui.cursor.primary and hashmap lookups
commit016640f4fb6f620df13a2cab15e749d623197a51 authorGokul Soumya <gokulps15@gmail.com> dateSat 18 Dec 2021, 08:25 (5 years ago) parent058796c
4 files changed+35−25
book/src/themes.md−1 helix-term/src/ui/editor.rs+8−9 helix-view/src/document.rs+3−3 helix-view/src/editor.rs+24−12
book/src/themes.md−1
@@ -173,7 +173,6 @@ These scopes are used for theming the editor interface.
173173| `ui.cursor.insert` | |
174174| `ui.cursor.select` | |
175175| `ui.cursor.match` | Matching bracket etc. |
176−| `ui.cursor.primary` | Cursor with primary selection |
177176| `ui.linenr` | |
178177| `ui.linenr.selected` | |
179178| `ui.statusline` | Statusline |
helix-term/src/ui/editor.rs+8−9
@@ -233,24 +233,23 @@ impl EditorView {
233233 }
234234 .unwrap_or(base_cursor_scope);
235235
236− let primary_cursor_scope = theme
237− .find_scope_index("ui.cursor.primary")
238− .unwrap_or(cursor_scope);
239236 let primary_selection_scope = theme
240237 .find_scope_index("ui.selection.primary")
241238 .unwrap_or(selection_scope);
242239
243240 let mut spans: Vec<(usize, std::ops::Range<usize>)> = Vec::new();
244241 for (i, range) in selection.iter().enumerate() {
245− let (cursor_scope, selection_scope) = if i == primary_idx {
246− (primary_cursor_scope, primary_selection_scope)
242+ let selection_is_primary = i == primary_idx;
243+ let selection_scope = if selection_is_primary {
244+ primary_selection_scope
247245 } else {
248− (cursor_scope, selection_scope)
246+ selection_scope
249247 };
250248
251249 // Special-case: cursor at end of the rope.
252250 if range.head == range.anchor && range.head == text.len_chars() {
253− if i != primary_idx {
251+ if !selection_is_primary {
252+ // Terminal cursor acts as the primary cursor
254253 spans.push((cursor_scope, range.head..range.head + 1));
255254 }
256255 continue;
@@ -261,13 +260,13 @@ impl EditorView {
261260 // Standard case.
262261 let cursor_start = prev_grapheme_boundary(text, range.head);
263262 spans.push((selection_scope, range.anchor..cursor_start));
264− if i != primary_idx {
263+ if !selection_is_primary {
265264 spans.push((cursor_scope, cursor_start..range.head));
266265 }
267266 } else {
268267 // Reverse case.
269268 let cursor_end = next_grapheme_boundary(text, range.head);
270− if i != primary_idx {
269+ if !selection_is_primary {
271270 spans.push((cursor_scope, range.head..cursor_end));
272271 }
273272 spans.push((selection_scope, cursor_end..range.anchor));
helix-view/src/document.rs+3−3
@@ -29,9 +29,9 @@ pub const SCRATCH_BUFFER_NAME: &str = "[scratch]";
2929
3030#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
3131pub enum Mode {
32− Normal,
33− Select,
34− Insert,
32+ Normal = 0,
33+ Select = 1,
34+ Insert = 2,
3535}
3636
3737impl Display for Mode {
helix-view/src/editor.rs+24−12
@@ -22,10 +22,10 @@ use anyhow::Error;
2222
2323pub use helix_core::diagnostic::Severity;
2424pub use helix_core::register::Registers;
25−use helix_core::{hashmap, syntax};
25+use helix_core::syntax;
2626use helix_core::{Position, Selection};
2727
28−use serde::Deserialize;
28+use serde::{Deserialize, Deserializer};
2929
3030fn deserialize_duration_millis<'de, D>(deserializer: D) -> Result<Duration, D::Error>
3131where
@@ -107,12 +107,28 @@ pub struct Config {
107107 pub cursor_shape: CursorShapeConfig,
108108}
109109
110−#[derive(Debug, Clone, PartialEq, Deserialize)]
111−#[serde(transparent)]
112−pub struct CursorShapeConfig(HashMap<Mode, CursorKind>);
110+// Cursor shape is read and used on every rendered frame and so needs
111+// to be fast. Therefore we avoid a hashmap and use an enum indexed array.
112+#[derive(Debug, Clone, PartialEq)]
113+pub struct CursorShapeConfig([CursorKind; 3]);
114+
115+impl<'de> Deserialize<'de> for CursorShapeConfig {
116+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
117+ where
118+ D: Deserializer<'de>,
119+ {
120+ let m = HashMap::<Mode, CursorKind>::deserialize(deserializer)?;
121+ let into_cursor = |mode: Mode| m.get(&mode).copied().unwrap_or_default();
122+ Ok(CursorShapeConfig([
123+ into_cursor(Mode::Normal),
124+ into_cursor(Mode::Select),
125+ into_cursor(Mode::Insert),
126+ ]))
127+ }
128+}
113129
114130impl std::ops::Deref for CursorShapeConfig {
115− type Target = HashMap<Mode, CursorKind>;
131+ type Target = [CursorKind; 3];
116132
117133 fn deref(&self) -> &Self::Target {
118134 &self.0
@@ -121,11 +137,7 @@ impl std::ops::Deref for CursorShapeConfig {
121137
122138impl Default for CursorShapeConfig {
123139 fn default() -> Self {
124− Self(hashmap!(
125− Mode::Insert => CursorKind::Block,
126− Mode::Normal => CursorKind::Block,
127− Mode::Select => CursorKind::Block,
128− ))
140+ Self([CursorKind::Block; 3])
129141 }
130142}
131143
@@ -621,7 +633,7 @@ impl Editor {
621633 let cursorkind = self
622634 .config
623635 .cursor_shape
624− .get(&doc.mode())
636+ .get(doc.mode() as usize)
625637 .copied()
626638 .unwrap_or_default();
627639 (Some(pos), cursorkind)