on master help sign in
Support drawing popup frame (#4313)

Co-authored-by: Blaž Hrastnik <blaz@mxxn.io>

commit9ba691cd3a8ffb021cb194bd3185317a65c3194a authorath3 <45574139+ath3@users.noreply.github.com> dateTue 19 Dec 2023, 02:17 (3 years ago) parent06d7dc6
9 files changed+123−28
book/src/configuration.md+1 helix-term/src/commands/dap.rs+7−2 helix-term/src/commands/lsp.rs+11−1 helix-term/src/commands/typed.rs−1 helix-term/src/ui/completion.rs+17−1 helix-term/src/ui/lsp.rs+3−1 helix-term/src/ui/menu.rs+34−13 helix-term/src/ui/popup.rs+27−7 helix-view/src/editor.rs+23−2
book/src/configuration.md+1
@@ -65,6 +65,7 @@ Its settings will be merged with the configuration directory `config.toml` and t
6565| `workspace-lsp-roots` | Directories relative to the workspace root that are treated as LSP roots. Should only be set in `.helix/config.toml` | `[]` |
6666| `default-line-ending` | The line ending to use for new documents. Can be `native`, `lf`, `crlf`, `ff`, `cr` or `nel`. `native` uses the platform's native line ending (`crlf` on Windows, otherwise `lf`). | `native` |
6767| `insert-final-newline` | Whether to automatically insert a trailing line-ending on write if missing | `true` |
68+| `popup-border` | Draw border around `popup`, `menu`, `all`, or `none` | `none` |
6869| `indent-heuristic` | How the indentation for a newly inserted line is computed: `simple` just copies the indentation level from the previous line, `tree-sitter` computes the indentation based on the syntax tree and `hybrid` combines both approaches. If the chosen heuristic is not available, a different one will be used as a fallback (the fallback order being `hybrid` -> `tree-sitter` -> `simple`). | `hybrid`
6970
7071### `[editor.statusline]` Section
helix-term/src/commands/dap.rs+7−2
@@ -8,7 +8,7 @@ use dap::{StackFrame, Thread, ThreadStates};
88use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion, DebugTemplate};
99use helix_dap::{self as dap, Client};
1010use helix_lsp::block_on;
11−use helix_view::editor::Breakpoint;
11+use helix_view::{editor::Breakpoint, graphics::Margin};
1212
1313use serde_json::{to_value, Value};
1414use tokio_stream::wrappers::UnboundedReceiverStream;
@@ -581,7 +581,12 @@ pub fn dap_variables(cx: &mut Context) {
581581 }
582582
583583 let contents = Text::from(tui::text::Text::from(variables));
584− let popup = Popup::new("dap-variables", contents);
584+ let margin = if cx.editor.popup_border() {
585+ Margin::all(1)
586+ } else {
587+ Margin::none()
588+ };
589+ let popup = Popup::new("dap-variables", contents).margin(margin);
585590 cx.replace_or_push_layer("dap-variables", popup);
586591}
587592
helix-term/src/commands/lsp.rs+11−1
@@ -23,6 +23,7 @@ use helix_core::{
2323use helix_view::{
2424 document::{DocumentInlayHints, DocumentInlayHintsId, Mode},
2525 editor::Action,
26+ graphics::Margin,
2627 theme::Style,
2728 Document, View,
2829};
@@ -744,7 +745,16 @@ pub fn code_action(cx: &mut Context) {
744745 });
745746 picker.move_down(); // pre-select the first item
746747
747− let popup = Popup::new("code-action", picker).with_scrollbar(false);
748+ let margin = if editor.menu_border() {
749+ Margin::vertical(1)
750+ } else {
751+ Margin::none()
752+ };
753+
754+ let popup = Popup::new("code-action", picker)
755+ .with_scrollbar(false)
756+ .margin(margin);
757+
748758 compositor.replace_or_push("code-action", popup);
749759 };
750760
helix-term/src/commands/typed.rs−1
@@ -574,7 +574,6 @@ fn set_line_ending(
574574
575575 Ok(())
576576}
577−
578577fn earlier(
579578 cx: &mut compositor::Context,
580579 args: &[Cow<str>],
helix-term/src/ui/completion.rs+17−1
@@ -2,6 +2,7 @@ use crate::compositor::{Component, Context, Event, EventResult};
22use helix_view::{
33 document::SavePoint,
44 editor::CompleteAction,
5+ graphics::Margin,
56 theme::{Modifier, Style},
67 ViewId,
78};
@@ -326,9 +327,18 @@ impl Completion {
326327 }
327328 };
328329 });
330+
331+ let margin = if editor.menu_border() {
332+ Margin::vertical(1)
333+ } else {
334+ Margin::none()
335+ };
336+
329337 let popup = Popup::new(Self::ID, menu)
330338 .with_scrollbar(false)
331− .ignore_escape_key(true);
339+ .ignore_escape_key(true)
340+ .margin(margin);
341+
332342 let mut completion = Self {
333343 popup,
334344 start_offset,
@@ -569,6 +579,12 @@ impl Component for Completion {
569579 // clear area
570580 let background = cx.editor.theme.get("ui.popup");
571581 surface.clear_with(doc_area, background);
582+
583+ if cx.editor.popup_border() {
584+ use tui::widgets::{Block, Borders, Widget};
585+ Widget::render(Block::default().borders(Borders::ALL), doc_area, surface);
586+ }
587+
572588 markdown_doc.render(doc_area, surface, cx);
573589 }
574590}
helix-term/src/ui/lsp.rs+3−1
@@ -92,7 +92,9 @@ impl Component for SignatureHelp {
9292 Some(doc) => Markdown::new(doc.clone(), Arc::clone(&self.config_loader)),
9393 };
9494 let sig_doc = sig_doc.parse(Some(&cx.editor.theme));
95− let sig_doc_area = area.clip_top(sig_text_area.height + 2);
95+ let sig_doc_area = area
96+ .clip_top(sig_text_area.height + 2)
97+ .clip_bottom(u16::from(cx.editor.popup_border()));
9698 let sig_doc_para = Paragraph::new(sig_doc)
9799 .wrap(Wrap { trim: false })
98100 .scroll((cx.scroll.unwrap_or_default() as u16, 0));
helix-term/src/ui/menu.rs+34−13
@@ -7,11 +7,18 @@ use crate::{
77use helix_core::fuzzy::MATCHER;
88use nucleo::pattern::{Atom, AtomKind, CaseMatching};
99use nucleo::{Config, Utf32Str};
10−use tui::{buffer::Buffer as Surface, widgets::Table};
10+use tui::{
11+ buffer::Buffer as Surface,
12+ widgets::{Block, Borders, Table, Widget},
13+};
1114
1215pub use tui::widgets::{Cell, Row};
1316
14−use helix_view::{editor::SmartTabConfig, graphics::Rect, Editor};
17+use helix_view::{
18+ editor::SmartTabConfig,
19+ graphics::{Margin, Rect},
20+ Editor,
21+};
1522use tui::layout::Constraint;
1623
1724pub trait Item: Sync + Send + 'static {
@@ -322,6 +329,15 @@ impl<T: Item + 'static> Component for Menu<T> {
322329 let selected = theme.get("ui.menu.selected");
323330 surface.clear_with(area, style);
324331
332+ let render_borders = cx.editor.menu_border();
333+
334+ let area = if render_borders {
335+ Widget::render(Block::default().borders(Borders::ALL), area, surface);
336+ area.inner(&Margin::vertical(1))
337+ } else {
338+ area
339+ };
340+
325341 let scroll = self.scroll;
326342
327343 let options: Vec<_> = self
@@ -362,15 +378,19 @@ impl<T: Item + 'static> Component for Menu<T> {
362378 false,
363379 );
364380
365− if let Some(cursor) = self.cursor {
366− let offset_from_top = cursor - scroll;
367− let left = &mut surface[(area.left(), area.y + offset_from_top as u16)];
368− left.set_style(selected);
369− let right = &mut surface[(
370− area.right().saturating_sub(1),
371− area.y + offset_from_top as u16,
372− )];
373− right.set_style(selected);
381+ let render_borders = cx.editor.menu_border();
382+
383+ if !render_borders {
384+ if let Some(cursor) = self.cursor {
385+ let offset_from_top = cursor - scroll;
386+ let left = &mut surface[(area.left(), area.y + offset_from_top as u16)];
387+ left.set_style(selected);
388+ let right = &mut surface[(
389+ area.right().saturating_sub(1),
390+ area.y + offset_from_top as u16,
391+ )];
392+ right.set_style(selected);
393+ }
374394 }
375395
376396 let fits = len <= win_height;
@@ -385,12 +405,13 @@ impl<T: Item + 'static> Component for Menu<T> {
385405 for i in 0..win_height {
386406 cell = &mut surface[(area.right() - 1, area.top() + i as u16)];
387407
388− cell.set_symbol("▐"); // right half block
408+ let half_block = if render_borders { "▌" } else { "▐" };
389409
390410 if scroll_line <= i && i < scroll_line + scroll_height {
391411 // Draw scroll thumb
412+ cell.set_symbol(half_block);
392413 cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
393− } else {
414+ } else if !render_borders {
394415 // Draw scroll track
395416 cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
396417 }
helix-term/src/ui/popup.rs+27−7
@@ -3,7 +3,10 @@ use crate::{
33 compositor::{Callback, Component, Context, Event, EventResult},
44 ctrl, key,
55};
6−use tui::buffer::Buffer as Surface;
6+use tui::{
7+ buffer::Buffer as Surface,
8+ widgets::{Block, Borders, Widget},
9+};
710
811use helix_core::Position;
912use helix_view::{
@@ -252,13 +255,29 @@ impl<T: Component> Component for Popup<T> {
252255 let background = cx.editor.theme.get("ui.popup");
253256 surface.clear_with(area, background);
254257
255− let inner = area.inner(&self.margin);
258+ let render_borders = cx.editor.popup_border();
259+
260+ let inner = if self
261+ .contents
262+ .type_name()
263+ .starts_with("helix_term::ui::menu::Menu")
264+ {
265+ area
266+ } else {
267+ area.inner(&self.margin)
268+ };
269+
270+ let border = usize::from(render_borders);
271+ if render_borders {
272+ Widget::render(Block::default().borders(Borders::ALL), area, surface);
273+ }
274+
256275 self.contents.render(inner, surface, cx);
257276
258277 // render scrollbar if contents do not fit
259278 if self.has_scrollbar {
260− let win_height = inner.height as usize;
261− let len = self.child_size.1 as usize;
279+ let win_height = (inner.height as usize).saturating_sub(2 * border);
280+ let len = (self.child_size.1 as usize).saturating_sub(2 * border);
262281 let fits = len <= win_height;
263282 let scroll = self.scroll;
264283 let scroll_style = cx.editor.theme.get("ui.menu.scroll");
@@ -274,14 +293,15 @@ impl<T: Component> Component for Popup<T> {
274293
275294 let mut cell;
276295 for i in 0..win_height {
277− cell = &mut surface[(inner.right() - 1, inner.top() + i as u16)];
296+ cell = &mut surface[(inner.right() - 1, inner.top() + (border + i) as u16)];
278297
279− cell.set_symbol("▐"); // right half block
298+ let half_block = if render_borders { "▌" } else { "▐" };
280299
281300 if scroll_line <= i && i < scroll_line + scroll_height {
282301 // Draw scroll thumb
302+ cell.set_symbol(half_block);
283303 cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
284− } else {
304+ } else if !render_borders {
285305 // Draw scroll track
286306 cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
287307 }
helix-view/src/editor.rs+23−2
@@ -43,9 +43,8 @@ pub use helix_core::diagnostic::Severity;
4343use helix_core::{
4444 auto_pairs::AutoPairs,
4545 syntax::{self, AutoPairConfig, IndentationHeuristic, SoftWrap},
46− Change, LineEnding, NATIVE_LINE_ENDING,
46+ Change, LineEnding, Position, Selection, NATIVE_LINE_ENDING,
4747};
48−use helix_core::{Position, Selection};
4948use helix_dap as dap;
5049use helix_lsp::lsp;
5150
@@ -291,6 +290,8 @@ pub struct Config {
291290 pub insert_final_newline: bool,
292291 /// Enables smart tab
293292 pub smart_tab: Option<SmartTabConfig>,
293+ /// Draw border around popups.
294+ pub popup_border: PopupBorderConfig,
294295 /// Which indent heuristic to use when a new line is inserted
295296 #[serde(default)]
296297 pub indent_heuristic: IndentationHeuristic,
@@ -797,6 +798,15 @@ impl From<LineEndingConfig> for LineEnding {
797798 }
798799}
799800
801+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
802+#[serde(rename_all = "kebab-case")]
803+pub enum PopupBorderConfig {
804+ None,
805+ All,
806+ Popup,
807+ Menu,
808+}
809+
800810impl Default for Config {
801811 fn default() -> Self {
802812 Self {
@@ -844,6 +854,7 @@ impl Default for Config {
844854 default_line_ending: LineEndingConfig::default(),
845855 insert_final_newline: true,
846856 smart_tab: Some(SmartTabConfig::default()),
857+ popup_border: PopupBorderConfig::None,
847858 indent_heuristic: IndentationHeuristic::default(),
848859 }
849860 }
@@ -1064,6 +1075,16 @@ impl Editor {
10641075 }
10651076 }
10661077
1078+ pub fn popup_border(&self) -> bool {
1079+ self.config().popup_border == PopupBorderConfig::All
1080+ || self.config().popup_border == PopupBorderConfig::Popup
1081+ }
1082+
1083+ pub fn menu_border(&self) -> bool {
1084+ self.config().popup_border == PopupBorderConfig::All
1085+ || self.config().popup_border == PopupBorderConfig::Menu
1086+ }
1087+
10671088 pub fn apply_motion<F: Fn(&mut Self) + 'static>(&mut self, motion: F) {
10681089 motion(self);
10691090 self.last_motion = Some(Box::new(motion));