on master help sign in
Refactor, add `ui.cursor.primary`
c534fdewojciechkepka
book/src/themes.md+1
@@ -79,6 +79,7 @@ Possible keys:
79| `ui.cursor.insert` | |
79| `ui.cursor.insert` | |
80| `ui.cursor.select` | |
80| `ui.cursor.select` | |
81| `ui.cursor.match` | Matching bracket etc. |
81| `ui.cursor.match` | Matching bracket etc. |
82+| `ui.cursor.primary` | Cursor with primary selection |
82| `ui.linenr` | |
83| `ui.linenr` | |
83| `ui.statusline` | |
84| `ui.statusline` | |
84| `ui.statusline.inactive` | |
85| `ui.statusline.inactive` | |
helix-term/src/ui/editor.rs+20−19
@@ -281,22 +281,23 @@ impl EditorView {
281 let end = text.line_to_char(last_line + 1);
281 let end = text.line_to_char(last_line + 1);
282 Range::new(start, end)
282 Range::new(start, end)
283 };
283 };
284+
285+ let base_cursor_style = theme
286+ .try_get("ui.cursor")
287+ .unwrap_or_else(|| Style::default().add_modifier(Modifier::REVERSED));
284− let scope = match doc.mode() {
288+ let cursor_style = match doc.mode() {
285− Mode::Insert => "ui.cursor.insert",
289+ Mode::Insert => theme.try_get("ui.cursor.insert"),
286− Mode::Select => "ui.cursor.select",
290+ Mode::Select => theme.try_get("ui.cursor.select"),
287− Mode::Normal => "ui.cursor",
291+ Mode::Normal => Some(base_cursor_style),
288− };
292+ }
289− let cursor_style = theme.try_get(scope).unwrap_or_else(|| {
293+ .unwrap_or(base_cursor_style);
290− theme
294+ let primary_cursor_style = theme.try_get("ui.cursor.primary").unwrap_or(cursor_style);
291− //if cursor.insert or cursor.select was not present try to default to cursor
292− .try_get("ui.cursor")
293− .unwrap_or_else(|| Style::default().add_modifier(Modifier::REVERSED))
294− });
295
295
296 let selection_style = theme.get("ui.selection");
296 let selection_style = theme.get("ui.selection");
297− let primary_style = theme
297+ let primary_selection_style = theme
298 .try_get("ui.selection.primary")
298 .try_get("ui.selection.primary")
299 .unwrap_or(selection_style);
299 .unwrap_or(selection_style);
300+
300 let selection = doc.selection(view.id);
301 let selection = doc.selection(view.id);
301 let primary_idx = selection.primary_index();
302 let primary_idx = selection.primary_index();
302
303
@@ -309,10 +310,10 @@ impl EditorView {
309 let mut start = view.screen_coords_at_pos(doc, text, selection.anchor);
310 let mut start = view.screen_coords_at_pos(doc, text, selection.anchor);
310 let mut end = view.screen_coords_at_pos(doc, text, selection.head);
311 let mut end = view.screen_coords_at_pos(doc, text, selection.head);
311
312
312− let style = if i != primary_idx {
313+ let (cursor_style, selection_style) = if i == primary_idx {
313− selection_style
314+ (primary_cursor_style, primary_selection_style)
314 } else {
315 } else {
315− primary_style
316+ (cursor_style, selection_style)
316 };
317 };
317
318
318 let head = end;
319 let head = end;
@@ -342,7 +343,7 @@ impl EditorView {
342 ),
343 ),
343 1,
344 1,
344 ),
345 ),
345− style,
346+ selection_style,
346 );
347 );
347 } else {
348 } else {
348 surface.set_style(
349 surface.set_style(
@@ -353,7 +354,7 @@ impl EditorView {
353 viewport.width.saturating_sub(start.col as u16),
354 viewport.width.saturating_sub(start.col as u16),
354 1,
355 1,
355 ),
356 ),
356− style,
357+ selection_style,
357 );
358 );
358 for i in start.row + 1..end.row {
359 for i in start.row + 1..end.row {
359 surface.set_style(
360 surface.set_style(
@@ -364,7 +365,7 @@ impl EditorView {
364 viewport.width,
365 viewport.width,
365 1,
366 1,
366 ),
367 ),
367− style,
368+ selection_style,
368 );
369 );
369 }
370 }
370 surface.set_style(
371 surface.set_style(
@@ -374,7 +375,7 @@ impl EditorView {
374 (end.col as u16).min(viewport.width),
375 (end.col as u16).min(viewport.width),
375 1,
376 1,
376 ),
377 ),
377− style,
378+ selection_style,
378 );
379 );
379 }
380 }
380
381