From 077b554121506e8de9ca1ed5c60da1136f9e4d24 Mon Sep 17 00:00:00 2001 From: Sora Date: Mon, 20 Mar 2023 21:49:51 +0000 Subject: [PATCH] Make the indicator less redundant, use ui.sticky.* for themeing Fix some clippy issues Fix clippy --- helix-term/src/ui/editor.rs | 58 +++++++++++++++------------------- runtime/themes/tokyonight.toml | 4 +-- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index 603d99476..0e55f249f 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -42,6 +42,7 @@ pub struct StickyNode { byte_range: std::ops::Range, indicator: Option, top_first_byte: usize, + cursor_byte: usize, has_context_end: bool, } @@ -762,8 +763,8 @@ impl EditorView { let viewport = view.inner_area(doc); // define sticky context styles - let context_style = theme.get("ui.virtual.sticky.context"); - let indicator_style = theme.get("ui.virtual.sticky.indicator"); + let context_style = theme.get("ui.sticky.context"); + let indicator_style = theme.get("ui.sticky.indicator"); let mut context_area = viewport; context_area.height = 1; @@ -893,23 +894,25 @@ impl EditorView { ) -> Option> { query_nodes .flat_map(move |qnode| { - let context_nodes: Vec<_> = qnode.nodes_for_capture_index(start_index).collect(); let end_nodes: Vec<_> = qnode.nodes_for_capture_index(end_index).collect(); - context_nodes.into_iter().flat_map(move |context| { - end_nodes - .clone() - .into_iter() - .filter(move |it| { - let start_range = context.byte_range(); - let end = it.start_byte(); - start_range.contains(&end) + qnode + .nodes_for_capture_index(start_index) + .flat_map(move |context| { + end_nodes + .clone() + .into_iter() + .filter(move |it| { + let start_range = context.byte_range(); + let end = it.start_byte(); + start_range.contains(&end) && start_range.contains(&top_first_byte) && start_range.contains(&cursor_byte) - // only match @context.end nodes + // only match @context.end nodes that aren't at the end of the line && context.start_position().row != it.start_position().row - }) - .map(move |it| context.start_byte()..it.start_byte()) - }) + }) + .map(move |it| context.start_byte()..it.start_byte()) + }) + .collect::>() }) .next() } @@ -943,7 +946,8 @@ impl EditorView { if let Some(nodes) = nodes { if nodes.iter().any(|node| { - node.top_first_byte == top_first_byte && (visual_cursor_pos as usize) >= nodes.len() + (node.top_first_byte == top_first_byte && node.cursor_byte == cursor_byte) + && (visual_cursor_pos as usize) >= nodes.len() }) { return Some(nodes.to_vec()); } @@ -957,7 +961,7 @@ impl EditorView { let end_index = context_nodes .query .capture_index_for_name("context.end") - .unwrap_or_else(|| start_index); + .unwrap_or(start_index); let mut cursor_parent = tree .root_node() @@ -1025,6 +1029,7 @@ impl EditorView { byte_range: node_bytes, indicator: None, top_first_byte, + cursor_byte, has_context_end: true, }); } @@ -1038,6 +1043,7 @@ impl EditorView { byte_range: node.start_byte()..node.start_byte(), indicator: None, top_first_byte, + cursor_byte, has_context_end: false, }); } @@ -1080,28 +1086,14 @@ impl EditorView { .collect(); if config.sticky_context.indicator { - let message = "┤Sticky Context├"; - let side_placeholder = (viewport.width as usize) - .saturating_div(2) - .saturating_sub(message.len()); - - let added_length = if side_placeholder > 1 { - message.len() - } else { - 0 - }; - let mut str = String::with_capacity("─".len() * side_placeholder * 2 + added_length); - str.extend(std::iter::repeat("─").take(side_placeholder)); - if side_placeholder > 1 { - str.push_str(message); - } - str.extend(std::iter::repeat("─").take(side_placeholder)); + let str = "─".repeat(viewport.width as usize); context.push(StickyNode { visual_line: context.len() as u16, byte_range: 0..0, indicator: Some(str), top_first_byte, + cursor_byte, has_context_end: false, }) } diff --git a/runtime/themes/tokyonight.toml b/runtime/themes/tokyonight.toml index f02218b9b..abab44006 100644 --- a/runtime/themes/tokyonight.toml +++ b/runtime/themes/tokyonight.toml @@ -44,12 +44,12 @@ "ui.statusline.normal" = { fg = "black", bg = "blue" } "ui.statusline.insert" = { fg = "black", bg = "green" } "ui.statusline.select" = { fg = "black", bg = "magenta" } +"ui.sticky.context" = { bg = "background_highlight" } +"ui.sticky.indicator" = { fg = "comment", bg = "background_highlight" } "ui.text" = { fg = "foreground" } "ui.text.focus" = { fg = "cyan" } "ui.virtual.ruler" = { bg = "foreground_gutter" } "ui.virtual.whitespace" = { fg = "foreground_gutter" } -"ui.virtual.sticky.context" = { bg = "background_highlight" } -"ui.virtual.sticky.indicator" = { fg = "comment", bg = "background_highlight" } "ui.window" = { fg = "black" } "error" = { fg = "red" }