From 669add4d85e46ec60cc801a7f905779d0188bc9e Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Sun, 20 Jun 2021 21:47:32 +0200 Subject: [PATCH 1/5] Update docs --- book/src/themes.md | 89 ++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/book/src/themes.md b/book/src/themes.md index 80fee3d75a53..d552f41af784 100644 --- a/book/src/themes.md +++ b/book/src/themes.md @@ -46,48 +46,53 @@ Possible modifiers: Possible keys: -| Key | Notes | -| --- | --- | -| `attribute` | | -| `keyword` | | -| `keyword.directive` | Preprocessor directives (\#if in C) | -| `namespace` | | -| `punctuation` | | -| `punctuation.delimiter` | | -| `operator` | | -| `special` | | -| `property` | | -| `variable` | | -| `variable.parameter` | | -| `type` | | -| `type.builtin` | | -| `constructor` | | -| `function` | | -| `function.macro` | | -| `function.builtin` | | -| `comment` | | -| `variable.builtin` | | -| `constant` | | -| `constant.builtin` | | -| `string` | | -| `number` | | -| `escape` | Escaped characters | -| `label` | For lifetimes | -| `module` | | -| `ui.background` | | -| `ui.linenr` | | -| `ui.statusline` | | -| `ui.popup` | | -| `ui.window` | | -| `ui.help` | | -| `ui.text` | | -| `ui.text.focus` | | -| `ui.menu.selected` | | -| `ui.selection` | For selections in the editing area | -| `warning` | LSP warning | -| `error` | LSP error | -| `info` | LSP info | -| `hint` | LSP hint | +| Key | Notes | +| --- | --- | +| `attribute` | | +| `keyword` | | +| `keyword.directive` | Preprocessor directives (\#if in C) | +| `namespace` | | +| `punctuation` | | +| `punctuation.delimiter` | | +| `operator` | | +| `special` | | +| `property` | | +| `variable` | | +| `variable.parameter` | | +| `type` | | +| `type.builtin` | | +| `constructor` | | +| `function` | | +| `function.macro` | | +| `function.builtin` | | +| `comment` | | +| `variable.builtin` | | +| `constant` | | +| `constant.builtin` | | +| `string` | | +| `number` | | +| `escape` | Escaped characters | +| `label` | For lifetimes | +| `module` | | +| `ui.background` | | +| `ui.cursor` | | +| `ui.cursor.insert` | | +| `ui.cursor.select` | | +| `ui.cursor.match` | Matching bracket etc. | +| `ui.linenr` | | +| `ui.statusline` | | +| `ui.statusline.inactive` | | +| `ui.popup` | | +| `ui.window` | | +| `ui.help` | | +| `ui.text` | | +| `ui.text.focus` | | +| `ui.menu.selected` | | +| `ui.selection` | For selections in the editing area | +| `warning` | LSP warning | +| `error` | LSP error | +| `info` | LSP info | +| `hint` | LSP hint | These keys match [tree-sitter scopes](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#theme). We half-follow the common scopes from [macromates language grammars](https://macromates.com/manual/en/language_grammars) with some differences. From d52c7017bfe4bccacfac8f434ece9b17586d5c79 Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Sun, 20 Jun 2021 21:47:35 +0200 Subject: [PATCH 2/5] Add ability to theme cursor --- helix-term/src/ui/editor.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 7f0d06e983a7..23e76b2b6ba3 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -275,9 +275,17 @@ impl EditorView { let end = text.line_to_char(last_line + 1); Range::new(start, end) }; - let cursor_style = Style::default() - // .bg(Color::Rgb(255, 255, 255)) - .add_modifier(Modifier::REVERSED); + let scope = match doc.mode() { + Mode::Insert => "ui.cursor.insert", + Mode::Select => "ui.cursor.select", + Mode::Normal => "ui.cursor", + }; + let cursor_style = theme.try_get(scope).unwrap_or_else(|| { + theme + //if cursor.insert or cursor.select was not present try to default to cursor + .try_get("ui.cursor") + .unwrap_or_else(|| Style::default().add_modifier(Modifier::REVERSED)) + }); let selection_style = theme.get("ui.selection"); @@ -382,9 +390,12 @@ impl EditorView { if (pos.col as u16) < viewport.width + view.first_col as u16 && pos.col >= view.first_col { - let style = Style::default() - .add_modifier(Modifier::REVERSED) - .add_modifier(Modifier::DIM); + let style = + theme.try_get("ui.cursor.match").unwrap_or_else(|| { + Style::default() + .add_modifier(Modifier::REVERSED) + .add_modifier(Modifier::DIM) + }); surface .get_mut( From 5cd4d9f09975eb3943380ad3910b8cce73143bc0 Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Sun, 20 Jun 2021 22:02:41 +0200 Subject: [PATCH 3/5] Add ability to theme primary selecition --- book/src/themes.md | 1 + helix-term/src/ui/editor.rs | 25 ++++++++++++++++++------- theme.toml | 3 ++- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/book/src/themes.md b/book/src/themes.md index d552f41af784..01fda34a6844 100644 --- a/book/src/themes.md +++ b/book/src/themes.md @@ -89,6 +89,7 @@ Possible keys: | `ui.text.focus` | | | `ui.menu.selected` | | | `ui.selection` | For selections in the editing area | +| `ui.selection.primary` | | | `warning` | LSP warning | | `error` | LSP error | | `info` | LSP info | diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 23e76b2b6ba3..786ffde013a0 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -288,16 +288,27 @@ impl EditorView { }); let selection_style = theme.get("ui.selection"); + let primary_style = theme + .try_get("ui.selection.primary") + .unwrap_or(selection_style); + let selection = doc.selection(view.id); + let primary_idx = selection.primary_index(); - for selection in doc - .selection(view.id) + for (i, selection) in selection .iter() - .filter(|range| range.overlaps(&screen)) + .enumerate() + .filter(|(_, range)| range.overlaps(&screen)) { // TODO: render also if only one of the ranges is in viewport let mut start = view.screen_coords_at_pos(doc, text, selection.anchor); let mut end = view.screen_coords_at_pos(doc, text, selection.head); + let style = if i != primary_idx { + selection_style + } else { + primary_style + }; + let head = end; if selection.head < selection.anchor { @@ -325,7 +336,7 @@ impl EditorView { ), 1, ), - selection_style, + style, ); } else { surface.set_style( @@ -336,7 +347,7 @@ impl EditorView { viewport.width.saturating_sub(start.col as u16), 1, ), - selection_style, + style, ); for i in start.row + 1..end.row { surface.set_style( @@ -347,7 +358,7 @@ impl EditorView { viewport.width, 1, ), - selection_style, + style, ); } surface.set_style( @@ -357,7 +368,7 @@ impl EditorView { (end.col as u16).min(viewport.width), 1, ), - selection_style, + style, ); } diff --git a/theme.toml b/theme.toml index a4f402675d93..6c357ef47d2d 100644 --- a/theme.toml +++ b/theme.toml @@ -52,7 +52,8 @@ "ui.text" = { fg = "#a4a0e8" } # lavender "ui.text.focus" = { fg = "#dbbfef"} # lilac -"ui.selection" = { bg = "#540099" } +"ui.selection" = { bg = "#44258b" } +"ui.selection.primary" = { bg = "#540099" } "ui.menu.selected" = { fg = "#281733", bg = "#ffffff" } # revolver "warning" = "#ffcd1c" From 4f83cd5f168d6c4311c1942d4309965bbc01a036 Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Mon, 21 Jun 2021 18:01:35 +0200 Subject: [PATCH 4/5] Refactor, add `ui.cursor.primary` --- book/src/themes.md | 1 + helix-term/src/ui/editor.rs | 39 +++++++++++++++++++------------------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/book/src/themes.md b/book/src/themes.md index 01fda34a6844..2ece249124b3 100644 --- a/book/src/themes.md +++ b/book/src/themes.md @@ -79,6 +79,7 @@ Possible keys: | `ui.cursor.insert` | | | `ui.cursor.select` | | | `ui.cursor.match` | Matching bracket etc. | +| `ui.cursor.primary` | Cursor with primary selection | | `ui.linenr` | | | `ui.statusline` | | | `ui.statusline.inactive` | | diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 786ffde013a0..479dc8485821 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -275,22 +275,23 @@ impl EditorView { let end = text.line_to_char(last_line + 1); Range::new(start, end) }; - let scope = match doc.mode() { - Mode::Insert => "ui.cursor.insert", - Mode::Select => "ui.cursor.select", - Mode::Normal => "ui.cursor", - }; - let cursor_style = theme.try_get(scope).unwrap_or_else(|| { - theme - //if cursor.insert or cursor.select was not present try to default to cursor - .try_get("ui.cursor") - .unwrap_or_else(|| Style::default().add_modifier(Modifier::REVERSED)) - }); + + let base_cursor_style = theme + .try_get("ui.cursor") + .unwrap_or_else(|| Style::default().add_modifier(Modifier::REVERSED)); + let cursor_style = match doc.mode() { + Mode::Insert => theme.try_get("ui.cursor.insert"), + Mode::Select => theme.try_get("ui.cursor.select"), + Mode::Normal => Some(base_cursor_style), + } + .unwrap_or(base_cursor_style); + let primary_cursor_style = theme.try_get("ui.cursor.primary").unwrap_or(cursor_style); let selection_style = theme.get("ui.selection"); - let primary_style = theme + let primary_selection_style = theme .try_get("ui.selection.primary") .unwrap_or(selection_style); + let selection = doc.selection(view.id); let primary_idx = selection.primary_index(); @@ -303,10 +304,10 @@ impl EditorView { let mut start = view.screen_coords_at_pos(doc, text, selection.anchor); let mut end = view.screen_coords_at_pos(doc, text, selection.head); - let style = if i != primary_idx { - selection_style + let (cursor_style, selection_style) = if i == primary_idx { + (primary_cursor_style, primary_selection_style) } else { - primary_style + (cursor_style, selection_style) }; let head = end; @@ -336,7 +337,7 @@ impl EditorView { ), 1, ), - style, + selection_style, ); } else { surface.set_style( @@ -347,7 +348,7 @@ impl EditorView { viewport.width.saturating_sub(start.col as u16), 1, ), - style, + selection_style, ); for i in start.row + 1..end.row { surface.set_style( @@ -358,7 +359,7 @@ impl EditorView { viewport.width, 1, ), - style, + selection_style, ); } surface.set_style( @@ -368,7 +369,7 @@ impl EditorView { (end.col as u16).min(viewport.width), 1, ), - style, + selection_style, ); } From 23be5bb4fefd307033825ccfb0e6bae31a3c3d8a Mon Sep 17 00:00:00 2001 From: wojciechkepka Date: Wed, 23 Jun 2021 08:31:53 +0200 Subject: [PATCH 5/5] Patch the primary cursor with insert and select styles --- helix-term/src/ui/editor.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 479dc8485821..c1f23cbbbf47 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -276,16 +276,28 @@ impl EditorView { Range::new(start, end) }; + let mode = doc.mode(); let base_cursor_style = theme .try_get("ui.cursor") .unwrap_or_else(|| Style::default().add_modifier(Modifier::REVERSED)); - let cursor_style = match doc.mode() { + let cursor_style = match mode { Mode::Insert => theme.try_get("ui.cursor.insert"), Mode::Select => theme.try_get("ui.cursor.select"), Mode::Normal => Some(base_cursor_style), } .unwrap_or(base_cursor_style); - let primary_cursor_style = theme.try_get("ui.cursor.primary").unwrap_or(cursor_style); + let primary_cursor_style = theme + .try_get("ui.cursor.primary") + .map(|style| { + if mode != Mode::Normal { + // we want to make sure that the insert and select highlights + // also affect the primary cursor if set + style.patch(cursor_style) + } else { + style + } + }) + .unwrap_or(cursor_style); let selection_style = theme.get("ui.selection"); let primary_selection_style = theme