Skip to content

Commit

Permalink
Support drawing popup border
Browse files Browse the repository at this point in the history
  • Loading branch information
ath3 committed Nov 17, 2022
1 parent fe11ae2 commit c255b7f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 35 deletions.
1 change: 1 addition & 0 deletions book/src/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ on unix operating systems.
| `rulers` | List of column positions at which to display the rulers. Can be overridden by language specific `rulers` in `languages.toml` file. | `[]` |
| `bufferline` | Renders a line at the top of the editor displaying open buffers. Can be `always`, `never` or `multiple` (only shown if more than one buffer is in use) | `never` |
| `color-modes` | Whether to color the mode indicator with different colors depending on the mode itself | `false` |
| `popup-border` | Draw border around `popup`, `menu`, `all`, or `none` | `none` |

### `[editor.statusline]` Section

Expand Down
21 changes: 19 additions & 2 deletions helix-term/src/commands/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ use tui::text::{Span, Spans};
use super::{align_view, push_jump, Align, Context, Editor, Open};

use helix_core::{path, Selection};
use helix_view::{apply_transaction, document::Mode, editor::Action, theme::Style};
use helix_view::{
apply_transaction,
document::Mode,
editor::{Action, PopupBorderConfig},
graphics::Margin,
theme::Style,
};

use crate::{
compositor::{self, Compositor},
Expand Down Expand Up @@ -598,7 +604,18 @@ pub fn code_action(cx: &mut Context) {
});
picker.move_down(); // pre-select the first item

let popup = Popup::new("code-action", picker).with_scrollbar(false);
let margin = if editor.config().popup_border == PopupBorderConfig::All
|| editor.config().popup_border == PopupBorderConfig::Menu
{
Margin::vertical(1)
} else {
Margin::none()
};

let popup = Popup::new("code-action", picker)
.with_scrollbar(false)
.margin(margin);

compositor.replace_or_push("code-action", popup);
},
)
Expand Down
45 changes: 33 additions & 12 deletions helix-term/src/ui/completion.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use crate::compositor::{Component, Context, Event, EventResult};
use helix_view::{apply_transaction, editor::CompleteAction};
use helix_view::{
apply_transaction,
editor::{CompleteAction, PopupBorderConfig},
graphics::Margin,
};
use tui::buffer::Buffer as Surface;
use tui::text::Spans;

Expand Down Expand Up @@ -227,7 +231,19 @@ impl Completion {
}
};
});
let popup = Popup::new(Self::ID, menu).with_scrollbar(false);

let margin = if editor.config().popup_border == PopupBorderConfig::All
|| editor.config().popup_border == PopupBorderConfig::Menu
{
Margin::vertical(1)
} else {
Margin::none()
};

let popup = Popup::new(Self::ID, menu)
.with_scrollbar(false)
.margin(margin);

let mut completion = Self {
popup,
start_offset,
Expand Down Expand Up @@ -371,7 +387,7 @@ impl Component for Completion {
"```{}\n{}\n```\n{}",
language,
option.detail.as_deref().unwrap_or_default(),
contents.clone()
contents
),
cx.editor.syn_loader.clone(),
)
Expand All @@ -381,15 +397,14 @@ impl Component for Completion {
value: contents,
})) => {
// TODO: set language based on doc scope
Markdown::new(
format!(
"```{}\n{}\n```\n{}",
language,
option.detail.as_deref().unwrap_or_default(),
contents.clone()
),
cx.editor.syn_loader.clone(),
)
if let Some(detail) = &option.detail.as_deref() {
Markdown::new(
format!("```{}\n{}\n```\n{}", language, detail, contents),
cx.editor.syn_loader.clone(),
)
} else {
Markdown::new(contents.to_string(), cx.editor.syn_loader.clone())
}
}
None if option.detail.is_some() => {
// TODO: copied from above
Expand Down Expand Up @@ -442,6 +457,12 @@ impl Component for Completion {
// clear area
let background = cx.editor.theme.get("ui.popup");
surface.clear_with(area, background);
if cx.editor.config().popup_border == helix_view::editor::PopupBorderConfig::All
|| cx.editor.config().popup_border == helix_view::editor::PopupBorderConfig::Popup
{
use tui::widgets::{Block, Borders, Widget};
Widget::render(Block::default().borders(Borders::ALL), area, surface);
}
markdown_doc.render(area, surface, cx);
}
}
Expand Down
7 changes: 6 additions & 1 deletion helix-term/src/ui/lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,12 @@ impl Component for SignatureHelp {
Some(doc) => Markdown::new(doc.clone(), Arc::clone(&self.config_loader)),
};
let sig_doc = sig_doc.parse(Some(&cx.editor.theme));
let sig_doc_area = area.clip_top(sig_text_area.height + 2);
let mut sig_doc_area = area.clip_top(sig_text_area.height + 2);
if cx.editor.config().popup_border == helix_view::editor::PopupBorderConfig::All
|| cx.editor.config().popup_border == helix_view::editor::PopupBorderConfig::Popup
{
sig_doc_area = sig_doc_area.clip_bottom(1);
}
let sig_doc_para = Paragraph::new(sig_doc)
.wrap(Wrap { trim: false })
.scroll((cx.scroll.unwrap_or_default() as u16, 0));
Expand Down
42 changes: 29 additions & 13 deletions helix-term/src/ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,25 @@ impl<T: Item + 'static> Component for Menu<T> {
Some(self.size)
}

