Skip to content

Commit

Permalink
Render code actions as a menu, allow adding padding to popup
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jan 31, 2022
1 parent f7f5514 commit 094a0aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
40 changes: 26 additions & 14 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use movement::Movement;
use crate::{
args,
compositor::{self, Component, Compositor},
ui::{self, FilePicker, Picker, Popup, Prompt, PromptEvent},
ui::{self, FilePicker, Popup, Prompt, PromptEvent},
};

use crate::job::{self, Job, Jobs};
Expand Down Expand Up @@ -3463,6 +3463,15 @@ fn workspace_symbol_picker(cx: &mut Context) {
)
}

impl ui::menu::Item for lsp::CodeActionOrCommand {
fn label(&self) -> &str {
match self {
lsp::CodeActionOrCommand::CodeAction(action) => action.title.as_str(),
lsp::CodeActionOrCommand::Command(command) => command.title.as_str(),
}
}
}

pub fn code_action(cx: &mut Context) {
let (view, doc) = current!(cx.editor);

Expand Down Expand Up @@ -3491,16 +3500,15 @@ pub fn code_action(cx: &mut Context) {
return;
}

let picker = Picker::new(
false,
actions,
|action| match action {
lsp::CodeActionOrCommand::CodeAction(action) => {
action.title.as_str().into()
}
lsp::CodeActionOrCommand::Command(command) => command.title.as_str().into(),
},
move |editor, code_action, _action| match code_action {
let picker = ui::Menu::new(actions, move |editor, code_action, event| {
if event != PromptEvent::Validate {
return;
}

// always present here
let code_action = code_action.unwrap();

match code_action {
lsp::CodeActionOrCommand::Command(command) => {
log::debug!("code action command: {:?}", command);
execute_lsp_command(editor, command.clone());
Expand All @@ -3518,9 +3526,13 @@ pub fn code_action(cx: &mut Context) {
execute_lsp_command(editor, command.clone());
}
}
},
);
let popup = Popup::new("code-action", picker);
}
});
let popup =
Popup::new("code-action", picker).margin(helix_view::graphics::Margin {
vertical: 1,
horizontal: 1,
});
compositor.push(Box::new(popup))
}
},
Expand Down
15 changes: 11 additions & 4 deletions helix-term/src/ui/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ use helix_view::{graphics::Rect, Editor};
use tui::layout::Constraint;

pub trait Item {
fn sort_text(&self) -> &str;
fn filter_text(&self) -> &str;

fn label(&self) -> &str;
fn row(&self) -> Row;

fn sort_text(&self) -> &str {
self.label()
}
fn filter_text(&self) -> &str {
self.label()
}

fn row(&self) -> Row {
Row::new(vec![Cell::from(self.label())])
}
}

pub struct Menu<T: Item> {
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod completion;
pub(crate) mod editor;
mod info;
mod markdown;
mod menu;
pub mod menu;
mod picker;
mod popup;
mod prompt;
Expand Down
24 changes: 20 additions & 4 deletions helix-term/src/ui/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use crossterm::event::Event;
use tui::buffer::Buffer as Surface;

use helix_core::Position;
use helix_view::graphics::Rect;
use helix_view::graphics::{Margin, Rect};

// TODO: share logic with Menu, it's essentially Popup(render_fn), but render fn needs to return
// a width/height hint. maybe Popup(Box<Component>)

pub struct Popup<T: Component> {
contents: T,
position: Option<Position>,
margin: Margin,
size: (u16, u16),
child_size: (u16, u16),
scroll: usize,
Expand All @@ -25,6 +26,10 @@ impl<T: Component> Popup<T> {
Self {
contents,
position: None,
margin: Margin {
vertical: 0,
horizontal: 0,
},
size: (0, 0),
child_size: (0, 0),
scroll: 0,
Expand All @@ -36,6 +41,11 @@ impl<T: Component> Popup<T> {
self.position = pos;
}

pub fn margin(mut self, margin: Margin) -> Self {
self.margin = margin;
self
}

pub fn get_rel_position(&mut self, viewport: Rect, cx: &Context) -> (u16, u16) {
let position = self
.position
Expand Down Expand Up @@ -126,13 +136,18 @@ impl<T: Component> Component for Popup<T> {
let max_width = 120.min(viewport.0);
let max_height = 26.min(viewport.1.saturating_sub(2)); // add some spacing in the viewport

let inner = Rect::new(0, 0, max_width, max_height).inner(&self.margin);

let (width, height) = self
.contents
.required_size((max_width, max_height))
.required_size((inner.width, inner.height))
.expect("Component needs required_size implemented in order to be embedded in a popup");

self.child_size = (width, height);
self.size = (width.min(max_width), height.min(max_height));
self.size = (
(width + self.margin.horizontal * 2).min(max_width),
(height + self.margin.vertical * 2).min(max_height),
);

// re-clamp scroll offset
let max_offset = self.child_size.1.saturating_sub(self.size.1);
Expand All @@ -156,7 +171,8 @@ impl<T: Component> Component for Popup<T> {
let background = cx.editor.theme.get("ui.popup");
surface.clear_with(area, background);

self.contents.render(area, surface, cx);
let inner = area.inner(&self.margin);
self.contents.render(inner, surface, cx);
}

fn id(&self) -> Option<&'static str> {
Expand Down
2 changes: 1 addition & 1 deletion helix-tui/src/widgets/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ impl<'a> Widget for Block<'a> {
if area.area() == 0 {
return;
}
buf.set_style(area, self.style);
buf.clear_with(area, self.style);
let symbols = BorderType::line_symbols(self.border_type);

// Sides
Expand Down

0 comments on commit 094a0aa

Please sign in to comment.