fn render(&mut self, area: Rect, surface: &mut Surface, cx: &mut Context) {
fn render(&mut self, mut area: Rect, surface: &mut Surface, cx: &mut Context) {
let theme = &cx.editor.theme;
let style = theme
.try_get("ui.menu")
.unwrap_or_else(|| theme.get("ui.text"));
let selected = theme.get("ui.menu.selected");
surface.clear_with(area, style);

use helix_view::editor::PopupBorderConfig::{All, Menu};
let render_borders =
cx.editor.config().popup_border == All || cx.editor.config().popup_border == Menu;

if render_borders {
use tui::widgets::{Block, Borders, Widget};
Widget::render(Block::default().borders(Borders::ALL), area, surface);

area = area.clip_top(1).clip_bottom(1);
}

let scroll = self.scroll;

let options: Vec<_> = self
Expand Down Expand Up @@ -338,15 +349,17 @@ impl<T: Item + 'static> Component for Menu<T> {
},
);

if let Some(cursor) = self.cursor {
let offset_from_top = cursor - scroll;
let left = &mut surface[(area.left(), area.y + offset_from_top as u16)];
left.set_style(selected);
let right = &mut surface[(
area.right().saturating_sub(1),
area.y + offset_from_top as u16,
)];
right.set_style(selected);
if !render_borders {
if let Some(cursor) = self.cursor {
let offset_from_top = cursor - scroll;
let left = &mut surface[(area.left(), area.y + offset_from_top as u16)];
left.set_style(selected);
let right = &mut surface[(
area.right().saturating_sub(1),
area.y + offset_from_top as u16,
)];
right.set_style(selected);
}
}

let fits = len <= win_height;
Expand All @@ -361,12 +374,15 @@ impl<T: Item + 'static> Component for Menu<T> {
for i in 0..win_height {
cell = &mut surface[(area.right() - 1, area.top() + i as u16)];

cell.set_symbol("▐"); // right half block

if scroll_line <= i && i < scroll_line + scroll_height {
// Draw scroll thumb
if render_borders {
cell.set_symbol("▌"); // left half block
} else {
cell.set_symbol("▐"); // right half block
}
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
} else {
} else if !render_borders {
// Draw scroll track
cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
}
Expand Down
29 changes: 22 additions & 7 deletions helix-term/src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,25 @@ impl<T: Component> Component for Popup<T> {
let background = cx.editor.theme.get("ui.popup");
surface.clear_with(area, background);

let inner = area.inner(&self.margin);
use helix_view::editor::PopupBorderConfig::{All, Popup};
let render_borders =
cx.editor.config().popup_border == All || cx.editor.config().popup_border == Popup;

let (inner, border) = if render_borders {
use tui::widgets::{Block, Borders, Widget};
Widget::render(Block::default().borders(Borders::ALL), area, surface);
(area, 1)
} else {
let inner = area.inner(&self.margin);
(inner, 0)
};

self.contents.render(inner, surface, cx);

// render scrollbar if contents do not fit
if self.has_scrollbar {
let win_height = inner.height as usize;
let len = self.child_size.1 as usize;
let win_height = inner.height as usize - 2 * border;
let len = self.child_size.1 as usize - 2 * border;
let fits = len <= win_height;
let scroll = self.scroll;
let scroll_style = cx.editor.theme.get("ui.menu.scroll");
Expand All @@ -258,14 +270,17 @@ impl<T: Component> Component for Popup<T> {

let mut cell;
for i in 0..win_height {
cell = &mut surface[(inner.right() - 1, inner.top() + i as u16)];

cell.set_symbol("▐"); // right half block
cell = &mut surface[(inner.right() - 1, inner.top() + (border + i) as u16)];

if scroll_line <= i && i < scroll_line + scroll_height {
// Draw scroll thumb
if render_borders {
cell.set_symbol("▌"); // left half block
} else {
cell.set_symbol("▐"); // right half block
}
cell.set_fg(scroll_style.fg.unwrap_or(helix_view::theme::Color::Reset));
} else {
} else if !render_borders {
// Draw scroll track
cell.set_fg(scroll_style.bg.unwrap_or(helix_view::theme::Color::Reset));
}
Expand Down
12 changes: 12 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ pub struct Config {
pub indent_guides: IndentGuidesConfig,
/// Whether to color modes with different colors. Defaults to `false`.
pub color_modes: bool,
/// Draw border around popups.
pub popup_border: PopupBorderConfig,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
Expand Down Expand Up @@ -582,6 +584,15 @@ impl Default for IndentGuidesConfig {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum PopupBorderConfig {
None,
All,
Popup,
Menu,
}

impl Default for Config {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -621,6 +632,7 @@ impl Default for Config {
bufferline: BufferLine::default(),
indent_guides: IndentGuidesConfig::default(),
color_modes: false,
popup_border: PopupBorderConfig::None,
}
}
}
Expand Down

0 comments on commit c255b7f

Please sign in to comment.